@@ -14,10 +14,13 @@ USAGE="[--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <re
14
14
or: $dashless [--quiet] foreach [--recursive] <command>
15
15
or: $dashless [--quiet] sync [--recursive] [--] [<path>...]"
16
16
OPTIONS_SPEC=
17
+ SUBDIRECTORY_OK=Yes
17
18
. git-sh-setup
18
19
. git-sh-i18n
19
20
. git-parse-remote
20
21
require_work_tree
22
+ wt_prefix=$( git rev-parse --show-prefix)
23
+ cd_to_toplevel
21
24
22
25
command=
23
26
branch=
@@ -106,12 +109,48 @@ resolve_relative_url ()
106
109
echo " ${is_relative: +${up_path} }${remoteurl# ./ } "
107
110
}
108
111
112
+ # Resolve a path to be relative to another path. This is intended for
113
+ # converting submodule paths when git-submodule is run in a subdirectory
114
+ # and only handles paths where the directory separator is '/'.
115
+ #
116
+ # The output is the first argument as a path relative to the second argument,
117
+ # which defaults to $wt_prefix if it is omitted.
118
+ relative_path ()
119
+ {
120
+ local target curdir result
121
+ target=$1
122
+ curdir=${2-$wt_prefix }
123
+ curdir=${curdir%/ }
124
+ result=
125
+
126
+ while test -n " $curdir "
127
+ do
128
+ case " $target " in
129
+ " $curdir /" * )
130
+ target=${target# " $curdir " / }
131
+ break
132
+ ;;
133
+ esac
134
+
135
+ result=" ${result} ../"
136
+ if test " $curdir " = " ${curdir%/* } "
137
+ then
138
+ curdir=
139
+ else
140
+ curdir=" ${curdir%/* } "
141
+ fi
142
+ done
143
+
144
+ echo " $result$target "
145
+ }
146
+
109
147
#
110
148
# Get submodule info for registered submodules
111
149
# $@ = path to limit submodule list
112
150
#
113
151
module_list ()
114
152
{
153
+ eval " set $( git rev-parse --sq --prefix " $wt_prefix " -- " $@ " ) "
115
154
(
116
155
git ls-files --error-unmatch --stage -- " $@ " ||
117
156
echo " unmatched pathspec exists"
@@ -282,6 +321,7 @@ isnumber()
282
321
cmd_add ()
283
322
{
284
323
# parse $args after "submodule ... add".
324
+ reference_path=
285
325
while test $# -ne 0
286
326
do
287
327
case " $1 " in
@@ -298,11 +338,11 @@ cmd_add()
298
338
;;
299
339
--reference)
300
340
case " $2 " in ' ' ) usage ;; esac
301
- reference= " --reference= $2 "
341
+ reference_path= $2
302
342
shift
303
343
;;
304
344
--reference=* )
305
- reference =" $1 "
345
+ reference_path =" ${1 # --reference=} "
306
346
;;
307
347
--name)
308
348
case " $2 " in ' ' ) usage ;; esac
@@ -323,6 +363,14 @@ cmd_add()
323
363
shift
324
364
done
325
365
366
+ if test -n " $reference_path "
367
+ then
368
+ is_absolute_path " $reference_path " ||
369
+ reference_path=" $wt_prefix$reference_path "
370
+
371
+ reference=" --reference=$reference_path "
372
+ fi
373
+
326
374
repo=$1
327
375
sm_path=$2
328
376
@@ -335,9 +383,14 @@ cmd_add()
335
383
usage
336
384
fi
337
385
386
+ is_absolute_path " $sm_path " || sm_path=" $wt_prefix$sm_path "
387
+
338
388
# assure repo is absolute or relative to parent
339
389
case " $repo " in
340
390
./* |../* )
391
+ test -z " $wt_prefix " ||
392
+ die " $( gettext " Relative path can only be used from the toplevel of the working tree" ) "
393
+
341
394
# dereference source url relative to parent's url
342
395
realrepo=$( resolve_relative_url " $repo " ) || exit
343
396
;;
@@ -471,21 +524,23 @@ cmd_foreach()
471
524
die_if_unmatched " $mode "
472
525
if test -e " $sm_path " /.git
473
526
then
474
- say " $( eval_gettext " Entering '\$ prefix\$ sm_path'" ) "
527
+ displaypath=$( relative_path " $sm_path " )
528
+ say " $( eval_gettext " Entering '\$ prefix\$ displaypath'" ) "
475
529
name=$( module_name " $sm_path " )
476
530
(
477
531
prefix=" $prefix$sm_path /"
478
532
clear_local_git_env
479
- # we make $path available to scripts ...
480
- path=$sm_path
481
533
cd " $sm_path " &&
534
+ sm_path=$( relative_path " $sm_path " ) &&
535
+ # we make $path available to scripts ...
536
+ path=$sm_path &&
482
537
eval " $@ " &&
483
538
if test -n " $recursive "
484
539
then
485
540
cmd_foreach " --recursive" " $@ "
486
541
fi
487
542
) < & 3 3< & - ||
488
- die " $( eval_gettext " Stopping at '\$ prefix\$ sm_path '; script returned non-zero status." ) "
543
+ die " $( eval_gettext " Stopping at '\$ prefix\$ displaypath '; script returned non-zero status." ) "
489
544
fi
490
545
done
491
546
}
@@ -524,12 +579,14 @@ cmd_init()
524
579
die_if_unmatched " $mode "
525
580
name=$( module_name " $sm_path " ) || exit
526
581
582
+ displaypath=$( relative_path " $sm_path " )
583
+
527
584
# Copy url setting when it is not set yet
528
585
if test -z " $( git config " submodule.$name .url" ) "
529
586
then
530
587
url=$( git config -f .gitmodules submodule." $name " .url)
531
588
test -z " $url " &&
532
- die " $( eval_gettext " No url found for submodule path '\$ sm_path ' in .gitmodules" ) "
589
+ die " $( eval_gettext " No url found for submodule path '\$ displaypath ' in .gitmodules" ) "
533
590
534
591
# Possibly a url relative to parent
535
592
case " $url " in
@@ -538,17 +595,17 @@ cmd_init()
538
595
;;
539
596
esac
540
597
git config submodule." $name " .url " $url " ||
541
- die " $( eval_gettext " Failed to register url for submodule path '\$ sm_path '" ) "
598
+ die " $( eval_gettext " Failed to register url for submodule path '\$ displaypath '" ) "
542
599
543
- say " $( eval_gettext " Submodule '\$ name' (\$ url) registered for path '\$ sm_path '" ) "
600
+ say " $( eval_gettext " Submodule '\$ name' (\$ url) registered for path '\$ displaypath '" ) "
544
601
fi
545
602
546
603
# Copy "update" setting when it is not set yet
547
604
upd=" $( git config -f .gitmodules submodule." $name " .update) "
548
605
test -z " $upd " ||
549
606
test -n " $( git config submodule." $name " .update) " ||
550
607
git config submodule." $name " .update " $upd " ||
551
- die " $( eval_gettext " Failed to register update mode for submodule path '\$ sm_path '" ) "
608
+ die " $( eval_gettext " Failed to register update mode for submodule path '\$ displaypath '" ) "
552
609
done
553
610
}
554
611
@@ -594,27 +651,29 @@ cmd_deinit()
594
651
die_if_unmatched " $mode "
595
652
name=$( module_name " $sm_path " ) || exit
596
653
654
+ displaypath=$( relative_path " $sm_path " )
655
+
597
656
# Remove the submodule work tree (unless the user already did it)
598
657
if test -d " $sm_path "
599
658
then
600
659
# Protect submodules containing a .git directory
601
660
if test -d " $sm_path /.git"
602
661
then
603
- echo >&2 " $( eval_gettext " Submodule work tree '\$ sm_path ' contains a .git directory" ) "
662
+ echo >&2 " $( eval_gettext " Submodule work tree '\$ displaypath ' contains a .git directory" ) "
604
663
die " $( eval_gettext " (use 'rm -rf' if you really want to remove it including all of its history)" ) "
605
664
fi
606
665
607
666
if test -z " $force "
608
667
then
609
668
git rm -qn " $sm_path " ||
610
- die " $( eval_gettext " Submodule work tree '\$ sm_path ' contains local modifications; use '-f' to discard them" ) "
669
+ die " $( eval_gettext " Submodule work tree '\$ displaypath ' contains local modifications; use '-f' to discard them" ) "
611
670
fi
612
671
rm -rf " $sm_path " &&
613
- say " $( eval_gettext " Cleared directory '\$ sm_path '" ) " ||
614
- say " $( eval_gettext " Could not remove submodule work tree '\$ sm_path '" ) "
672
+ say " $( eval_gettext " Cleared directory '\$ displaypath '" ) " ||
673
+ say " $( eval_gettext " Could not remove submodule work tree '\$ displaypath '" ) "
615
674
fi
616
675
617
- mkdir " $sm_path " || say " $( eval_gettext " Could not create empty submodule directory '\$ sm_path '" ) "
676
+ mkdir " $sm_path " || say " $( eval_gettext " Could not create empty submodule directory '\$ displaypath '" ) "
618
677
619
678
# Remove the .git/config entries (unless the user already did it)
620
679
if test -n " $( git config --get-regexp submodule." $name \." ) "
@@ -623,7 +682,7 @@ cmd_deinit()
623
682
# the user later decides to init this submodule again
624
683
url=$( git config submodule." $name " .url)
625
684
git config --remove-section submodule." $name " 2> /dev/null &&
626
- say " $( eval_gettext " Submodule '\$ name' (\$ url) unregistered for path '\$ sm_path '" ) "
685
+ say " $( eval_gettext " Submodule '\$ name' (\$ url) unregistered for path '\$ displaypath '" ) "
627
686
fi
628
687
done
629
688
}
@@ -717,9 +776,11 @@ cmd_update()
717
776
update_module=$( git config submodule." $name " .update)
718
777
fi
719
778
779
+ displaypath=$( relative_path " $prefix$sm_path " )
780
+
720
781
if test " $update_module " = " none"
721
782
then
722
- echo " Skipping submodule '$prefix$sm_path '"
783
+ echo " Skipping submodule '$displaypath '"
723
784
continue
724
785
fi
725
786
@@ -728,7 +789,7 @@ cmd_update()
728
789
# Only mention uninitialized submodules when its
729
790
# path have been specified
730
791
test " $# " ! = " 0" &&
731
- say " $( eval_gettext " Submodule path '\$ prefix \$ sm_path ' not initialized
792
+ say " $( eval_gettext " Submodule path '\$ displaypath ' not initialized
732
793
Maybe you want to use 'update --init'?" ) "
733
794
continue
734
795
fi
@@ -741,7 +802,7 @@ Maybe you want to use 'update --init'?")"
741
802
else
742
803
subsha1=$( clear_local_git_env; cd " $sm_path " &&
743
804
git rev-parse --verify HEAD) ||
744
- die " $( eval_gettext " Unable to find current revision in submodule path '\$ prefix \$ sm_path '" ) "
805
+ die " $( eval_gettext " Unable to find current revision in submodule path '\$ displaypath '" ) "
745
806
fi
746
807
747
808
if test -n " $remote "
@@ -774,7 +835,7 @@ Maybe you want to use 'update --init'?")"
774
835
(clear_local_git_env; cd " $sm_path " &&
775
836
( (rev=$( git rev-list -n 1 $sha1 --not --all 2> /dev/null) &&
776
837
test -z " $rev " ) || git-fetch)) ||
777
- die " $( eval_gettext " Unable to fetch in submodule path '\$ prefix \$ sm_path '" ) "
838
+ die " $( eval_gettext " Unable to fetch in submodule path '\$ displaypath '" ) "
778
839
fi
779
840
780
841
# Is this something we just cloned?
@@ -788,20 +849,20 @@ Maybe you want to use 'update --init'?")"
788
849
case " $update_module " in
789
850
rebase)
790
851
command=" git rebase"
791
- die_msg=" $( eval_gettext " Unable to rebase '\$ sha1' in submodule path '\$ prefix \$ sm_path '" ) "
792
- say_msg=" $( eval_gettext " Submodule path '\$ prefix \$ sm_path ': rebased into '\$ sha1'" ) "
852
+ die_msg=" $( eval_gettext " Unable to rebase '\$ sha1' in submodule path '\$ displaypath '" ) "
853
+ say_msg=" $( eval_gettext " Submodule path '\$ displaypath ': rebased into '\$ sha1'" ) "
793
854
must_die_on_failure=yes
794
855
;;
795
856
merge)
796
857
command=" git merge"
797
- die_msg=" $( eval_gettext " Unable to merge '\$ sha1' in submodule path '\$ prefix \$ sm_path '" ) "
798
- say_msg=" $( eval_gettext " Submodule path '\$ prefix \$ sm_path ': merged in '\$ sha1'" ) "
858
+ die_msg=" $( eval_gettext " Unable to merge '\$ sha1' in submodule path '\$ displaypath '" ) "
859
+ say_msg=" $( eval_gettext " Submodule path '\$ displaypath ': merged in '\$ sha1'" ) "
799
860
must_die_on_failure=yes
800
861
;;
801
862
* )
802
863
command=" git checkout $subforce -q"
803
- die_msg=" $( eval_gettext " Unable to checkout '\$ sha1' in submodule path '\$ prefix \$ sm_path '" ) "
804
- say_msg=" $( eval_gettext " Submodule path '\$ prefix \$ sm_path ': checked out '\$ sha1'" ) "
864
+ die_msg=" $( eval_gettext " Unable to checkout '\$ sha1' in submodule path '\$ displaypath '" ) "
865
+ say_msg=" $( eval_gettext " Submodule path '\$ displaypath ': checked out '\$ sha1'" ) "
805
866
;;
806
867
esac
807
868
@@ -828,7 +889,7 @@ Maybe you want to use 'update --init'?")"
828
889
res=$?
829
890
if test $res -gt 0
830
891
then
831
- die_msg=" $( eval_gettext " Failed to recurse into submodule path '\$ prefix \$ sm_path '" ) "
892
+ die_msg=" $( eval_gettext " Failed to recurse into submodule path '\$ displaypath '" ) "
832
893
if test $res -eq 1
833
894
then
834
895
err=" ${err} ;$die_msg "
@@ -942,6 +1003,7 @@ cmd_summary() {
942
1003
fi
943
1004
944
1005
cd_to_toplevel
1006
+ eval " set $( git rev-parse --sq --prefix " $wt_prefix " -- " $@ " ) "
945
1007
# Get modified modules cared by user
946
1008
modules=$( git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- " $@ " |
947
1009
sane_egrep ' ^:([0-7]* )?160000' |
@@ -991,16 +1053,18 @@ cmd_summary() {
991
1053
! GIT_DIR=" $name /.git" git-rev-parse -q --verify $sha1_dst ^0 > /dev/null &&
992
1054
missing_dst=t
993
1055
1056
+ display_name=$( relative_path " $name " )
1057
+
994
1058
total_commits=
995
1059
case " $missing_src ,$missing_dst " in
996
1060
t,)
997
- errmsg=" $( eval_gettext " Warn: \$ name doesn't contain commit \$ sha1_src" ) "
1061
+ errmsg=" $( eval_gettext " Warn: \$ display_name doesn't contain commit \$ sha1_src" ) "
998
1062
;;
999
1063
,t)
1000
- errmsg=" $( eval_gettext " Warn: \$ name doesn't contain commit \$ sha1_dst" ) "
1064
+ errmsg=" $( eval_gettext " Warn: \$ display_name doesn't contain commit \$ sha1_dst" ) "
1001
1065
;;
1002
1066
t,t)
1003
- errmsg=" $( eval_gettext " Warn: \$ name doesn't contain commits \$ sha1_src and \$ sha1_dst" ) "
1067
+ errmsg=" $( eval_gettext " Warn: \$ display_name doesn't contain commits \$ sha1_src and \$ sha1_dst" ) "
1004
1068
;;
1005
1069
* )
1006
1070
errmsg=
@@ -1029,12 +1093,12 @@ cmd_summary() {
1029
1093
submodule=" $( gettext " submodule" ) "
1030
1094
if test $mod_dst = 160000
1031
1095
then
1032
- echo " * $name $sha1_abbr_src ($blob )->$sha1_abbr_dst ($submodule )$total_commits :"
1096
+ echo " * $display_name $sha1_abbr_src ($blob )->$sha1_abbr_dst ($submodule )$total_commits :"
1033
1097
else
1034
- echo " * $name $sha1_abbr_src ($submodule )->$sha1_abbr_dst ($blob )$total_commits :"
1098
+ echo " * $display_name $sha1_abbr_src ($submodule )->$sha1_abbr_dst ($blob )$total_commits :"
1035
1099
fi
1036
1100
else
1037
- echo " * $name $sha1_abbr_src ...$sha1_abbr_dst$total_commits :"
1101
+ echo " * $display_name $sha1_abbr_src ...$sha1_abbr_dst$total_commits :"
1038
1102
fi
1039
1103
if test -n " $errmsg "
1040
1104
then
@@ -1118,7 +1182,7 @@ cmd_status()
1118
1182
die_if_unmatched " $mode "
1119
1183
name=$( module_name " $sm_path " ) || exit
1120
1184
url=$( git config submodule." $name " .url)
1121
- displaypath=" $prefix$sm_path "
1185
+ displaypath=$( relative_path " $prefix$sm_path " )
1122
1186
if test " $stage " = U
1123
1187
then
1124
1188
say " U$sha1 $displaypath "
@@ -1213,7 +1277,8 @@ cmd_sync()
1213
1277
1214
1278
if git config " submodule.$name .url" > /dev/null 2> /dev/null
1215
1279
then
1216
- say " $( eval_gettext " Synchronizing submodule url for '\$ prefix\$ sm_path'" ) "
1280
+ displaypath=$( relative_path " $prefix$sm_path " )
1281
+ say " $( eval_gettext " Synchronizing submodule url for '\$ displaypath'" ) "
1217
1282
git config submodule." $name " .url " $super_config_url "
1218
1283
1219
1284
if test -e " $sm_path " /.git
0 commit comments