-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgit-split-commit
executable file
·87 lines (72 loc) · 1.81 KB
/
git-split-commit
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
#!/bin/bash
#
# Split the last commit into multiple patches. Usually used during
# re-basing.
#
#
usage() {
cat <<- EOF
Usage: $(basename $0) [--before|--after]
--before ... re-commit the patch before any leftovers (default)
--after ... commit everything else first, then the leftover
EOF
}
mode="before"
case "$1" in
--after)
mode="after"
;;
--before)
mode="before"
;;
--help)
usage
exit 0
;;
"")
;;
*)
echo "Invalid argument: $1"
usage
exit 1
;;
esac
git diff > /dev/null
if [ $? -ne 0 ]; then
echo "Unstaged changes, please commit them first"
exit 1
fi
readonly sha=$(git rev-list -n 1 HEAD)
git reset HEAD~1
if [ $mode = "before" ]; then
git add -p
if [ $? -ne 0 ]; then
echo "---------------------------------------------"
echo "git add -p failed, dropping you into a shell."
echo "Exiting the shell will commit"
$SHELL
fi
echo -n "Need to add any files? [y/N] "
read addfiles
if [ "$addfiles" = "y" -o "$addfiles" = "Y" ]; then
echo "------------------------------"
echo "Dropping you into a shell now."
echo "Exiting the shell will commit."
$SHELL
fi
git commit -c $sha
echo "--------------------------------------"
echo "Add remaining hunks now. To abort, run"
echo " git reset --hard $sha"
else # mode == after
echo "---------------------------------------------------"
echo "Starting a shell. Commit everything unrelated first"
echo "Exiting the shell will re-commit"
$SHELL
git commit -ac $sha
echo -n "Happy with the split? N will reset [Y/n] "
read happy
if [ "$happy" = "n" -o "$happy" = "N" ]; then
git reset --hard $sha
fi
fi