-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-commit-fixup-ask
executable file
·47 lines (39 loc) · 1.39 KB
/
git-commit-fixup-ask
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
#!/usr/bin/env bash
if ! [ -d .git ]; then
echo "Not a git repository";
exit 1;
fi
if git diff --cached --quiet --exit-code; then
echo "Nothing to commit";
exit 1;
fi
relativeToHead=0;
for commitId in `git --no-pager log --format=%H "$(git get-main-branch)...HEAD"`; do
commitMessage=$(git --no-pager log --format=%B -n 1 $commitId)
if [[ ${commitMessage:0:7} == "fixup! " ]] || [[ ${commitMessage:0:8} == "squash! " ]]; then
((relativeToHead++));
continue;
fi
git --no-pager log --pretty="format:${relativeToHead}. %Cred%h%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --date=relative -n1 $commitId;
echo
for filename in `git --no-pager diff --cached --name-only;`; do
git --no-pager diff --stat=160 --color "$commitId" "$commitId^" -- "$filename" | head -n 1;
done
((relativeToHead++));
done
if [ "$relativeToHead" -eq 0 ]; then
echo "no commits in branch found using $(git get-main-branch)...HEAD range";
exit 1;
fi
((relativeToHead--))
echo;
read -p "Pick a commit [0..${relativeToHead}]: " targetCommit
if [ -z "${targetCommit##*[!0-9]*}" ]; then
echo "expected a number in range [0..${relativeToHead}]";
exit 2;
fi
if [ ! "$targetCommit" -ge 0 -o ! "$targetCommit" -le "$relativeToHead" ]; then
echo "expected a number in range [0..${relativeToHead}]";
exit 3;
fi
git commit --fixup "HEAD~$targetCommit"