-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-qpop
executable file
·101 lines (82 loc) · 2.13 KB
/
git-qpop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
#
# Copyright (C) 2013 MORITA Kazutaka <[email protected]>
#
# This code is based on guilt-pop from Guilt (Git Quilt):
# Copyright (c) Josef "Jeff" Sipek, 2006-2011
#
. git-qsh-setup
USAGE="[-f] [-a | --all | -n <num> | <patchname>]"
while [ $# -gt 0 ]; do
case "$1" in
-f)
force=t
;;
-a|--all)
all=t
;;
-n)
num=t
;;
*)
break
;;
esac
shift
done
# "git qpop" or "git qpop foo" or "git qpop -n foo"
if [ -z "$all" ] && [ $# -gt 1 ]; then
usage
fi
# "git qpop -n foo"
if [ ! -z "$num" ]; then
if [ $# -gt 1 ] || [ $# -eq 0 ]; then
usage
fi
fi
# "git qpop -a"
if [ ! -z "$all" ] && [ $# -gt 0 ]; then
usage
fi
patch="$1"
[ ! -z "$all" ] && patch="-a"
if [ ! -s "$applied" ]; then
echo "No patches applied."
exit 0
elif [ "$patch" = "-a" ]; then
# we are supposed to pop all patches
sidx=`wc -l < "$applied"`
eidx=0
elif [ ! -z "$num" ]; then
# we are supposed to pop a set number of patches
[ "$patch" -lt 0 ] && die "Invalid number of patches to pop."
sidx=`wc -l < "$applied"`
eidx=`expr $sidx - $patch`
# catch underflow
[ $eidx -lt 0 ] && eidx=0
[ $eidx -eq $sidx ] && die "No patches requested to be removed."
elif [ -z "$patch" ]; then
# we are supposed to pop only the current patch on the stack
sidx=`wc -l < "$applied"`
eidx=`expr $sidx - 1`
else
# we're supposed to pop only up to a patch, make sure the patch is
# in the series
eidx=`cat "$applied" | grep -ne "^$patch$" | cut -d: -f 1`
if [ -z "$eidx" ]; then
die "Patch $patch is not in the series/is not applied"
fi
eidx=`expr $eidx - 1`
sidx=`wc -l < "$applied"`
fi
# make sure that there are no unapplied changes
# if we are forcing the pop, reset first
if [ ! -z "$force" ]; then
git reset --hard
elif ! must_commit_first; then
die "Uncommited changes detected. Refresh first."
fi
l=`awk "BEGIN{n=0}(n==$eidx){print \\$0; exit}{n=n+1}END{}" < "$applied"`
pop_many_patches `git rev-parse refs/patches/$branch/$l^` `expr $sidx - $eidx`
p=`get_top`
[ ! -z "$p" ] && echo "Now at $p." || echo "All patches popped."