submodule: remove bashism from shell script
Junio pointed out `relative_path` was using bashisms via the local variables. As the longer term goal is to rewrite most of the submodule code in C, do it now. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stefan Beller committed
May 31, 2016 at 17:27 UTC
44431df02459ec6c4d8c705dd15f376015407043
2 files changed
+20
-43
builtin/submodule--helper.c
+12
@@ -831,6 +831,17 @@ static int update_clone(int argc, const char **argv, const char *prefix)
831
return 0;
832
}
833
834
+static int resolve_relative_path(int argc, const char **argv, const char *prefix)
835
+{
836
+ struct strbuf sb = STRBUF_INIT;
837
+ if (argc != 3)
838
+ die("submodule--helper relative_path takes exactly 2 arguments, got %d", argc);
839
+
840
+ printf("%s", relative_path(argv[1], argv[2], &sb));
841
+ strbuf_release(&sb);
842
+ return 0;
843
+}
844
+
845
struct cmd_struct {
846
const char *cmd;
847
int (*fn)(int, const char **, const char *);
@@ -841,6 +852,7 @@ static struct cmd_struct commands[] = {
852
{"name", module_name},
853
{"clone", module_clone},
854
{"update-clone", update_clone},
855
+ {"relative-path", resolve_relative_path},
856
{"resolve-relative-url", resolve_relative_url},
857
{"resolve-relative-url-test", resolve_relative_url_test},
858
{"init", module_init}
git-submodule.sh
+8
-43
@@ -46,41 +46,6 @@ prefix=
46
custom_name=
47
depth=
48
49
-# Resolve a path to be relative to another path. This is intended for
50
-# converting submodule paths when git-submodule is run in a subdirectory
51
-# and only handles paths where the directory separator is '/'.
52
-#
53
-# The output is the first argument as a path relative to the second argument,
54
-# which defaults to $wt_prefix if it is omitted.
55
-relative_path ()
56
-{
57
- local target curdir result
58
- target=$1
59
- curdir=${2-$wt_prefix}
60
- curdir=${curdir%/}
61
- result=
62
-
63
- while test -n "$curdir"
64
- do
65
- case "$target" in
66
- "$curdir/"*)
67
- target=${target#"$curdir"/}
68
- break
69
- ;;
70
- esac
71
-
72
- result="${result}../"
73
- if test "$curdir" = "${curdir%/*}"
74
- then
75
- curdir=
76
- else
77
- curdir="${curdir%/*}"
78
- fi
79
- done
80
-
81
- echo "$result$target"
82
-}
83
-
49
die_if_unmatched ()
50
{
51
if test "$1" = "#unmatched"
@@ -354,14 +319,14 @@ cmd_foreach()
319
die_if_unmatched "$mode"
320
if test -e "$sm_path"/.git
321
then
357
- displaypath=$(relative_path "$prefix$sm_path")
322
+ displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
323
say "$(eval_gettext "Entering '\$displaypath'")"
324
name=$(git submodule--helper name "$sm_path")
325
(
326
prefix="$prefix$sm_path/"
327
sanitize_submodule_env
328
cd "$sm_path" &&
364
- sm_path=$(relative_path "$sm_path") &&
329
+ sm_path=$(git submodule--helper relative-path "$sm_path" "$wt_prefix") &&
330
# we make $path available to scripts ...
331
path=$sm_path &&
332
if test $# -eq 1
@@ -465,7 +430,7 @@ cmd_deinit()
430
die_if_unmatched "$mode"
431
name=$(git submodule--helper name "$sm_path") || exit
432
468
- displaypath=$(relative_path "$sm_path")
433
+ displaypath=$(git submodule--helper relative-path "$sm_path" "$wt_prefix")
434
435
# Remove the submodule work tree (unless the user already did it)
436
if test -d "$sm_path"
@@ -629,7 +594,7 @@ cmd_update()
594
fi
595
fi
596
632
- displaypath=$(relative_path "$prefix$sm_path")
597
+ displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
598
599
if test $just_cloned -eq 1
600
then
@@ -723,7 +688,7 @@ cmd_update()
688
if test -n "$recursive"
689
then
690
(
726
- prefix=$(relative_path "$prefix$sm_path/")
691
+ prefix=$(git submodule--helper relative-path "$prefix$sm_path/" "$wt_prefix")
692
wt_prefix=
693
sanitize_submodule_env
694
cd "$sm_path" &&
@@ -907,7 +872,7 @@ cmd_summary() {
872
! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
873
missing_dst=t
874
910
- display_name=$(relative_path "$name")
875
+ display_name=$(git submodule--helper relative-path "$name" "$wt_prefix")
876
877
total_commits=
878
case "$missing_src,$missing_dst" in
@@ -1028,7 +993,7 @@ cmd_status()
993
die_if_unmatched "$mode"
994
name=$(git submodule--helper name "$sm_path") || exit
995
url=$(git config submodule."$name".url)
1031
- displaypath=$(relative_path "$prefix$sm_path")
996
+ displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
997
if test "$stage" = U
998
then
999
say "U$sha1 $displaypath"
@@ -1131,7 +1096,7 @@ cmd_sync()
1096
1097
if git config "submodule.$name.url" >/dev/null 2>/dev/null
1098
then
1134
- displaypath=$(relative_path "$prefix$sm_path")
1099
+ displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
1100
say "$(eval_gettext "Synchronizing submodule url for '\$displaypath'")"
1101
git config submodule."$name".url "$super_config_url"
1102