-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-resolve.sh
117 lines (102 loc) · 2.18 KB
/
git-resolve.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
die () {
echo "ERROR: $*. Aborting" >&2
exit 1
}
args=( )
for arg; do
case "$arg" in
--help|-h)
args+=( -h ) ;;
--ours|-o)
args+=( -o ) ;;
--theirs|-t)
args+=( -t ) ;;
--both|-b)
args+=( -b ) ;;
--add|-a)
args+=( -a ) ;;
*)
args+=( "$arg" ) ;;
esac
done
[[ $- =~ x ]] &&
printf 'args before update : %q\n' "$@" >&2
set -- "${args[@]}"
[[ $- =~ x ]] &&
printf 'args after update : %q\n' "$@" >&2
ours=false
theirs=false
both=false
add=false
while getopts ":otba" opt; do
case $opt in
o ) if [ "$theirs" = true ]; then die "Cannot specify ours and theirs" ;fi
ours=true ;;
t ) if [ "$ours" = true ]; then die "Cannot specify ours and theirs" ;fi
theirs=true ;;
b ) both=true ;;
a ) add=true ;;
\?) die "Unknown option: -$OPTARG. Abort" ;;
: ) die "Missing option: -$OPTARG. Abort" ;;
* ) die "Unimplemented option: -$OPTARG. Abort" ;;
esac
done
files=("${@:$OPTIND}")
case "${files[@]}" in
'')
# Default is everything (.) relative to PWD
files=('.')
;;
'-')
files=('/dev/stdin')
;;
esac
[[ $- =~ x ]] &&
printf 'args after getopts : %q\n' "$@" >&2 &&
printf 'files (a pathspec) after getopts : %q\n' "${files[@]}" >&2
if [ "$ours" == false ] && [ "$theirs" == false ] && [ "$both" == false ]; then
die "You need to specify --ours, --theirs or --both"
fi
ffiles() {
git ls-files --unmerged -- "$@" |
cut -f 2 |
uniq
}
if [ "$both" = true ]; then
sed_script='
# Just delete all conflict markers
/^<\{7\}/d
/^[|=]\{7\}/d
/^>\{7\}/d'
elif [ "$ours" = true ]; then
sed_script='
# Delete "our" markers
/^<\{7\}/d
# Delete everything in "their" conflicts
/^[|=]\{7\}/,/^>\{7\}/d'
elif [ "$theirs" = true ]; then
sed_script='
# Delete everything in "our" conflicts
/^<\{7\}/,/^=\{7\}/d
# Delete "their" markers
/^>\{7\}/d'
fi
case ${files[0]} in
- | /dev/stdin )
sed "$sed_script" /dev/stdin &&
exit 0
;;
*)
ffiles "${files[@]}" |
xargs -d '\n' -I% find % |
xargs -d '\n' -I% sed -i "$sed_script" % ||
die "Files not found"
ffiles "${files[@]}" |
if [ "$add" = true ]; then
tee >(xargs -d '\n' git add --sparse --)
else
cat
fi
;;
esac