Ignore empty/commented series lines
[gq.git] / gq.lib
blob709395925deda52b1fa2869cbd77bfea3f747071
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 function get_series
68 # ignore all lines matching:
69 # - empty lines
70 # - whitespace only
71 # - optional whitespace followed by '#' followed by more
72 # optional whitespace
73 grep -ve '^[[:space:]]*\(#.*\)*$' < $series
76 # usage: index_update_magic
77 function index_update_magic
79 while read l; do
80 fil=`echo $l | cut -d: -f 2`
81 git-update-index --add --remove "$fil"
82 done
85 # usage: do_get_header patchfile
86 function do_get_header
88 cat $1 | awk 'BEGIN{ok=1}/^(diff|---)/{ok=0}{if (ok==1) print $0}'
91 # usage: pop_patch
92 function pop_patch
94 git reset --hard HEAD^
96 head -n -1 < $applied > $applied.tmp
97 mv $applied{.tmp,}
100 # usage: push_patch patchname
101 function push_patch
103 local p="$GQ_DIR/$branch/$1"
104 local pname="$1"
106 local bail=0
108 # apply the patch if and only if there is something to apply
109 if [ `wc -l < $p` -gt 0 ]; then
110 git-apply --reject $p > /dev/null 2> /tmp/gq.log.$$
111 bail=$?
112 [ $bail -ne 0 ] && cat /tmp/gq.log.$$
114 [ $bail -ne 0 ] && cat /tmp/gq.log.$$ >&2
116 ( git-apply --numstat $p | awk '{print "changed:" $3}';
117 #git-apply --summary $p | awk '
118 # /^ (create|delete)/{print $1 ":" $4}
119 # /^ mode change/{print "mode:" $6}'
120 )| index_update_magic
123 # grab a commit message out of the patch
124 do_get_header $p > /tmp/gq.msg.$$
126 # make a default commit message if patch doesn't contain one
127 [ ! -s /tmp/gq.msg.$$ ] && echo "patch $pname" > /tmp/gq.msg.$$
129 # commit
130 local treeish=`git-write-tree`
131 local commitish=`git-commit-tree $treeish -p HEAD < /tmp/gq.msg.$$`
132 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
134 rm -f /tmp/gq.msg.$$ /tmp/gq.log.$$
136 return $bail
139 # usage: must_commit_first
140 function must_commit_first
142 [ `git-diff-files | wc -l` -eq 0 ]
143 return $?
146 # usage: refresh_patch patchname
147 function refresh_patch
149 local p="$GQ_DIR/$branch/$1"
151 git-diff-files --name-only | (while read n; do git-update-index $n ; done)
153 # get the patch header
154 do_get_header $p > /tmp/gq.diff.$$
156 # get the new patch
157 git-diff HEAD^ >> /tmp/gq.diff.$$
159 # drop the current commit
160 git-reset --hard HEAD^
162 # move the new patch in
163 mv $p $p.prev
164 mv /tmp/gq.diff.$$ $p
166 push_patch $1
170 # The following gets run every time this file is source'd
173 export GIT_DIR=`find_git_dir`
174 [ $? -ne 0 ] && exit 1
176 GQ_DIR="$GIT_DIR/patches"
178 branch=`get_branch`
180 # most of the time we want to verify that the repo's branch has been
181 # initialized, but every once in a blue moon (e.g., we want to run gq-init),
182 # we must avoid the checks
183 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
184 verify_branch || exit 1
187 # very useful files
188 series="$GQ_DIR/$branch/series"
189 applied="$GQ_DIR/$branch/status"