makefile: Include import-commit in list of scripts
[guilt.git] / guilt
blob7b065c57d5efd30606552eda7a26cccc8e7cc8db
1 #!/bin/bash
3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 GUILT_VERSION="0.20"
7 GUILT_NAME="Buddy Holly"
9 if [ `basename $0` = "guilt" ]; then
10 # being run as standalone
12 # by default, we shouldn't fail
13 fail=0
15 if [ $# -ne 0 ]; then
16 # take first arg, and try to execute it
18 cmd="$1"
19 dir=`dirname $0`
21 if [ ! -x "$dir/guilt-$cmd" ]; then
22 echo "Command $cmd not found" >&2
23 echo "" >&2
24 fail=1
25 else
26 shift
27 exec "$dir/guilt-$cmd" "$@"
29 # this is not reached because of the exec
30 echo "Exec failed! Something is terribly wrong!" >&2
31 exit 1
35 # no args passed or invalid command entered, just output help summary
37 echo "Guilt v$GUILT_VERSION"
38 echo ""
39 echo "Pick a command:"
40 for x in `dirname $0`/guilt-*; do
41 [ -x $x ] && echo -e "\t`basename $x`"
42 done
44 echo ""
45 echo "Example:"
46 echo -e "\tguilt-push"
47 echo "or"
48 echo -e "\tguilt push"
50 # now, let's exit
51 exit $fail
54 ########
57 # Library goodies
60 function print_usage
62 echo "$USAGE" >&2
65 function find_git_dir
67 local d=`git-rev-parse --git-dir`
69 if [ $? -ne 0 -o -z "$d" ]; then
70 echo "Not a git repository" >&2
71 return 1
74 echo "$d"
77 function get_branch
79 git-symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
82 function verify_branch
84 local b=$branch
86 [ ! -d "$GIT_DIR/patches" ] &&
87 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
88 return 1
89 [ ! -d "$GIT_DIR/patches/$b" ] &&
90 echo "Branch $b is not initialized, try guilt-init" >&2 &&
91 return 1
92 [ ! -f "$GIT_DIR/patches/$b/series" ] &&
93 echo "Branch $b does not have a series file" >&2 &&
94 return 1
95 [ ! -f "$GIT_DIR/patches/$b/status" ] &&
96 echo "Branch $b does not have a status file" >&2 &&
97 return 1
98 [ -f "$GIT_DIR/patches/$b/applied" ] &&
99 echo "Warning: Branch $b has 'applied' file - guilt is not compatible with stgit" >&2 &&
100 return 1
102 return 0
105 function get_top
107 tail -1 $GUILT_DIR/$branch/status | cut -d: -f 2-
110 function get_prev
112 local n=`wc -l < $GUILT_DIR/$branch/status`
113 local n=`expr $n - 1`
115 local idx=0
116 for p in `cat $GUILT_DIR/$branch/status`; do
117 idx=`expr $idx + 1`
118 [ $idx -lt $n ] && continue
119 [ $idx -gt $n ] && break
121 echo $p
122 done
125 function get_series
127 # ignore all lines matching:
128 # - empty lines
129 # - whitespace only
130 # - optional whitespace followed by '#' followed by more
131 # optional whitespace
132 grep -ve '^[[:space:]]*\(#.*\)*$' < $series
135 # usage: do_make_header <hash>
136 function do_make_header
138 # FIXME: store the author string as "From: "
139 git-cat-file -p $rev | awk 'BEGIN{ok=0}(ok==1){print $0}/^$/{ok=1}'
142 # usage: do_get_header patchfile
143 function do_get_header
145 # The complexity arises from the fact that we want to ignore the
146 # From line and the empty line after it if it exists
148 # 2nd line skips the From line
149 # 3rd line skips the empty line right after a From line
150 do_get_full_header $1 | awk '
151 BEGIN{skip=0}
152 /^From:/{skip=1; next}
153 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
154 {print $0}
155 END{}
159 # usage: do_get_full_header patchfile
160 function do_get_full_header
162 # 2nd line checks for the begining of a patch
163 # 3rd line outputs the line if it didn't get pruned by the above rules
164 cat $1 | awk '
165 BEGIN{ok=1}
166 /^(diff|---)/{ok=0}
167 (ok==1){print $0}
168 END{}
172 # usage: assert_head_check
173 function assert_head_check
175 local eh=`tail -1 < $applied | cut -d: -f 1`
177 if ! head_check $eh; then
178 echo "aborting..." >&2
179 exit 1
182 return 0
185 # usage: head_check <expected hash>
186 function head_check
188 # make sure we're not doing funky things to commits that don't
189 # belong to us
190 local ch=`cat $GIT_DIR/refs/heads/$branch`
192 # if the expected hash is empty, just return
193 [ -z "$1" ] && return 0
195 if [ "$ch" != "$1" ]; then
196 echo "Expected HEAD commit $1" >&2
197 echo " got $ch" >&2
198 return 1
200 return 0
203 # usage: series_insert_patch <patchname>
204 function series_insert_patch
206 local top=`get_top`
208 if [ ! -z "$top" ]; then
209 sed -i -e "s,^$top\$,$top\n$1," $series
210 else
211 echo "$1" > $series.tmp
212 cat $series >> $series.tmp
213 mv $series.tmp $series
217 # usage: pop_patch
218 function pop_patch
220 pop_many_patches HEAD^ 1
223 # usage: pop_many_patches <commitish> <number of patches>
224 function pop_many_patches
226 assert_head_check
228 cd $TOP_DIR
230 git-reset --hard $1 > /dev/null
231 head -n -$2 < $applied > $applied.tmp
232 mv $applied{.tmp,}
234 cd - 2>&1 >/dev/null
237 # usage: push_patch patchname
238 function push_patch
240 local p="$GUILT_DIR/$branch/$1"
241 local pname="$1"
243 local bail=0
245 assert_head_check
247 cd $TOP_DIR
249 # apply the patch if and only if there is something to apply
250 if [ `git-apply --numstat $p | wc -l` -gt 0 ]; then
251 git-apply -C$guilt_push_diff_context \
252 --reject $p > /dev/null 2> /tmp/guilt.log.$$
253 bail=$?
255 [ $bail -ne 0 ] && cat /tmp/guilt.log.$$ >&2
257 # FIXME: Path munging is being done, we need to convince
258 # git-apply to just give us list of files with \0 as a
259 # delimiter, and pass -z to git-update-index
260 git-apply --numstat $p | cut -f 3- | git-update-index --add --remove --stdin
263 # grab a commit message out of the patch
264 do_get_header $p > /tmp/guilt.msg.$$
266 # make a default commit message if patch doesn't contain one
267 [ ! -s /tmp/guilt.msg.$$ ] && echo "patch $pname" > /tmp/guilt.msg.$$
269 # extract a From line from the patch header, and set
270 # GIT_AUTHOR_{NAME,EMAIL}
271 local author_str=`cat $p | grep -e '^From: ' | sed -e 's/^From: //'`
272 if [ ! -z "$author_str" ]; then
273 local backup_author_name="$GIT_AUTHOR_NAME"
274 local backup_author_email="$GIT_AUTHOR_EMAIL"
275 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
276 GIT_AUTHOR_EMAIL=`echo $author_str | sed -e 's/[^<]*//'`
278 if [ -z "$GIT_AUTHOR_NAME" ]; then
279 GIT_AUTHOR_NAME=" "
282 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
284 local backup_author_date="$GIT_AUTHOR_DATE"
285 local backup_committer_date="$GIT_COMMITTER_DATE"
286 export GIT_AUTHOR_DATE=`stat -c %y $p`
287 export GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE
289 # commit
290 local treeish=`git-write-tree`
291 local commitish=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
292 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
294 # mark patch as applied
295 echo "$commitish:$pname" >> $applied
297 # restore original GIT_AUTHOR_{NAME,EMAIL}
298 if [ ! -z "$author_str" ]; then
299 if [ ! -z "$backup_author_name" ]; then
300 export GIT_AUTHOR_NAME="$backup_author_name"
301 else
302 unset GIT_AUTHOR_NAME
305 if [ ! -z "$backup_author_name" ]; then
306 export GIT_AUTHOR_EMAIL="$backup_author_email"
307 else
308 unset GIT_AUTHOR_EMAIL
311 if [ ! -z "$backup_author_date" ]; then
312 export GIT_AUTHOR_DATE="$backup_author_date"
313 else
314 unset GIT_AUTHOR_DATE
316 if [ ! -z "$backup_committer_date" ]; then
317 export GIT_COMMITTER_DATE="$backup_committer_date"
318 else
319 unset GIT_COMMITTER_DATE
322 rm -f /tmp/guilt.msg.$$ /tmp/guilt.log.$$
324 cd - 2>&1 >/dev/null
326 return $bail
329 # usage: must_commit_first
330 function must_commit_first
332 [ `git-diff-files | wc -l` -eq 0 ]
333 return $?
336 # usage: refresh_patch patchname
337 function refresh_patch
339 local p="$GUILT_DIR/$branch/$1"
341 assert_head_check
343 cd $TOP_DIR
345 git-diff-files --name-only | (while read n; do git-update-index $n ; done)
347 # get the patch header
348 do_get_full_header $p > /tmp/guilt.diff.$$
350 # get the new patch
351 git-diff HEAD^ >> /tmp/guilt.diff.$$
353 # move the new patch in
354 mv $p $p.prev
355 mv /tmp/guilt.diff.$$ $p
357 cd - 2>&1 >/dev/null
359 # drop the currently applied patch, pop_patch does it's own cd
360 # $TOP_DIR
361 pop_patch
363 # push_patch does it's own cd $TOP_DIR
364 push_patch $1
367 # usage: munge_hash_range <hash range>
369 # this means:
370 # <hash> - one commit
371 # <hash>.. - hash until head (excludes hash, includes head)
372 # ..<hash> - until hash (includes hash)
373 # <hash1>..<hash2> - from hash to hash (inclusive)
375 # The output of this function is suitable to be passed to git-rev-list
376 function munge_hash_range
378 local l=`echo "$1" | sed -e 's/\.\./ /'`
380 local h1=`echo "$l" | cut -s -d' ' -f 1`
381 local h2=`echo "$l" | cut -s -d' ' -f 2`
383 if [ -z "$h1" -a -z "$h2" ]; then
384 # e.g., "v0.19"
385 echo "$l^..$l"
386 elif [ -z "$h1" ]; then
387 # e.g., "..v0.10"
388 echo "$h2"
389 elif [ -z "$h2" ]; then
390 # e.g., "v0.19.."
391 echo "$h1..HEAD"
392 else
393 # e.g., "v0.19-rc1..v0.19"
394 echo "$h1..$h2"
398 # Some constants
400 # used for: git-apply -C <val>
401 guilt_push_diff_context=1
404 # The following gets run every time this file is source'd
407 GIT_DIR=`find_git_dir`
408 [ $? -ne 0 ] && exit 1
410 TOP_DIR=`git-rev-parse --show-cdup`
411 if [ -z "$TOP_DIR" ]; then
412 TOP_DIR="./"
415 GUILT_DIR="$GIT_DIR/patches"
417 branch=`get_branch`
419 # most of the time we want to verify that the repo's branch has been
420 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
421 # we must avoid the checks
422 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
423 verify_branch || exit 1
426 # very useful files
427 series="$GUILT_DIR/$branch/series"
428 applied="$GUILT_DIR/$branch/status"
430 USAGE="Usage: `basename $0`"
432 # determine an editor to use for anything interactive (fall back to vi)
433 editor="vi"
434 [ ! -z "$EDITOR" ] && editor="$EDITOR"
436 # determine a pager to use for anything interactive (fall back to more)
437 pager="more"
438 [ -z "$pager" ] && pager="$PAGER"