Fixed a bug in guilt-pop introduced by the status file format switch
[guilt.git] / guilt
bloba32a35ccbf52e694778a15157465d0ef0ad02a7c
1 #!/bin/bash
3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 GUILT_VERSION="0.15"
8 if [ `basename $0` = "guilt" ]; then
9 # being run as standalone
11 # by default, we shouldn't fail
12 fail=0
14 if [ $# -ne 0 ]; then
15 # take first arg, and try to execute it
17 cmd="$1"
18 dir=`dirname $0`
20 if [ ! -x "$dir/guilt-$cmd" ]; then
21 echo "Command $cmd not found" >&2
22 echo "" >&2
23 fail=1
24 else
25 shift
26 exec "$dir/guilt-$cmd" "$@"
28 # this is not reached because of the exec
29 echo "Exec failed! Something is terribly wrong!" >&2
30 exit 1
34 # no args passed or invalid command entered, just output help summary
36 echo "Guilt v$GUILT_VERSION"
37 echo ""
38 echo "Pick a command:"
39 for x in `dirname $0`/guilt-*; do
40 [ -x $x ] && echo -e "\t`basename $x`"
41 done
43 echo ""
44 echo "Example:"
45 echo -e "\tguilt-push"
46 echo "or"
47 echo -e "\tguilt push"
49 # now, let's exit
50 exit $fail
53 ########
56 # Library goodies
59 function find_git_dir
61 local d=`git-rev-parse --git-dir`
63 if [ $? -ne 0 -o -z "$d" ]; then
64 echo "Not a git repository" >&2
65 return 1
68 echo "$d"
71 function get_branch
73 git-symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
76 function verify_branch
78 local b=$branch
80 [ ! -d "$GIT_DIR/patches" ] &&
81 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
82 return 1
83 [ ! -d "$GIT_DIR/patches/$b" ] &&
84 echo "Branch $b is not initialized, try guilt-init" >&2 &&
85 return 1
86 [ ! -f "$GIT_DIR/patches/$b/series" ] &&
87 echo "Branch $b does not have a series file" >&2 &&
88 return 1
89 [ ! -f "$GIT_DIR/patches/$b/status" ] &&
90 echo "Branch $b does not have a status file" >&2 &&
91 return 1
92 [ -f "$GIT_DIR/patches/$b/applied" ] &&
93 echo "Warning: Branch $b has 'applied' file - guilt is not compatible with stgit" >&2 &&
94 return 1
96 return 0
99 function get_top
101 tail -1 $GUILT_DIR/$branch/status | cut -d: -f 2-
104 function get_prev
106 local n=`wc -l < $GUILT_DIR/$branch/status`
107 local n=`expr $n - 1`
109 local idx=0
110 for p in `cat $GUILT_DIR/$branch/status`; do
111 idx=`expr $idx + 1`
112 [ $idx -lt $n ] && continue
113 [ $idx -gt $n ] && break
115 echo $p
116 done
119 function get_series
121 # ignore all lines matching:
122 # - empty lines
123 # - whitespace only
124 # - optional whitespace followed by '#' followed by more
125 # optional whitespace
126 grep -ve '^[[:space:]]*\(#.*\)*$' < $series
129 # usage: index_update_magic
130 function index_update_magic
132 while read l; do
133 fil=`echo $l | cut -d: -f 2`
134 git-update-index --add --remove "$fil"
135 done
138 # usage: do_get_header patchfile
139 function do_get_header
141 # The complexity arises from the fact that we want to ignore the
142 # From line and the empty line after it if it exists
144 # 2nd line skips the From line
145 # 3rd line skips the empty line right after a From line
146 do_get_full_header $1 | awk '
147 BEGIN{skip=0}
148 /^From:/{skip=1; next}
149 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
150 {print $0}
151 END{}
155 # usage: do_get_full_header patchfile
156 function do_get_full_header
158 # 2nd line checks for the begining of a patch
159 # 3rd line outputs the line if it didn't get pruned by the above rules
160 cat $1 | awk '
161 BEGIN{ok=1}
162 /^(diff|---)/{ok=0}
163 (ok==1){print $0}
164 END{}
168 # usage: pop_patch
169 function pop_patch
171 cd $TOP_DIR
173 git reset --hard HEAD^
175 head -n -1 < $applied > $applied.tmp
176 mv $applied{.tmp,}
178 cd - 2>&1 >/dev/null
181 # usage: push_patch patchname
182 function push_patch
184 local p="$GUILT_DIR/$branch/$1"
185 local pname="$1"
187 local bail=0
189 cd $TOP_DIR
191 # apply the patch if and only if there is something to apply
192 if [ `wc -l < $p` -gt 0 ]; then
193 git-apply --reject $p > /dev/null 2> /tmp/guilt.log.$$
194 bail=$?
196 [ $bail -ne 0 ] && cat /tmp/guilt.log.$$ >&2
198 ( git-apply --numstat $p | awk '{print "changed:" $3}';
199 #git-apply --summary $p | awk '
200 # /^ (create|delete)/{print $1 ":" $4}
201 # /^ mode change/{print "mode:" $6}'
202 )| index_update_magic
205 # grab a commit message out of the patch
206 do_get_header $p > /tmp/guilt.msg.$$
208 # make a default commit message if patch doesn't contain one
209 [ ! -s /tmp/guilt.msg.$$ ] && echo "patch $pname" > /tmp/guilt.msg.$$
211 # extract a From line from the patch header, and set
212 # GIT_AUTHOR_{NAME,EMAIL}
213 local author_str=`cat $p | grep -e '^From: ' | sed -e 's/^From: //'`
214 if [ ! -z "$author_str" ]; then
215 local backup_author_name="$GIT_AUTHOR_NAME"
216 local backup_author_email="$GIT_AUTHOR_EMAIL"
217 export GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
218 export GIT_AUTHOR_EMAIL=`echo $author_str | sed -e 's/[^<]*//'`
221 # commit
222 local treeish=`git-write-tree`
223 local commitish=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
224 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
226 # mark patch as applied
227 echo "$commitish:$pname" >> $applied
229 # restore original GIT_AUTHOR_{NAME,EMAIL}
230 if [ ! -z "$author_str" ]; then
231 if [ ! -z "$backup_author_name" ]; then
232 export GIT_AUTHOR_NAME="$backup_author_name"
233 else
234 unset GIT_AUTHOR_NAME
237 if [ ! -z "$backup_author_name" ]; then
238 export GIT_AUTHOR_EMAIL="$backup_author_email"
239 else
240 unset GIT_AUTHOR_EMAIL
244 rm -f /tmp/guilt.msg.$$ /tmp/guilt.log.$$
246 cd - 2>&1 >/dev/null
248 return $bail
251 # usage: must_commit_first
252 function must_commit_first
254 [ `git-diff-files | wc -l` -eq 0 ]
255 return $?
258 # usage: refresh_patch patchname
259 function refresh_patch
261 local p="$GUILT_DIR/$branch/$1"
263 cd $TOP_DIR
265 git-diff-files --name-only | (while read n; do git-update-index $n ; done)
267 # get the patch header
268 do_get_full_header $p > /tmp/guilt.diff.$$
270 # get the new patch
271 git-diff HEAD^ >> /tmp/guilt.diff.$$
273 # drop the current commit
274 git-reset --hard HEAD^
276 # move the new patch in
277 mv $p $p.prev
278 mv /tmp/guilt.diff.$$ $p
280 cd - 2>&1 >/dev/null
282 # push_patch does it's own cd $TOP_DIR
283 push_patch $1
287 # The following gets run every time this file is source'd
290 GIT_DIR=`find_git_dir`
291 [ $? -ne 0 ] && exit 1
293 TOP_DIR=`git-rev-parse --show-cdup`
294 if [ -z "$TOP_DIR" ]; then
295 TOP_DIR="./"
298 GUILT_DIR="$GIT_DIR/patches"
300 branch=`get_branch`
302 # most of the time we want to verify that the repo's branch has been
303 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
304 # we must avoid the checks
305 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
306 verify_branch || exit 1
309 # very useful files
310 series="$GUILT_DIR/$branch/series"
311 applied="$GUILT_DIR/$branch/status"