-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-fixup.sh
executable file
·73 lines (58 loc) · 1.51 KB
/
git-fixup.sh
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
#!/usr/bin/env sh
set -e
help() {
cat <<EOF
git-fixup
Creates a git fixup commit that is amended automatically to the target commit.
This script should be used as a git alias. Aliases can be added to .gitconfig
file. Add an alias to the .gitconfig file called 'fixup' (or some other
prefered name) and point it to the location of this script.
USAGE
git-fixup <commit_hash>
EXAMPLES
Amend to older commit behind 2 commits in the current branch
git add file.txt
git-fixup HEAD~2
Amend to older commit in the current branch using git alias
git add file.txt
git fixup HEAD~2
EOF
exit 2
}
err() {
printf >&2 "error: %s\n" "$@"
exit 1
}
print() {
printf "%b\n" "$@"
}
git_log_branch_commits() {
git --no-pager log --oneline origin..
}
git_commit_select() {
git_log_branch_commits |
fzf \
--pointer=" " \
--reverse \
--height=10 \
--no-scrollbar \
--preview="printf '%s' {} | awk '{ print \$1 }' | xargs git show" | awk '{ print $1 }'
}
git_fixup() {
total_commits="$(git_log_branch_commits | wc -l | bc)"
# GIT_SEQUENCE_EDITOR is set to colon to prevent git from opening a text editor
git commit --fixup "$1" && GIT_SEQUENCE_EDITOR=: git rebase --interactive --autosquash --autostash HEAD~"${total_commits}"
}
fixup() {
for arg; do
case "${arg}" in
-h | --help) help ;;
-*) err "Unknown option ${arg}" ;;
esac
done
if ! command -v fzf >/dev/null; then
err "Could not find fzf in the path"
fi
git_fixup "$(git_commit_select)"
}
fixup "$@"