gq-header & TODO update
[guilt.git] / gq.lib
blobdf18a12f379fd448199afbd6924d73290e84ddaf
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 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 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 n=`wc -l < $GQ_DIR/$branch/status`
45 n=`expr $n - 1`
47 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 function index_update_magic
59 while read l; do
60 fil=`echo $l | cut -d: -f 2`
61 git-update-index --add --remove "$fil"
62 done
65 # usage: do_get_header patchfile
66 function do_get_header
68 cat $1 | awk 'BEGIN{ok=1}/^(diff|---)/{ok=0}{if (ok==1) print $0}'
71 # usage: push_patch patchname
72 function push_patch
74 local p="$GQ_DIR/$branch/$1"
75 local pname="$1"
77 git-apply --reject $p
78 local bail=$?
80 ( git-apply --numstat $p | awk '{print "changed:" $3}';
81 #git-apply --summary $p | awk '
82 # /^ (create|delete)/{print $1 ":" $4}
83 # /^ mode change/{print "mode:" $6}'
84 )| index_update_magic
86 # grab a commit message out of the patch
87 do_get_header $p > /tmp/gq.msg.$$
89 # make a default commit message if patch doesn't contain one
90 [ ! -s /tmp/gq.msg.$$ ] && echo "patch $pname" > /tmp/gq.msg.$$
92 # commit
93 local treeish=`git-write-tree`
94 local commitish=`git-commit-tree $treeish -p HEAD < /tmp/gq.msg.$$`
95 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
97 rm -f /tmp/gq.msg.$$
99 return $bail
102 function must_commit_first
104 [ `git-diff-files | wc -l` -eq 0 ]
105 return $?