Check for outstanding changes before pushing/poping a patch
[guilt.git] / gq.lib
blobe5ae2814fe9962966a6143e56a8bb79ad19f0ae7
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 refresh_patch
59 rev="$1"
60 fname="$GQ_DIR/$branch/$2"
62 # grab the commit message
63 git-cat-file -p $rev | awk 'BEGIN{ok=0}{if (ok==1) print $0}/^$/{ok=1}' > "$fname.tmp"
65 lines=`wc -l < "$fname.tmp"`
66 tline=`cat "$fname.tmp"`
67 if [ $lines -eq 1 -a "$tline" = "patch $2" ]; then
68 # no real commit message
69 echo -n "" > "$fname.tmp"
70 else
71 # real commit message, separate by an empty line
72 echo "" >> "$fname.tmp"
75 # get the diff
76 git-diff $rev^ $rev >> "$fname.tmp"
78 mv "$fname.tmp" "$fname"
81 function index_update_magic
83 while read l; do
84 fil=`echo $l | cut -d: -f 2`
85 git-update-index --add --remove "$fil"
86 done
89 # usage: push_patch patch
90 function push_patch
92 local p="$GQ_DIR/$branch/$1"
93 local pname="$1"
95 git-apply --reject $p
96 local bail=$?
98 ( git-apply --numstat $p | awk '{print "changed:" $3}';
99 #git-apply --summary $p | awk '
100 # /^ (create|delete)/{print $1 ":" $4}
101 # /^ mode change/{print "mode:" $6}'
102 )| index_update_magic
104 # grab a commit message out of the patch
105 cat $p | awk 'BEGIN{ok=1}/^(diff|---)/{ok=0}{if (ok==1) print $0}' > /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.$$
117 return $bail
120 function must_commit_first
122 [ `git-diff-files | wc -l` -eq 0 ]
123 return $?