Rename regression scripts to enforce certain order
[guilt.git] / gq.lib
blob18d2f7fc0688705ebd60f1a0d647791c12677c0e
1 #!/bin/bash
3 # Copyright (c) Josef "Jeff" Sipek, 2006
6 function find_git_dir_nice
8 git-rev-parse --git-dir
11 function find_git_dir
13 local d=`find_git_dir_nice`
15 [ $? -ne 0 -o -z "$d" ] && exit 1
17 echo "$d"
20 function get_branch
22 git-symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
25 function get_branch_verify
27 local b=`get_branch`
29 [ ! -d "$GIT_DIR/patches/$b" ] && echo "Branch $b is not initialized, try gq-init" >&2 && exit 1
30 [ ! -f "$GIT_DIR/patches/$b/series" ] && echo "Branch $b does not have a series file" >&2 && exit 1
31 [ ! -f "$GIT_DIR/patches/$b/status" ] && echo "Branch $b does not have a status file" >&2 && exit 1
32 [ -f "$GIT_DIR/patches/$b/applied" ] && echo "Warning: Branch $b has 'applied' file - gq is not compatible with stgit" >&2
34 echo "$b"
37 function get_top
39 tail -1 $GQ_DIR/$branch/status
42 function get_prev
44 local n=`wc -l < $GQ_DIR/$branch/status`
45 local n=`expr $n - 1`
47 local idx=0
48 for p in `cat $GQ_DIR/$branch/status`; do
49 idx=`expr $idx + 1`
50 [ $idx -lt $n ] && continue
51 [ $idx -gt $n ] && break
53 echo $p
54 done
57 # usage: index_update_magic
58 function index_update_magic
60 while read l; do
61 fil=`echo $l | cut -d: -f 2`
62 git-update-index --add --remove "$fil"
63 done
66 # usage: do_get_header patchfile
67 function do_get_header
69 cat $1 | awk 'BEGIN{ok=1}/^(diff|---)/{ok=0}{if (ok==1) print $0}'
72 # usage: pop_patch
73 function pop_patch
75 git reset --hard HEAD^
77 head -n -1 < $applied > $applied.tmp
78 mv $applied{.tmp,}
81 # usage: push_patch patchname
82 function push_patch
84 local p="$GQ_DIR/$branch/$1"
85 local pname="$1"
87 local bail=0
89 # apply the patch if and only if there is something to apply
90 if [ `wc -l < $p` -gt 0 ]; then
91 git-apply --reject $p > /dev/null 2> /tmp/gq.log.$$
92 bail=$?
93 [ $bail -ne 0 ] && cat /tmp/gq.log.$$
95 [ $bail -ne 0 ] && cat /tmp/gq.log.$$ >&2
97 ( git-apply --numstat $p | awk '{print "changed:" $3}';
98 #git-apply --summary $p | awk '
99 # /^ (create|delete)/{print $1 ":" $4}
100 # /^ mode change/{print "mode:" $6}'
101 )| index_update_magic
104 # grab a commit message out of the patch
105 do_get_header $p > /tmp/gq.msg.$$
107 # make a default commit message if patch doesn't contain one
108 [ ! -s /tmp/gq.msg.$$ ] && echo "patch $pname" > /tmp/gq.msg.$$
110 # commit
111 local treeish=`git-write-tree`
112 local commitish=`git-commit-tree $treeish -p HEAD < /tmp/gq.msg.$$`
113 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
115 rm -f /tmp/gq.msg.$$ /tmp/gq.log.$$
117 return $bail
120 # usage: must_commit_first
121 function must_commit_first
123 [ `git-diff-files | wc -l` -eq 0 ]
124 return $?
127 # usage: refresh_patch patchname
128 function refresh_patch
130 local p="$GQ_DIR/$branch/$1"
132 git-diff-files --name-only | (while read n; do git-update-index $n ; done)
134 # get the patch header
135 do_get_header $p > /tmp/gq.diff.$$
137 # get the new patch
138 git-diff HEAD^ >> /tmp/gq.diff.$$
140 # drop the current commit
141 git-reset --hard HEAD^
143 # move the new patch in
144 mv $p $p.prev
145 mv /tmp/gq.diff.$$ $p
147 push_patch $1