-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-shamend
executable file
·115 lines (97 loc) · 3.49 KB
/
git-shamend
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
#!/usr/bin/env sh
#
# Copied from Danielle Sucher's blog post at
# http://www.daniellesucher.com/2014/05/08/git-shamend/
# source: https://github.com/DanielleSucher/dotfiles/blob/master/git/git-shamend
set -o nounset
# Copyright (c) 2014 Danielle E. Sucher <[email protected]>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
PrintGitShamendUsage() {
echo "Usage: git shamend [options] [<revision>]"
echo "Amends your staged changes as a fixup (keeping the pre-existing commit"
echo "message) to the specified commit, or HEAD if no revision is specified."
echo
echo "Options:"
echo " -a, --all Commit unchanged files, same as git commit."
echo " -h, --help Print this help."
}
ALL=""
SHA_TO_AMEND=""
# parse arguments
while [ "$#" -gt 0 ]; do
case $1 in
-h | --help)
PrintGitShamendUsage
exit 0
;;
-a | --all)
ALL="--all"
;;
*)
if [ -n "$SHA_TO_AMEND" ]; then
echo "ERROR: unknown parameter \"$1\""
PrintGitShamendUsage
exit 1
fi
SHA_TO_AMEND=$(git rev-parse "$1")
if [ ! $? ]; then
echo "ERROR: unparsable revision \"$1\""
PrintGitShamendUsage
exit 1
fi
;;
esac
shift
done
# fall back to HEAD if no SHA was given
if [ -z "$SHA_TO_AMEND" ]; then
SHA_TO_AMEND="HEAD"
fi
# shortcut the HEAD case for simplicity
if [ "$SHA_TO_AMEND" = "HEAD" ]; then
git commit --amend --no-edit $ALL
exit 0
fi
BOLD=$(tput bold)
RED=$(tput setaf 1)
NORMAL=$(tput sgr0)
if git merge-base --is-ancestor $SHA_TO_AMEND HEAD; then
if [ -z "$ALL" ]; then
echo "${BOLD}Warning: your unstaged changes will be stashed during this process${NORMAL}"
fi
git commit $ALL --fixup $SHA_TO_AMEND >/dev/null
git diff-index --quiet HEAD
NOTHING_TO_STASH=$?
if [ $NOTHING_TO_STASH -ne 0 ]; then
git stash >/dev/null
fi
GIT_EDITOR=true git rebase -i --autosquash "$SHA_TO_AMEND^"
REBASE_EXIT=$?
if [ ${REBASE_EXIT} -ne 0 ]; then
git rebase --abort
git reset --soft HEAD^
echo "${RED}${BOLD}Whoops, that didn't work! Cleaning up after this attempt now, but"
echo "it looks like you'll have to interactive rebase this one manually.${NORMAL}"
fi
if [ $NOTHING_TO_STASH -ne 0 ]; then
git stash pop >/dev/null
fi
else
echo "${RED}${BOLD}${SHA_TO_AMEND} is not an ancestor of HEAD.${NORMAL}"
PrintGitShamendUsage
exit 1
fi