Major cleanup/refactoring
[guilt.git] / gq.lib
blob5becf983f97f584e9e949d09c80de9d938c9fc81
1 #!/bin/bash
3 # Copyright (c) Josef "Jeff" Sipek, 2006
6 function find_git_dir
8 local d=`git-rev-parse --git-dir`
10 if [ $? -ne 0 -o -z "$d" ]; then
11 echo "Not a git repository" >&2
12 return 1
15 echo "$d"
18 function get_branch
20 git-symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
23 function verify_branch
25 local b=$branch
27 [ ! -d "$GIT_DIR/patches" ] &&
28 echo "Patches directory doesn't exist, try gq-init" >&2 &&
29 return 1
30 [ ! -d "$GIT_DIR/patches/$b" ] &&
31 echo "Branch $b is not initialized, try gq-init" >&2 &&
32 return 1
33 [ ! -f "$GIT_DIR/patches/$b/series" ] &&
34 echo "Branch $b does not have a series file" >&2 &&
35 return 1
36 [ ! -f "$GIT_DIR/patches/$b/status" ] &&
37 echo "Branch $b does not have a status file" >&2 &&
38 return 1
39 [ -f "$GIT_DIR/patches/$b/applied" ] &&
40 echo "Warning: Branch $b has 'applied' file - gq is not compatible with stgit" >&2 &&
41 return 1
43 return 0
46 function get_top
48 tail -1 $GQ_DIR/$branch/status
51 function get_prev
53 local n=`wc -l < $GQ_DIR/$branch/status`
54 local n=`expr $n - 1`
56 local idx=0
57 for p in `cat $GQ_DIR/$branch/status`; do
58 idx=`expr $idx + 1`
59 [ $idx -lt $n ] && continue
60 [ $idx -gt $n ] && break
62 echo $p
63 done
66 # usage: index_update_magic
67 function index_update_magic
69 while read l; do
70 fil=`echo $l | cut -d: -f 2`
71 git-update-index --add --remove "$fil"
72 done
75 # usage: do_get_header patchfile
76 function do_get_header
78 cat $1 | awk 'BEGIN{ok=1}/^(diff|---)/{ok=0}{if (ok==1) print $0}'
81 # usage: pop_patch
82 function pop_patch
84 git reset --hard HEAD^
86 head -n -1 < $applied > $applied.tmp
87 mv $applied{.tmp,}
90 # usage: push_patch patchname
91 function push_patch
93 local p="$GQ_DIR/$branch/$1"
94 local pname="$1"
96 local bail=0
98 # apply the patch if and only if there is something to apply
99 if [ `wc -l < $p` -gt 0 ]; then
100 git-apply --reject $p > /dev/null 2> /tmp/gq.log.$$
101 bail=$?
102 [ $bail -ne 0 ] && cat /tmp/gq.log.$$
104 [ $bail -ne 0 ] && cat /tmp/gq.log.$$ >&2
106 ( git-apply --numstat $p | awk '{print "changed:" $3}';
107 #git-apply --summary $p | awk '
108 # /^ (create|delete)/{print $1 ":" $4}
109 # /^ mode change/{print "mode:" $6}'
110 )| index_update_magic
113 # grab a commit message out of the patch
114 do_get_header $p > /tmp/gq.msg.$$
116 # make a default commit message if patch doesn't contain one
117 [ ! -s /tmp/gq.msg.$$ ] && echo "patch $pname" > /tmp/gq.msg.$$
119 # commit
120 local treeish=`git-write-tree`
121 local commitish=`git-commit-tree $treeish -p HEAD < /tmp/gq.msg.$$`
122 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
124 rm -f /tmp/gq.msg.$$ /tmp/gq.log.$$
126 return $bail
129 # usage: must_commit_first
130 function must_commit_first
132 [ `git-diff-files | wc -l` -eq 0 ]
133 return $?
136 # usage: refresh_patch patchname
137 function refresh_patch
139 local p="$GQ_DIR/$branch/$1"
141 git-diff-files --name-only | (while read n; do git-update-index $n ; done)
143 # get the patch header
144 do_get_header $p > /tmp/gq.diff.$$
146 # get the new patch
147 git-diff HEAD^ >> /tmp/gq.diff.$$
149 # drop the current commit
150 git-reset --hard HEAD^
152 # move the new patch in
153 mv $p $p.prev
154 mv /tmp/gq.diff.$$ $p
156 push_patch $1
160 # The following gets run every time this file is source'd
163 export GIT_DIR=`find_git_dir`
164 [ $? -ne 0 ] && exit 1
166 GQ_DIR="$GIT_DIR/patches"
168 branch=`get_branch`
170 # most of the time we want to verify that the repo's branch has been
171 # initialized, but every once in a blue moon (e.g., we want to run gq-init),
172 # we must avoid the checks
173 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
174 verify_branch || exit 1
177 # very useful files
178 series="$GQ_DIR/$branch/series"
179 applied="$GQ_DIR/$branch/status"