-
Notifications
You must be signed in to change notification settings - Fork 21
/
git-generate.old
executable file
·86 lines (71 loc) · 2.36 KB
/
git-generate.old
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
#!/bin/bash
help="usage: git-generate [-conflict]
git-generate regenerates a commit from a script kept in the commit message.
Specifically, the topmost commit in the current git repo should have a script
preceded by [git-generate] on a line by itself. The script continues until the
end of the message or a Change-Id: line. The script starts execution in the
root of the git repo.
For example, a commit message might say:
We are moving from Old to New.
[git-generate]
cd some/dir
sed -i '' 's/Old/New/g' *
To regenerate the commit, git-generate resets the working file state to before
the commit and then runs the script. The script runs using 'bash -e', so any
single command failing will abort the generation.
When a merge conflict occurs while rebasing, git does not stop with the
conflicting commit at the top, so git-generate will not find it by default.
The -conflict flag tells git-generate to look for the script in the upcoming
(conflicting) commit.
"
if [ "$1" = "-help" -o "$1" = "-h" ]; then
echo "$help"
exit 0
fi
conflict=false
if [ "$1" = "-conflict" ]; then
conflict=true
shift
fi
if [ $# != 0 ]; then
echo >&2 'usage: git-generate [-conflict]'
echo >&2 ' git-generate -help'
exit 2
fi
if ! git log -n1 >/dev/null; then
echo >&2 'git-generate: cannot find git repo'
exit 2
fi
set -e
gitdir=$(git rev-parse --show-toplevel)
what=HEAD
if $conflict; then
if ! [ -e $gitdir/.git/rebase-merge/stopped-sha ]; then
echo >&2 "git-generate: no merge conflict in progress"
exit 2
fi
what=$(cat $gitdir/.git/rebase-merge/stopped-sha)
short=$(echo $what | cut -c 1-8)
echo >&2 "git-generate: using $short to resolve merge conflict"
fi
msg=$(mktemp /tmp/replayXXXXXX)
git log -n1 $what | sed -n 's/^ //p' | sed -n '/^ *\[git-generate\]$/,$p' | sed '1d; /Change-Id:/d' | grep -v Change-Id: >$msg
if ! [ -s $msg ]; then
echo >&2 'git-generate: no script found in commit'
exit 2
fi
now=$(git rev-parse HEAD)
# Reset files to HEAD^ but keep branch itself at HEAD.
# For a brief moment HEAD is pointing at HEAD^, which is unfortunate.
# It would be very nice if there were some single command to do this pair.
if [ "$what" = HEAD ]; then
git reset -q --hard 'HEAD^'
git reset -q $now
else
# Resolving merge conflict, HEAD is parent before changes,
# which is exactly what we want.
git reset -q --hard HEAD
fi
cd $(git rev-parse --show-toplevel)
bash -e $msg
rm -f $msg