forked from bartman/git-wip
/
git-wip
executable file
·328 lines (263 loc) · 5.89 KB
/
git-wip
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/bin/sh
#
# Copyright Bart Trojanowski <bart@jukie.net>
#
# git-wip is a script that will manage Work In Progress (or WIP) branches.
# WIP branches are mostly throw away but identify points of development
# between commits. The intent is to tie this script into your editor so
# that each time you save your file, the git-wip script captures that
# state in git. git-wip also helps you return back to a previous state of
# development.
#
# See also http://github.com/bartman/git-wip
#
# The code is licensed as GPL v2 or, at your option, any later version.
# Please see http://www.gnu.org/licenses/gpl-2.0.txt
#
USAGE='[ info | save <message> [ --editor | --untracked ] | log [ --pretty ] | delete ] [ [--] <file>... ]'
LONG_USAGE="Manage Work In Progress branches
Commands:
git wip - create a new WIP commit
git wip save <message> - create a new WIP commit with custom message
git wip info [<branch>] - brief WIP info
git wip log [<branch>] - show changes on the WIP branch
git wip delete [<branch>] - delete a WIP branch
Options for save:
-e --editor - be less verbose, assume called from an editor
-u --untracked - capture also untracked files
-i --ignored - capture also ignored files
Options for log:
-p --pretty - show a pretty graph
"
SUBDIRECTORY_OK=Yes
OPTIONS_SPEC=
. "$(git --exec-path)/git-sh-setup"
require_work_tree
TMP="$GIT_DIR/.git-wip.$$"
trap 'rm -f "$TMP-*"' 0
WIP_INDEX="$TMP-INDEX"
WIP_PREFIX=refs/wip/
WIP_COMMAND=
WIP_MESSAGE=WIP
EDITOR_MODE=false
dbg() {
if test -n "$WIP_DEBUG"
then
printf '# %s\n' "$*"
fi
}
# some errors are not worth reporting in --editor mode
report_soft_error () {
$EDITOR_MODE && exit 0
die "$@"
}
cleanup () {
rm -f "$TMP-*"
}
get_work_branch () {
ref=$(git symbolic-ref -q HEAD) \
|| report_soft_error "git-wip requires a branch"
branch=${ref#refs/heads/}
if [ $branch = $ref ] ; then
die "git-wip requires a local branch"
fi
echo $branch
}
get_wip_branch () {
return 0
}
check_files () {
local files=$@
for f in $files
do
[ -f "$f" -o -d "$f" ] || die "$f: No such file or directory."
done
}
build_new_tree () {
local untracked=$1 ; shift
local ignored=$1 ; shift
local files=$@
(
set -e
rm -f "$WIP_INDEX"
cp -p "$GIT_DIR/index" "$WIP_INDEX"
export GIT_INDEX_FILE="$WIP_INDEX"
git read-tree $wip_parent
if [ -n "$files" ]
then
git add -f $files
else
git add --update .
fi
[ -n "$untracked" ] && git add .
[ -n "$ignored" ] && git add -f -A .
git write-tree
rm -f "$WIP_INDEX"
)
}
do_save () {
local msg="$1" ; shift
local add_untracked=
local add_ignored=
while test $# != 0
do
case "$1" in
-e|--editor)
EDITOR_MODE=true
;;
-u|--untracked)
add_untracked=t
;;
-i|--ignored)
add_ignored=t
;;
--)
shift
break
;;
*)
[ -f "$1" ] && break
die "Unknown option '$1'."
;;
esac
shift
done
local files=$@
local "add_untracked=$add_untracked"
local "add_ignored=$add_ignored"
if test ${#files} -gt 0
then
check_files $files
fi
dbg "msg=$msg"
dbg "files=$files"
local work_branch=$(get_work_branch)
local wip_branch="$WIP_PREFIX$work_branch"
dbg "work_branch=$work_branch"
dbg "wip_branch=$wip_branch"
# enable reflog
local wip_branch_file="$GIT_DIR/logs/$wip_branch"
dbg "wip_branch_file=$wip_branch_file"
mkdir -p "$(dirname "$wip_branch_file")"
: >>"$wip_branch_file"
if ! work_last=$(git rev-parse --verify $work_branch)
then
report_soft_error "'$work_branch' branch has no commits."
fi
dbg "work_last=$work_last"
if wip_last=$(git rev-parse --quiet --verify $wip_branch)
then
local base=$(git merge-base $wip_last $work_last) \
|| die "'work_branch' and '$wip_branch' are unrelated."
if [ $base = $work_last ] ; then
wip_parent=$wip_last
else
wip_parent=$work_last
fi
else
wip_parent=$work_last
fi
dbg "wip_parent=$wip_parent"
new_tree=$( build_new_tree "$add_untracked" "$add_ignored" $files ) \
|| die "Cannot save the current worktree state."
dbg "new_tree=$new_tree"
if git diff-tree --exit-code --quiet $new_tree $wip_parent ; then
report_soft_error "no changes"
fi
dbg "... has changes"
new_wip=$(printf '%s\n' "$msg" | git commit-tree $new_tree -p $wip_parent) \
|| die "Cannot record working tree state"
dbg "new_wip=$new_wip"
msg1=$(printf '%s\n' "$msg" | sed -e 1q)
git update-ref -m "git-wip: $msg1" $wip_branch $new_wip $wip_last
dbg "SUCCESS"
}
do_info () {
local branch=$1
die "info not implemented"
}
do_log () {
local work_branch=$1
[ -z $branch ] && work_branch=$(get_work_branch)
local wip_branch="$WIP_PREFIX$work_branch"
local git_log="git log"
if [ "$1" = --pretty -o "$1" = -p ]
then
shift
git_log="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative"
fi
if ! work_last=$(git rev-parse --verify $work_branch)
then
die "'$work_branch' branch has no commits."
fi
dbg work_last=$work_last
if ! wip_last=$(git rev-parse --quiet --verify $wip_branch)
then
die "'$work_branch' branch has no commits."
fi
dbg wip_last=$wip_last
local base=$(git merge-base $wip_last $work_last)
dbg base=$base
echo $git_log $@ $wip_last $work_last "^$base~1" | sh
}
do_delete () {
local branch=$1
die "delete not implemented"
}
do_help () {
local rc=$1
cat <<END
Usage: git wip $USAGE
$LONG_USAGE
END
exit $rc
}
if test $# -eq 0
then
dbg "no arguments"
do_save "WIP"
exit $?
fi
dbg "args: $@"
case "$1" in
save)
WIP_COMMAND=$1
shift
if [ -n "$1" ]
then
WIP_MESSAGE="$1"
shift
fi
;;
info|log|delete)
WIP_COMMAND=$1
shift
;;
help)
do_help 0
;;
--*)
;;
*)
[ -f "$1" ] || die "Unknown command '$1'."
;;
esac
case $WIP_COMMAND in
save)
do_save "$WIP_MESSAGE" $@
;;
info)
do_info $@
;;
log)
do_log $@
;;
delete)
do_delete $@
;;
*)
usage
exit 1
;;
esac
# vim: set noet sw=8