-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-wip
executable file
·58 lines (50 loc) · 1.64 KB
/
git-wip
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
#!/bin/bash
#
# For every file that has been altered since the last commit, create
# a commit containing only that file. If the file is part of an
# existing commit on this branch (and not the parent branch), the
# commit will be called
#
# wip <previous commit title>
#
# Otherwise it will be called
#
# wip <filename>
#
# It then starts an interactive rebase where wip commits can be
# easily matched with and [f]ixup'd exsting commits or [r]enamed
# into new commits.
#
# This is intended for situations where revisions to files in a PR
# are expected to be squashed into one of the original commits.
# NEXT GEN:
# automatically run this vim script to turn wips into fixups and sort
#
#:%s,^pick\( [0-9a-z]\+ add\)\@=,f,
#/^#
#k
#dG
#:!sort -k3 " TODO this only sorts by the 3rd work, need 3-EOL
#G
#p
set -xeu
# Get the name of the parent branch
# Credit: https://stackoverflow.com/questions/3161204/how-to-find-the-nearest-parent-of-a-git-branch
PARENT=$(git show-branch | grep '*' | grep -v "$(git rev-parse --abbrev-ref HEAD)" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//')
# Back up to the repo root
while [ ! -d .git ] ; do
cd ..
if [ "$(pwd)" = '/' ]; then
echo "Could not find .git in this or any parent directory" >&2
exit 1
fi
done
CHANGED=$(git diff --name-only --diff-filter CM)
ADD_REMOVED=$(git status --porcelain | grep '^[ADR]' | awk '{ print $2 }')
for i in $CHANGED $ADD_REMOVED
do
lastCommit=$(git log origin...HEAD --format=oneline -- $i | cut -d' ' -f2- | head -n1)
[ -n "$lastCommit" ] && desc="$lastCommit" || desc=$i
git commit -m "wip ${desc}" $i
done
[ -d .git/rebase-merge ] || git rebase -i $PARENT