Skip to content

Commit 0ea306e

Browse files
stefanbellergitster
authored andcommitted
submodule: rewrite module_name shell function in C
This implements the helper `name` in C instead of shell, yielding a nice performance boost. Before this patch, I measured a time (best out of three): $ time ./t7400-submodule-basic.sh >/dev/null real 0m11.066s user 0m3.348s sys 0m8.534s With this patch applied I measured (also best out of three) $ time ./t7400-submodule-basic.sh >/dev/null real 0m10.063s user 0m3.044s sys 0m7.487s Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 74703a1 commit 0ea306e

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

builtin/submodule--helper.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include "pathspec.h"
66
#include "dir.h"
77
#include "utf8.h"
8+
#include "submodule.h"
9+
#include "submodule-config.h"
10+
#include "string-list.h"
811

912
struct module_list {
1013
const struct cache_entry **entries;
@@ -102,6 +105,24 @@ static int module_list(int argc, const char **argv, const char *prefix)
102105
return 0;
103106
}
104107

108+
static int module_name(int argc, const char **argv, const char *prefix)
109+
{
110+
const struct submodule *sub;
111+
112+
if (argc != 2)
113+
usage(_("git submodule--helper name <path>"));
114+
115+
gitmodules_config();
116+
sub = submodule_from_path(null_sha1, argv[1]);
117+
118+
if (!sub)
119+
die(_("no submodule mapping found in .gitmodules for path '%s'"),
120+
argv[1]);
121+
122+
printf("%s\n", sub->name);
123+
124+
return 0;
125+
}
105126

106127
struct cmd_struct {
107128
const char *cmd;
@@ -110,6 +131,7 @@ struct cmd_struct {
110131

111132
static struct cmd_struct commands[] = {
112133
{"list", module_list},
134+
{"name", module_name},
113135
};
114136

115137
int cmd_submodule__helper(int argc, const char **argv, const char *prefix)

git-submodule.sh

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,6 @@ get_submodule_config () {
178178
printf '%s' "${value:-$default}"
179179
}
180180

181-
182-
#
183-
# Map submodule path to submodule name
184-
#
185-
# $1 = path
186-
#
187-
module_name()
188-
{
189-
# Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
190-
sm_path="$1"
191-
re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
192-
name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
193-
sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
194-
test -z "$name" &&
195-
die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
196-
printf '%s\n' "$name"
197-
}
198-
199181
#
200182
# Clone a submodule
201183
#
@@ -498,7 +480,7 @@ cmd_foreach()
498480
then
499481
displaypath=$(relative_path "$sm_path")
500482
say "$(eval_gettext "Entering '\$prefix\$displaypath'")"
501-
name=$(module_name "$sm_path")
483+
name=$(git submodule--helper name "$sm_path")
502484
(
503485
prefix="$prefix$sm_path/"
504486
clear_local_git_env
@@ -554,7 +536,7 @@ cmd_init()
554536
while read mode sha1 stage sm_path
555537
do
556538
die_if_unmatched "$mode"
557-
name=$(module_name "$sm_path") || exit
539+
name=$(git submodule--helper name "$sm_path") || exit
558540

559541
displaypath=$(relative_path "$sm_path")
560542

@@ -636,7 +618,7 @@ cmd_deinit()
636618
while read mode sha1 stage sm_path
637619
do
638620
die_if_unmatched "$mode"
639-
name=$(module_name "$sm_path") || exit
621+
name=$(git submodule--helper name "$sm_path") || exit
640622

641623
displaypath=$(relative_path "$sm_path")
642624

@@ -758,7 +740,7 @@ cmd_update()
758740
echo >&2 "Skipping unmerged submodule $prefix$sm_path"
759741
continue
760742
fi
761-
name=$(module_name "$sm_path") || exit
743+
name=$(git submodule--helper name "$sm_path") || exit
762744
url=$(git config submodule."$name".url)
763745
branch=$(get_submodule_config "$name" branch master)
764746
if ! test -z "$update"
@@ -1022,7 +1004,7 @@ cmd_summary() {
10221004
# Respect the ignore setting for --for-status.
10231005
if test -n "$for_status"
10241006
then
1025-
name=$(module_name "$sm_path")
1007+
name=$(git submodule--helper name "$sm_path")
10261008
ignore_config=$(get_submodule_config "$name" ignore none)
10271009
test $status != A && test $ignore_config = all && continue
10281010
fi
@@ -1184,7 +1166,7 @@ cmd_status()
11841166
while read mode sha1 stage sm_path
11851167
do
11861168
die_if_unmatched "$mode"
1187-
name=$(module_name "$sm_path") || exit
1169+
name=$(git submodule--helper name "$sm_path") || exit
11881170
url=$(git config submodule."$name".url)
11891171
displaypath=$(relative_path "$prefix$sm_path")
11901172
if test "$stage" = U
@@ -1261,7 +1243,7 @@ cmd_sync()
12611243
while read mode sha1 stage sm_path
12621244
do
12631245
die_if_unmatched "$mode"
1264-
name=$(module_name "$sm_path")
1246+
name=$(git submodule--helper name "$sm_path")
12651247
url=$(git config -f .gitmodules --get submodule."$name".url)
12661248

12671249
# Possibly a url relative to parent

0 commit comments

Comments
 (0)