-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit-squash
executable file
·73 lines (66 loc) · 1.18 KB
/
git-squash
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
#!/bin/bash
# Author: Joel Nothman
usage() {
echo Usage: $0 '[-m <commit msg>] <base>'
exit 1
}
unset base
unset message
while [ -n "$1" ]
do
case "$1" in
-h)
usage
;;
-m)
shift
if [ -z "$1" ]
then
echo '-m requires an argument' >&2
usage
fi
if [ -n "$message" ]
then
message+=$'\n\n'"$1"
else
message="$1"
fi
;;
*)
if [ -z "$base" ]
then
base="$1"
else
echo 'Multiple bases specified' >&2
usage
fi
esac
shift
done
if [ -z "$base" ]
then
echo 'No base ref provided' >&2
usage
fi
seq_ed="$(mktemp -t squash_edXXXXXX)" &&
echo '#!'$(which bash)'
echo Squashing... >&2
tmp="$(mktemp -t squash_outXXXXXX)"
awk '"'"'NR != 1 { sub(/^pick/, "squash") } {print}'"'"' "$1" > "$tmp"
mv "$tmp" "$1"
' > $seq_ed &&
chmod +x "$seq_ed" &&
echo 'Warning: Forgetting local history. To revert, use:
$ git reset --hard' $(git rev-parse --short HEAD) >&2
if [ -n "$message" ]
then
GIT_SEQUENCE_EDITOR=$seq_ed GIT_EDITOR=touch git rebase -i $base
else
GIT_SEQUENCE_EDITOR=$seq_ed git rebase -i $base
fi
if [ -n "$message" ]
then
echo 'Updating commit message' >&2
git commit --amend -m "$message" | head -n1
fi
rm -r "$seq_ed"