3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 if [ `basename $0` = "guilt" ]; then
7 # being run as standalone
9 # by default, we shouldn't fail
13 # take first arg, and try to execute it
18 if [ ! -x "$dir/guilt-$cmd" ]; then
19 echo "Command $cmd not found" >&2
24 exec "$dir/guilt-$cmd" "$@"
26 # this is not reached because of the exec
27 echo "Exec failed! Something is terribly wrong!" >&2
32 # no args passed or invalid command entered, just output help summary
34 echo "Pick a command:"
35 for x
in `dirname $0`/guilt-
*; do
36 [ -x $x ] && echo -e "\t`basename $x`"
41 echo -e "\tguilt-push"
43 echo -e "\tguilt push"
57 local d
=`git-rev-parse --git-dir`
59 if [ $?
-ne 0 -o -z "$d" ]; then
60 echo "Not a git repository" >&2
69 git-symbolic-ref HEAD |
sed -e 's,^refs/heads/,,'
72 function verify_branch
76 [ ! -d "$GIT_DIR/patches" ] &&
77 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
79 [ ! -d "$GIT_DIR/patches/$b" ] &&
80 echo "Branch $b is not initialized, try guilt-init" >&2 &&
82 [ ! -f "$GIT_DIR/patches/$b/series" ] &&
83 echo "Branch $b does not have a series file" >&2 &&
85 [ ! -f "$GIT_DIR/patches/$b/status" ] &&
86 echo "Branch $b does not have a status file" >&2 &&
88 [ -f "$GIT_DIR/patches/$b/applied" ] &&
89 echo "Warning: Branch $b has 'applied' file - guilt is not compatible with stgit" >&2 &&
97 tail -1 $GUILT_DIR/$branch/status
102 local n
=`wc -l < $GUILT_DIR/$branch/status`
103 local n
=`expr $n - 1`
106 for p
in `cat $GUILT_DIR/$branch/status`; do
108 [ $idx -lt $n ] && continue
109 [ $idx -gt $n ] && break
117 # ignore all lines matching:
120 # - optional whitespace followed by '#' followed by more
121 # optional whitespace
122 grep -ve '^[[:space:]]*\(#.*\)*$' < $series
125 # usage: index_update_magic
126 function index_update_magic
129 fil
=`echo $l | cut -d: -f 2`
130 git-update-index
--add --remove "$fil"
134 # usage: do_get_header patchfile
135 function do_get_header
137 cat $1 |
awk 'BEGIN{ok=1}/^(diff|---)/{ok=0}{if (ok==1) print $0}'
143 git
reset --hard HEAD^
145 head -n -1 < $applied > $applied.tmp
149 # usage: push_patch patchname
152 local p
="$GUILT_DIR/$branch/$1"
157 # apply the patch if and only if there is something to apply
158 if [ `wc -l < $p` -gt 0 ]; then
159 git-apply
--reject $p > /dev
/null
2> /tmp
/guilt.log.$$
162 [ $bail -ne 0 ] && cat /tmp
/guilt.log.$$
>&2
164 ( git-apply
--numstat $p |
awk '{print "changed:" $3}';
165 #git-apply --summary $p | awk '
166 # /^ (create|delete)/{print $1 ":" $4}
167 # /^ mode change/{print "mode:" $6}'
168 )| index_update_magic
171 # grab a commit message out of the patch
172 do_get_header
$p > /tmp
/guilt.msg.$$
174 # make a default commit message if patch doesn't contain one
175 [ ! -s /tmp
/guilt.msg.$$
] && echo "patch $pname" > /tmp
/guilt.msg.$$
178 local treeish
=`git-write-tree`
179 local commitish
=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
180 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
182 rm -f /tmp
/guilt.msg.$$
/tmp
/guilt.log.$$
187 # usage: must_commit_first
188 function must_commit_first
190 [ `git-diff-files | wc -l` -eq 0 ]
194 # usage: refresh_patch patchname
195 function refresh_patch
197 local p
="$GUILT_DIR/$branch/$1"
199 git-diff-files
--name-only |
(while read n
; do git-update-index
$n ; done)
201 # get the patch header
202 do_get_header
$p > /tmp
/guilt.
diff.$$
205 git-diff HEAD^
>> /tmp
/guilt.
diff.$$
207 # drop the current commit
208 git-reset
--hard HEAD^
210 # move the new patch in
212 mv /tmp
/guilt.
diff.$$
$p
218 # The following gets run every time this file is source'd
221 export GIT_DIR
=`find_git_dir`
222 [ $?
-ne 0 ] && exit 1
224 GUILT_DIR
="$GIT_DIR/patches"
228 # most of the time we want to verify that the repo's branch has been
229 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
230 # we must avoid the checks
231 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
232 verify_branch ||
exit 1
236 series
="$GUILT_DIR/$branch/series"
237 applied
="$GUILT_DIR/$branch/status"