-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrej-merge.sh
executable file
·125 lines (102 loc) · 2.38 KB
/
rej-merge.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
118
119
120
121
122
123
124
#!/usr/bin/env bash
# my-dotfiles | Copyright (C) 2021 eth-p
# Repository: https://github.com/eth-p/my-dotfiles
set -euo pipefail
FILE="$1"
FILE_LINES=()
FILE_LINES_OFFSET=0
OURS_LINE_NUMBER=''
OURS=()
THEIRS_LINE_NUMBER=''
THEIRS=()
# -----------------------------------------------------------------------------
# Functions:
# -----------------------------------------------------------------------------
insert() {
local line="$1"
IFS= local text="$2"
# Determine the real line number after the previous inserts.
local real_line=$((FILE_LINES_OFFSET+line)) || true
# Perform insert.
FILE_LINES_OFFSET=$((FILE_LINES_OFFSET+1)) || true
FILE_LINES=(
"${FILE_LINES[@]:0:$real_line}" \
"$text" \
"${FILE_LINES[@]:$real_line}"
)
}
replace() {
local line="$1"
IFS= local text="$2"
# Determine the real line number after the previous inserts.
local real_line=$((FILE_LINES_OFFSET+line)) || true
# Perform replace.
FILE_LINES[$real_line]="$text"
}
merge_reject() {
if [[ -z "$OURS_LINE_NUMBER" ]]; then
return
fi
# Generate the text to insert.
local str=''
local line
while IFS= read -r line; do
if [[ -z "$str" ]]; then
str="$line"
else
str="$str"$'\n'"$line"
fi
done < <({
echo "<<<<<<< YOUR CHANGES (approximately)"
printf "%s\n" "${THEIRS[@]}"
echo "======="
printf "%s\n" "${OURS[@]}"
echo ">>>>>>> UPDATE"
})
# Insert the text.
#replace "$((THEIRS_LINE_NUMBER - 1))" "$str"
# Clear the previous reject.
OURS=()
OURS_LINE_NUMBER=
THEIRS=()
THEIRS_LINE_NUMBER=
}
# -----------------------------------------------------------------------------
# Main:
# -----------------------------------------------------------------------------
# Load the patched file into memory.
while read -r line; do
FILE_LINES+=("$line")
done < "$FILE"
# Iteratively apply rejected hunks.
while read -r line; do
case "$line" in
# New reject:
"***************")
merge_reject
;;
# Ours line number:
"*** "*)
OURS_LINE_NUMBER="$(sed 's/^\*\*\* *//; ;s/,[0-9]*$//' <<< "$line")"
;;
# Ours line:
"- "*)
OURS+=("${line:2}")
;;
# Theirs line number:
"--- "*" -----")
THEIRS_LINE_NUMBER="$(sed 's/^--- *//; s/ *-----$//; ;s/,[0-9]*$//' <<< "$line")"
;;
# Theirs line:
"+ "*)
THEIRS+=("${line:2}")
;;
esac
done < "$FILE.rej"
merge_reject
# Print back the file.
{
for line in "${FILE_LINES[@]}"; do
printf "%s\n" "$line"
done
}