-
Notifications
You must be signed in to change notification settings - Fork 8
/
smooth
executable file
·889 lines (753 loc) · 21 KB
/
smooth
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
#! /bin/bash
# smooth (BASH script) -- git/svn/hg wrapper
#
# See [instructions](doc/s for smooth source control.md) for usage.
# Options:
# -s (Git only) Commit the staged files (i.e. the cache)
# -d Debug mode; for smooth developers only.
# -q quiet mode; don't report the top level working directory
# -M/--mercurial/-G/--git Force repo type
# -r (Git only) recursive clone
# -o (Git only) Specify remote to create/use
# -b (Git only) Specify local branch
# -B (Git only) Specify remote branch to push to
#
# Version: 1.5.0
# Copyright: (c) 2014 Alastair Irvine <[email protected]>
# Keywords: git svn hg source-control wrapper
# Requires: bash v4.0+
# Licence: This file is released under the GNU General Public License
#
#
# Licence details:
# This program 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.
#
# See http://www.gnu.org/licenses/gpl-2.0.html for more information.
#
# You can find the complete text of the GPLv2 in the file
# /usr/share/common-licenses/GPL-2 on Debian systems.
# Or see the file COPYING in the same directory as this program.
#
#
# TO-DO:
# + pipe output for svn (when $action is like [dl]*, dispatch should pipe through less or `vim -`)
# + 'init' action: detect if http[s] URL hosts a Mercurial repo
# + hg commit functions
# + need a means to override 'remote.<name>.push' branch
# + convert Mercurial hg@host:[path] repo locations to ssh://[user@]host[:port]/[path] format
# + select branch/remote for 'here', 'transfer', etc.
self=$(basename "$0")
allowed_options=dsqCfrMGo:b:B:m:
allowed_long_options=quiet,force,mercurial,git,origin=,branch=,local-branch=,remote-branch=,message=
# *** FUNCTIONS ***
svn_uuid()
{
svn info "$1" | awk '/^Repository UUID:/ { print $3 }'
}
# Evaluate successively higher candidate directories until the base of the
# working tree for the current repo is found
svn_toplevel()
{
local toplevel dir=$(readlink -f "${1-.}")
# start at arg (or $PWD) and work up the tree
while [ "$dir" != / ] ; do
# if the dir is a working copy, it's a candidate for the toplevel
if [ -d "$dir/.svn" ] ; then
# if there hasn't been a candidate before, flag this dir straightaway
if [ -z "$toplevel" ] ; then
toplevel=$dir
else
# but if there is a candidate, do the magic test: see if there's a
# change in repo UUID
## echo "[$dir] [$toplevel]" >&2
if [ "$(svn_uuid "$dir")" = "$(svn_uuid "$toplevel")" ] ; then
# if no change, appoint a new candidate
toplevel=$dir
else
# if the UUID changed, accept the current candidate
break
fi
fi
elif [ -n "$toplevel" ] ; then
# if the dir isn't an SVN working copy, and there's a candidate, accept it
# (can assume we've ascended too far)
break
fi
# prepare for the next loop iteration
prev_dir=$dir
dir=$(dirname "$dir")
done
# go with the most recent candidate or return an error if there isn't one
if [ -n "$toplevel" ] ; then
echo "$toplevel"
else
return 2
fi
}
# This will return 0 if on a branch or 1 if detached head (outputs nothing)
git_current_branch()
{
git branch | sed -n 's/^\* \(.*\)/\1/p' | grep -v '(no branch)'
}
# Outputs $branch (if set during command-line parsing) or the current branch
# or nothing if detached head (and returns 1)
git_which_branch()
{
## # force output of branch name
## [ $debug -ge 3 ] && exec 2>&1
if [ -n "$local_branch" ] ; then
echo "$local_branch"
else
git_current_branch
fi
}
# Outputs the name of the remote for the appropriate branch (current or
# specified, i.e. present in $local_branch) or nothing (and an non-zero exit
# code) if the branch doesn't exist or doesn't have a remote.
git_remote()
{
local branch
branch=${1:-$(git_which_branch)}
if ! git config --get "branch.$branch.remote"
then
echo "smooth: WARNING: branch '$branch' doesn't have a remote" >&2
return 1
fi
}
# Outputs the URL of the remote for the appropriate branch (current or
# specified, i.e. present in $branch).
git_url()
{
local r
if r=$(git_remote)
then
# Get the origin ID for the branch and look up its URL
git config --get "remote.$r.url"
fi
}
# Pushes the current/specified branch only. Is smart about which branch to push to.
git_transfer()
{
local remote branch rb
# Determine local branch to push
if ! branch=$(git_which_branch)
then
echo "smooth: ERROR: branch '$branch' does not exist" >&2
exit 11
fi
if [ -n "$origin_name" ] ; then
remote=$origin_name
else
if ! remote=$(git_remote "$branch")
then
echo "smooth: ERROR: branch '$branch' does not have a remote" >&2
exit 8
fi
fi
# Determine the remote branch to push to:
# 1) Use specified remote branch (if set)
# 2) Otherwise, get 'remote.<name>.push' (if set)
# 3) Otherwise, get 'branch.<name>.merge' (if set)
# (This emulates "push.default = tracking")
# 4) Otherwise, default to local branch name
# (note: this can create a new (non-tracking) remote branch)
if [ -n "$remote_branch" ] ; then
rb=$remote_branch
elif ! rb=$(git config --get "remote.$remote.push")
then
if ! rb=$(git config --get "branch.$branch.merge")
then
rb=$branch
fi
fi
git push $extra_opts "$remote" "$branch:$rb"
}
# Creates a tag and pushes it.
#
# Options:
# -l Don't push the tag
git_push_tag()
{
local r transfer
if [ -z "$tagname" ] ; then
echo "smooth: ERROR: tag name required" >&2
exit 9
fi
if [ "x$1" = "x-l" ] ; then
transfer=n
shift
else
transfer=y
fi
set -e
git tag "$@"
if [ $transfer = y ] ; then
if ! r=$(git_remote)
then
echo "smooth: ERROR: branch '$branch' does not exist or doesn't have a remote" >&2
exit 8
fi
git push "$r" "$tagname"
fi
set +e
}
git_object_dump()
{
git show $branch
}
git_print_tags()
{
git show-ref --tags $branch | sed 's@refs/tags/@@'
}
# Internal use only
git_do_commit()
{
# Temporary equivalent of `git config --global commit.verbose = true`
local extra_args=(--verbose)
if [ -n "$message" ] ; then
extra_args+=(-m "$message")
fi
if [ $git_use_staged = y ] ; then
git commit "${extra_args[@]}"
elif [ $# -eq 0 -o "$*" = . ] ; then
git commit -a "${extra_args[@]}"
else
git commit "${extra_args[@]}" "$@"
fi
}
git_commit()
{
local transfer
if [ "x$1" = "x-l" ] ; then
shift
transfer=n
else
transfer=y
fi
if [ "x$1" = "x-q" ] ; then
# quick
shift
else
echo 'Running "git pull"...'
set -e
git pull
set +e
echo '...done.'
sleep 1
fi
if [ "x$1" = "x-a" ] ; then
# all (specified) files, even if new
shift
git_use_staged=y
git add -A "$@"
fi
set -e
git_do_commit "$@"
if [ $transfer = y ] ; then
git_transfer
fi
set +e
}
git_commit_paranoid()
{
local needs_merge
## if git status | grep -q '^nothing to commit'
# Figure out if a fast-forward merge is possible
git fetch
if git branch -v | grep -q '^\* [^ ]* [0-9a-f]* \[behind'
then
## needs_merge=y
git stash save
git merge FETCH_HEAD
git stash pop || echo "smooth: WARNING: unstaged changes didn't re-apply; fix manually and run 'git stash drop'" >&2
fi
git_do_commit "$@"
## if [ "$needs_merge" = y ] ; then
## fi
}
# If the branch doesn't exist, try to remote-track the equivalent branch from
# the current branch's remote.
git_smart_jump()
{
not_implemented # FIXME
}
# Merge the source branch's remote updates before merging it into this branch.
git_smart_merge()
{
not_implemented # FIXME
}
# Checks for a detached head, and if so, switches back to the master branch
git_update()
{
if ! git_current_branch > /dev/null
then
git checkout master
fi
git pull
}
svn_commit()
{
svn update && svn commit "$@"
}
hg_commit()
{
local transfer
if [ "x$1" = "x-l" ] ; then
shift
transfer=n
else
transfer=y
fi
if [ "x$1" = "x-q" ] ; then
# quick
shift
else
echo 'Running "hg pull"...'
hg pull -u
echo '...done.'
sleep 1
fi
set -e
if [ "x$1" = "x-a" ] ; then
# all (specified) files, even if new
shift
hg_do_commit --addremove "$@"
else
hg_do_commit "$@"
fi
if [ $transfer = y ] ; then
hg_transfer
fi
set +e
}
hg_do_commit() {
hg commit
}
hg_transfer() {
hg push
}
# $1=revs $2=rtype
mk_revopt()
{
case $2 in
svn) echo -r ${1/../:} ;;
hg) if [[ $1 == *..* ]] ; then echo -r ${1/../ -r } ; else echo -r $1 ; fi ;;
git) echo $1 -- ;;
esac
}
not_implemented()
{
echo "smooth: ERROR: action '$action' not implemented for $rtype" >&2
exit 4
}
# Outputs the command and its args (on separate lines) instead of running it
dispatch_debug()
{
pwd
eval echo \${r_$1[$action]}
shift
echo "$@"
echo --------
}
# Usage: dispatch <rtype> <arg> ...
dispatch()
{
local rtype=$1
shift
if [ $quiet = n ] ; then
echo "${bold}$PWD${normal} ($rtype):"
fi
if [ -z "$(eval echo \${r_$rtype[$action]})" ] ; then
echo "smooth: ERROR: action '$action' not supported for $rtype" >&2
exit 4
fi
## set -x
[ $debug -ge 2 ] && eval echo "'cmd:['\${r_$rtype[$action]} '$*]'"
eval \${r_$rtype[$action]} \
$(if [ -n "$revs" ] ; then mk_revopt "$revs" $rtype ; fi) \
'$extra_opts "$@"'
}
# Handle option if present (and hope pass-through options don't overlap with
# options taken by individual functions).
# Then immediately starts the next cycle of the loop that called this function.
maybe_store_option()
{
if [ "$no_more_options" != y ] ; then
if [[ "$1" != -- && "$1" == -?* ]] ; then
args+=($1)
continue
else
no_more_options=y
fi
fi
}
# Queries the arg (a relative directory) to see if it belongs to a work tree.
# If so, set $toplevel and $rtype. If not, exit with an error message & code.
set_toplevel()
{
# test for .svn first, and last (to account for un-added subdirectories)
if [ -d "$1"/.svn ] ; then
rtype=svn
toplevel=$(svn_toplevel "$1")
elif toplevel=$(cd "$1" && git rev-parse --show-toplevel 2> /dev/null) ; then
# TO-DO: see what happens with submodules
rtype=git
elif toplevel=$(cd "$1" && hg root 2> /dev/null) ; then
rtype=hg
elif toplevel=$(svn_toplevel "$1") ; then
rtype=svn
else
echo "smooth: ERROR: no revision control directory found in $1 or parents" >&2
exit 3
fi
}
# File/dir args don't make sense for some actions.
maybe_clear_args()
{
# The following actions don't take args; only $PWD is important:
# 'transfer', 'jump', 'branch', 'update', 'here', 'print-tags', 'object-dump'
if [[ $action == [tjbuh]* ]] ; then
args=()
fi
}
# Identify if the latest arg represents a change of toplevel, and if so, do
# what is necessary to finish the previous arg group and start a new one.
# Usage: maybe_start_arggroup <dir>
maybe_start_arggroup()
{
# If the action is 'move' (which doesn't use multiple arg groups), only
# handle the first one (i.e. set toplevel and instead just let the args up.
if [[ $action != [m]* || -z "$prev_toplevel" ]] ; then
set_toplevel "$1"
# Check if the arg represents a new or changed toplevel
if [ "$toplevel" != "$prev_toplevel" ] ; then
# Try to identify the start of a new group other than the first
if [ -n "$prev_toplevel" ] ; then
maybe_clear_args
# Dispatch previous group of args
(cd "$prev_toplevel" && $dispatch $prev_rtype "${args[@]}")
# Start the next arg group
args=()
fi
# Store information needed by the next call of this function that will be
# overwritten by its set_toplevel() call
prev_toplevel=$toplevel
prev_rtype=$rtype
fi
fi
}
# If it's a filename, find the directory
dir_only()
{
local dir
if [ -f "$1" ] ; then
dir=$(dirname "$1")
else
dir=$1
fi
echo "$dir"
}
# *** MAINLINE ***
# == dependency checks ==
if [ -z "$BASH_VERSION" ] ; then
echo "${self}: Error: script must be run as a program" >&2
exit 4
else
if ((BASH_VERSINFO[0] < 4 || BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] < 1)) ; then
echo "${self}: Error: bash v4.0+ required" >&2
exit 4
fi
fi
# == initialisation ==
# Fill the associative arrays (one for each repo type), mapping actions to commands.
# (_s.sh contains mappings generated from the docs by Makefile.)
declare -A r_git r_svn r_hg
. $(dirname "$0")/_s.sh
r_git[commit]=git_commit
r_git[c]=git_commit
r_git[commit-quick]="git_commit -q"
r_git[cq]="git_commit -q"
r_git[commit-quick-local]="git_commit -l -q"
r_git[cql]="git_commit -l -q"
r_git[commit-local]="git_commit -l"
r_git[cl]="git_commit -l"
r_git[commit-all]="git_commit -a"
r_git[ca]="git_commit -a"
r_git[commit-all-quick]="git_commit -q -a"
r_git[caq]="git_commit -q -a"
r_git[commit-all-quick-local]="git_commit -l -q -a"
r_git[caql]="git_commit -l -q -a"
r_hg[commit]=hg_commit
r_hg[c]=hg_commit
r_hg[commit-quick]="hg_commit -q"
r_hg[cq]="hg_commit -q"
r_hg[commit-quick-local]="hg_commit -l -q"
r_hg[cql]="hg_commit -l -q"
r_hg[commit-local]="hg_commit -l"
r_hg[cl]="hg_commit -l"
r_hg[commit-all]="hg_commit -a"
r_hg[ca]="hg_commit -a"
r_hg[commit-all-quick]="hg_commit -q -a"
r_hg[caq]="hg_commit -q -a"
r_hg[commit-all-quick-local]="hg_commit -l -q -a"
r_hg[caql]="hg_commit -l -q -a"
r_svn[commit]=svn_commit
r_svn[c]=svn_commit
r_svn[status]="svn status"
r_svn[s]="svn status"
r_svn[st]="svn status"
r_git[status]="git status"
r_git[s]="git status"
r_git[st]="git status"
r_hg[status]="hg status"
r_hg[s]="hg status"
r_hg[st]="hg status"
r_git[transfer]="git_transfer"
r_git[t]="git_transfer"
r_git[stage]="git add --all"
r_git[g]="git add --all"
r_git[p]=git_print_tags
r_git[print-tags]=git_print_tags
r_git[od]=git_object_dump
r_git[object-dump]=git_object_dump
r_git[u]=git_update
r_git[update]=git_update
r_git[i]="git clone --recursive"
r_git[init]="git clone --recursive"
# == Command-line parsing ==
# determine action, i.e. which smooth subcommand to run
case $self in
s|smooth)
if [ $# = 0 ] ; then
action=status
else
action=$1
shift
fi
;;
s-?*) action=${self#s-} ;;
*)
echo "smooth: ERROR: run as unknown name" >&2
exit 4
;;
esac
# (1st arg was already used as the action (and discarded), if necessary)
# -- defaults --
debug=0
quiet=n
git_use_staged=n
if [ -t 1 ] ; then
# by default, only use colour if stdout is a terminal
colour=y
# also, suppress headings for toplevel when git etc. will use a pager
if [[ $action == [dlo]* ]] ; then
quiet=y
fi
else
colour=n
fi
# -- option handling --
set -e
orthogonal_opts=$(getopt --shell=sh --name=smooth \
--options=+$allowed_options --longoptions=$allowed_long_options -- "$@")
cmd_line=$*
eval set -- "$orthogonal_opts"
set +e # getopt would have already reported the error
# Equivalent to '$debug -ge 2' (or 3 for the 2nd one), given that options
# haven't been parsed yet.
# (Only works if -d comes first on command line.)
[ x"$1" = x-d -a x"$2" = x-d ] && echo "options:$orthogonal_opts"
[ x"$1" = x-d -a x"$2" = x-d -a x"$3" = x-d ] && echo "command line: $cmd_line"
while [ x"$1" != x-- ] ; do
case "$1" in
-d) debug=$((debug + 1)) ;;
-f|--force) extra_opts="$extra_opts --force" ;;
-s) git_use_staged=y ;;
-r|--recursive) extra_opts="$extra_opts -r" ;;
-G|--git)
# git repo cloned from local or remote working dir
rtype=git
;;
-M|--mercurial)
# Mercurial repo
rtype=hg
;;
-q|--quiet) quiet=y ;;
-C) colour=y ;;
-o|--origin)
origin_name=$2
shift
;;
-b|--branch|--local-branch)
local_branch=$2
shift
;;
-B|--remote-branch)
remote_branch=$2
shift
;;
-m|--message)
message=$2
shift
;;
esac
shift # get rid of the option (or its arg if the inner shift already got rid it)
done
shift # get rid of the "--"
# -- argument handling --
# Check for a revision range, allowing for open-ended
if [[ "$1" == *[^/]..[^/]* || "$1" == ..[^/]* || "$1" == *[^/].. ]] ; then
if [[ $action != [dl]* ]] ; then
echo "smooth: ERROR: revision arguments make no sense here" >&2
exit 5
fi
revs=$1
shift
fi
# Check for a branch name for 'transfer', 'jump' or 'here'.
# For 'jump' and 'object-dump', the first arg is always a branch name.
# For 'transfer', 'print-tags' or 'here', the first arg is only a branch name
# if followed by "--".
if [[ $action == [tjhpo]* ]] ; then
[ $debug -ge 2 ] && echo '$#:' $# action: $action '$2:' $2
if [[ $action != [htp]* || ( $# -ge 2 && "x$2" == "x--" ) ]] ; then
# For some actions, this won't be done unless "--" is the second arg
# (signifying that the first arg is a branch name).
[ $debug -ge 1 ] && echo "(setting branch to $1)"
branch=$1
shift
fi
if [ "x$1" = "x--" ] ; then
shift
fi
fi
# == Preparation ==
if [ $debug -lt 3 ] ; then
dispatch=dispatch
else
dispatch=dispatch_debug
fi
if [ $colour = y ] ; then
bold=$(setterm -bold on)
normal=$(setterm -bold off)
fi
if [[ $action == d* && $git_use_staged = y ]] ; then
## case $rtype in
## git)
r_git[d]="git diff --staged"
r_git[diff]="git diff --staged"
## ;;
## *)
## echo "smooth: ERROR: option -s not supported for $rtype" >&2
## exit 10
## esac
fi
# == Processing ==
# Break the argument list into a number of separate commands, each having the
# same repository root.
declare -a args
args=()
# -- Identify separate arg groups --
# These might be useful:
# git rev-parse --is-inside-work-tree
# git rev-parse --show-prefix
# (returns the local path under the git repo root. (empty if you are at the git repo root))
case $action in
[e]*) # 'empty' (repo creation)
if [ $# -eq 0 ] ; then
echo "smooth: ERROR: repo type needed" >&2
exit 7
fi
rtype=$1
shift
$dispatch $rtype "$@"
;;
i*) # 'init' (repo clone)
url=$1
if [ -z "$rtype" ] ; then
case $url in
git://*|*.git|https://git*|http://git*|https://*.github.io|https://gist.github.com/*|git@*) rtype=git ;;
http://bitbucket.org/*|https://bitbucket.org/*) rtype=git ;;
ssh://git-codecommit*|git-codecommit*) rtype=git ;;
http*visualstudio.com/*) rtype=git ;;
svn+ssh:*|https://*/svn/*|http://*/svn/*) rtype=svn ;;
*) rtype=hg ;;
esac
fi
if [ -n "$origin_name" ] ; then
extra_opts="$extra_opts -o $origin_name"
fi
if [ -n "$remote_branch" ] ; then
extra_opts="$extra_opts --branch=$remote_branch"
fi
# check if the last argument is an existing directory
if [ -d "${@: -1}" ] ; then
# If it was, cd into it and don't pass it to dispatch()
echo "smooth: Changing directory to ${@: -1}"
cd "${@: -1}"
# strip off the last argument when calling dispatch()
let num_subtract_one=$(($# - 1))
$dispatch $rtype "${@:1:$num_subtract_one}"
else
# Otherwise, assume that no directory was given or that the last
# argument is the directory to be created
$dispatch $rtype "$@"
fi
;;
n*) # 'new-tag*'
# Usage: s new-tag [<option>...] <dir> <tagname> [<commit> | <object>]
for arg ; do
maybe_store_option "$arg" # Might start the next loop cycle
# Set the toplevel based on the first non-option arg, but otherwise ignore it.
if [ -z "$toplevel" ] ; then
if [ ! -e "$arg" ] ; then
echo "smooth: ERROR: '$arg' does not exist" >&2
exit 2
fi
set_toplevel "$(dir_only "$arg")"
else
# Assumes that the second non-option arg is the tag name
if [ -z "$tagname" ] ; then
tagname=$arg
fi
args[${#args[@]}]=$arg
fi
done
(cd "$toplevel" && $dispatch $rtype "${args[@]}")
;;
*)
for arg ; do
maybe_store_option "$arg" # Might start the next cycle
if [[ $action != [m]* && ! -e "$arg" ]] ; then
echo "smooth: ERROR: '$arg' does not exist" >&2
exit 2
fi
maybe_start_arggroup "$(dir_only "$arg")"
# Append the current argument to the existing or new list, but first
# strip off the path fragment between current directory and the toplevel
# (avoid using readlink when the arg is a symlink as this translates
# symlinks to referents)
if [ -L "$arg" ] ; then
arg_fullpath=$(readlink -f "$(dirname "$arg")")/${arg##*/}
else
arg_fullpath=$(readlink -f "$arg")
fi
if [ "$arg_fullpath" = "$toplevel" ] ; then
args[${#args[@]}]=.
else
args[${#args[@]}]=${arg_fullpath#$toplevel/}
fi
done
# set a default in case there were no args or the action takes non-file/dir args
if [ $# -eq 0 ] ; then
set_toplevel .
else
maybe_clear_args
fi
# Dispatch final group of args, if any
(cd "$toplevel" && $dispatch $rtype "${args[@]}")
;;
esac