-
Notifications
You must be signed in to change notification settings - Fork 0
/
gear-update
executable file
·337 lines (296 loc) · 9.48 KB
/
gear-update
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
#!/bin/sh -efu
#
# Copyright (C) 2006-2018 Alexey Gladkov <[email protected]>
# Copyright (C) 2006-2012 Dmitry V. Levin <[email protected]>
# Copyright (C) 2006 Sergey Vlasov <[email protected]>
# Copyright (C) 2019 Vladimir D. Seleznev <[email protected]>
#
# gear-update updates subdirectory from source directory or archive file.
#
# 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.
#
. gear-update-sh-functions
handlers_list=
show_help()
{
local list
list="'$(printf '%s\n' "$handlers_list" |
sed "s/\([[:alnum:]]\) \([[:alnum:]]\)/\1', '\2/g")';"
cat <<EOF
$PROG - update subdirectory from source directory or archive file.
Usage: $PROG [Options] <source> <subdirectory>
Options:
--exclude=PATTERN exclude files matching posix-egrep regular expression PATTERN;
--ignore-exclude ignore .gitignore and .git/info/exclude (the default);
--honor-exclude honor .gitignore and .git/info/exclude;
--remove-source remove the <source> after updating;
--strip-components=NUMBER
strip NUMBER of leading path components from file names;
-a,--all extract all files and directories from archive;
-c,--create create the <subdirectory> before unpacking the <source>,
the <subdirectory> should not exist;
-s,--subdir=DIR extract specified directory name from archive;
-t,--type=TYPE define source type:
$list
-f,--force remove files from <subdirectory> even
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) 2006-2018 Alexey Gladkov <[email protected]>
Copyright (C) 2006-2012 Dmitry V. Levin <[email protected]>
Copyright (C) 2006 Sergey Vlasov <[email protected]>
Copyright (C) 2019 Vladimir D. Seleznev <[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
}
subdir=
source_handler=
validate_subdir()
{
if [ -z "$subdir" ]; then
subdir="$($source_handler --ls)"
subdir="$(printf %s "$subdir" |
sed 's#\(\([./]\+\)\?[^/]\+\)/.*#\1#g' |
LC_ALL=C grep '^[^[:space:]]' |
LC_COLLATE=C sort -u)"
fi
[ -n "$subdir" ] ||
fatal "$source: subdirectory not found."
[ "$(printf %s "$subdir" |wc -l)" -eq 0 ] ||
fatal "More than one subdirectory specified: $(printf %s "$subdir" |tr -s '[:space:]' ' ')"
local quoted_subdir
quote_sed_regexp_variable quoted_subdir "$subdir"
$source_handler --ls |LC_ALL=C grep -qs "^$quoted_subdir\(/\|\$\)" ||
fatal "$source: directory \`$subdir' not found in archive."
# We found the subdirectory automatically and we found this directory
# in the archive so we need to update the variable.
export gear_update_subdir="$subdir"
}
destdir=
dest_create=
cleanup_handler()
{
if [ "$*" != 0 ]; then
# Rollback actions
if [ -d "$destdir" ]; then
find "$destdir" -mindepth 1 -maxdepth 1 \! -name '.git' -print0 |
xargs -r0 rm $verbose -rf --
else
rm $verbose -f -- "$destdir"
fi
if [ -z "$dest_create" ]; then
mkdir -p -- "$destdir"
cd "$destdir"
git checkout-index -a
fi
message 'failed to update target directory.'
fi
}
path_to_self="$(type -p "$0")" && [ -x "$path_to_self" ] ||
fatal "Failed to locate \"$0\" binary"
[ -z "${path_to_self##/*}" ] ||
path_to_self="$(readlink -ev "$path_to_self")"
plugin_dir="${path_to_self%/*}"
set +f
for s_handler in "$plugin_dir/gear-update-src-"*; do
[ -f "$s_handler" ] ||
break
if [ ! -x "$s_handler" ]; then
message "$s_handler: Handler must be executable."
continue
fi
handlers_list="${handlers_list:+$handlers_list }${s_handler##*/gear-update-src-}"
done
set -f
TEMP=$(getopt -n $PROG -o a,c,f,t:,s:,v,V,h -l all,create,exclude:,ignore-exclude,honor-exclude,force,remove-source,type:,subdir:,strip-components:,verbose,version,help -- "$@") || show_usage
eval set -- "$TEMP"
gear_config_option exclude_pattern exclude-pattern ''
ignore_exclude=
honor_exclude=
force=
remove_source=
source_type=
all_subdirs=
strip_components=0
while :; do
case "$1" in
-a|--all) all_subdirs=1
;;
-c|--create) dest_create=1
;;
--exclude) shift
exclude_pattern="$1"
;;
--ignore-exclude) ignore_exclude=1
;;
--honor-exclude) honor_exclude=1
;;
--remove-source) remove_source=1
;;
--strip-components)
strip_components=$(opt_check_number "$1" "$2")
shift
;;
-f|--force) force=-f
;;
-t|--type) shift; source_type="$1"
;;
-s|--subdir) shift; subdir="$1"
;;
-v|--verbose) verbose=-v
;;
-V|--version) print_version
;;
-h|--help) show_help
;;
--) shift; break
;;
*) fatal "unrecognized option: $1"
;;
esac
shift
done
[ "$#" -ge 2 ] ||
show_usage 'Not enough arguments.'
[ "$#" -eq 2 ] ||
show_usage 'Too many arguments.'
if [ -n "$ignore_exclude" ] && [ -n "$honor_exclude" ]; then
fatal "--ingore-exclude and --honor-exclude are mutually exclusive"
fi
source="$(readlink -ev -- "$1")"; shift
if [ -n "$dest_create" ]; then
destdir="$(readlink -fv -- "$1")"
[ ! -e "$destdir" ] ||
fatal "$destdir: Destination directory already exists."
else
destdir="$(readlink -ev -- "$1")"
fi
shift
canon_destdir="$destdir"
[ "$source" != "$destdir" ] ||
fatal "\`$source' and \`$destdir' are the same place."
[ -n "$dest_create" ] || [ -d "$destdir" ] ||
fatal "$destdir: not a directory."
[ -z "$subdir" ] || [ -z "$all_subdirs" ] ||
fatal "Cannot use --all and --subdir at the same time."
# Export options
export gear_update_all_subdirs="$all_subdirs"
export gear_update_canon_destdir="$canon_destdir"
export gear_update_destdir="$destdir"
export gear_update_force="$force"
export gear_update_source="$source"
export gear_update_subdir="$subdir"
export gear_update_verbose="$verbose"
export gear_update_strip_components="$strip_components"
if [ -z "$source_type" ]; then
for s_type in $handlers_list; do
rc=0
"$plugin_dir/gear-update-src-$s_type" --check-type || rc=$?
case "$rc" in
0)
source_type="$s_type"
break
;;
1) exit 1
;;
2) continue
;;
esac
done
fi
source_handler="$plugin_dir/gear-update-src-$source_type"
[ -x "$source_handler" ] ||
fatal "$source: unrecognized source type: $source_type"
# Change to toplevel directory
chdir_to_toplevel
topdir="$(readlink -ev .)"
# Make destdir relative
destdir="$destdir/"
destdir="${destdir#"$topdir/"}"
[ "$destdir" = "${destdir#/}" ] ||
fatal "$destdir: directory out of \`$topdir' tree."
[ -n "$destdir" ] &&
destdir="${destdir%/}" ||
destdir='.'
initial_commit=
git rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=1
if [ -z "$force$initial_commit" ]; then
out="$(git diff-index --cached --name-only HEAD -- "$destdir")"
[ -z "$out" ] ||
fatal "$destdir: Changed files found in the index."
out="$(git diff --name-only -- "$destdir" &&
git ls-files --directory --others --exclude-per-directory=.gitignore -- "$destdir")"
[ -z "$out" ] ||
fatal "$destdir: Untracked or modified files found."
fi
[ -n "$dest_create$initial_commit" ] ||
git rm -n -r $force -- "$destdir" >/dev/null
[ -n "$all_subdirs" ] ||
validate_subdir
# Trap for rollback
install_cleanup_handler cleanup_handler
# Remove $destdir
if [ -z "$dest_create" ]; then
find "$destdir" -mindepth 1 -maxdepth 1 \! \( -name '.git' -o -name '.gear' -o -name '.gear-rules' -o -name '.gear-tags' \) -print0 |
xargs -r0 rm $verbose -rf --
[ "$destdir" = '.' ] ||
rmdir $verbose -- "$destdir"
fi
# Copy new sources
$source_handler --unpack
[ -d "$destdir" ] ||
fatal "$destdir: result of extraction is not a directory."
if [ -n "$exclude_pattern" ]; then
cd "$destdir"
find . -regextype 'posix-egrep' -regex "$exclude_pattern" -print0 |
xargs -r0 rm $verbose -rf --
cd - >/dev/null
fi
if [ -n "$(find "$destdir" -maxdepth 0 -empty)" ]; then
echo '.gitattributes export-ignore' >"$destdir/export-ignore"
else
export_ignore="$(mktemp -- "$destdir/$PROG.XXXXXXXX")"
echo '.gitattributes export-ignore' >"$export_ignore"
find "$destdir" -type d -empty -exec ln -- "$export_ignore" '{}/.gitattributes' ';'
rm -- "$export_ignore"
fi
if [ -z "$honor_exclude" ]; then
git ls-files -z -- "$destdir" |
git update-index --remove --force-remove -z --stdin
git ls-files -z --others --modified -- "$destdir" |
git update-index --add ${verbose:+--verbose} -z --stdin
else
[ -n "$dest_create$initial_commit" ] ||
git rm -f -r --cached -- "$destdir" >/dev/null
git add $verbose -- "$destdir"
fi
if [ -n "$remove_source" ]; then
[ -n "${source##"$topdir"/*}" ] ||
git ls-files -z -- "$source" |
git update-index --remove --force-remove -z --stdin
rm -rf -- "$source"
fi