Merged guilt.lib into guilt
[guilt.git] / guilt
blob9c3e180b5ea91f2eca7def41a99d87c0f39d09d1
1 #!/bin/bash
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
10 fail=0
12 if [ $# -ne 0 ]; then
13 # take first arg, and try to execute it
15 cmd="$1"
16 dir=`dirname $0`
18 if [ ! -x "$dir/guilt-$cmd" ]; then
19 echo "Command $cmd not found" >&2
20 echo "" >&2
21 fail=1
22 else
23 shift
24 exec "$dir/guilt-$cmd" "$@"
26 # this is not reached because of the exec
27 echo "Exec failed! Something is terribly wrong!" >&2
28 exit 1
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`"
37 done
39 echo ""
40 echo "Example:"
41 echo -e "\tguilt-push"
42 echo "or"
43 echo -e "\tguilt push"
45 # now, let's exit
46 exit $fail
49 ########
52 # Library goodies
55 function find_git_dir
57 local d=`git-rev-parse --git-dir`
59 if [ $? -ne 0 -o -z "$d" ]; then
60 echo "Not a git repository" >&2
61 return 1
64 echo "$d"
67 function get_branch
69 git-symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
72 function verify_branch
74 local b=$branch
76 [ ! -d "$GIT_DIR/patches" ] &&
77 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
78 return 1
79 [ ! -d "$GIT_DIR/patches/$b" ] &&
80 echo "Branch $b is not initialized, try guilt-init" >&2 &&
81 return 1
82 [ ! -f "$GIT_DIR/patches/$b/series" ] &&
83 echo "Branch $b does not have a series file" >&2 &&
84 return 1
85 [ ! -f "$GIT_DIR/patches/$b/status" ] &&
86 echo "Branch $b does not have a status file" >&2 &&
87 return 1
88 [ -f "$GIT_DIR/patches/$b/applied" ] &&
89 echo "Warning: Branch $b has 'applied' file - guilt is not compatible with stgit" >&2 &&
90 return 1
92 return 0
95 function get_top
97 tail -1 $GUILT_DIR/$branch/status
100 function get_prev
102 local n=`wc -l < $GUILT_DIR/$branch/status`
103 local n=`expr $n - 1`
105 local idx=0
106 for p in `cat $GUILT_DIR/$branch/status`; do
107 idx=`expr $idx + 1`
108 [ $idx -lt $n ] && continue
109 [ $idx -gt $n ] && break
111 echo $p
112 done
115 function get_series
117 # ignore all lines matching:
118 # - empty lines
119 # - whitespace only
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
128 while read l; do
129 fil=`echo $l | cut -d: -f 2`
130 git-update-index --add --remove "$fil"
131 done
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}'
140 # usage: pop_patch
141 function pop_patch
143 git reset --hard HEAD^
145 head -n -1 < $applied > $applied.tmp
146 mv $applied{.tmp,}
149 # usage: push_patch patchname
150 function push_patch
152 local p="$GUILT_DIR/$branch/$1"
153 local pname="$1"
155 local bail=0
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.$$
160 bail=$?
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.$$
177 # commit
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.$$
184 return $bail
187 # usage: must_commit_first
188 function must_commit_first
190 [ `git-diff-files | wc -l` -eq 0 ]
191 return $?
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.$$
204 # get the new patch
205 git-diff HEAD^ >> /tmp/guilt.diff.$$
207 # drop the current commit
208 git-reset --hard HEAD^
210 # move the new patch in
211 mv $p $p.prev
212 mv /tmp/guilt.diff.$$ $p
214 push_patch $1
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"
226 branch=`get_branch`
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
235 # very useful files
236 series="$GUILT_DIR/$branch/series"
237 applied="$GUILT_DIR/$branch/status"