submodule--helper: Have some refactoring only patches first
v4 cover submodule--helper: Have some refactoring only patches first
I added all suggestions from Eric and rewrote the main function to not have hardcoded all the commands we're introducing.
diff to patch series 3 below.
Stefan Beller (3):
submodule: implement list as a builtin helper
submodule: implement name as a builtin helper
submodule: implement clone as a builtin helper
.gitignore | 1 + Makefile | 1 + builtin.h | 1 + builtin/submodule--helper.c | 299 ++++++++++++++++++++++++++++++++++++++++++++ git-submodule.sh | 166 +++--------------------- git.c | 1 + 6 files changed, 319 insertions(+), 150 deletions(-) create mode 100644 builtin/submodule--helper.c
builtin/submodule--helper.c +56 -39
@@ -33,7 +33,7 @@ static int module_list_compute(int argc, const char **argv, ps_matched =xcalloc (pathspec->nr,1 );if (read_cache () <0 ) -die ("index file corrupt" ); +die (_ ("index file corrupt" ));for (i =0 ; i < active_nr; i++) {const struct cache_entry *ce = active_cache[i];
@@ -79,7 +79,7 @@ static int module_list(int argc, const char **argv, const char *prefix) };const char *const git_submodule_helper_usage[] = { -N_ ("git submodule--helper module_list [--prefix=<path>] [<path>...]" ), +N_ ("git submodule--helper list [--prefix=<path>] [<path>...]" ),NULL };
@@ -110,21 +110,20 @@ static int module_name(int argc, const char **argv, const char *prefix) {const struct submodule *sub; -if (argc !=1 ) +if (argc !=2 )usage ("git submodule--helper module_name <path>\n" );gitmodules_config (); - sub =submodule_from_path (null_sha1, argv[0 ]); + sub =submodule_from_path (null_sha1, argv[1 ]);if (!sub)die (N_ ("No submodule mapping found in .gitmodules for path '%s'" ), - argv[0 ]); + argv[1 ]);printf ("%s\n" , sub->name);return 0 ; } -static int clone_submodule (const char *path,const char *gitdir,const char *url,const char *depth,const char *reference,int quiet) {
@@ -166,7 +165,7 @@ static int module_clone(int argc, const char **argv, const char *prefix)struct strbuf rel_path =STRBUF_INIT ;struct strbuf sb =STRBUF_INIT ; -struct option module_update_options[] = { +struct option module_clone_options[] = {OPT_STRING (0 ,"prefix" , &alternative_path,N_ ("path" ),N_ ("alternative anchor for relative paths" )),
@@ -189,38 +188,41 @@ static int module_clone(int argc, const char **argv, const char *prefix)OPT_END () }; -static const char *const git_submodule_helper_usage[] = { -N_ ("git submodule--helper update [--prefix=<path>] [--quiet] [--remote] [-N|--no-fetch]" -"[-f|--force] [--rebase|--merge] [--reference <repository>]" -"[--depth <depth>] [--recursive] [--] [<path>...]" ), +const char *const git_submodule_helper_usage[] = { +N_ ("git submodule--helper clone [--prefix=<path>] [--quiet] " +"[--reference <repository>] [--name <name>] [--url <url>]" +"[--depth <depth>] [--] [<path>...]" ),NULL }; - argc =parse_options (argc, argv, prefix, module_update_options, + argc =parse_options (argc, argv, prefix, module_clone_options, git_submodule_helper_usage,0 );strbuf_addf (&sb,"%s/modules/%s" ,get_git_dir (), name); sm_gitdir =strbuf_detach (&sb,NULL );if (!file_exists (sm_gitdir)) { -safe_create_leading_directories_const (sm_gitdir); +if (safe_create_leading_directories_const (sm_gitdir) <0 ) +die (_ ("could not create directory '%s'" ), sm_gitdir);if (clone_submodule (path, sm_gitdir, url, depth, reference, quiet)) -die (N_ ("Clone of '%s' into submodule path '%s' failed" ), +die (N_ ("clone of '%s' into submodule path '%s' failed" ), url, path); }else { -safe_create_leading_directories_const (path); -unlink (sm_gitdir); +if (safe_create_leading_directories_const (path) <0 ) +die (_ ("could not create directory '%s'" ), path); +if (unlink (sm_gitdir) <0 ) +die_errno (_ ("failed to delete '%s'" ), sm_gitdir); }/* Write a .git file in the submodule to redirect to the superproject. */ -if (alternative_path && !strcmp (alternative_path,"" )) { + if (alternative_path && *alternative_path)) { p =relative_path (path, alternative_path, &sb);strbuf_reset (&sb); }else p = path;if (safe_create_leading_directories_const (p) <0 ) -die ("Could not create directory '%s'" , p); +die (_ ("could not create directory '%s'" ), p);strbuf_addf (&sb,"%s/.git" , p);
@@ -228,31 +230,32 @@ static int module_clone(int argc, const char **argv, const char *prefix)die (_ ("could not create leading directories of '%s'" ), sb.buf); submodule_dot_git =fopen (sb.buf,"w" );if (!submodule_dot_git) -die ("Cannot open file '%s': %s" , sb.buf,strerror (errno)); +die (_ ("cannot open file '%s': %s" ), sb.buf,strerror (errno));fprintf (submodule_dot_git,"gitdir: %s\n" ,relative_path (sm_gitdir, path, &rel_path));if (fclose (submodule_dot_git)) -die ("Could not close file %s" , sb.buf); +die (_ ("could not close file %s" ), sb.buf);strbuf_reset (&sb); -/* Redirect the worktree of the submodule in the superprojects config */ +/* Redirect the worktree of the submodule in the superproject's config */ +if (strbuf_getcwd (&sb)) +die_errno (_ ("unable to get current working directory" )); +if (!is_absolute_path (sm_gitdir)) { -char *s = (char *)sm_gitdir;if (strbuf_getcwd (&sb)) -die_errno ("unable to get current working directory" ); +die_errno (_ ("unable to get current working directory" ));strbuf_addf (&sb,"/%s" , sm_gitdir); +free (sm_gitdir); sm_gitdir =strbuf_detach (&sb,NULL ); -free (s); } -if (strbuf_getcwd (&sb)) -die_errno ("unable to get current working directory" ); +strbuf_addf (&sb,"/%s" , path); p =git_pathdup_submodule (path,"config" );if (!p) -die ("Could not get submodule directory for '%s'" , path); +die (_ ("could not get submodule directory for '%s'" ), path);git_config_set_in_file (p,"core.worktree" ,relative_path (sb.buf, sm_gitdir, &rel_path));strbuf_release (&sb);
@@ -260,23 +263,37 @@ static int module_clone(int argc, const char **argv, const char *prefix)return 0 ; } +struct cmd_struct { +const char *cmd; +int (*fct)(int ,const char **,const char *); +}; + +static struct cmd_struct commands[] = { + {"list" , module_list}, + {"name" , module_name}, + {"clone" , module_clone}, +}; +int cmd_submodule__helper (int argc,const char **argv,const char *prefix) { +int i;if (argc <2 ) -die (N_ ("fatal: submodule--helper subcommand must be called with " -"a subcommand, which are module-list, module-name, " -"module-clone\n" )); + goto out; -if (!strcmp (argv[1 ],"module-list" )) -return module_list (argc -1 , argv +1 , prefix); +for (i =0 ; i <ARRAY_SIZE (commands); i++) +if (!strcmp (argv[1 ], commands[i].cmd)) +return commands[i].fct (argc -1 , argv +1 , prefix); -if (!strcmp (argv[1 ],"module-name" )) -return module_name (argc -2 , argv +2 , prefix); +out: +if (argc >1 ) +fprintf (stderr,_ ("fatal: '%s' is not a valid submodule--helper " +"subcommand, which are:\n" ), argv[1 ]); +else +fprintf (stderr,_ ("fatal: submodule--helper subcommand must be " +"called with a subcommand, which are:\n" )); -if (!strcmp (argv[1 ],"module-clone" )) -return module_clone (argc -1 , argv +1 , prefix); +for (i =0 ; i <ARRAY_SIZE (commands); i++) +fprintf (stderr,"\t%s\n" , commands[i].cmd); -die (N_ ("fatal: '%s' is not a valid submodule--helper subcommand, " -"which are module-list, module-name, module-clone\n" ), - argv[1 ]); +exit (129 ); }
git-submodule.sh +15 -15
@@ -338,7 +338,7 @@ Use -f if you really want to add it." >&2 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")" fi fi - git submodule--helper module-clone ${GIT_QUIET:+--quiet} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" "$reference" "$depth" || exit + git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" "$reference" "$depth" || exit ( clear_local_git_env cd "$sm_path" &&
@@ -398,7 +398,7 @@ cmd_foreach() # command in the subshell (and a recursive call to this function) exec 3<&0 - git submodule--helper module-list --prefix "$wt_prefix"| + git submodule--helper list --prefix "$wt_prefix"| while read mode sha1 stage sm_path do die_if_unmatched "$mode"
@@ -406,7 +406,7 @@ cmd_foreach() then displaypath=$(relative_path "$sm_path") say "$(eval_gettext "Entering '\$prefix\$displaypath'")" - name=$(git submodule--helper module-name "$sm_path") + name=$(git submodule--helper name "$sm_path") ( prefix="$prefix$sm_path/" clear_local_git_env
@@ -458,11 +458,11 @@ cmd_init() shift done - git submodule--helper module-list --prefix "$wt_prefix" "$@" | + git submodule--helper list --prefix "$wt_prefix" "$@" | while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(git submodule--helper module-name "$sm_path") || exit + name=$(git submodule--helper name "$sm_path") || exit displaypath=$(relative_path "$sm_path")
@@ -540,11 +540,11 @@ cmd_deinit() die "$(eval_gettext "Use '.' if you really want to deinitialize all submodules")" fi - git submodule--helper module-list --prefix "$wt_prefix" "$@" | + git submodule--helper list --prefix "$wt_prefix" "$@" | while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(git submodule--helper module-name "$sm_path") || exit + name=$(git submodule--helper name "$sm_path") || exit displaypath=$(relative_path "$sm_path")
@@ -656,7 +656,7 @@ cmd_update() fi cloned_modules= - git submodule--helper module-list --prefix "$wt_prefix" "$@" | { + git submodule--helper list --prefix "$wt_prefix" "$@" | { err= while read mode sha1 stage sm_path do
@@ -666,7 +666,7 @@ cmd_update() echo >&2 "Skipping unmerged submodule $prefix$sm_path" continue fi - name=$(git submodule--helper module-name "$sm_path") || exit + name=$(git submodule--helper name "$sm_path") || exit url=$(git config submodule."$name".url) branch=$(get_submodule_config "$name" branch master) if ! test -z "$update"
@@ -700,7 +700,7 @@ Maybe you want to use 'update --init'?")" if ! test -d "$sm_path"/.git && ! test -f "$sm_path"/.git then - git submodule--helper module-clone ${GIT_QUIET:+--quiet} --prefix "$prefix" --path "$sm_path" --name "$name" --url "$url" "$reference" "$depth" || exit + git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$prefix" --path "$sm_path" --name "$name" --url "$url" "$reference" "$depth" || exit cloned_modules="$cloned_modules;$name" subsha1= else
@@ -930,7 +930,7 @@ cmd_summary() { # Respect the ignore setting for --for-status. if test -n "$for_status" then - name=$(git submodule--helper module-name "$sm_path") + name=$(git submodule--helper name "$sm_path") ignore_config=$(get_submodule_config "$name" ignore none) test $status != A && test $ignore_config = all && continue fi
@@ -1088,11 +1088,11 @@ cmd_status() shift done - git submodule--helper module-list --prefix "$wt_prefix" "$@" | + git submodule--helper list --prefix "$wt_prefix" "$@" | while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(git submodule--helper module-name "$sm_path") || exit + name=$(git submodule--helper name "$sm_path") || exit url=$(git config submodule."$name".url) displaypath=$(relative_path "$prefix$sm_path") if test "$stage" = U
@@ -1165,11 +1165,11 @@ cmd_sync() esac done cd_to_toplevel - git submodule--helper module-list --prefix "$wt_prefix" "$@" | + git submodule--helper list --prefix "$wt_prefix" "$@" | while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(git submodule--helper module-name "$sm_path") + name=$(git submodule--helper name "$sm_path") url=$(git config -f .gitmodules --get submodule."$name".url) # Possibly a url relative to parent
Signature · 1 line
2.5.0.256.g89f8063.dirty
PATCH v4 1/3 submodule: implement `list` as a builtin helper
Most of the submodule operations work on a set of submodules. Calculating and using this set is usually done via:
module_list "$@" | {
while read mode sha1 stage sm_path
do
# the actual operation
done
}
Currently the function module_list is implemented in the git-submodule.sh as a shell script wrapping a perl script. The rewrite is in C, such that it is faster and can later be easily adapted when other functions are rewritten in C.
git-submodule.sh, similar to the builtin commands, will navigate to the top-most directory of the repository and keep the subdirectory as a variable. As the helper is called from within the git-submodule.sh script, we are already navigated to the root level, but the path arguments are still relative to the subdirectory we were in when calling git-submodule.sh. That's why there is a --prefix option pointing to an alternative path which to anchor relative path arguments.
--- .gitignore | 1 + Makefile | 1 + builtin.h | 1 + builtin/submodule--helper.c | 137 ++++++++++++++++++++++++++++++++++++++++++++ git-submodule.sh | 54 ++--------------- git.c | 1 + 6 files changed, 147 insertions(+), 48 deletions(-) create mode 100644 builtin/submodule--helper.c
.gitignore +1 -0
@@ -155,6 +155,7 @@ /git-status /git-stripspace /git-submodule +/git-submodule--helper /git-svn /git-symbolic-ref /git-tag
Makefile +1 -0
@@ -901,6 +901,7 @@ BUILTIN_OBJS += builtin/shortlog.o BUILTIN_OBJS += builtin/show-branch.o BUILTIN_OBJS += builtin/show-ref.o BUILTIN_OBJS += builtin/stripspace.o +BUILTIN_OBJS += builtin/submodule--helper.o BUILTIN_OBJS += builtin/symbolic-ref.o BUILTIN_OBJS += builtin/tag.o BUILTIN_OBJS += builtin/unpack-file.o
builtin.h +1 -0
@@ -119,6 +119,7 @@ extern int cmd_show(int argc, const char **argv, const char *prefix);extern int cmd_show_branch (int argc,const char **argv,const char *prefix);extern int cmd_status (int argc,const char **argv,const char *prefix);extern int cmd_stripspace (int argc,const char **argv,const char *prefix); +extern int cmd_submodule__helper (int argc,const char **argv,const char *prefix);extern int cmd_symbolic_ref (int argc,const char **argv,const char *prefix);extern int cmd_tag (int argc,const char **argv,const char *prefix);extern int cmd_tar_tree (int argc,const char **argv,const char *prefix);
builtin/submodule--helper.c new file +137 -0
137 lines not on this page · raw message
git-submodule.sh +6 -48
96 lines not on this page · raw message
git.c +1 -0
@@ -469,6 +469,7 @@ static struct cmd_struct commands[] = { { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE }, { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE }, { "stripspace", cmd_stripspace }, + { "submodule--helper", cmd_submodule__helper, RUN_SETUP }, { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP }, { "tag", cmd_tag, RUN_SETUP }, { "unpack-file", cmd_unpack_file, RUN_SETUP },
Signature · 1 line
2.5.0.256.g89f8063.dirty
PATCH v4 2/3 submodule: implement `name` as a builtin helper
In reply to Stefan BellerThis implements the helper module_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.534sWith 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--- builtin/submodule--helper.c | 22 ++++++++++++++++++++++ git-submodule.sh | 32 +++++++------------------------- 2 files changed, 29 insertions(+), 25 deletions(-)
builtin/submodule--helper.c +22 -0
@@ -5,6 +5,9 @@#include "pathspec.h" #include "dir.h" #include "utf8.h" +#include "submodule.h" +#include "submodule-config.h" +#include "string-list.h" static const struct cache_entry **ce_entries;static int ce_alloc, ce_used;
@@ -102,6 +105,24 @@ static int module_list(int argc, const char **argv, const char *prefix)return 0 ; } +static int module_name (int argc,const char **argv,const char *prefix) +{ +const struct submodule *sub; + +if (argc !=2 ) +usage ("git submodule--helper module_name <path>\n" ); + +gitmodules_config (); + sub =submodule_from_path (null_sha1, argv[1 ]); + +if (!sub) +die (N_ ("No submodule mapping found in .gitmodules for path '%s'" ), + argv[1 ]); + +printf ("%s\n" , sub->name); + +return 0 ; +}struct cmd_struct {const char *cmd;
@@ -110,6 +131,7 @@ struct cmd_struct {static struct cmd_struct commands[] = { {"list" , module_list}, + {"name" , module_name}, };int cmd_submodule__helper (int argc,const char **argv,const char *prefix)
git-submodule.sh +7 -25
@@ -178,24 +178,6 @@ get_submodule_config () { printf '%s' "${value:-$default}" } - -# -# Map submodule path to submodule name -# -# $1 = path -# -module_name() -{ - # Do we have "submodule.<something>.path = $1" defined in .gitmodules file? - sm_path="$1" - re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g') - name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' | - sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' ) - test -z "$name" && - die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")" - printf '%s\n' "$name" -} - # # Clone a submodule #
@@ -498,7 +480,7 @@ cmd_foreach() then displaypath=$(relative_path "$sm_path") say "$(eval_gettext "Entering '\$prefix\$displaypath'")" - name=$(module_name "$sm_path") + name=$(git submodule--helper name "$sm_path") ( prefix="$prefix$sm_path/" clear_local_git_env
@@ -554,7 +536,7 @@ cmd_init() while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(module_name "$sm_path") || exit + name=$(git submodule--helper name "$sm_path") || exit displaypath=$(relative_path "$sm_path")
@@ -636,7 +618,7 @@ cmd_deinit() while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(module_name "$sm_path") || exit + name=$(git submodule--helper name "$sm_path") || exit displaypath=$(relative_path "$sm_path")
@@ -758,7 +740,7 @@ cmd_update() echo >&2 "Skipping unmerged submodule $prefix$sm_path" continue fi - name=$(module_name "$sm_path") || exit + name=$(git submodule--helper name "$sm_path") || exit url=$(git config submodule."$name".url) branch=$(get_submodule_config "$name" branch master) if ! test -z "$update"
@@ -1022,7 +1004,7 @@ cmd_summary() { # Respect the ignore setting for --for-status. if test -n "$for_status" then - name=$(module_name "$sm_path") + name=$(git submodule--helper name "$sm_path") ignore_config=$(get_submodule_config "$name" ignore none) test $status != A && test $ignore_config = all && continue fi
@@ -1184,7 +1166,7 @@ cmd_status() while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(module_name "$sm_path") || exit + name=$(git submodule--helper name "$sm_path") || exit url=$(git config submodule."$name".url) displaypath=$(relative_path "$prefix$sm_path") if test "$stage" = U
@@ -1261,7 +1243,7 @@ cmd_sync() while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(module_name "$sm_path") + name=$(git submodule--helper name "$sm_path") url=$(git config -f .gitmodules --get submodule."$name".url) # Possibly a url relative to parent
Signature · 1 line
2.5.0.256.g89f8063.dirty
Re: [PATCHv4 2/3] submodule: implement `name` as a builtin helper
Quoted 1 line · Stefan Beller
This implements the helper
module_namein C instead of shell,
You probably want s/module_name/name/ or state more explicitly:
Reimplement module_name shell function in C as name.
or something.
Quoted 9 lines
yielding a nice performance boost.
Signed-off-by Stefan Beller <[email protected]>---
builtin/submodule--helper.c +0
-0
builtin/submodule--helper.c static int module_list(int argc, const char **argv, const char *prefix) · line 110
Quoted 6 lines
builtin/submodule--helper.c +6
-0@@ -102,6 +105,24 @@ static int module_list(int argc, const char **argv, const char *prefix) +static int module_name (int argc,const char **argv,const char *prefix) +{ +const struct submodule *sub; + +if (argc !=2 ) +usage ("git submodule--helper module_name <path>\n" );
Mentioned previously[1]:
usage(_("..."));
s/module_name/name/
s/\\n//
[1]: http://article.gmane.org/gmane.comp.version-control.git/276965
builtin/submodule--helper.c static int module_list(int argc, const char **argv, const char *prefix) · line 120
Quoted 6 lines
builtin/submodule--helper.c +6
-0+gitmodules_config (); + sub =submodule_from_path (null_sha1, argv[1 ]); + +if (!sub) +die (N_ ("No submodule mapping found in .gitmodules for path '%s'" ), + argv[1 ]);
s/N_/_/
s/No/no/
Quoted 13 lines
builtin/submodule--helper.c +5
-0+printf ("%s\n" , sub->name); + +return 0 ; +}struct cmd_struct {const char *cmd;@@ -110,6 +131,7 @@ struct cmd_struct {static struct cmd_struct commands[] = { {"list" , module_list}, + {"name" , module_name}, };
PATCH v4 3/3 submodule: implement `clone` as a builtin helper
In reply to Stefan BellerThis converts implements the helper module_clone. This functionality is needed for converting git submodule update later on, which we want to add threading to.
--- builtin/submodule--helper.c | 140 ++++++++++++++++++++++++++++++++++++++++++++ git-submodule.sh | 80 +------------------------ 2 files changed, 143 insertions(+), 77 deletions(-)
builtin/submodule--helper.c +140 -0
@@ -8,6 +8,7 @@#include "submodule.h" #include "submodule-config.h" #include "string-list.h" +#include "run-command.h" static const struct cache_entry **ce_entries;static int ce_alloc, ce_used;
@@ -123,6 +124,144 @@ static int module_name(int argc, const char **argv, const char *prefix)return 0 ; } +static int clone_submodule (const char *path,const char *gitdir,const char *url, +const char *depth,const char *reference,int quiet) +{ +struct child_process cp; +child_process_init (&cp); + +argv_array_push (&cp.args,"clone" ); +argv_array_push (&cp.args,"--no-checkout" ); +if (quiet) +argv_array_push (&cp.args,"--quiet" ); +if (depth && *depth) +argv_array_pushl (&cp.args,"--depth" , depth,NULL ); +if (reference && *reference) +argv_array_pushl (&cp.args,"--reference" , reference,NULL ); +if (gitdir && *gitdir) +argv_array_pushl (&cp.args,"--separate-git-dir" , gitdir,NULL ); + +argv_array_push (&cp.args, url); +argv_array_push (&cp.args, path); + + cp.git_cmd =1 ; + cp.env = local_repo_env; + + cp.no_stdin =1 ; + cp.no_stdout =1 ; + cp.no_stderr =1 ; + +return run_command (&cp); +} + +static int module_clone (int argc,const char **argv,const char *prefix) +{ +const char *path =NULL , *name =NULL , *url =NULL ; +const char *reference =NULL , *depth =NULL ; +const char *alternative_path =NULL , *p; +int quiet =0 ; +FILE *submodule_dot_git; +char *sm_gitdir; +struct strbuf rel_path =STRBUF_INIT ; +struct strbuf sb =STRBUF_INIT ; + +struct option module_clone_options[] = { +OPT_STRING (0 ,"prefix" , &alternative_path, +N_ ("path" ), +N_ ("alternative anchor for relative paths" )), +OPT_STRING (0 ,"path" , &path, +N_ ("path" ), +N_ ("where the new submodule will be cloned to" )), +OPT_STRING (0 ,"name" , &name, +N_ ("string" ), +N_ ("name of the new submodule" )), +OPT_STRING (0 ,"url" , &url, +N_ ("string" ), +N_ ("url where to clone the submodule from" )), +OPT_STRING (0 ,"reference" , &reference, +N_ ("string" ), +N_ ("reference repository" )), +OPT_STRING (0 ,"depth" , &depth, +N_ ("string" ), +N_ ("depth for shallow clones" )), +OPT__QUIET (&quiet,"Suppress output for cloning a submodule" ), +OPT_END () + }; + +const char *const git_submodule_helper_usage[] = { +N_ ("git submodule--helper clone [--prefix=<path>] [--quiet] " +"[--reference <repository>] [--name <name>] [--url <url>]" +"[--depth <depth>] [--] [<path>...]" ), +NULL + }; + + argc =parse_options (argc, argv, prefix, module_clone_options, + git_submodule_helper_usage,0 ); + +strbuf_addf (&sb,"%s/modules/%s" ,get_git_dir (), name); + sm_gitdir =strbuf_detach (&sb,NULL ); + +if (!file_exists (sm_gitdir)) { +if (safe_create_leading_directories_const (sm_gitdir) <0 ) +die (_ ("could not create directory '%s'" ), sm_gitdir); +if (clone_submodule (path, sm_gitdir, url, depth, reference, quiet)) +die (N_ ("clone of '%s' into submodule path '%s' failed" ), + url, path); + }else { +if (safe_create_leading_directories_const (path) <0 ) +die (_ ("could not create directory '%s'" ), path); +if (unlink (sm_gitdir) <0 ) +die_errno (_ ("failed to delete '%s'" ), sm_gitdir); + } + +/* Write a .git file in the submodule to redirect to the superproject. */ + if (alternative_path && *alternative_path)) { + p =relative_path (path, alternative_path, &sb); +strbuf_reset (&sb); + }else + p = path; + +if (safe_create_leading_directories_const (p) <0 ) +die (_ ("could not create directory '%s'" ), p); + +strbuf_addf (&sb,"%s/.git" , p); + +if (safe_create_leading_directories_const (sb.buf) <0 ) +die (_ ("could not create leading directories of '%s'" ), sb.buf); + submodule_dot_git =fopen (sb.buf,"w" ); +if (!submodule_dot_git) +die (_ ("cannot open file '%s': %s" ), sb.buf,strerror (errno)); + +fprintf (submodule_dot_git,"gitdir: %s\n" , +relative_path (sm_gitdir, path, &rel_path)); +if (fclose (submodule_dot_git)) +die (_ ("could not close file %s" ), sb.buf); +strbuf_reset (&sb); + +/* Redirect the worktree of the submodule in the superproject's config */ +if (strbuf_getcwd (&sb)) +die_errno (_ ("unable to get current working directory" )); + +if (!is_absolute_path (sm_gitdir)) { +if (strbuf_getcwd (&sb)) +die_errno (_ ("unable to get current working directory" )); +strbuf_addf (&sb,"/%s" , sm_gitdir); +free (sm_gitdir); + sm_gitdir =strbuf_detach (&sb,NULL ); + } + + +strbuf_addf (&sb,"/%s" , path); + + p =git_pathdup_submodule (path,"config" ); +if (!p) +die (_ ("could not get submodule directory for '%s'" ), path); +git_config_set_in_file (p,"core.worktree" , +relative_path (sb.buf, sm_gitdir, &rel_path)); +strbuf_release (&sb); +free (sm_gitdir); +return 0 ; +}struct cmd_struct {const char *cmd;
@@ -132,6 +271,7 @@ struct cmd_struct {static struct cmd_struct commands[] = { {"list" , module_list}, {"name" , module_name}, + {"clone" , module_clone}, };int cmd_submodule__helper (int argc,const char **argv,const char *prefix)
git-submodule.sh +3 -77
104 lines not on this page · raw message
Signature · 1 line
2.5.0.256.g89f8063.dirty
Re: [PATCHv4 3/3] submodule: implement `clone` as a builtin helper
Quoted 1 line · Stefan Beller
This converts implements the helper
module_clone. This functionality is
Mentioned previously[1]: "converts implements"?
[1]: http://article.gmane.org/gmane.comp.version-control.git/276966
Quoted 34 lines
needed for converting
git submodule updatelater on, which we want to add threading to.Signed-off-by Stefan Beller <[email protected]>---
builtin/submodule--helper.c +25
-0+static int module_clone(int argc, const char **argv, const char *prefix) +{ + const char *path = NULL, *name = NULL, *url = NULL; + const char *reference = NULL, *depth = NULL; + const char *alternative_path = NULL, *p; + int quiet = 0; + FILE *submodule_dot_git; + char *sm_gitdir; + struct strbuf rel_path = STRBUF_INIT; + struct strbuf sb = STRBUF_INIT; + + struct option module_clone_options[] = { + OPT_STRING(0, "prefix", &alternative_path, + N_("path"), + N_("alternative anchor for relative paths")), + OPT_STRING(0, "path", &path, + N_("path"), + N_("where the new submodule will be cloned to")), + OPT_STRING(0, "name", &name, + N_("string"), + N_("name of the new submodule")), + OPT_STRING(0, "url", &url, + N_("string"), + N_("url where to clone the submodule from")), + OPT_STRING(0, "reference", &reference,
builtin/submodule--helper.c static int module_name(int argc, const char **argv, const char *prefix) · line 191
Quoted 10 lines
builtin/submodule--helper.c +10
-0+ N_("string"), + N_("reference repository")), +OPT_STRING (0 ,"depth" , &depth, +N_ ("string" ), +N_ ("depth for shallow clones" )), +OPT__QUIET (&quiet,"Suppress output for cloning a submodule" ), +OPT_END () + }; + + const char * const git_submodule_helper_usage[] = {
Style: *const
Quoted 8 lines
builtin/submodule--helper.c +8
-0+N_ ("git submodule--helper clone [--prefix=<path>] [--quiet] " +"[--reference <repository>] [--name <name>] [--url <url>]" +"[--depth <depth>] [--] [<path>...]" ), + NULL + }; + + argc =parse_options (argc, argv, prefix, module_clone_options, + git_submodule_helper_usage,0 );
builtin/submodule--helper.c static int module_name(int argc, const char **argv, const char *prefix) · line 209
Quoted 10 lines
builtin/submodule--helper.c +10
-0+ +strbuf_addf (&sb,"%s/modules/%s" ,get_git_dir (), name); + sm_gitdir =strbuf_detach (&sb,NULL ); + +if (!file_exists (sm_gitdir)) { +if (safe_create_leading_directories_const (sm_gitdir) <0 ) +die (_ ("could not create directory '%s'" ), sm_gitdir); +if (clone_submodule (path, sm_gitdir, url, depth, reference, quiet)) +die (N_ ("clone of '%s' into submodule path '%s' failed" ), + url, path);
s/N_/_/
Quoted 14 lines
builtin/submodule--helper.c +14
-0+ }else { +if (safe_create_leading_directories_const (path) <0 ) +die (_ ("could not create directory '%s'" ), path); +if (unlink (sm_gitdir) <0 ) +die_errno (_ ("failed to delete '%s'" ), sm_gitdir); + } + +/* Write a .git file in the submodule to redirect to the superproject. */ + if (alternative_path && *alternative_path)) { + p =relative_path (path, alternative_path, &sb); +strbuf_reset (&sb); + }else + p = path; +
builtin/submodule--helper.c static int module_name(int argc, const char **argv, const char *prefix) · line 233
Quoted 10 lines
builtin/submodule--helper.c +10
-0+if (safe_create_leading_directories_const (p) <0 ) +die (_ ("could not create directory '%s'" ), p); + +strbuf_addf (&sb,"%s/.git" , p); + +if (safe_create_leading_directories_const (sb.buf) <0 ) +die (_ ("could not create leading directories of '%s'" ), sb.buf); + submodule_dot_git =fopen (sb.buf,"w" ); +if (!submodule_dot_git) +die (_ ("cannot open file '%s': %s" ), sb.buf,strerror (errno));
Style: drop space before '('
Also, can't you use die_errno() here?
builtin/submodule--helper.c static int module_name(int argc, const char **argv, const char *prefix) · line 247
Quoted 13 lines
builtin/submodule--helper.c +13
-0+fprintf (submodule_dot_git,"gitdir: %s\n" , +relative_path (sm_gitdir, path, &rel_path)); +if (fclose (submodule_dot_git)) +die (_ ("could not close file %s" ), sb.buf); +strbuf_reset (&sb); + +/* Redirect the worktree of the submodule in the superproject's config */ +if (strbuf_getcwd (&sb)) +die_errno (_ ("unable to get current working directory" )); + +if (!is_absolute_path (sm_gitdir)) { +if (strbuf_getcwd (&sb)) +die_errno (_ ("unable to get current working directory" ));
Why does this need to call getcwd() on 'sb' when it was called immediately above the conditional and its value hasn't changed?
Quoted 4 lines
builtin/submodule--helper.c +4
-0+strbuf_addf (&sb,"/%s" , sm_gitdir); +free (sm_gitdir); + sm_gitdir =strbuf_detach (&sb,NULL ); + }
builtin/submodule--helper.c static int module_name(int argc, const char **argv, const char *prefix) · line 261
Quoted 10 lines
builtin/submodule--helper.c +10
-0+ + +strbuf_addf (&sb,"/%s" , path); + + p =git_pathdup_submodule (path,"config" ); +if (!p) +die (_ ("could not get submodule directory for '%s'" ), path); +git_config_set_in_file (p,"core.worktree" , +relative_path (sb.buf, sm_gitdir, &rel_path)); +strbuf_release (&sb);
Is 'rel_path' being leaked here?
Quoted 12 lines
builtin/submodule--helper.c +4
-0+free (sm_gitdir); +return 0 ; +}struct cmd_struct {const char *cmd;@@ -132,6 +271,7 @@ struct cmd_struct {static struct cmd_struct commands[] = { {"list" , module_list}, {"name" , module_name}, + {"clone" , module_clone}, };
Re: [PATCHv4 3/3] submodule: implement `clone` as a builtin helper
Quoted 10 lines · Eric Sunshine
builtin/submodule--helper.c static int module_name(int argc, const char **argv, const char *prefix) · line 247
Quoted 7 lines
builtin/submodule--helper.c +7
-0+/* Redirect the worktree of the submodule in the superproject's config */ +if (strbuf_getcwd (&sb)) +die_errno (_ ("unable to get current working directory" )); + +if (!is_absolute_path (sm_gitdir)) { +if (strbuf_getcwd (&sb)) +die_errno (_ ("unable to get current working directory" ));Why does this need to call
getcwd()on 'sb' when it was called immediately above the conditional and its value hasn't changed?
Even weirder, the next code is:
Quoted 8 lines
builtin/submodule--helper.c static int module_name(int argc, const char **argv, const char *prefix) · line 254
Quoted 8 lines
builtin/submodule--helper.c +7
-0+strbuf_addf (&sb,"/%s" , sm_gitdir); +free (sm_gitdir); + sm_gitdir =strbuf_detach (&sb,NULL ); + } + + +strbuf_addf (&sb,"/%s" , path);
So if it _was_ an absolute path, we are adding "/$path" to nothing (having just detached the strbuf in the conditional above). That seems weird.
-Peff
Re: [PATCHv4 1/3] submodule: implement `list` as a builtin helper
In reply to Stefan BellerQuoted 19 lines · Stefan Beller
Most of the submodule operations work on a set of submodules. Calculating and using this set is usually done via:
module_list"$@" | {
while read mode sha1 stagesm_path
do
# the actual operation
done
}Currently the function
module_listis implemented in thegit-submodule.shas a shell script wrapping a perl script. The rewrite is in C, such that it is faster and can later be easily adapted when other functions are rewritten in C.[...]
Signed-off-by Stefan Beller <[email protected]>---
builtin/submodule--helper.c line 108
Quoted 3 lines
builtin/submodule--helper.c new file +3
-0+struct cmd_struct { +const char *cmd; +int (*fct)(int ,const char **,const char *);
It would be better to stick with a more idiomatic name such as 'f' or 'func' than invent an entirely new one ('fct').
Quoted 16 lines
builtin/submodule--helper.c new file +16
-0+}; + +static struct cmd_struct commands[] = { + {"list" , module_list}, +}; + +int cmd_submodule__helper (int argc,const char **argv,const char *prefix) +{ +int i; +if (argc <2 ) + goto out; + +for (i =0 ; i <ARRAY_SIZE (commands); i++) +if (!strcmp (argv[1 ], commands[i].cmd)) +return commands[i].fct (argc -1 , argv +1 , prefix); +
builtin/submodule--helper.c line 134
Quoted 10 lines
builtin/submodule--helper.c new file +10
-0+out: +if (argc >1 ) +fprintf (stderr,_ ("fatal: '%s' is not a valid submodule--helper " +"subcommand, which are:\n" ), argv[1 ]); +else +fprintf (stderr,_ ("fatal: submodule--helper subcommand must be " +"called with a subcommand, which are:\n" )); + +for (i =0 ; i <ARRAY_SIZE (commands); i++) +fprintf (stderr,"\t%s\n" , commands[i].cmd);
A couple observations:
1. git-submodule--helper isn't the only Git command featuring subcommands which could benefit from this dispatching and error reporting code, so making it re-usable would be sensible rather than hiding it away inside this one (very low-level, not user-facing) command. If you go that route, it would deserve its own patch series, and well thought out design and interface. However...
2. I realize that the suggestion of listing available subcommands was put forth tentatively by Junio[1], but it seems overkill for a command like this which is not user-facing, and is inconsistent with other subcommand-bearing commands. At this stage, it should be sufficient merely to emit a plain error message (without any usage information):
unrecognized subcommand: %s
missing subcommand
at which point the user can consult the man page or "git subcommand--helper -h" to find out what went wrong.
[1]: http://article.gmane.org/gmane.comp.version-control.git/276935
Quoted 3 lines
builtin/submodule--helper.c new file +3
-0+ +exit (129 ); +}
Re: [PATCHv4 1/3] submodule: implement `list` as a builtin helper
In reply to Stefan BellerQuoted 32 lines · Stefan Beller
(quoted diff) +32
-0+static int module_list_compute(int argc, const char **argv, + const char *prefix, + struct pathspec *pathspec) +{ + int i, result = 0; + char *max_prefix, *ps_matched = NULL; + int max_prefix_len; + parse_pathspec(pathspec, 0, + PATHSPEC_PREFER_FULL | + PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP, + prefix, argv); + + /* Find common prefix for all pathspec's */ + max_prefix = common_prefix(pathspec); + max_prefix_len = max_prefix ? strlen(max_prefix) : 0; + + if (pathspec->nr) + ps_matched = xcalloc(pathspec->nr, 1); + + if (read_cache() < 0) + die(_("index file corrupt")); + + for (i = 0; i < active_nr; i++) { + const struct cache_entry *ce = active_cache[i]; + + if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), + max_prefix_len, ps_matched, + S_ISGITLINK(ce->ce_mode) | S_ISDIR(ce->ce_mode))) + continue; + + if (S_ISGITLINK(ce->ce_mode)) { + ALLOC_GROW(ce_entries, ce_used + 1, ce_alloc);
Review
Quoted 10 lines
(quoted diff) +10
-0+ ce_entries[ce_used++] = ce; + } + + while (i + 1 < active_nr && !strcmp(ce->name, active_cache[i + 1]->name)) + /* + * Skip entries with the same name in different stages + * to make sure an entry is returned only once. + */ + i++; + }
I hate myself not catching this earlier, but I suspect that this is not quite a faithful rewrite of the original. It changes behaviour when a conflicted path records a non submodule in stage #1 and a submodule in stage #2 (or stage #3), doesn't it?
The original scripted version will see the stage #1 entry and skips it because it is not a submodule, then will see the stage #2 entry and because the path is not yet in the %unmerged hash, and it will push it to @out.
This loop instead sees a non-submodule entry at stage #1, skips it (because it is not a submodule), then goes on to skip the entries with the same name, including the stage #2 entry that _is_ a submodule.
I think the clever "we are done with this entry, so let's skip all others that have the same name" optimization should be done only when we did handle an entry with the same name. I.e. something like this squashed in.
--------------------------------------------------
builtin/submodule--helper.c +6 -5
@@ -39,12 +39,13 @@ static int module_list_compute(int argc, const char **argv, S_ISGITLINK(ce->ce_mode) | S_ISDIR(ce->ce_mode)))continue ; -if (S_ISGITLINK (ce->ce_mode)) { -ALLOC_GROW (ce_entries, ce_used +1 , ce_alloc); - ce_entries[ce_used++] = ce; - } +if (!S_ISGITLINK (ce->ce_mode)) +continue ; -while (i +1 < active_nr && !strcmp (ce->name, active_cache[i +1 ]->name)) +ALLOC_GROW (ce_entries, ce_used +1 , ce_alloc); + ce_entries[ce_used++] = ce; +while (i +1 < active_nr && + !strcmp (ce->name, active_cache[i +1 ]->name))/* * Skip entries with the same name in different stages * to make sure an entry is returned only once.
--------------------------------------------------
Review
Quoted 12 lines
(quoted diff) +12
-0+static int module_list(int argc, const char **argv, const char *prefix) +{ + int i; + struct pathspec pathspec; + const char *alternative_path; + + struct option module_list_options[] = { + OPT_STRING(0, "prefix", &alternative_path, + N_("path"), + N_("alternative anchor for relative paths")), + OPT_END() + };
Do we really need this alternative_path variable? The "--prefix" option that overrides the passed-in variable prefix would be easier to understand if it touched the same variable, i.e.
--------------------------------------------------
builtin/submodule--helper.c +2 -5
@@ -65,10 +66,9 @@ static int module_list(int argc, const char **argv, const char *prefix) {int i;struct pathspec pathspec; -const char *alternative_path;struct option module_list_options[] = { -OPT_STRING (0 ,"prefix" , &alternative_path, +OPT_STRING (0 ,"prefix" , &prefix,N_ ("path" ),N_ ("alternative anchor for relative paths" )),OPT_END ()
@@ -82,9 +82,7 @@ static int module_list(int argc, const char **argv, const char *prefix) argc =parse_options (argc, argv, prefix, module_list_options, git_submodule_helper_usage,0 ); -if (module_list_compute (argc, argv, alternative_path - ? alternative_path - : prefix, &pathspec) <0 ) { +if (module_list_compute (argc, argv, prefix, &pathspec) <0 ) {printf ("#unmatched\n" );return 1 ; }
--------------------------------------------------
Re: [PATCHv4 1/3] submodule: implement `list` as a builtin helper
Quoted 14 lines · Junio C Hamano
Review
Quoted 12 lines · Stefan Beller
(quoted diff) +11
-0+static int module_list_compute(int argc, const char **argv, + const char *prefix, + struct pathspec *pathspec) +{ + for (i = 0; i < active_nr; i++) { + const struct cache_entry *ce = active_cache[i]; + + if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), + max_prefix_len, ps_matched, + S_ISGITLINK(ce->ce_mode) | S_ISDIR(ce->ce_mode))) + continue;
Another minor thing I noticed here is that ce->ce_mode would never be S_ISDIR().
It is not immediately clear if it is necessary to pass is_dir=true upon S_ISGITLINK(ce->ce_mode) to match_pathspec(), but I think that is probably a right thing to do. The only difference this makes, I think, is when a pathspec ends with a slash. E.g. when you have a submodule at path ce->ce_name == "dir" and the caller says "dir/". Then DO_MATCH_DIRECTORY logic would say "dir/" does match "dir".
So taken together with the remainder of the loop, perhaps
for (i = 0; i < active_nr; i++) {
ce = active_cache[i];
if (!S_ISGITLINK(ce->ce_mode) ||
!match_pathspec(..., is_dir=1))
continue;
ALLOC_GROW(ce_entries, ce_nr + 1, ce_alloc);
ce_entries[ce_nr++] = ce;
while (...)
skip the entries with the same name;
}would be what we want. Pathspec matching is much more expensive than ce_mode check, and after passing that check, you know the last parameter to the match_pathspec() at that point, so the above structure would also make sense from performance point of view, I think. And of course, the structure makes it clear that we only care about GITLINK entries and nothing else in this loop, so it is good from readability point of view, too.
Re: [PATCHv4 3/3] submodule: implement `clone` as a builtin helper
In reply to Jeff KingQuoted 14 lines · Jeff King
Quoted 10 lines · Eric Sunshine
builtin/submodule--helper.c static int module_name(int argc, const char **argv, const char *prefix) · line 247
Quoted 7 lines
builtin/submodule--helper.c +7
-0+/* Redirect the worktree of the submodule in the superproject's config */ +if (strbuf_getcwd (&sb)) +die_errno (_ ("unable to get current working directory" )); + +if (!is_absolute_path (sm_gitdir)) { +if (strbuf_getcwd (&sb)) +die_errno (_ ("unable to get current working directory" ));Why does this need to call
getcwd()on 'sb' when it was called immediately above the conditional and its value hasn't changed?Even weirder, the next code is:
Quoted 11 lines
Quoted 7 lines
builtin/submodule--helper.c static int module_name(int argc, const char **argv, const char *prefix) · line 254
Quoted 7 lines
builtin/submodule--helper.c +7
-0+strbuf_addf (&sb,"/%s" , sm_gitdir); +free (sm_gitdir); + sm_gitdir =strbuf_detach (&sb,NULL ); + } + + +strbuf_addf (&sb,"/%s" , path);So if it _was_ an absolute path, we are adding "/$path" to nothing (having just detached the strbuf in the conditional above). That seems weird.
Indeed, I saw that too, but didn't mention it since this appears to be an incomplete refactoring from the previous round, and my hope was that mentioning the getcwd() oddity would be enough to trigger a thorough check of the code before sending the next version.
Re: [PATCHv4 3/3] submodule: implement `clone` as a builtin helper
In reply to Eric SunshineQuoted 6 lines · Eric Sunshine
Quoted 1 line · Stefan Beller
This converts implements the helper
module_clone. This functionality isMentioned previously[1]: "converts implements"?
[1]: http://article.gmane.org/gmane.comp.version-control.git/276966
Stefan, perhaps another round of proofreading before sending would have helped.
I am neutral with asterisk around const myself. I think I saw this often spelled with SP on both sides, i.e.
$ git grep ' \* const ' \*.c | wc -l
121
$ git grep ' \*const ' \*.c | wc -l
45Majority of the latter, when viewed without pipe to wc, tells me they are mostly borrowed code.
All the other comments from you on this patch makes sense to me.
Thanks.