3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 GUILT_VERSION
="0.19-rc1"
9 if [ `basename $0` = "guilt" ]; then
10 # being run as standalone
12 # by default, we shouldn't fail
16 # take first arg, and try to execute it
21 if [ ! -x "$dir/guilt-$cmd" ]; then
22 echo "Command $cmd not found" >&2
27 exec "$dir/guilt-$cmd" "$@"
29 # this is not reached because of the exec
30 echo "Exec failed! Something is terribly wrong!" >&2
35 # no args passed or invalid command entered, just output help summary
37 echo "Guilt v$GUILT_VERSION"
39 echo "Pick a command:"
40 for x
in `dirname $0`/guilt-
*; do
41 [ -x $x ] && echo -e "\t`basename $x`"
46 echo -e "\tguilt-push"
48 echo -e "\tguilt push"
67 local d
=`git-rev-parse --git-dir`
69 if [ $?
-ne 0 -o -z "$d" ]; then
70 echo "Not a git repository" >&2
79 git-symbolic-ref HEAD |
sed -e 's,^refs/heads/,,'
82 function verify_branch
86 [ ! -d "$GIT_DIR/patches" ] &&
87 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
89 [ ! -d "$GIT_DIR/patches/$b" ] &&
90 echo "Branch $b is not initialized, try guilt-init" >&2 &&
92 [ ! -f "$GIT_DIR/patches/$b/series" ] &&
93 echo "Branch $b does not have a series file" >&2 &&
95 [ ! -f "$GIT_DIR/patches/$b/status" ] &&
96 echo "Branch $b does not have a status file" >&2 &&
98 [ -f "$GIT_DIR/patches/$b/applied" ] &&
99 echo "Warning: Branch $b has 'applied' file - guilt is not compatible with stgit" >&2 &&
107 tail -1 $GUILT_DIR/$branch/status | cut
-d: -f 2-
112 local n
=`wc -l < $GUILT_DIR/$branch/status`
113 local n
=`expr $n - 1`
116 for p
in `cat $GUILT_DIR/$branch/status`; do
118 [ $idx -lt $n ] && continue
119 [ $idx -gt $n ] && break
127 # ignore all lines matching:
130 # - optional whitespace followed by '#' followed by more
131 # optional whitespace
132 grep -ve '^[[:space:]]*\(#.*\)*$' < $series
135 # usage: do_get_header patchfile
136 function do_get_header
138 # The complexity arises from the fact that we want to ignore the
139 # From line and the empty line after it if it exists
141 # 2nd line skips the From line
142 # 3rd line skips the empty line right after a From line
143 do_get_full_header
$1 |
awk '
145 /^From:/{skip=1; next}
146 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
152 # usage: do_get_full_header patchfile
153 function do_get_full_header
155 # 2nd line checks for the begining of a patch
156 # 3rd line outputs the line if it didn't get pruned by the above rules
165 # usage: assert_head_check
166 function assert_head_check
168 # make sure we're not doing funky things to commits that don't
170 local ch
=`cat $GIT_DIR/refs/heads/$branch`
171 local eh
=`tail -1 < $applied | cut -d: -f 1`
173 # if there are no patches pushed, there can be no mis-matches
174 [ -z "$eh" ] && return 0
176 if [ "$ch" != "$eh" ]; then
177 echo "Expected HEAD commit $eh" >&2
179 echo "aborting..." >&2
187 pop_many_patches HEAD^
1
190 # usage: pop_many_patches <commitish> <number of patches>
191 function pop_many_patches
197 git-reset
--hard $1 > /dev
/null
198 head -n -$2 < $applied > $applied.tmp
204 # usage: push_patch patchname
207 local p
="$GUILT_DIR/$branch/$1"
216 # apply the patch if and only if there is something to apply
217 if [ `git-apply --numstat $p | wc -l` -gt 0 ]; then
218 git-apply
-C$guilt_push_diff_context \
219 --reject $p > /dev
/null
2> /tmp
/guilt.log.$$
222 [ $bail -ne 0 ] && cat /tmp
/guilt.log.$$
>&2
224 # FIXME: Path munging is being done, we need to convince
225 # git-apply to just give us list of files with \0 as a
226 # delimiter, and pass -z to git-update-index
227 git-apply
--numstat $p | cut
-f 3- | git-update-index
--add --remove --stdin
230 # grab a commit message out of the patch
231 do_get_header
$p > /tmp
/guilt.msg.$$
233 # make a default commit message if patch doesn't contain one
234 [ ! -s /tmp
/guilt.msg.$$
] && echo "patch $pname" > /tmp
/guilt.msg.$$
236 # extract a From line from the patch header, and set
237 # GIT_AUTHOR_{NAME,EMAIL}
238 local author_str
=`cat $p | grep -e '^From: ' | sed -e 's/^From: //'`
239 if [ ! -z "$author_str" ]; then
240 local backup_author_name
="$GIT_AUTHOR_NAME"
241 local backup_author_email
="$GIT_AUTHOR_EMAIL"
242 GIT_AUTHOR_NAME
=`echo $author_str | sed -e 's/ *<.*$//'`
243 GIT_AUTHOR_EMAIL
=`echo $author_str | sed -e 's/[^<]*//'`
245 if [ -z "$GIT_AUTHOR_NAME" ]; then
249 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
253 local treeish
=`git-write-tree`
254 local commitish
=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
255 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
257 # mark patch as applied
258 echo "$commitish:$pname" >> $applied
260 # restore original GIT_AUTHOR_{NAME,EMAIL}
261 if [ ! -z "$author_str" ]; then
262 if [ ! -z "$backup_author_name" ]; then
263 export GIT_AUTHOR_NAME
="$backup_author_name"
265 unset GIT_AUTHOR_NAME
268 if [ ! -z "$backup_author_name" ]; then
269 export GIT_AUTHOR_EMAIL
="$backup_author_email"
271 unset GIT_AUTHOR_EMAIL
275 rm -f /tmp
/guilt.msg.$$
/tmp
/guilt.log.$$
282 # usage: must_commit_first
283 function must_commit_first
285 [ `git-diff-files | wc -l` -eq 0 ]
289 # usage: refresh_patch patchname
290 function refresh_patch
292 local p
="$GUILT_DIR/$branch/$1"
298 git-diff-files
--name-only |
(while read n
; do git-update-index
$n ; done)
300 # get the patch header
301 do_get_full_header
$p > /tmp
/guilt.
diff.$$
304 git-diff HEAD^
>> /tmp
/guilt.
diff.$$
306 # move the new patch in
308 mv /tmp
/guilt.
diff.$$
$p
312 # drop the currently applied patch, pop_patch does it's own cd
316 # push_patch does it's own cd $TOP_DIR
322 # used for: git-apply -C <val>
323 guilt_push_diff_context
=1
326 # The following gets run every time this file is source'd
329 GIT_DIR
=`find_git_dir`
330 [ $?
-ne 0 ] && exit 1
332 TOP_DIR
=`git-rev-parse --show-cdup`
333 if [ -z "$TOP_DIR" ]; then
337 GUILT_DIR
="$GIT_DIR/patches"
341 # most of the time we want to verify that the repo's branch has been
342 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
343 # we must avoid the checks
344 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
345 verify_branch ||
exit 1
349 series
="$GUILT_DIR/$branch/series"
350 applied
="$GUILT_DIR/$branch/status"
352 USAGE
="Usage: `basename $0`"