-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-squash-branch.zsh
executable file
·49 lines (39 loc) · 1.24 KB
/
git-squash-branch.zsh
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
#! /bin/zsh
compdef _git-squash-branch git-squash-branch
_git-squash-branch() {
_arguments \
':Name of Source Branch:__branch_names'
}
zstyle ':completion:*' group-name ''
zstyle ':completion:*:descriptions' format '%B%d%b'
function git-squash-branch() {
if [[ $# -ne 1 ]]; then
echo "Usage: $0 (branch built from)"
return 1
fi
local branch=$1
local commit=$(git rev-parse HEAD)
local count_commits=$(git rev-list --count HEAD ^${branch})
if [[ $count_commits -le 1 ]]; then
echo "No commits to squash"
return
fi
echo "Number of commits since '$branch': $count_commits"
commits=($(git rev-list HEAD ^$branch))
messages=$(git log HEAD ^${commits[-1]} --pretty=%s)
git reset --soft HEAD~$count_commits
if verify "Would you like to include commit messages? (y/n)" ; then
git commit -e -m "Squashed messages:" -m "$messages"
else
if git commit ; then
echo Done
return 0
else
if verify "Would you like to reset? (y/n)"; then
git reset --soft $commit
echo "Reset to commit $commit: $(git log --format=%B -n 1 ${commit})"
return 1
fi
fi
fi
}