-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-recommit
executable file
·118 lines (91 loc) · 2.69 KB
/
git-recommit
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/sh
# Copyright (c) 2012, 2013, 2014 Michele Bini <[email protected]>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the version 3 of the GNU General Public License
# as published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set -e
println() {
printf "%s
" "$*"
}
warn() {
println "$0: $*" >&2
}
die() {
warn "$*"
exit 1
}
noerr() {
"$@" 2>/dev/null || true
}
# Directory where to save patches, can be absolute or relative to top-level of the worktree
patchdir="$(noerr git config --path --local --get git.uncommit.patchdir)"
# Top level of the work tree, if possible relative to current directory
topleveldir="$(noerr git rev-parse --show-cdup || noerr git rev-parse --show-toplevel)"
# Current directory, relative to top level of work tree
prefixdir="$(noerr git rev-parse --show-prefix)"
if [ -n "$patchdir" ]; then
patchdir="$( ([ -n "$topleveldir" ] && cd "$topleveldir") && cd "$patchdir" && pwd)"
fi
withfinalslash() {
if [ -z "$1" ]; then return; fi
case "$1" in
*/) println "$1" ;;
*) println "$1" ;;
esac
}
nocommit=""
while case "$1" in
--patchdir) shift; patchdir="$1"; shift ;;
--no-commit) shift; nocommit=1 ;;
--) shift; false ;;
--*) die "Unknown option: $1" ;;
*) false ;;
esac; do :; done
f="$1"
current_dir=1
patchdir="$(withfinalslash "$patchdir")"
topleveldir="$(withfinalslash "$topleveldir")"
prefixdir="$(withfinalslash "$prefixdir")"
u="$topleveldir".uncommitted-patches
if [ -z "$f" ]; then
if [ -f "$u" ]; then
f="$(tail -n1 "$u"|sed "s/^Uncommit: //")"
current_dir=""
sed -i "$ d" "$u"
fi
# Delete .uncommitted-patches if it's empty
if [ -e "$u" ]; then
[ -s "$u" ] || rm "$u"
fi
fi
# This should only be performed in interactive mode
if [ -z "$f" ]; then
# yn "Cannot find a patch to apply. Look for the most recent one?"
f="$(ls -R --sort=time|grep "[.]patch$"|head -n1)"
current_dir=1
fi
if [ -n "$patchdir" ]; then
f="$patchdir""$(basename "$f")"
elif [ -z "$current_dir" ]; then
case "$f" in
/*) : ;;
*) f="$topleveldir"/"$f" ;;
esac
fi
if [ -z "$f" ]; then
die "No patch files found!"
fi
if [ -n "$nocommit" ]; then
prehead="$(git rev-parse HEAD)"
fi
git am -3 "$f" && rm "$f"
if [ -n "$nocommit" ]; then
git reset --soft "$prehead"
fi