Skip to content

Commit 283badc

Browse files
committed
Merge branch 'sb/submodule-helper-relative-path'
A bash-ism "local" has been removed from "git submodule" scripted Porcelain. * sb/submodule-helper-relative-path: submodule: remove bashism from shell script
2 parents f6136f3 + 44431df commit 283badc

File tree

2 files changed

+20
-43
lines changed

2 files changed

+20
-43
lines changed

builtin/submodule--helper.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,17 @@ static int update_clone(int argc, const char **argv, const char *prefix)
831831
return 0;
832832
}
833833

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+
834845
struct cmd_struct {
835846
const char *cmd;
836847
int (*fn)(int, const char **, const char *);
@@ -841,6 +852,7 @@ static struct cmd_struct commands[] = {
841852
{"name", module_name},
842853
{"clone", module_clone},
843854
{"update-clone", update_clone},
855+
{"relative-path", resolve_relative_path},
844856
{"resolve-relative-url", resolve_relative_url},
845857
{"resolve-relative-url-test", resolve_relative_url_test},
846858
{"init", module_init}

git-submodule.sh

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -46,41 +46,6 @@ prefix=
4646
custom_name=
4747
depth=
4848

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-
8449
die_if_unmatched ()
8550
{
8651
if test "$1" = "#unmatched"
@@ -354,14 +319,14 @@ cmd_foreach()
354319
die_if_unmatched "$mode"
355320
if test -e "$sm_path"/.git
356321
then
357-
displaypath=$(relative_path "$prefix$sm_path")
322+
displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
358323
say "$(eval_gettext "Entering '\$displaypath'")"
359324
name=$(git submodule--helper name "$sm_path")
360325
(
361326
prefix="$prefix$sm_path/"
362327
sanitize_submodule_env
363328
cd "$sm_path" &&
364-
sm_path=$(relative_path "$sm_path") &&
329+
sm_path=$(git submodule--helper relative-path "$sm_path" "$wt_prefix") &&
365330
# we make $path available to scripts ...
366331
path=$sm_path &&
367332
if test $# -eq 1
@@ -465,7 +430,7 @@ cmd_deinit()
465430
die_if_unmatched "$mode"
466431
name=$(git submodule--helper name "$sm_path") || exit
467432

468-
displaypath=$(relative_path "$sm_path")
433+
displaypath=$(git submodule--helper relative-path "$sm_path" "$wt_prefix")
469434

470435
# Remove the submodule work tree (unless the user already did it)
471436
if test -d "$sm_path"
@@ -629,7 +594,7 @@ cmd_update()
629594
fi
630595
fi
631596

632-
displaypath=$(relative_path "$prefix$sm_path")
597+
displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
633598

634599
if test $just_cloned -eq 1
635600
then
@@ -723,7 +688,7 @@ cmd_update()
723688
if test -n "$recursive"
724689
then
725690
(
726-
prefix=$(relative_path "$prefix$sm_path/")
691+
prefix=$(git submodule--helper relative-path "$prefix$sm_path/" "$wt_prefix")
727692
wt_prefix=
728693
sanitize_submodule_env
729694
cd "$sm_path" &&
@@ -907,7 +872,7 @@ cmd_summary() {
907872
! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
908873
missing_dst=t
909874

910-
display_name=$(relative_path "$name")
875+
display_name=$(git submodule--helper relative-path "$name" "$wt_prefix")
911876

912877
total_commits=
913878
case "$missing_src,$missing_dst" in
@@ -1028,7 +993,7 @@ cmd_status()
1028993
die_if_unmatched "$mode"
1029994
name=$(git submodule--helper name "$sm_path") || exit
1030995
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")
1032997
if test "$stage" = U
1033998
then
1034999
say "U$sha1 $displaypath"
@@ -1131,7 +1096,7 @@ cmd_sync()
11311096

11321097
if git config "submodule.$name.url" >/dev/null 2>/dev/null
11331098
then
1134-
displaypath=$(relative_path "$prefix$sm_path")
1099+
displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
11351100
say "$(eval_gettext "Synchronizing submodule url for '\$displaypath'")"
11361101
git config submodule."$name".url "$super_config_url"
11371102

0 commit comments

Comments
 (0)