-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-delorean.sh
55 lines (50 loc) · 1.17 KB
/
git-delorean.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
#!/bin/bash
staged=$(git diff --name-only --relative --cached --diff-filter=M)
revspec="${1:-HEAD}"
working_tree_sha=$(git stash create)
#TODO: replace with git restore?
git stash --keep-index
IFS=$'\n'
for file in $staged; do
# BUG: When there are only adds (@@ -XX,0 +YY,x) the first line will be XX
# It should probably be YY + x
# TODO: should this also use revspec?
lines=$(
git diff --word-diff=porcelain -U0 --cached HEAD -- "$file" |\
awk -F '[, ]' '/^@@/ {
gsub(/^[^0-9]*/, "", $2)
r = 0
if (substr($3,1,1) != "+") {
r = gensub(/([0-9]*).*/, "\\1", "g", $3)
}
if (r != 0) {
print $2","$2 + r - 1
}
else {
print $2","$2
}}'
)
commits=$(
<<< "$lines" \
xargs \
--verbose \
-I% \
git blame \
--incremental \
-L \
% \
"$revspec" \
-- \
"$file" |\
awk \
-e '/^[a-f0-9]{40} /{ a[$1]++ }' \
-e ' END { for (b in a) { print b }}'
)
git rev-list --topo-order "$revspec" |\
{ grep "$commits" || test $? = 1; } |\
head -n 1 |\
xargs \
--replace=first_parent \
git commit --fixup first_parent -- "$file"
done
git stash apply "$working_tree_sha" --index