-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathmodgit
More file actions
executable file
·694 lines (576 loc) · 17.3 KB
/
modgit
File metadata and controls
executable file
·694 lines (576 loc) · 17.3 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#!/bin/bash
VERSION="1.1.0"
GITHUB="https://github.com/jreinke/modgit"
AUTHOR="Johann Reinke"
SCRIPT=${0##*/}
IFS=$'\n'
USAGE="\
$(tput bold)modgit v$VERSION by $AUTHOR$(tput sgr0)
Fork me at $GITHUB
$(tput bold)Global Commands:$(tput sgr0)
$SCRIPT <command> [options]
-----------------------------------------------------------------------------------
init Initialize .modgit/ folder
ls List installed modules
up-all Update all installed modules
rm-all Remove all installed modules
Available options:
-n Dry run mode (show what would be done)
-v Show modgit version
-h Show this help
$(tput bold)Module Commands:$(tput sgr0)
$SCRIPT <command> [options] <module> [<repository>]
-----------------------------------------------------------------------------------
add <module> <repository> Install a module by cloning specified git repository
up <module> Update specified module
rm <module> Remove specified module
info <module> Show information about a specific module
files <module> List deployed files of specified module
proxy <module> <git_args> Run git command into specified module
Available options:
-n Dry run mode (show what would be done)
-f Force all files and folders to be deployed (ignore modman mapping file)
-i Include filters (only for add command)
Example: $SCRIPT add $(tput bold)-i lib/ -i foo/:bar/$(tput sgr0) my_module https://github.com/account/repository.git
=> will deploy only lib/ (to lib/) and foo/ (to bar/)
-e Exclude filters (only for add command)
Example: $SCRIPT add $(tput bold)-e lib/tests/ -e lib/README.txt$(tput sgr0) my_module https://github.com/account/repository.git
=> will exclude both directory lib/tests/ and file lib/README.txt
-b Specify a repository branch (only for add command)
Example: $SCRIPT add $(tput bold)-b 1.0-stable$(tput sgr0) my_module https://github.com/account/repository.git
=> will checkout 1.0-stable branch of specified repository
-t Specify a repository tag (only for add and update command)
Example: $SCRIPT add $(tput bold)-t 1.2.0$(tput sgr0) my_module https://github.com/account/repository.git
=> will checkout 1.2.0 tag of specified repository
"
# Case-insensitive for regex matching
shopt -s nocasematch
DRY_RUN=0
IGNORE_MODMAN=0
ACTION=""
INCLUDES=""
EXCLUDES="\
readme.*
about.*
license.*
copyright.*
changelog.*
credit.*
faq.*
\.travis.*
\.git.*
modman
composer\.json"
BRANCH="master"
TAG=""
# Echo in bold
echo_b()
{
if [ "$1" = "-e" ]; then
echo -e "$(tput bold)$2$(tput sgr0)"
else
echo "$(tput bold)$1$(tput sgr0)"
fi
}
show_help()
{
echo -e "$USAGE"
}
# Fatal error
fault()
{
echo_b "-e" "ERROR: $1"
exit 1
}
add_include()
{
if [ -z "$INCLUDES" ]; then
INCLUDES="$1"
else
INCLUDES="$INCLUDES\n$1"
fi
}
add_exclude()
{
if [ -z "$EXCLUDES" ]; then
EXCLUDES="$1"
else
EXCLUDES="$EXCLUDES\n$1"
fi
}
# Show help if asked or if no argument specified
if [ "$1" = "help" ] || [ -z "$1" ]; then
show_help
exit 0
fi
REGEX_ACTION="(list|info|files|ls|clone|add|update|up|update-all|up-all|remove|rm|remove-all|rm-all|proxy)"
# Accept action as first argument
if [[ "$1" =~ $REGEX_ACTION ]]; then
ACTION="$1"; shift
fi
# Handle options
while getopts ":nvhfi:e:b:t:" opt; do
case $opt in
h)
show_help
exit 0
;;
v)
echo_b "$SCRIPT v$VERSION by $AUTHOR"
echo "Fork me at $GITHUB"
exit 0
;;
n)
DRY_RUN=1
shift $((OPTIND-1)); OPTIND=1
;;
f)
IGNORE_MODMAN=1
shift $((OPTIND-1)); OPTIND=1
;;
i)
add_include "$OPTARG"
shift $((OPTIND-1)); OPTIND=1
;;
e)
add_exclude "$OPTARG"
shift $((OPTIND-1)); OPTIND=1
;;
b)
BRANCH="$OPTARG"
shift $((OPTIND-1)); OPTIND=1
;;
t)
TAG="$OPTARG"
shift $((OPTIND-1)); OPTIND=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument" >&2
exit 1
;;
esac
done
# Initializes .modgit folder in root dir then quit
if [ "$1" = "init" ]; then
[ -d ".modgit" ] && fault "$SCRIPT is already initialized"
mkdir .modgit || fault "Could not create .modgit directory"
echo_b "Initialized $SCRIPT at $(pwd)/.modgit/"
exit 0
fi
if [ $DRY_RUN -eq 1 ]; then
echo_b "DRY RUN MODE ON"
sleep 1
fi
# Checks if module dir exists or quit
require_module_dir()
{
[ -d "$MODGIT_DIR/$1" ] || fault "Module '$1' does not exist"
return 0
}
# Safe module removal
remove_module_dir()
{
local module="$1"
local module_dir="$MODGIT_DIR/$module"
if [ -n "$module" ] && [ -d "$module_dir" ]; then
rm -rf "$module_dir" 2>/dev/null
fi
return 0
}
# Deletes deployed files of specified module
delete_files()
{
local module="$1"
local module_dir="$MODGIT_DIR/$module"
local deployed_file="$module_dir/deployed.modgit"
[ $DRY_RUN -eq 1 ] && echo_b "Would remove files:" || echo_b "Removing files:"
for line in $(cat "$deployed_file" 2>/dev/null); do
if ! [ -z "$line" ] && [ -f "$ROOT/$line" ]; then
echo $line
# Remove deployed file and empty dirs
if [ $DRY_RUN -eq 0 ]; then
rm "$ROOT/$line" 2>/dev/null
prev_dir=""
dir=$(dirname $line)
while [ "$dir" != "$prev_dir" ]; do
prev_dir=$dir
test_dir="$ROOT/$dir"
if ! [ "$(ls -A $test_dir)" ]; then
rmdir $test_dir
fi
dir=${dir%/*}
done
fi
fi
done
return 0
}
# Removes specified module
delete_module()
{
local module="$1"
local module_dir="$MODGIT_DIR/$module"
require_module_dir "$module"
if [ $DRY_RUN -eq 0 ]; then
read -p "Are you sure you want to remove '$module' module? (y/n): " confirm
[ "$confirm" != "y" ] && [ "$confirm" != "" ] && echo "Aborting..." && return 0
fi
echo_b "Removing '$module' module..."
sleep 1
delete_files "$module" || return 1
[ $DRY_RUN -eq 1 ] && return 0
remove_module_dir "$module"
return $?
}
# Copies specified file to root directory
deploy_file()
{
local root_tmp="${ROOT//\//\\/}\\/"
echo $(echo "$2" | sed "s/^$root_tmp//g")
if [ $DRY_RUN -eq 0 ]; then
mkdir -p $(dirname $2) && cp "$1" "$2"
fi
}
# Synchronizes files from module dir to root dir, then store deployed files for easy remove
move_files()
{
local module="$1"
local module_dir="$MODGIT_DIR/$module"
local source_dir="$module_dir/source"
local includes_file="$module_dir/includes.modgit"
local excludes_file="$module_dir/excludes.modgit"
local modman_file="$source_dir/modman"
local deployed_file="$module_dir/deployed.modgit"
> "$deployed_file" # empty file
cd "$source_dir" || return 1
[ $DRY_RUN -eq 1 ] && echo_b "Would deploy:" || echo_b "Deploying:"
for file in $(find . -type f -not -iwholename '*.git*' | sed 's/^\.\///'); do
# Copy file by default
copy=1
target="$file"
# Include filters
if [ -s "$includes_file" ]; then
copy=0
for filter in $(cat "$includes_file"); do
src=$(echo $filter | cut -d: -f1)
if [ -z "$src" ]; then
continue
fi
if [[ "$file" =~ ^$src ]]; then
copy=1
# Handle optional different target
real=$(echo $filter | cut -d: -f2)
if [ "$src" != "$real" ]; then
tmp_src=$(echo $src | sed 's/^[\/]*//;s/[\/]*$//')
tmp_src="$tmp_src/"
tmp_src="${tmp_src//\//\\/}"
tmp_real=$(echo $real | sed 's/^[\/]*//;s/[\/]*$//')
tmp_real="$tmp_real/"
tmp_real="${tmp_real//\//\\/}"
target=$(echo $file | sed "s/$tmp_src/$tmp_real/g")
fi
break
fi
done
fi
# Exclude filters
if [ -s "$excludes_file" ] && [ $copy -eq 1 ]; then
for filter in $(cat "$excludes_file"); do
if [[ "$file" =~ ^$filter ]]; then
copy=0
break
fi
done
fi
if [ $copy -eq 1 ]; then
# Handle modman file
if [ $IGNORE_MODMAN -eq 0 ] && [ -s "$modman_file" ]; then
IFS=$'\n'
for line in $(cat "$modman_file"); do
if [ -z "$line" ] || [[ $line =~ ^# ]] || [[ $line =~ ^@ ]]; then
continue
fi
IFS=$' \t'
line="${line/\*/}" # remove * char
set -- $line # set $1 and $2
if [[ "$file" =~ ^$1 ]]; then
# Remove trailing slashes and escape paths for sed
src=$(echo $1 | sed 's/^[\/]*//;s/[\/]*$//')
dest=$(echo $2 | sed 's/^[\/]*//;s/[\/]*$//')
src="${src//\//\\/}"
dest="${dest//\//\\/}"
target=$(echo $file | sed "s/$src/$dest/g")
deploy_file "$file" "$ROOT/$target"
echo "$target" >> "$deployed_file"
fi
done
else
deploy_file "$file" "$ROOT/$target"
echo "$target" >> "$deployed_file"
fi
fi
done
return 0
}
# Creates module dir, clones git repo and optionally stores include/exclude filters
create_module()
{
local repo="$1"
local module="$2"
local module_dir="$MODGIT_DIR/$module"; shift 2
local source_dir="$module_dir/source"
cd "$MODGIT_DIR"
echo_b "Cloning $repo..."
if ! git clone --quiet -- $repo "$source_dir"; then
remove_module_dir "$module"
fault "An error occurred while cloning repository"
fi
echo "$repo" > "$module_dir/repository.modgit"
cd "$source_dir"
# Ignore chmod changes in future
git config core.filemode false
if [ -n "$TAG" ]; then
if [ -z $(git tag -l "$TAG") ]; then
remove_module_dir "$module"
fault "Tag '$TAG' does not exist"
fi
if ! git checkout --quiet "$TAG"; then
remove_module_dir "$module"
fault "An error occurred while fetching tag $TAG"
fi
echo "$TAG" > "$module_dir/tag.modgit"
elif ! git checkout --quiet "$BRANCH"; then
remove_module_dir "$module"
fault "An error occurred while fetching branch $BRANCH"
fi
echo "$BRANCH" > "$module_dir/branch.modgit"
echo "Fetching submodules..."
if ! git submodule --quiet update --init --recursive; then
remove_module_dir "$module"
fault "An error occurred while cloning submodules of $repo"
fi
# Save includes filter if not empty
[ -z "$INCLUDES" ] || echo -e "$INCLUDES" > "$module_dir/includes.modgit"
# Save excludes filter if not empty
[ -z "$EXCLUDES" ] || echo -e "$EXCLUDES" > "$module_dir/excludes.modgit"
return 0
}
# Updates a module
update_module()
{
local module="$1"
local module_dir="$MODGIT_DIR/$module"
local source_dir="$module_dir/source"
require_module_dir "$module"
echo_b "Updating '$module' module..."
sleep 1
cd "$source_dir"
local old_commit=$(git rev-parse HEAD)
# If module was fetched with a specific tag
if [ -s "$module_dir/tag.modgit" ]; then
if [ -z "$TAG" ]; then
local repo_tag=$(cat "$module_dir/tag.modgit")
echo "'$module' module was fetched from tag '$repo_tag' and thus cannot be updated without -t option"
return 1
fi
if [ -z $(git tag -l "$TAG") ]; then
echo "Tag '$TAG' does not exist"
return 1
fi
if ! git checkout --quiet "$TAG"; then
echo "An error occurred while fetching tag $TAG"
return 1
fi
local old_tag=$(cat "$module_dir/tag.modgit")
echo "Switching from tag $old_tag to $TAG"
[ $DRY_RUN -eq 0 ] && echo "$TAG" > "$module_dir/tag.modgit"
else
# Ignoring -b option (switching branch) because not yet supported
[ -s "$module_dir/branch.modgit" ] && BRANCH=$(cat "$module_dir/branch.modgit")
git pull --quiet origin "$BRANCH" && git submodule --quiet update --init --recursive || return 1
fi
local new_commit=$(git rev-parse HEAD)
local count=$(git diff --shortstat "$old_commit" "$new_commit" | cut -d" " -f2)
[ $DRY_RUN -eq 1 ] && git checkout --quiet "$old_commit"
[ "$count" = "" ] && echo "No changes found" && return 0
if [ $DRY_RUN -eq 1 ]; then
echo_b "Would modify:"
git --no-pager diff --name-only "$old_commit" "$new_commit"
echo "$count file(s) changed"
return 0
fi
if delete_files "$module" &&
move_files "$module";
then
return 0
else
return 1
fi
}
module_info()
{
local module="$1"
local module_dir="$MODGIT_DIR/$module"
require_module_dir "$module"
echo_b "Information about '$module' module:"
if [ -s "$module_dir/repository.modgit" ]; then
local repo=$(cat "$module_dir/repository.modgit")
echo "Repository: $repo"
fi
if [ -s "$module_dir/tag.modgit" ]; then
local current_tag=$(cat "$module_dir/tag.modgit")
echo "Tag: $current_tag"
else
local current_branch="master"
[ -s "$module_dir/branch.modgit" ] && current_branch=$(cat "$module_dir/branch.modgit")
echo "Branch: $current_branch"
fi
return 0
}
module_info_inline()
{
local module="$1"
local module_dir="$MODGIT_DIR/$module"
local info=""
require_module_dir "$module"
if [ -s "$module_dir/repository.modgit" ]; then
local repo=$(cat "$module_dir/repository.modgit")
info="$info $repo"
fi
if [ -s "$module_dir/tag.modgit" ]; then
local current_tag=$(cat "$module_dir/tag.modgit")
info="$info (tag: $current_tag)"
else
local current_branch="master"
[ -s "$module_dir/branch.modgit" ] && current_branch=$(cat "$module_dir/branch.modgit")
info="$info (branch: $current_branch)"
fi
printf "$(tput bold)%-20s$(tput sgr0) $info\n" "$module"
return 0
}
require_modules()
{
count=$(ls -A "$MODGIT_DIR" | wc -l | sed 's/ //g')
[ $count -eq 0 ] && fault "There is no module installed"
}
# Try to find .modgit dir
ROOT=$(pwd -P)
[ -d "$ROOT/.modgit" ] || fault "modgit directory not found\nRun '$SCRIPT init' in root folder of your project"
# Path to .modgit
MODGIT_DIR="$ROOT/.modgit"
# Action is first argument
if [ -z "$ACTION" ] && [[ "$1" =~ $REGEX_ACTION ]]; then
ACTION="$1"; shift
fi
[ -z "$ACTION" ] && show_help && fault "No action specified"
# Handle action
case "$ACTION" in
list|ls)
# List all installed modules
count=$(ls -A "$MODGIT_DIR" | wc -l | sed 's/ //g')
for module in $(ls -1 "$MODGIT_DIR"); do
[ -d "$MODGIT_DIR/$module" ] || continue;
module_info_inline "$module"
done
echo "$count module(s) found"
;;
clone|add)
# Install new module
module="$1"; shift
[ -z "$module" ] && fault "No module specified"
module_dir="$MODGIT_DIR/$module"
[[ "$module" =~ [^a-z0-9_-]+ ]] &&
fault "You cannot add a module with a name not matching [^a-zA-Z0-9_-]+ pattern\nModule specified: $module"
[ -d "$module_dir" ] && fault "A module with this name already exists"
success=1
repo="$1"; shift
create_module "$repo" "$module" || success=0
cd "$MODGIT_DIR"
if [ $success -eq 1 ]; then
if require_module_dir "$module" &&
move_files "$module";
then
[ $DRY_RUN -eq 0 ] && echo_b "Installation complete" || remove_module_dir "$module"
fi
else
remove_module_dir "$module"
fault "Error cloning '$module', operation cancelled"
fi
;;
update|up)
# Update specified module
module="$1"; shift
[ -z "$module" ] && fault "No module specified"
if ! update_module "$module"; then
fault "Error updating '$module', operation cancelled"
fi
;;
update-all|up-all)
# Update all installed modules
[ -n "$1" ] && fault "Too many arguments for '$ACTION' command"
require_modules
errors=0
for module in $(ls -1 "$MODGIT_DIR"); do
[ -d "$MODGIT_DIR/$module" ] || continue;
if ! update_module "$module"; then
echo_b "-e" "Error occurred while updating '$module'"
errors=$((errors+1))
fi
done
[ $DRY_RUN -eq 0 ] && echo_b "Updated all modules with $errors error(s)"
;;
remove|rm)
# Remove specified module
module="$1"; shift
[ -z "$module" ] && fault "No module specified"
if ! require_module_dir "$module" || ! delete_module "$module"; then
echo_b "Error removing '$module', operation cancelled"
fi
;;
remove-all|rm-all)
# Remove all installed modules
[ -n "$1" ] && fault "Too many arguments for '$ACTION' command"
require_modules
errors=0
for module in $(ls -1 "$MODGIT_DIR"); do
[ -d "$MODGIT_DIR/$module" ] || continue
if ! delete_module "$module"; then
echo_b "-e" "Error occurred while removing '$module' module"
errors=$((errors+1))
fi
done
[ $DRY_RUN -eq 0 ] && echo_b "Removed desired modules with $errors error(s)"
;;
info)
# Show information about specified module
module="$1"; shift
[ -z "$module" ] && fault "No module specified"
module_info "$module"
;;
files)
# List deployed files of specified module
module="$1"; shift
[ -z "$module" ] && fault "No module specified"
require_module_dir "$module"
echo_b "Deployed files of '$module' module:"
sleep 1
module_dir="$MODGIT_DIR/$module"
cat "$module_dir/deployed.modgit"
;;
proxy)
# Run git command into specified module
module="$1"; shift
[ -z "$module" ] && fault "No module specified"
require_module_dir "$module"
cd "$MODGIT_DIR/$module/source"
git "$@"
;;
*)
show_help
echo_b "Invalid action: $ACTION"
exit 1
esac