This file is indexed.

/usr/bin/edit-patch is in devscripts 2.15.3+deb8u1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/sh
#
# Copyright (C) 2009 Canonical
#
# Authors:
#  Michael Vogt
#  Daniel Holbach
#  David Futcher
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; version 3.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

set -e


PATCHSYSTEM="unknown"
PATCHNAME="no-patch-name"
PREFIX="debian/patches"

PATCH_DESC=$(cat<<EOF
## Description: add some description\
\n## Origin/Author: add some origin or author\
\n## Bug: bug URL
EOF
)

fatal_error() {
    echo "$@" >&2
    exit 1
}

# check if the given binary is installed and give a error if not
# arg1: binary
# arg2: error message
require_installed() {
    if ! which "$1" >/dev/null; then
        fatal_error "$2"
    fi
}

ensure_debian_dir() {
    if [ ! -e debian/control ] || [ ! -e debian/rules ]; then
        fatal_error "Can not find debian/rules or debian/control. Not in a debian dir?"
    fi

}

detect_patchsystem() {
    CDBS_PATCHSYS="^[^#]*simple-patchsys.mk"

    if grep -q "$CDBS_PATCHSYS" debian/rules; then
        PATCHSYSTEM="cdbs"
        require_installed cdbs-edit-patch "no cdbs-edit-patch found, is 'cdbs' installed?"
    elif [ -e debian/patches/00list ]; then
        PATCHSYSTEM="dpatch"
        require_installed dpatch-edit-patch "no dpatch-edit-patch found, is 'dpatch' installed?"
    elif [ -e debian/patches/series -o \
           "$(cat debian/source/format 2> /dev/null)" = "3.0 (quilt)" ]; then
        PATCHSYSTEM="quilt"
        require_installed quilt "no quilt found, is 'quilt' installed?"
    else
        PATCHSYSTEM="none"
        PREFIX="debian/applied-patches"
    fi
}

# remove full path if given
normalize_patch_path() {
    PATCHNAME=${PATCHNAME##*/}
    echo "Normalizing patch path to $PATCHNAME"
}

# ensure (for new patches) that:
# - dpatch ends with .dpatch
# - cdbs/quilt with .patch
normalize_patch_extension() {
    # check if we have a patch already
    if [ -e $PREFIX/$PATCHNAME ]; then
        echo "Patch $PATCHNAME exists, not normalizing"
        return
    fi

    # normalize name for new patches
    PATCHNAME=${PATCHNAME%.*}
    if [ "$PATCHSYSTEM" = "quilt" ]; then
        PATCHNAME="${PATCHNAME}.patch"
    elif [ "$PATCHSYSTEM" = "cdbs" ]; then
        PATCHNAME="${PATCHNAME}.patch"
    elif [ "$PATCHSYSTEM" = "dpatch" ]; then
        PATCHNAME="${PATCHNAME}.dpatch"
    elif [ "$PATCHSYSTEM" = "none" ]; then
        PATCHNAME="${PATCHNAME}.patch"
    fi

    echo "Normalizing patch name to $PATCHNAME"
}

edit_patch_cdbs() {
    cdbs-edit-patch $PATCHNAME
    vcs_add debian/patches/$1
}

edit_patch_dpatch() {
    dpatch-edit-patch $PATCHNAME
    # add if needed
    if ! grep -q $1 $PREFIX/00list; then
        echo "$1" >> $PREFIX/00list
    fi
    vcs_add $PREFIX/00list $PREFIX/$1
}

edit_patch_quilt() {
    export QUILT_PATCHES=debian/patches
    top_patch=$(quilt top)
    echo "Top patch: $top_patch"
    if [ -e $PREFIX/$1 ]; then
        # if its a existing patch and we are at the end of the stack,
        # go back at the beginning
        if ! quilt unapplied; then
            quilt pop -a
        fi
        quilt push $1
    else
        # if its a new patch make sure we are at the end of the stack
        if quilt unapplied >/dev/null; then
            quilt push -a
        fi
        quilt new $1
    fi
    # use a sub-shell
    quilt shell
    quilt refresh
    echo "Reverting quilt back to $top_patch"
    quilt pop $top_patch
    vcs_add $PREFIX/$1 $PREFIX/series
}

edit_patch_none() {
    # Dummy edit-patch function, just display a warning message
    echo "No patchsystem could be found so the patch was applied inline and a copy \
stored in debian/patches-applied. Please remember to mention this in your changelog."
}

add_patch_quilt() {
    # $1 is the original patchfile, $2 the normalized name
    # FIXME: use quilt import instead?
    cp $1 $PREFIX/$2
    if ! grep -q $2 $PREFIX/series; then
        echo "$2" >> $PREFIX/series
    fi
    vcs_add $PREFIX/$2 $PREFIX/series
}

add_patch_cdbs() {
    # $1 is the original patchfile, $2 the normalized name
    cp $1 $PREFIX/$2
    vcs_add $PREFIX/$2
}

add_patch_dpatch() {
    # $1 is the original patchfile, $2 the normalized name
    cp $1 $PREFIX
    if ! grep -q $2 $PREFIX/00list; then
        echo "$2" >> $PREFIX/00list
    fi
    vcs_add $PREFIX/$2 $PREFIX/00list
}

add_patch_none() {
    # $1 is the original patchfile, $2 the normalized name
    cp $1 $PREFIX/$2
    vcs_add $PREFIX/$2
}

vcs_add() {
    if [ -d .bzr ]; then
        bzr add $@
    elif [ -d .git ];then
        git add $@
    else
        echo "Remember to add $@ to a VCS if you use one"
    fi
}

vcs_commit() {
    # check if debcommit is happy
    if ! debcommit --noact 2>/dev/null; then
        return
    fi
    # commit (if the user confirms)
    debcommit --confirm
}

add_changelog() {
    S="$PREFIX/$1: [DESCRIBE CHANGES HERE]"
    if head -n1 debian/changelog|grep UNRELEASED; then
        dch --append "$S"
    else
        dch --increment "$S"
    fi
    # let the user edit it
    dch --edit
}

add_patch_tagging() {
    # check if we have a description already
    if grep "## Description:" $PREFIX/$1; then
        return
    fi
    # if not, add one
    RANGE=1,1
    # make sure we keep the first line (for dpatch)
    if head -n1 $PREFIX/$1|grep -q '^#'; then
        RANGE=2,2
    fi
    sed -i ${RANGE}i"$PATCH_DESC" $PREFIX/$1
}

detect_patch_location() {
    # Checks whether the specified patch exists in debian/patches or on the filesystem
    FILENAME=${PATCHNAME##*/}

    if [ -f "$PREFIX/$FILENAME" ]; then
        PATCHTYPE="debian"
    elif [ -f "$PATCHNAME" ]; then
        PATCHTYPE="file"
        PATCHORIG="$PATCHNAME"
    else
        if [ "$PATCHSYSTEM" = "none" ]; then
            fatal_error "No patchsystem detected, cannot create new patch (no dpatch/quilt/cdbs?)"
        else
            PATCHTYPE="new"
        fi
    fi
}

handle_file_patch() {
    if [ "$PATCHTYPE" = "file" ]; then
        [ -f "$PATCHORIG" ] || fatal_error "No patch detected"

        if [ "$PATCHSYSTEM" = "none" ]; then
            # If we're supplied a file and there is no patchsys we apply it directly
            # and store it in debian/applied patches
            [ -d $PREFIX ] || mkdir $PREFIX

            patch -p0 < "$PATCHORIG"
            cp "$PATCHORIG" "$PREFIX/$PATCHNAME"
        else
            # Patch type is file but there is a patchsys present, so we add it
            # correctly
            cp "$PATCHORIG" "$PREFIX/$PATCHNAME"

            if [ "$PATCHSYSTEM" = "quilt" ]; then
                echo "$PATCHNAME" >> $PREFIX/series
            elif [ "$PATCHSYSTEM" = "dpatch" ]; then
                echo "$PATCHNAME" >> $PREFIX/00list

                # Add the dpatch header to files that don't already have it
                if ! grep -q "@DPATCH@" "$PREFIX/$PATCHNAME"; then
                    sed -i '1i#! /bin/sh /usr/share/dpatch/dpatch-run\n@DPATCH@' $PREFIX/$PATCHNAME
                fi
            fi

            echo "Copying and applying new patch. You can now edit the patch or exit the subshell to save."
        fi
    fi
}

# TODO:
# - edit-patch --remove implementieren
# - dbs patch system

main() {
    # parse args
    if [ $# -ne 1 ]; then
        fatal_error "Need exactly one patch name"
    fi
    PATCHNAME="$1"
    # do the work
    ensure_debian_dir
    detect_patchsystem
    detect_patch_location
    normalize_patch_path
    normalize_patch_extension
    handle_file_patch
    if [ "$(basename $0 .sh)" = "edit-patch" ]; then
        edit_patch_$PATCHSYSTEM $PATCHNAME
    elif [ "$(basename $0 .sh)" = "add-patch" ]; then
        add_patch_$PATCHSYSTEM $1 $PATCHNAME
    else
        fatal_error "Unknown script name: $0"
    fi
    add_patch_tagging $PATCHNAME
    add_changelog $PATCHNAME
    vcs_commit
}

main $@