-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgear-merge
executable file
·378 lines (312 loc) · 8.45 KB
/
gear-merge
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/bin/sh -efu
#
# Copyright (C) 2007-2018 Alexey Gladkov <[email protected]>
# Copyright (C) 2008-2016 Dmitry V. Levin <[email protected]>
# Copyright (C) 2008 Alexey I. Froloff <[email protected]>
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Rules:
# directive: <from-branch> <to-branch> args
#
# directive := ( merge | gendiff )
# args := ( strategy | squash | message | unified | name )
# name := <file>
# message := <text>
# unified := <number>
# strategy := <git merge strategy>
# squash := NONE
#
# <git merge strategy> - Use the given merge strategy. See man git-merge(1).
#
. gear-utils-sh-functions
show_help() {
cat <<EOF
Usage: $PROG [Options]
This utility might help you to store upstream sources, patches
and metafiles to facilitate packaging for different repository branches.
Its main purpose is merging differerent branches (usually upstream
sources and/or patch branches) into single master branch.
Options:
-I, --no-interactive merge branches non-interactively;
-a, --add add new patches in git repository;
-r, --rules=FILENAME name of file with rules, default is .gear/merge;
-s, --stop=BRANCH stop merge when BRANCH shows in <to-branch>,
implies --no-interactive;
--ignore-changed do not bail if index changed;
--ignore-untracked do not bail if untracked or modified files found;
-v, --verbose print a message for each action;
-V, --version print program version and exit;
-h, --help show this text and exit.
Report bugs to https://bugzilla.altlinux.org/
EOF
exit
}
print_version() {
cat <<EOF
$PROG version $PROG_VERSION
Written by Alexey Gladkov <[email protected]>
Copyright (C) 2007-2018 Alexey Gladkov <[email protected]>
Copyright (C) 2008-2016 Dmitry V. Levin <[email protected]>
Copyright (C) 2008 Alexey I. Froloff <[email protected]>
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
exit
}
cur_branch=
set_current_branch() {
local id="${1:-HEAD}"
cur_branch="$(git symbolic-ref "$id")"
[ -z "${cur_branch%%refs/heads/*}" ] ||
fatal "$id: not a branch."
cur_branch="${cur_branch#refs/heads/}"
}
restore_branch=
cleanup_handler()
{
cleanup_workdir
if [ "$*" = 0 ]; then
set_current_branch
[ "$cur_branch" = "$restore_branch" ] ||
git checkout "$restore_branch"
fi
}
main_tree_id='HEAD'
rules=
find_merge_rules() {
: >"$workdir/rules"
if [ -n "$(git ls-tree "$main_tree_id" "$rules")" ]; then
cat_blob "$main_tree_id" "$rules" >"$workdir/rules"
fi
rules_cleanup "$workdir/rules"
}
validate_rule() {
local cmd
cmd="$1"; shift
for b in "$src_branch" "$dst_branch"; do
[ -n "$(git ls-remote -h . "refs/heads/$b")" ] ||
fatal "$b: branch not found."
done
[ "$cmd" != 'gendiff' ] || [ -n "$name" ] ||
rules_error "patch name not defined. Use 'name' option."
}
parse_rule() {
cmd="${1%:}"; shift
src_branch='' dst_branch='' name='' message='' squash='' strategy='' unified=''
case "$cmd" in
merge|gendiff) ;;
*) rules_error "unknown command: $cmd" ;;
esac
[ "$#" -ge 2 ] ||
rules_error 'expected syntax: <command:> <from-branch> <to-branch> [<options>]'
src_branch="$1"; shift
dst_branch="$1"; shift
set_current_branch
while [ "$#" -gt 0 ]; do
case "$1" in
name=*) name="${1#name=}" ;;
message=*) message="${1#message=}" ;;
unified=*) unified="--unified=${1#unified=}" ;;
strategy=*) strategy="--strategy=${1#strategy=}" ;;
squash) squash='--sq' ;;
*) rules_error "unknown option: $1" ;;
esac
shift
done
}
interactive_parse_rules() {
local ans args app_lineno cur_lineno max_lineno line lineno
cur_lineno=1
app_lineno=0
max_lineno=$(wc -l < "$workdir/rules")
max_lineno=$((max_lineno+1))
ans=
while :; do
lineno=0
while read -r line; do
lineno=$((lineno+1))
[ $lineno -ge $cur_lineno ] ||
continue
if [ -z "${line%%#*}" ]; then
cur_lineno=$((cur_lineno+1))
app_lineno=0
continue
fi
quote_shell_args args "$line"
eval "set -- $args"
parse_rule "$@"
if [ $lineno -eq $app_lineno ]; then
branch_$cmd
cur_lineno=$((cur_lineno+1))
continue
fi
break
done < "$workdir/rules"
[ $max_lineno != $cur_lineno ] ||
break
if [ "$ans" != "all" ]; then
printf '======\n%s' "Confirm $cmd '$src_branch' -> '$dst_branch' (rule $lineno) [Y/n/a/q]: "
read -r ans
fi
case "$ans" in
[nN]*)
cur_lineno=$((lineno+1))
app_lineno=0
;;
[aA]*)
cur_lineno=$lineno
app_lineno=$lineno
ans="all"
;;
[qQ]*)
break
;;
*)
cur_lineno=$lineno
app_lineno=$lineno
;;
esac
done
}
stop=
parse_rules() {
local parser_func="${1-}"
local line cmd lineno=0
echo >>"$workdir/rules"
while read -r line; do
lineno=$((lineno+1))
[ -n "${line%%#*}" ] ||
continue
quote_shell_args args "$line"
eval "set -- $args"
parse_rule "$@"
if [ -n "$parser_func" ]; then
$parser_func "$cmd"
continue
elif [ -n "$stop" ] && [ "$dst_branch" = "$stop" ]; then
break
fi
branch_$cmd
done < "$workdir/rules"
}
is_already_merged() {
[ -z "$(git rev-list --max-count=1 "$1".."$2")" ] || return 1
}
branch_merge() {
if is_already_merged "$dst_branch" "$src_branch"; then
verbose "No new commits in branch '$dst_branch'"
return 0
fi
[ "$cur_branch" = "$dst_branch" ] ||
git checkout "$dst_branch"
git merge $squash $strategy \
${message:+-m "$message"} \
"refs/heads/$src_branch" ||
fatal "Unable to merge '$src_branch' automatically"
}
new_files=
add_new_files=
branch_gendiff() {
branch_merge
local dir="${name%/*}"
[ "$dir" = "$name" ] || [ -d "$dir" ] ||
mkdir -p -- "$dir"
{ [ -z "$message" ] || printf '%s\n\n' "$message"; } > "$name"
git diff $unified \
-p \
--no-color \
--ignore-space-change \
"refs/heads/$src_branch..refs/heads/$dst_branch" >> "$name" ||
fatal "Unable to generate diff between '$src_branch' and '$dst_branch'"
[ -z "$add_new_files" ] ||
new_files="$new_files $name"
}
TEMP=$(getopt -n $PROG -o a,r:,s:,I,V,v,h -l add,rules:,stop:,no-interactive,ignore-changed,ignore-untracked,version,verbose,help -- "$@") ||
show_usage
eval set -- "$TEMP"
no_interactive=
rules='.gear/merge'
skip_changed=
skip_untracked=
while :; do
case "$1" in
-I|--no-interactive) no_interactive=1
;;
-a|--add) add_new_files=1
;;
-r|--rules) shift; rules="$1"
;;
-s|--stop) shift; stop="$1"; no_interactive=1
;;
--ignore-changed) skip_changed=1
;;
--ignore-untracked) skip_untracked=1
;;
-v|--verbose) verbose=-v
;;
-V|--version) print_version
;;
-h|--help) show_help
;;
--) shift; break
;;
*) fatal "Unrecognized option: $1"
;;
esac
shift
done
# Save git directory for future use.
git_dir="$(git rev-parse --git-dir)"
git_dir="$(readlink -ev -- "$git_dir")"
# Change to toplevel directory.
chdir_to_toplevel
if [ -z "$skip_changed" ]; then
out="$(git diff-index --cached --name-only HEAD)"
[ -z "$out" ] ||
fatal "Changed files found in the index."
fi
if [ -z "$skip_untracked" ]; then
out="$(git diff --name-only &&
git ls-files --directory --others --exclude-per-directory=.gitignore)"
[ -z "$out" ] ||
fatal "Untracked or modified files found."
fi
GEAR_MERGE=1
export GEAR_MERGE
set_current_branch
restore_branch="$cur_branch"
install_cleanup_handler cleanup_handler
workdir="$(mktemp -dt "$PROG.XXXXXXXX")"
find_merge_rules
if [ ! -s "$workdir/rules" ]; then
message "No rules found."
show_usage
exit 0
fi
parse_rules 'validate_rule'
if [ -z "$no_interactive" ]; then
interactive_parse_rules
else
parse_rules
fi
if [ -n "$add_new_files" ] && [ -n "$new_files" ]; then
set_current_branch
if [ "$cur_branch" != "$restore_branch" ]; then
git checkout "$restore_branch"
restore_branch=
fi
git add $new_files
fi