Port `git submodule init` from shell to C
cover Port `git submodule init` from shell to C
The first patch is a reroll of "[PATCH] submodule: Port resolve_relative_url from shell to C" and the second patch is new.
This applies on top of origin/sb/submodule-parallel-update, as it makes use of the recorded update strategy in the submodule configuration.
Thanks,
Stefan
Stefan Beller (2):
submodule: Port resolve_relative_url from shell to C
submodule: Port init from shell to C
builtin/submodule--helper.c | 330 +++++++++++++++++++++++++++++++++++++++++++- git-submodule.sh | 118 +--------------- t/t0060-path-utils.sh | 42 ++++++ 3 files changed, 370 insertions(+), 120 deletions(-)
Signature · 1 line
2.7.0.rc0.37.gb4a0220.dirty
PATCH 1/2 submodule: Port resolve_relative_url from shell to C
Later on we want to automatically call git submodule init from other commands, such that the user doesn't have to initialize the submodule themself. As these other commands are written in C already, we'd need the init functionality in C, too. The resolve_relative_url function is a large part of that init functionality, so start by porting this function to C.
To create the tests in t0060, the function resolve_relative_url was temporarily enhanced to write all inputs and output to disk when running the test suite. The added tests in this patch are a small selection thereof.
---
Quoted 3 lines
... and ends here can easily be rewritten to become a single
strbuf_addf()without the xstrdup()s and without the early exit (at the cost of some additional ?: conditionals in the arguments).
I did not go that route, as I could not find a viable solution there.
It looks easy, but it is not, because we have a starts_with_dot_slash check which is either applied to remoteurl alone or a combination of all of the inputs (remoteurl=".", colonsep = "/", url + ./....)
builtin/submodule--helper.c | 215 +++++++++++++++++++++++++++++++++++++++++++- git-submodule.sh | 81 +---------------- t/t0060-path-utils.sh | 42 +++++++++ 3 files changed, 260 insertions(+), 78 deletions(-)
builtin/submodule--helper.c +214 -1
@@ -9,6 +9,217 @@#include "submodule-config.h" #include "string-list.h" #include "run-command.h" +#include "remote.h" +#include "refs.h" +#include "connect.h" + +static char *get_default_remote (void ) +{ +char *dest =NULL , *ret; +unsigned char sha1[20 ]; +int flag =0 ; +struct strbuf sb =STRBUF_INIT ; +const char *refname =resolve_ref_unsafe ("HEAD" ,0 , sha1, &flag); + +if (!refname) +die ("No such ref: HEAD" ); + +/* detached HEAD */ +if (!strcmp (refname,"HEAD" )) +return xstrdup ("origin" ); + +if (!skip_prefix (refname,"refs/heads/" , &refname)) +die (_ ("Expecting a full ref name, got %s" ), refname); + +strbuf_addf (&sb,"branch.%s.remote" , refname); +if (git_config_get_string (sb.buf, &dest)) + ret =xstrdup ("origin" ); +else + ret =xstrdup (dest); + +strbuf_release (&sb); +return ret; +} + +static int starts_with_dot_slash (const char *str) +{ +return str[0 ] =='.' &&is_dir_sep (str[1 ]); +} + +static int starts_with_dot_dot_slash (const char *str) +{ +return str[0 ] =='.' && str[1 ] =='.' &&is_dir_sep (str[2 ]); +} + +static char *last_dir_separator (char *str) +{ +char *p = str +strlen (str); +while (p-- > str) +if (is_dir_sep (*p)) +return p; +return NULL ; +} + +/* +* Returns 1 if it was the last chop before ':'. +*/ +static int chop_last_dir (char **remoteurl,int is_relative) +{ +char *rfind =last_dir_separator (*remoteurl); +if (rfind) { + *rfind ='\0' ; +return 0 ; + } + + rfind =strrchr (*remoteurl,':' ); +if (rfind) { + *rfind ='\0' ; +return 1 ; + } + +if (is_relative || !strcmp ("." , *remoteurl)) +die (_ ("cannot strip one component off url '%s'" ), + *remoteurl); + +free (*remoteurl); + *remoteurl =xstrdup ("." ); +return 0 ; +} + +/* +* The `url` argument is the URL that navigates to the submodule origin +* repo. When relative, this URL is relative to the superproject origin +* URL repo. The `up_path` argument, if specified, is the relative +* path that navigates from the submodule working tree to the superproject +* working tree. Returns the origin URL of the submodule. +* +* Return either an absolute URL or filesystem path (if the superproject +* origin URL is an absolute URL or filesystem path, respectively) or a +* relative file system path (if the superproject origin URL is a relative +* file system path). +* +* When the output is a relative file system path, the path is either +* relative to the submodule working tree, if up_path is specified, or to +* the superproject working tree otherwise. +* +* NEEDSWORK: This works incorrectly on the domain and protocol part. +* remote_url url outcome correct +* http://a.com/b ../c http://a.com/c yes +* http://a.com/b ../../c http://c no (domain should be kept) +* http://a.com/b ../../../c http:/c no +* http://a.com/b ../../../../c http:c no +* http://a.com/b ../../../../../c .:c no +*/ +static char *relative_url (const char *remote_url, +const char *url, +const char *up_path) +{ +int is_relative =0 ; +int colonsep =0 ; +char *out; +char *remoteurl =xstrdup (remote_url); +struct strbuf sb =STRBUF_INIT ; +size_t len =strlen (remoteurl); + +if (is_dir_sep (remoteurl[len])) + remoteurl[len] ='\0' ; + +if (!url_is_local_not_ssh (remoteurl) ||is_absolute_path (remoteurl)) + is_relative =0 ; +else { + is_relative =1 ; +/* +* Prepend a './' to ensure all relative +* remoteurls start with './' or '../' +*/ +if (!starts_with_dot_slash (remoteurl) && + !starts_with_dot_dot_slash (remoteurl)) { +strbuf_reset (&sb); +strbuf_addf (&sb,"./%s" , remoteurl); +free (remoteurl); + remoteurl =strbuf_detach (&sb,NULL ); + } + } +/* +* When the url starts with '../', remove that and the +* last directory in remoteurl. +*/ +while (url) { +if (starts_with_dot_dot_slash (url)) { + url +=3 ; + colonsep |=chop_last_dir (&remoteurl, is_relative); + }else if (starts_with_dot_slash (url)) + url +=2 ; +else +break ; + } +strbuf_reset (&sb); +strbuf_addf (&sb,"%s%s%s" , remoteurl, colonsep ?":" :"/" , url); + +if (starts_with_dot_slash (sb.buf)) + out =xstrdup (sb.buf +2 ); +else + out =xstrdup (sb.buf); +strbuf_reset (&sb); + +free (remoteurl); +if (!up_path || !is_relative) +return out; + +strbuf_addf (&sb,"%s%s" , up_path, out); +free (out); +return strbuf_detach (&sb,NULL ); +} + +static int resolve_relative_url (int argc,const char **argv,const char *prefix) +{ +char *remoteurl =NULL ; +char *remote =get_default_remote (); +const char *up_path =NULL ; +char *res; +const char *url; +struct strbuf sb =STRBUF_INIT ; + +if (argc !=2 && argc !=3 ) +die ("resolve-relative-url only accepts one or two arguments" ); + + url = argv[1 ]; +strbuf_addf (&sb,"remote.%s.url" , remote); +free (remote); + +if (git_config_get_string (sb.buf, &remoteurl)) +/* the repository is its own authoritative upstream */ + remoteurl =xgetcwd (); + +if (argc ==3 ) + up_path = argv[2 ]; + + res =relative_url (remoteurl, url, up_path); +puts (res); +free (res); +return 0 ; +} + +static int resolve_relative_url_test (int argc,const char **argv,const char *prefix) +{ +char *remoteurl, *res; +const char *up_path, *url; + +if (argc !=4 ) +die ("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>" ); + + up_path = argv[1 ]; + remoteurl =xstrdup (argv[2 ]); + url = argv[3 ]; + +if (!strcmp (up_path,"(null)" )) + up_path =NULL ; + + res =relative_url (remoteurl, url, up_path); +puts (res); +free (res); +return 0 ; +}struct module_list {const struct cache_entry **entries;
@@ -502,7 +713,9 @@ static struct cmd_struct commands[] = { {"list", module_list}, {"name", module_name}, {"clone", module_clone}, - {"update-clone", update_clone} + {"update-clone", update_clone}, + {"resolve-relative-url", resolve_relative_url}, + {"resolve-relative-url-test", resolve_relative_url_test}, };int cmd_submodule__helper (int argc,const char **argv,const char *prefix)
git-submodule.sh +4 -77
106 lines not on this page · raw message
t/t0060-path-utils.sh +42 -0
52 lines not on this page · raw message
Signature · 1 line
2.7.0.rc0.37.gb4a0220.dirty
PATCH 2/2 submodule: Port init from shell to C
In reply to Stefan BellerBy having the init functionality in C, we can reference it easier from other parts in the code.
--- builtin/submodule--helper.c | 115 ++++++++++++++++++++++++++++++++++++++++++-- git-submodule.sh | 39 +-------------- 2 files changed, 111 insertions(+), 43 deletions(-)
builtin/submodule--helper.c +110 -5
@@ -221,6 +221,115 @@ static int resolve_relative_url_test(int argc, const char **argv, const char *prreturn 0 ; } +static int git_submodule_config (const char *var,const char *value,void *cb) +{ +return parse_submodule_config_option (var, value); +} + +static void init_submodule (const char *path,const char *prefix) +{ +const struct submodule *sub; +struct strbuf sb =STRBUF_INIT ; +char *url =NULL ; +const char *upd =NULL ; +const char *displaypath =relative_path (xgetcwd (), prefix, &sb);; + +/* Only loads from .gitmodules, no overlay with .git/config */ +gitmodules_config (); + + sub =submodule_from_path (null_sha1, path); + +/* +* Copy url setting when it is not set yet. +* To look up the url in .git/config, we must not fall back to +* .gitmodules, so look it up directly. +*/ +strbuf_reset (&sb); +strbuf_addf (&sb,"submodule.%s.url" , sub->name); +if (git_config_get_string (sb.buf, &url)) { + url =xstrdup (sub->url);// as overlayed by .gitmodules + +if (!url) +die (_ ("No url found for submodule path '%s' in .gitmodules" ), + displaypath); + +/* Possibly a url relative to parent */ +if (starts_with_dot_dot_slash (url) || +starts_with_dot_slash (url)) { +char *remoteurl; +char *remote =get_default_remote (); +struct strbuf remotesb =STRBUF_INIT ; +strbuf_addf (&remotesb,"remote.%s.url" , remote); +free (remote); + +if (git_config_get_string (remotesb.buf, &remoteurl)) +/* +* The repository is its own +* authoritative upstream +*/ + remoteurl =xgetcwd (); + url =relative_url (remoteurl, url,NULL ); +strbuf_release (&remotesb); + } + +if (git_config_set (sb.buf, url)) +die (_ ("Failed to register url for submodule path '%s'" ), + displaypath); + +printf (_ ("Submodule '%s' (%s) registered for path '%s'\n" ), + sub->name, url, displaypath); +free (url); + } + +/* Copy "update" setting when it is not set yet */ +strbuf_reset (&sb); +strbuf_addf (&sb,"submodule.%s.update" , sub->name); +if (git_config_get_string_const (sb.buf, &upd) && sub->update) { + upd = sub->update; +if (strcmp (sub->update,"checkout" ) && +strcmp (sub->update,"rebase" ) && +strcmp (sub->update,"merge" ) && +strcmp (sub->update,"none" )) { +fprintf (stderr,_ ("warning: unknown update mode '%s' suggested for submodule '%s'\n" ), + upd, sub->name); + upd ="none" ; + } +if (git_config_set (sb.buf, upd)) +die (_ ("Failed to register update mode for submodule path '%s'" ), displaypath); + } +strbuf_release (&sb); +} + +static int module_init (int argc,const char **argv,const char *prefix) +{ +int quiet =0 ; +int i; + +struct option module_init_options[] = { +OPT_STRING (0 ,"prefix" , &prefix, +N_ ("path" ), +N_ ("alternative anchor for relative paths" )), +OPT__QUIET (&quiet,"Suppress output for initialzing a submodule" ), +OPT_END () + }; + +const char *const git_submodule_helper_usage[] = { +N_ ("git submodule--helper init [<path>]" ), +NULL + }; + + argc =parse_options (argc, argv, prefix, module_init_options, + git_submodule_helper_usage,0 ); + +if (argc ==0 ) +die (_ ("Pass at least one submodule" )); + +for (i =0 ; i < argc; i++) +init_submodule (argv[i], prefix); + +return 0 ; +} +struct module_list {const struct cache_entry **entries;int alloc, nr;
@@ -466,11 +575,6 @@ static int module_clone(int argc, const char **argv, const char *prefix)return 0 ; } -static int git_submodule_config (const char *var,const char *value,void *cb) -{ -return parse_submodule_config_option (var, value); -} -struct submodule_update_clone {/* states */ int count;
@@ -716,6 +820,7 @@ static struct cmd_struct commands[] = { {"update-clone", update_clone}, {"resolve-relative-url", resolve_relative_url}, {"resolve-relative-url-test", resolve_relative_url_test}, + {"init", module_init} };int cmd_submodule__helper (int argc,const char **argv,const char *prefix)
git-submodule.sh +1 -38
@@ -398,45 +398,8 @@ cmd_init() while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(git submodule--helper name "$sm_path") || exit - - displaypath=$(relative_path "$sm_path") - - # Copy url setting when it is not set yet - if test -z "$(git config "submodule.$name.url")" - then - url=$(git config -f .gitmodules submodule."$name".url) - test -z "$url" && - die "$(eval_gettext "No url found for submodule path '\$displaypath' in .gitmodules")" - - # Possibly a url relative to parent - case "$url" in - ./*|../*) - url=$(git submodule--helper resolve-relative-url "$url") || exit - ;; - esac - git config submodule."$name".url "$url" || - die "$(eval_gettext "Failed to register url for submodule path '\$displaypath'")" - say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$displaypath'")" - fi - - # Copy "update" setting when it is not set yet - if upd="$(git config -f .gitmodules submodule."$name".update)" && - test -n "$upd" && - test -z "$(git config submodule."$name".update)" - then - case "$upd" in - checkout | rebase | merge | none) - ;; # known modes of updating - *) - echo >&2 "warning: unknown update mode '$upd' suggested for submodule '$name'" - upd=none - ;; - esac - git config submodule."$name".update "$upd" || - die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")" - fi + git submodule--helper init ${GIT_QUIET:+--quiet} "$sm_path" || exit done }
Signature · 1 line
2.7.0.rc0.37.gb4a0220.dirty
I've queued these with a bit of tweak in their log message.
Thanks.
v2 cover Port `git submodule init` from shell to C
This replaces sb/submodule-init.
As I was crafting another series on top of this series I noticed a two bugs, so I am resending it fixed.
* honor the quiet setting
* print to stderr instead of stdout. (In the next series we want to call init_submodule from within update_clone whose stdout is piped into git-submodule.sh, so stderr is the only way to talk to the user)
Thanks,
Stefan
The interdiff:
builtin/submodule--helper.c +5 -5
@@ -226,7 +226,7 @@ static int git_submodule_config(const char *var, const char *value, void *cb)return parse_submodule_config_option (var, value); } -static void init_submodule (const char *path,const char *prefix) +static void init_submodule (const char *path,const char *prefix,int quiet) {const struct submodule *sub;struct strbuf sb =STRBUF_INIT ;
@@ -275,9 +275,9 @@ static void init_submodule(const char *path, const char *prefix)if (git_config_set (sb.buf, url))die (_ ("Failed to register url for submodule path '%s'" ), displaypath); - -printf (_ ("Submodule '%s' (%s) registered for path '%s'\n" ), - sub->name, url, displaypath); +if (!quiet) +fprintf (stderr,_ ("Submodule '%s' (%s) registered for path '%s'\n" ), + sub->name, url, displaypath);free (url); }
@@ -325,7 +325,7 @@ static int module_init(int argc, const char **argv, const char *prefix)die (_ ("Pass at least one submodule" ));for (i =0 ; i < argc; i++) -init_submodule (argv[i], prefix); +init_submodule (argv[i], prefix, quiet);return 0 ; }
Stefan Beller (2):
submodule: port resolve_relative_url from shell to C
submodule: port init from shell to C
builtin/submodule--helper.c | 330 +++++++++++++++++++++++++++++++++++++++++++- git-submodule.sh | 118 +--------------- t/t0060-path-utils.sh | 42 ++++++ 3 files changed, 370 insertions(+), 120 deletions(-)
Signature · 1 line
2.7.0.rc0.44.g6033384.dirty
PATCH v2 1/2 submodule: port resolve_relative_url from shell to C
Later on we want to automatically call git submodule init from other commands, such that the users don't have to initialize the submodule themselves. As these other commands are written in C already, we'd need the init functionality in C, too. The resolve_relative_url function is a large part of that init functionality, so start by porting this function to C.
To create the tests in t0060, the function resolve_relative_url was temporarily enhanced to write all inputs and output to disk when running the test suite. The added tests in this patch are a small selection thereof.
--- builtin/submodule--helper.c | 215 +++++++++++++++++++++++++++++++++++++++++++- git-submodule.sh | 81 +---------------- t/t0060-path-utils.sh | 42 +++++++++ 3 files changed, 260 insertions(+), 78 deletions(-)
builtin/submodule--helper.c +214 -1
@@ -9,6 +9,217 @@#include "submodule-config.h" #include "string-list.h" #include "run-command.h" +#include "remote.h" +#include "refs.h" +#include "connect.h" + +static char *get_default_remote (void ) +{ +char *dest =NULL , *ret; +unsigned char sha1[20 ]; +int flag =0 ; +struct strbuf sb =STRBUF_INIT ; +const char *refname =resolve_ref_unsafe ("HEAD" ,0 , sha1, &flag); + +if (!refname) +die ("No such ref: HEAD" ); + +/* detached HEAD */ +if (!strcmp (refname,"HEAD" )) +return xstrdup ("origin" ); + +if (!skip_prefix (refname,"refs/heads/" , &refname)) +die (_ ("Expecting a full ref name, got %s" ), refname); + +strbuf_addf (&sb,"branch.%s.remote" , refname); +if (git_config_get_string (sb.buf, &dest)) + ret =xstrdup ("origin" ); +else + ret =xstrdup (dest); + +strbuf_release (&sb); +return ret; +} + +static int starts_with_dot_slash (const char *str) +{ +return str[0 ] =='.' &&is_dir_sep (str[1 ]); +} + +static int starts_with_dot_dot_slash (const char *str) +{ +return str[0 ] =='.' && str[1 ] =='.' &&is_dir_sep (str[2 ]); +} + +static char *last_dir_separator (char *str) +{ +char *p = str +strlen (str); +while (p-- > str) +if (is_dir_sep (*p)) +return p; +return NULL ; +} + +/* +* Returns 1 if it was the last chop before ':'. +*/ +static int chop_last_dir (char **remoteurl,int is_relative) +{ +char *rfind =last_dir_separator (*remoteurl); +if (rfind) { + *rfind ='\0' ; +return 0 ; + } + + rfind =strrchr (*remoteurl,':' ); +if (rfind) { + *rfind ='\0' ; +return 1 ; + } + +if (is_relative || !strcmp ("." , *remoteurl)) +die (_ ("cannot strip one component off url '%s'" ), + *remoteurl); + +free (*remoteurl); + *remoteurl =xstrdup ("." ); +return 0 ; +} + +/* +* The `url` argument is the URL that navigates to the submodule origin +* repo. When relative, this URL is relative to the superproject origin +* URL repo. The `up_path` argument, if specified, is the relative +* path that navigates from the submodule working tree to the superproject +* working tree. Returns the origin URL of the submodule. +* +* Return either an absolute URL or filesystem path (if the superproject +* origin URL is an absolute URL or filesystem path, respectively) or a +* relative file system path (if the superproject origin URL is a relative +* file system path). +* +* When the output is a relative file system path, the path is either +* relative to the submodule working tree, if up_path is specified, or to +* the superproject working tree otherwise. +* +* NEEDSWORK: This works incorrectly on the domain and protocol part. +* remote_url url outcome correct +* http://a.com/b ../c http://a.com/c yes +* http://a.com/b ../../c http://c no (domain should be kept) +* http://a.com/b ../../../c http:/c no +* http://a.com/b ../../../../c http:c no +* http://a.com/b ../../../../../c .:c no +*/ +static char *relative_url (const char *remote_url, +const char *url, +const char *up_path) +{ +int is_relative =0 ; +int colonsep =0 ; +char *out; +char *remoteurl =xstrdup (remote_url); +struct strbuf sb =STRBUF_INIT ; +size_t len =strlen (remoteurl); + +if (is_dir_sep (remoteurl[len])) + remoteurl[len] ='\0' ; + +if (!url_is_local_not_ssh (remoteurl) ||is_absolute_path (remoteurl)) + is_relative =0 ; +else { + is_relative =1 ; +/* +* Prepend a './' to ensure all relative +* remoteurls start with './' or '../' +*/ +if (!starts_with_dot_slash (remoteurl) && + !starts_with_dot_dot_slash (remoteurl)) { +strbuf_reset (&sb); +strbuf_addf (&sb,"./%s" , remoteurl); +free (remoteurl); + remoteurl =strbuf_detach (&sb,NULL ); + } + } +/* +* When the url starts with '../', remove that and the +* last directory in remoteurl. +*/ +while (url) { +if (starts_with_dot_dot_slash (url)) { + url +=3 ; + colonsep |=chop_last_dir (&remoteurl, is_relative); + }else if (starts_with_dot_slash (url)) + url +=2 ; +else +break ; + } +strbuf_reset (&sb); +strbuf_addf (&sb,"%s%s%s" , remoteurl, colonsep ?":" :"/" , url); + +if (starts_with_dot_slash (sb.buf)) + out =xstrdup (sb.buf +2 ); +else + out =xstrdup (sb.buf); +strbuf_reset (&sb); + +free (remoteurl); +if (!up_path || !is_relative) +return out; + +strbuf_addf (&sb,"%s%s" , up_path, out); +free (out); +return strbuf_detach (&sb,NULL ); +} + +static int resolve_relative_url (int argc,const char **argv,const char *prefix) +{ +char *remoteurl =NULL ; +char *remote =get_default_remote (); +const char *up_path =NULL ; +char *res; +const char *url; +struct strbuf sb =STRBUF_INIT ; + +if (argc !=2 && argc !=3 ) +die ("resolve-relative-url only accepts one or two arguments" ); + + url = argv[1 ]; +strbuf_addf (&sb,"remote.%s.url" , remote); +free (remote); + +if (git_config_get_string (sb.buf, &remoteurl)) +/* the repository is its own authoritative upstream */ + remoteurl =xgetcwd (); + +if (argc ==3 ) + up_path = argv[2 ]; + + res =relative_url (remoteurl, url, up_path); +puts (res); +free (res); +return 0 ; +} + +static int resolve_relative_url_test (int argc,const char **argv,const char *prefix) +{ +char *remoteurl, *res; +const char *up_path, *url; + +if (argc !=4 ) +die ("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>" ); + + up_path = argv[1 ]; + remoteurl =xstrdup (argv[2 ]); + url = argv[3 ]; + +if (!strcmp (up_path,"(null)" )) + up_path =NULL ; + + res =relative_url (remoteurl, url, up_path); +puts (res); +free (res); +return 0 ; +}struct module_list {const struct cache_entry **entries;
@@ -502,7 +713,9 @@ static struct cmd_struct commands[] = { {"list", module_list}, {"name", module_name}, {"clone", module_clone}, - {"update-clone", update_clone} + {"update-clone", update_clone}, + {"resolve-relative-url", resolve_relative_url}, + {"resolve-relative-url-test", resolve_relative_url_test}, };int cmd_submodule__helper (int argc,const char **argv,const char *prefix)
git-submodule.sh +4 -77
106 lines not on this page · raw message
t/t0060-path-utils.sh +42 -0
52 lines not on this page · raw message
Signature · 1 line
2.7.0.rc0.44.g6033384.dirty
PATCH v2 2/2 submodule: port init from shell to C
In reply to Stefan BellerBy having the init functionality in C, we can reference it easier from other parts in the code.
--- builtin/submodule--helper.c | 115 ++++++++++++++++++++++++++++++++++++++++++-- git-submodule.sh | 39 +-------------- 2 files changed, 111 insertions(+), 43 deletions(-)
builtin/submodule--helper.c +110 -5
@@ -221,6 +221,115 @@ static int resolve_relative_url_test(int argc, const char **argv, const char *prreturn 0 ; } +static int git_submodule_config (const char *var,const char *value,void *cb) +{ +return parse_submodule_config_option (var, value); +} + +static void init_submodule (const char *path,const char *prefix,int quiet) +{ +const struct submodule *sub; +struct strbuf sb =STRBUF_INIT ; +char *url =NULL ; +const char *upd =NULL ; +const char *displaypath =relative_path (xgetcwd (), prefix, &sb);; + +/* Only loads from .gitmodules, no overlay with .git/config */ +gitmodules_config (); + + sub =submodule_from_path (null_sha1, path); + +/* +* Copy url setting when it is not set yet. +* To look up the url in .git/config, we must not fall back to +* .gitmodules, so look it up directly. +*/ +strbuf_reset (&sb); +strbuf_addf (&sb,"submodule.%s.url" , sub->name); +if (git_config_get_string (sb.buf, &url)) { + url =xstrdup (sub->url);// as overlayed by .gitmodules + +if (!url) +die (_ ("No url found for submodule path '%s' in .gitmodules" ), + displaypath); + +/* Possibly a url relative to parent */ +if (starts_with_dot_dot_slash (url) || +starts_with_dot_slash (url)) { +char *remoteurl; +char *remote =get_default_remote (); +struct strbuf remotesb =STRBUF_INIT ; +strbuf_addf (&remotesb,"remote.%s.url" , remote); +free (remote); + +if (git_config_get_string (remotesb.buf, &remoteurl)) +/* +* The repository is its own +* authoritative upstream +*/ + remoteurl =xgetcwd (); + url =relative_url (remoteurl, url,NULL ); +strbuf_release (&remotesb); + } + +if (git_config_set (sb.buf, url)) +die (_ ("Failed to register url for submodule path '%s'" ), + displaypath); +if (!quiet) +fprintf (stderr,_ ("Submodule '%s' (%s) registered for path '%s'\n" ), + sub->name, url, displaypath); +free (url); + } + +/* Copy "update" setting when it is not set yet */ +strbuf_reset (&sb); +strbuf_addf (&sb,"submodule.%s.update" , sub->name); +if (git_config_get_string_const (sb.buf, &upd) && sub->update) { + upd = sub->update; +if (strcmp (sub->update,"checkout" ) && +strcmp (sub->update,"rebase" ) && +strcmp (sub->update,"merge" ) && +strcmp (sub->update,"none" )) { +fprintf (stderr,_ ("warning: unknown update mode '%s' suggested for submodule '%s'\n" ), + upd, sub->name); + upd ="none" ; + } +if (git_config_set (sb.buf, upd)) +die (_ ("Failed to register update mode for submodule path '%s'" ), displaypath); + } +strbuf_release (&sb); +} + +static int module_init (int argc,const char **argv,const char *prefix) +{ +int quiet =0 ; +int i; + +struct option module_init_options[] = { +OPT_STRING (0 ,"prefix" , &prefix, +N_ ("path" ), +N_ ("alternative anchor for relative paths" )), +OPT__QUIET (&quiet,"Suppress output for initialzing a submodule" ), +OPT_END () + }; + +const char *const git_submodule_helper_usage[] = { +N_ ("git submodule--helper init [<path>]" ), +NULL + }; + + argc =parse_options (argc, argv, prefix, module_init_options, + git_submodule_helper_usage,0 ); + +if (argc ==0 ) +die (_ ("Pass at least one submodule" )); + +for (i =0 ; i < argc; i++) +init_submodule (argv[i], prefix, quiet); + +return 0 ; +} +struct module_list {const struct cache_entry **entries;int alloc, nr;
@@ -466,11 +575,6 @@ static int module_clone(int argc, const char **argv, const char *prefix)return 0 ; } -static int git_submodule_config (const char *var,const char *value,void *cb) -{ -return parse_submodule_config_option (var, value); -} -struct submodule_update_clone {/* states */ int count;
@@ -716,6 +820,7 @@ static struct cmd_struct commands[] = { {"update-clone", update_clone}, {"resolve-relative-url", resolve_relative_url}, {"resolve-relative-url-test", resolve_relative_url_test}, + {"init", module_init} };int cmd_submodule__helper (int argc,const char **argv,const char *prefix)
git-submodule.sh +1 -38
@@ -398,45 +398,8 @@ cmd_init() while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(git submodule--helper name "$sm_path") || exit - - displaypath=$(relative_path "$sm_path") - - # Copy url setting when it is not set yet - if test -z "$(git config "submodule.$name.url")" - then - url=$(git config -f .gitmodules submodule."$name".url) - test -z "$url" && - die "$(eval_gettext "No url found for submodule path '\$displaypath' in .gitmodules")" - - # Possibly a url relative to parent - case "$url" in - ./*|../*) - url=$(git submodule--helper resolve-relative-url "$url") || exit - ;; - esac - git config submodule."$name".url "$url" || - die "$(eval_gettext "Failed to register url for submodule path '\$displaypath'")" - say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$displaypath'")" - fi - - # Copy "update" setting when it is not set yet - if upd="$(git config -f .gitmodules submodule."$name".update)" && - test -n "$upd" && - test -z "$(git config submodule."$name".update)" - then - case "$upd" in - checkout | rebase | merge | none) - ;; # known modes of updating - *) - echo >&2 "warning: unknown update mode '$upd' suggested for submodule '$name'" - upd=none - ;; - esac - git config submodule."$name".update "$upd" || - die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")" - fi + git submodule--helper init ${GIT_QUIET:+--quiet} "$sm_path" || exit done }
Signature · 1 line
2.7.0.rc0.44.g6033384.dirty
PATCH v3 2/2 submodule: port init from shell to C
By having the init functionality in C, we can reference it easier from other parts in the code.
---
I noticed a syntactical mistake in this patch, interdiff to v2:
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index bcfcbdf..def382e 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -247,7 +247,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
strbuf_reset(&sb);
strbuf_addf(&sb, "submodule.%s.url", sub->name);
if (git_config_get_string(sb.buf, &url)) {
- url = xstrdup(sub->url); // as overlayed by .gitmodules
+ url = xstrdup(sub->url);builtin/submodule--helper.c | 115 ++++++++++++++++++++++++++++++++++++++++++-- git-submodule.sh | 39 +-------------- 2 files changed, 111 insertions(+), 43 deletions(-)
builtin/submodule--helper.c +110 -5
@@ -221,6 +221,115 @@ static int resolve_relative_url_test(int argc, const char **argv, const char *prreturn 0 ; } +static int git_submodule_config (const char *var,const char *value,void *cb) +{ +return parse_submodule_config_option (var, value); +} + +static void init_submodule (const char *path,const char *prefix,int quiet) +{ +const struct submodule *sub; +struct strbuf sb =STRBUF_INIT ; +char *url =NULL ; +const char *upd =NULL ; +const char *displaypath =relative_path (xgetcwd (), prefix, &sb);; + +/* Only loads from .gitmodules, no overlay with .git/config */ +gitmodules_config (); + + sub =submodule_from_path (null_sha1, path); + +/* +* Copy url setting when it is not set yet. +* To look up the url in .git/config, we must not fall back to +* .gitmodules, so look it up directly. +*/ +strbuf_reset (&sb); +strbuf_addf (&sb,"submodule.%s.url" , sub->name); +if (git_config_get_string (sb.buf, &url)) { + url =xstrdup (sub->url); + +if (!url) +die (_ ("No url found for submodule path '%s' in .gitmodules" ), + displaypath); + +/* Possibly a url relative to parent */ +if (starts_with_dot_dot_slash (url) || +starts_with_dot_slash (url)) { +char *remoteurl; +char *remote =get_default_remote (); +struct strbuf remotesb =STRBUF_INIT ; +strbuf_addf (&remotesb,"remote.%s.url" , remote); +free (remote); + +if (git_config_get_string (remotesb.buf, &remoteurl)) +/* +* The repository is its own +* authoritative upstream +*/ + remoteurl =xgetcwd (); + url =relative_url (remoteurl, url,NULL ); +strbuf_release (&remotesb); + } + +if (git_config_set (sb.buf, url)) +die (_ ("Failed to register url for submodule path '%s'" ), + displaypath); +if (!quiet) +fprintf (stderr,_ ("Submodule '%s' (%s) registered for path '%s'\n" ), + sub->name, url, displaypath); +free (url); + } + +/* Copy "update" setting when it is not set yet */ +strbuf_reset (&sb); +strbuf_addf (&sb,"submodule.%s.update" , sub->name); +if (git_config_get_string_const (sb.buf, &upd) && sub->update) { + upd = sub->update; +if (strcmp (sub->update,"checkout" ) && +strcmp (sub->update,"rebase" ) && +strcmp (sub->update,"merge" ) && +strcmp (sub->update,"none" )) { +fprintf (stderr,_ ("warning: unknown update mode '%s' suggested for submodule '%s'\n" ), + upd, sub->name); + upd ="none" ; + } +if (git_config_set (sb.buf, upd)) +die (_ ("Failed to register update mode for submodule path '%s'" ), displaypath); + } +strbuf_release (&sb); +} + +static int module_init (int argc,const char **argv,const char *prefix) +{ +int quiet =0 ; +int i; + +struct option module_init_options[] = { +OPT_STRING (0 ,"prefix" , &prefix, +N_ ("path" ), +N_ ("alternative anchor for relative paths" )), +OPT__QUIET (&quiet,"Suppress output for initialzing a submodule" ), +OPT_END () + }; + +const char *const git_submodule_helper_usage[] = { +N_ ("git submodule--helper init [<path>]" ), +NULL + }; + + argc =parse_options (argc, argv, prefix, module_init_options, + git_submodule_helper_usage,0 ); + +if (argc ==0 ) +die (_ ("Pass at least one submodule" )); + +for (i =0 ; i < argc; i++) +init_submodule (argv[i], prefix, quiet); + +return 0 ; +} +struct module_list {const struct cache_entry **entries;int alloc, nr;
@@ -466,11 +575,6 @@ static int module_clone(int argc, const char **argv, const char *prefix)return 0 ; } -static int git_submodule_config (const char *var,const char *value,void *cb) -{ -return parse_submodule_config_option (var, value); -} -struct submodule_update_clone {/* states */ int count;
@@ -716,6 +820,7 @@ static struct cmd_struct commands[] = { {"update-clone", update_clone}, {"resolve-relative-url", resolve_relative_url}, {"resolve-relative-url-test", resolve_relative_url_test}, + {"init", module_init} };int cmd_submodule__helper (int argc,const char **argv,const char *prefix)
git-submodule.sh +1 -38
@@ -398,45 +398,8 @@ cmd_init() while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(git submodule--helper name "$sm_path") || exit - - displaypath=$(relative_path "$sm_path") - - # Copy url setting when it is not set yet - if test -z "$(git config "submodule.$name.url")" - then - url=$(git config -f .gitmodules submodule."$name".url) - test -z "$url" && - die "$(eval_gettext "No url found for submodule path '\$displaypath' in .gitmodules")" - - # Possibly a url relative to parent - case "$url" in - ./*|../*) - url=$(git submodule--helper resolve-relative-url "$url") || exit - ;; - esac - git config submodule."$name".url "$url" || - die "$(eval_gettext "Failed to register url for submodule path '\$displaypath'")" - say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$displaypath'")" - fi - - # Copy "update" setting when it is not set yet - if upd="$(git config -f .gitmodules submodule."$name".update)" && - test -n "$upd" && - test -z "$(git config submodule."$name".update)" - then - case "$upd" in - checkout | rebase | merge | none) - ;; # known modes of updating - *) - echo >&2 "warning: unknown update mode '$upd' suggested for submodule '$name'" - upd=none - ;; - esac - git config submodule."$name".update "$upd" || - die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")" - fi + git submodule--helper init ${GIT_QUIET:+--quiet} "$sm_path" || exit done }
Signature · 1 line
2.7.0.rc0.41.g89994f2.dirty
Re: [PATCHv3 2/2] submodule: port init from shell to C
Quoted 6 lines · Stefan Beller
By having the
initfunctionality in C, we can reference it easier from other parts in the code.Signed-off-by Stefan Beller <[email protected]>Signed-off-by Junio C Hamano <[email protected]>---
How faithful a conversion is this aiming to be? For example, one thing I noticed is that some messages that were originally given with "say" and sent to the standard output, which is emitted to the standard error with this rewrite. I didn't read both patches carefully, so there may be other discrepancies I didn't spot.
I think you would want to do this in three steps:
- A faithful rewrite from shell to C;
- s/printf/fprintf(stderr, / for some messages; and finally
- Hiding of some messages under --quiet.
in the above order.
Thanks.
Quoted 194 lines
builtin/submodule--helper.c | 115 ++++++++++++++++++++++++++++++++++++++++++-- git-submodule.sh | 39 +-------------- 2 files changed, 111 insertions(+), 43 deletions(-)builtin/submodule--helper.c +110
-5@@ -221,6 +221,115 @@ static int resolve_relative_url_test(int argc, const char **argv, const char *prreturn 0 ; } +static int git_submodule_config (const char *var,const char *value,void *cb) +{ +return parse_submodule_config_option (var, value); +} + +static void init_submodule (const char *path,const char *prefix,int quiet) +{ +const struct submodule *sub; +struct strbuf sb =STRBUF_INIT ; +char *url =NULL ; +const char *upd =NULL ; +const char *displaypath =relative_path (xgetcwd (), prefix, &sb);; + +/* Only loads from .gitmodules, no overlay with .git/config */ +gitmodules_config (); + + sub =submodule_from_path (null_sha1, path); + +/* +* Copy url setting when it is not set yet. +* To look up the url in .git/config, we must not fall back to +* .gitmodules, so look it up directly. +*/ +strbuf_reset (&sb); +strbuf_addf (&sb,"submodule.%s.url" , sub->name); +if (git_config_get_string (sb.buf, &url)) { + url =xstrdup (sub->url); + +if (!url) +die (_ ("No url found for submodule path '%s' in .gitmodules" ), + displaypath); + +/* Possibly a url relative to parent */ +if (starts_with_dot_dot_slash (url) || +starts_with_dot_slash (url)) { +char *remoteurl; +char *remote =get_default_remote (); +struct strbuf remotesb =STRBUF_INIT ; +strbuf_addf (&remotesb,"remote.%s.url" , remote); +free (remote); + +if (git_config_get_string (remotesb.buf, &remoteurl)) +/* +* The repository is its own +* authoritative upstream +*/ + remoteurl =xgetcwd (); + url =relative_url (remoteurl, url,NULL ); +strbuf_release (&remotesb); + } + +if (git_config_set (sb.buf, url)) +die (_ ("Failed to register url for submodule path '%s'" ), + displaypath); +if (!quiet) +fprintf (stderr,_ ("Submodule '%s' (%s) registered for path '%s'\n" ), + sub->name, url, displaypath); +free (url); + } + +/* Copy "update" setting when it is not set yet */ +strbuf_reset (&sb); +strbuf_addf (&sb,"submodule.%s.update" , sub->name); +if (git_config_get_string_const (sb.buf, &upd) && sub->update) { + upd = sub->update; +if (strcmp (sub->update,"checkout" ) && +strcmp (sub->update,"rebase" ) && +strcmp (sub->update,"merge" ) && +strcmp (sub->update,"none" )) { +fprintf (stderr,_ ("warning: unknown update mode '%s' suggested for submodule '%s'\n" ), + upd, sub->name); + upd ="none" ; + } +if (git_config_set (sb.buf, upd)) +die (_ ("Failed to register update mode for submodule path '%s'" ), displaypath); + } +strbuf_release (&sb); +} + +static int module_init (int argc,const char **argv,const char *prefix) +{ +int quiet =0 ; +int i; + +struct option module_init_options[] = { +OPT_STRING (0 ,"prefix" , &prefix, +N_ ("path" ), +N_ ("alternative anchor for relative paths" )), +OPT__QUIET (&quiet,"Suppress output for initialzing a submodule" ), +OPT_END () + }; + +const char *const git_submodule_helper_usage[] = { +N_ ("git submodule--helper init [<path>]" ), +NULL + }; + + argc =parse_options (argc, argv, prefix, module_init_options, + git_submodule_helper_usage,0 ); + +if (argc ==0 ) +die (_ ("Pass at least one submodule" )); + +for (i =0 ; i < argc; i++) +init_submodule (argv[i], prefix, quiet); + +return 0 ; +} +struct module_list {const struct cache_entry **entries;int alloc, nr;@@ -466,11 +575,6 @@ static int module_clone(int argc, const char **argv, const char *prefix)return 0 ; } -static int git_submodule_config (const char *var,const char *value,void *cb) -{ -return parse_submodule_config_option (var, value); -} -struct submodule_update_clone {/* states */ int count;@@ -716,6 +820,7 @@ static struct cmd_struct commands[] = { {"update-clone", update_clone}, {"resolve-relative-url", resolve_relative_url}, {"resolve-relative-url-test", resolve_relative_url_test}, + {"init", module_init} };int cmd_submodule__helper (int argc,const char **argv,const char *prefix)git-submodule.sh +1
-38@@ -398,45 +398,8 @@ cmd_init() while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(git submodule--helper name "$sm_path") || exit - - displaypath=$(relative_path "$sm_path") - - # Copy url setting when it is not set yet - if test -z "$(git config "submodule.$name.url")" - then - url=$(git config -f .gitmodules submodule."$name".url) - test -z "$url" && - die "$(eval_gettext "No url found for submodule path '\$displaypath' in .gitmodules")" - - # Possibly a url relative to parent - case "$url" in - ./*|../*) - url=$(git submodule--helper resolve-relative-url "$url") || exit - ;; - esac - git config submodule."$name".url "$url" || - die "$(eval_gettext "Failed to register url for submodule path '\$displaypath'")" - say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$displaypath'")" - fi - - # Copy "update" setting when it is not set yet - if upd="$(git config -f .gitmodules submodule."$name".update)" && - test -n "$upd" && - test -z "$(git config submodule."$name".update)" - then - case "$upd" in - checkout | rebase | merge | none) - ;; # known modes of updating - *) - echo >&2 "warning: unknown update mode '$upd' suggested for submodule '$name'" - upd=none - ;; - esac - git config submodule."$name".update "$upd" || - die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")" - fi + git submodule--helper init ${GIT_QUIET:+--quiet} "$sm_path" || exit done }
Re: [PATCHv3 2/2] submodule: port init from shell to C
Quoted 17 lines · Junio C Hamano
Quoted 6 lines · Stefan Beller
By having the
initfunctionality in C, we can reference it easier from other parts in the code.Signed-off-by Stefan Beller <[email protected]>Signed-off-by Junio C Hamano <[email protected]>---
How faithful a conversion is this aiming to be? For example, one thing I noticed is that some messages that were originally given with "say" and sent to the standard output, which is emitted to the standard error with this rewrite. I didn't read both patches carefully, so there may be other discrepancies I didn't spot.
I think you would want to do this in three steps:
Quoted 7 lines
- A faithful rewrite from shell to C;
- s/printf/fprintf(stderr, / for some messages; and finally
- Hiding of some messages under --quiet.
in the above order.
"say" respects the setting of GIT_QUIET, which is usually set when --quiet is passed, so I think I want:
- A faithful rewrite from shell to C including messages respecting --quiet, such that the "say" behavior is kept.
- s/printf/fprintf(stderr, / for some messages
and then be done with it.
Thanks,
Stefan
Quoted 1 line
Re: [PATCHv3 2/2] submodule: port init from shell to C
Quoted 7 lines · Stefan Beller
"say" respects the setting of
GIT_QUIET, which is usually set when --quiet is passed, so I think I want:- A faithful rewrite from shell to C including messages respecting --quiet, such that the "say" behavior is kept.
- s/printf/fprintf(stderr, / for some messages
Yup, that is the correct order. Thanks.
Re: [PATCHv2 1/2] submodule: port resolve_relative_url from shell to C
In reply to Stefan BellerAm 20.01.2016 um 03:03 schrieb Stefan Beller:
Review
Quoted 9 lines
(quoted diff) +9
-0+static char *last_dir_separator(char *str) +{ + char *p = str + strlen(str); + while (p-- > str) + if (is_dir_sep(*p)) + return p; + return NULL; +} +
I just noticed that we already have this function. Please squash in the following.
builtin/submodule--helper.c +1 -10
@@ -51,21 +51,12 @@ static int starts_with_dot_dot_slash(const char *str)return str[0 ] =='.' && str[1 ] =='.' &&is_dir_sep (str[2 ]); } -static char *last_dir_separator (char *str) -{ -char *p = str +strlen (str); -while (p-- > str) -if (is_dir_sep (*p)) -return p; -return NULL ; -} -/* * Returns 1 if it was the last chop before ':'. */ static int chop_last_dir (char **remoteurl,int is_relative) { -char *rfind =last_dir_separator (*remoteurl); +char *rfind =find_last_dir_sep (*remoteurl);if (rfind) { *rfind ='\0' ;return 0 ;
Re: [PATCHv2 1/2] submodule: port resolve_relative_url from shell to C
Quoted 13 lines · Johannes Sixt
Am 20.01.2016 um 03:03 schrieb Stefan Beller:
Review
Quoted 9 lines
(quoted diff) +9
-0+static char *last_dir_separator(char *str) +{ + char *p = str + strlen(str); + while (p-- > str) + if (is_dir_sep(*p)) + return p; + return NULL; +} +I just noticed that we already have this function. Please squash in the following.
thanks for reviewing! squashed.
Quoted 29 lines
builtin/submodule--helper.c +1
-10@@ -51,21 +51,12 @@ static int starts_with_dot_dot_slash(const char *str)return str[0 ] =='.' && str[1 ] =='.' &&is_dir_sep (str[2 ]); } -static char *last_dir_separator (char *str) -{ -char *p = str +strlen (str); -while (p-- > str) -if (is_dir_sep (*p)) -return p; -return NULL ; -} -/* * Returns 1 if it was the last chop before ':'. */ static int chop_last_dir (char **remoteurl,int is_relative) { -char *rfind =last_dir_separator (*remoteurl); +char *rfind =find_last_dir_sep (*remoteurl);if (rfind) { *rfind ='\0' ;return 0 ;
PATCH v4 2/2 submodule: port init from shell to C
In reply to Stefan BellerBy having the init functionality in C, we can reference it easier from other parts in the code.
---
Hi Junio,
please replace the top-most patch of sb/submodule-init with this patch.
This will print to stdout instead of stderr, as it's the most faithful shell -> translation. The s/printf/fprintf(stderr, / patch will come with the series that needs the behavior, i.e. as one of the first patches in the groups series (that is under heavy mental construction currently).
Thanks,
Stefan
Interdiff to the top of sb/submodule-init (6a514f75c25, submodule: port init from shell to C, Committed: 2016-01-20 12:55:30):
# diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c # index 4684f16..6323048 100644 # --- a/builtin/submodule--helper.c # +++ b/builtin/submodule--helper.c # @@ -232,7 +232,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet) # struct strbuf sb = STRBUF_INIT; # char *url = NULL; # const char *upd = NULL; # - const char *displaypath = relative_path(xgetcwd(), prefix, &sb);; # + const char *displaypath = relative_path(xgetcwd(), prefix, &sb);
#
# /* Only loads from .gitmodules, no overlay with .git/config */
# gitmodules_config();
# @@ -276,7 +276,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
# die(_("Failed to register url for submodule path '%s'"),
# displaypath);
# if (!quiet)
# - fprintf(stderr, _("Submodule '%s' (%s) registered for path '%s'\n"),
# + printf(_("Submodule '%s' (%s) registered for path '%s'\n"),# sub->name, url, displaypath);
# free(url);
# }
builtin/submodule--helper.c | 115 ++++++++++++++++++++++++++++++++++++++++++-- git-submodule.sh | 39 +-------------- 2 files changed, 111 insertions(+), 43 deletions(-)
builtin/submodule--helper.c +110 -5
@@ -221,6 +221,115 @@ static int resolve_relative_url_test(int argc, const char **argv, const char *prreturn 0 ; } +static int git_submodule_config (const char *var,const char *value,void *cb) +{ +return parse_submodule_config_option (var, value); +} + +static void init_submodule (const char *path,const char *prefix,int quiet) +{ +const struct submodule *sub; +struct strbuf sb =STRBUF_INIT ; +char *url =NULL ; +const char *upd =NULL ; +const char *displaypath =relative_path (xgetcwd (), prefix, &sb); + +/* Only loads from .gitmodules, no overlay with .git/config */ +gitmodules_config (); + + sub =submodule_from_path (null_sha1, path); + +/* +* Copy url setting when it is not set yet. +* To look up the url in .git/config, we must not fall back to +* .gitmodules, so look it up directly. +*/ +strbuf_reset (&sb); +strbuf_addf (&sb,"submodule.%s.url" , sub->name); +if (git_config_get_string (sb.buf, &url)) { + url =xstrdup (sub->url); + +if (!url) +die (_ ("No url found for submodule path '%s' in .gitmodules" ), + displaypath); + +/* Possibly a url relative to parent */ +if (starts_with_dot_dot_slash (url) || +starts_with_dot_slash (url)) { +char *remoteurl; +char *remote =get_default_remote (); +struct strbuf remotesb =STRBUF_INIT ; +strbuf_addf (&remotesb,"remote.%s.url" , remote); +free (remote); + +if (git_config_get_string (remotesb.buf, &remoteurl)) +/* +* The repository is its own +* authoritative upstream +*/ + remoteurl =xgetcwd (); + url =relative_url (remoteurl, url,NULL ); +strbuf_release (&remotesb); + } + +if (git_config_set (sb.buf, url)) +die (_ ("Failed to register url for submodule path '%s'" ), + displaypath); +if (!quiet) +printf (_ ("Submodule '%s' (%s) registered for path '%s'\n" ), + sub->name, url, displaypath); +free (url); + } + +/* Copy "update" setting when it is not set yet */ +strbuf_reset (&sb); +strbuf_addf (&sb,"submodule.%s.update" , sub->name); +if (git_config_get_string_const (sb.buf, &upd) && sub->update) { + upd = sub->update; +if (strcmp (sub->update,"checkout" ) && +strcmp (sub->update,"rebase" ) && +strcmp (sub->update,"merge" ) && +strcmp (sub->update,"none" )) { +fprintf (stderr,_ ("warning: unknown update mode '%s' suggested for submodule '%s'\n" ), + upd, sub->name); + upd ="none" ; + } +if (git_config_set (sb.buf, upd)) +die (_ ("Failed to register update mode for submodule path '%s'" ), displaypath); + } +strbuf_release (&sb); +} + +static int module_init (int argc,const char **argv,const char *prefix) +{ +int quiet =0 ; +int i; + +struct option module_init_options[] = { +OPT_STRING (0 ,"prefix" , &prefix, +N_ ("path" ), +N_ ("alternative anchor for relative paths" )), +OPT__QUIET (&quiet,"Suppress output for initialzing a submodule" ), +OPT_END () + }; + +const char *const git_submodule_helper_usage[] = { +N_ ("git submodule--helper init [<path>]" ), +NULL + }; + + argc =parse_options (argc, argv, prefix, module_init_options, + git_submodule_helper_usage,0 ); + +if (argc ==0 ) +die (_ ("Pass at least one submodule" )); + +for (i =0 ; i < argc; i++) +init_submodule (argv[i], prefix, quiet); + +return 0 ; +} +struct module_list {const struct cache_entry **entries;int alloc, nr;
@@ -466,11 +575,6 @@ static int module_clone(int argc, const char **argv, const char *prefix)return 0 ; } -static int git_submodule_config (const char *var,const char *value,void *cb) -{ -return parse_submodule_config_option (var, value); -} -struct submodule_update_clone {/* states */ int count;
@@ -716,6 +820,7 @@ static struct cmd_struct commands[] = { {"update-clone", update_clone}, {"resolve-relative-url", resolve_relative_url}, {"resolve-relative-url-test", resolve_relative_url_test}, + {"init", module_init} };int cmd_submodule__helper (int argc,const char **argv,const char *prefix)
git-submodule.sh +1 -38
@@ -398,45 +398,8 @@ cmd_init() while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(git submodule--helper name "$sm_path") || exit - - displaypath=$(relative_path "$sm_path") - - # Copy url setting when it is not set yet - if test -z "$(git config "submodule.$name.url")" - then - url=$(git config -f .gitmodules submodule."$name".url) - test -z "$url" && - die "$(eval_gettext "No url found for submodule path '\$displaypath' in .gitmodules")" - - # Possibly a url relative to parent - case "$url" in - ./*|../*) - url=$(git submodule--helper resolve-relative-url "$url") || exit - ;; - esac - git config submodule."$name".url "$url" || - die "$(eval_gettext "Failed to register url for submodule path '\$displaypath'")" - say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$displaypath'")" - fi - - # Copy "update" setting when it is not set yet - if upd="$(git config -f .gitmodules submodule."$name".update)" && - test -n "$upd" && - test -z "$(git config submodule."$name".update)" - then - case "$upd" in - checkout | rebase | merge | none) - ;; # known modes of updating - *) - echo >&2 "warning: unknown update mode '$upd' suggested for submodule '$name'" - upd=none - ;; - esac - git config submodule."$name".update "$upd" || - die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")" - fi + git submodule--helper init ${GIT_QUIET:+--quiet} "$sm_path" || exit done }
Signature · 1 line
2.7.0.rc0.41.g89994f2.dirty
Re: [PATCHv4 2/2] submodule: port init from shell to C
Quoted 9 lines · Stefan Beller
By having the
initfunctionality in C, we can reference it easier from other parts in the code.Signed-off-by Stefan Beller <[email protected]>---
Hi Junio,
please replace the top-most patch of sb/submodule-init with this patch.
Will do, but it seems that you'd have to be replacing 1/2 as well, so perhaps send both when that happens?
v3 cover Port `git submodule init` from shell to C
This applies on top of sb/submodule-parallel-update, replacing sb/submodule-init.
Fixes:
* a more faithful conversion by staying on stdout (We switch to stderr later in another series)
* use the existing find_last_dir_sep instead of reinventing the wheel.
Stefan Beller (2):
submodule: port resolve_relative_url from shell to C
submodule: port init from shell to C
builtin/submodule--helper.c | 321 +++++++++++++++++++++++++++++++++++++++++++- git-submodule.sh | 118 +--------------- t/t0060-path-utils.sh | 42 ++++++ 3 files changed, 361 insertions(+), 120 deletions(-)
Interdiff to origin/sb/submodule-init (committed 2016-01-20 12:55:30)
builtin/submodule--helper.c +3 -12
@@ -51,21 +51,12 @@ static int starts_with_dot_dot_slash(const char *str)return str[0 ] =='.' && str[1 ] =='.' &&is_dir_sep (str[2 ]); } -static char *last_dir_separator (char *str) -{ -char *p = str +strlen (str); -while (p-- > str) -if (is_dir_sep (*p)) -return p; -return NULL ; -} -/* * Returns 1 if it was the last chop before ':'. */ static int chop_last_dir (char **remoteurl,int is_relative) { -char *rfind =last_dir_separator (*remoteurl); +char *rfind =find_last_dir_sep (*remoteurl);if (rfind) { *rfind ='\0' ;return 0 ;
@@ -232,7 +223,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)struct strbuf sb =STRBUF_INIT ;char *url =NULL ;const char *upd =NULL ; -const char *displaypath =relative_path (xgetcwd (), prefix, &sb);; +const char *displaypath =relative_path (xgetcwd (), prefix, &sb);/* Only loads from .gitmodules, no overlay with .git/config */ gitmodules_config ();
@@ -276,7 +267,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)die (_ ("Failed to register url for submodule path '%s'" ), displaypath);if (!quiet) -fprintf (stderr,_ ("Submodule '%s' (%s) registered for path '%s'\n" ), +printf (_ ("Submodule '%s' (%s) registered for path '%s'\n" ), sub->name, url, displaypath);free (url); }
Signature · 1 line
2.7.0.rc0.45.g0dba895.dirty
PATCH v3 1/2 submodule: port resolve_relative_url from shell to C
Later on we want to automatically call git submodule init from other commands, such that the users don't have to initialize the submodule themselves. As these other commands are written in C already, we'd need the init functionality in C, too. The resolve_relative_url function is a large part of that init functionality, so start by porting this function to C.
To create the tests in t0060, the function resolve_relative_url was temporarily enhanced to write all inputs and output to disk when running the test suite. The added tests in this patch are a small selection thereof.
--- builtin/submodule--helper.c | 206 +++++++++++++++++++++++++++++++++++++++++++- git-submodule.sh | 81 +---------------- t/t0060-path-utils.sh | 42 +++++++++ 3 files changed, 251 insertions(+), 78 deletions(-)
builtin/submodule--helper.c +205 -1
@@ -9,6 +9,208 @@#include "submodule-config.h" #include "string-list.h" #include "run-command.h" +#include "remote.h" +#include "refs.h" +#include "connect.h" + +static char *get_default_remote (void ) +{ +char *dest =NULL , *ret; +unsigned char sha1[20 ]; +int flag =0 ; +struct strbuf sb =STRBUF_INIT ; +const char *refname =resolve_ref_unsafe ("HEAD" ,0 , sha1, &flag); + +if (!refname) +die ("No such ref: HEAD" ); + +/* detached HEAD */ +if (!strcmp (refname,"HEAD" )) +return xstrdup ("origin" ); + +if (!skip_prefix (refname,"refs/heads/" , &refname)) +die (_ ("Expecting a full ref name, got %s" ), refname); + +strbuf_addf (&sb,"branch.%s.remote" , refname); +if (git_config_get_string (sb.buf, &dest)) + ret =xstrdup ("origin" ); +else + ret =xstrdup (dest); + +strbuf_release (&sb); +return ret; +} + +static int starts_with_dot_slash (const char *str) +{ +return str[0 ] =='.' &&is_dir_sep (str[1 ]); +} + +static int starts_with_dot_dot_slash (const char *str) +{ +return str[0 ] =='.' && str[1 ] =='.' &&is_dir_sep (str[2 ]); +} + +/* +* Returns 1 if it was the last chop before ':'. +*/ +static int chop_last_dir (char **remoteurl,int is_relative) +{ +char *rfind =find_last_dir_sep (*remoteurl); +if (rfind) { + *rfind ='\0' ; +return 0 ; + } + + rfind =strrchr (*remoteurl,':' ); +if (rfind) { + *rfind ='\0' ; +return 1 ; + } + +if (is_relative || !strcmp ("." , *remoteurl)) +die (_ ("cannot strip one component off url '%s'" ), + *remoteurl); + +free (*remoteurl); + *remoteurl =xstrdup ("." ); +return 0 ; +} + +/* +* The `url` argument is the URL that navigates to the submodule origin +* repo. When relative, this URL is relative to the superproject origin +* URL repo. The `up_path` argument, if specified, is the relative +* path that navigates from the submodule working tree to the superproject +* working tree. Returns the origin URL of the submodule. +* +* Return either an absolute URL or filesystem path (if the superproject +* origin URL is an absolute URL or filesystem path, respectively) or a +* relative file system path (if the superproject origin URL is a relative +* file system path). +* +* When the output is a relative file system path, the path is either +* relative to the submodule working tree, if up_path is specified, or to +* the superproject working tree otherwise. +* +* NEEDSWORK: This works incorrectly on the domain and protocol part. +* remote_url url outcome correct +* http://a.com/b ../c http://a.com/c yes +* http://a.com/b ../../c http://c no (domain should be kept) +* http://a.com/b ../../../c http:/c no +* http://a.com/b ../../../../c http:c no +* http://a.com/b ../../../../../c .:c no +*/ +static char *relative_url (const char *remote_url, +const char *url, +const char *up_path) +{ +int is_relative =0 ; +int colonsep =0 ; +char *out; +char *remoteurl =xstrdup (remote_url); +struct strbuf sb =STRBUF_INIT ; +size_t len =strlen (remoteurl); + +if (is_dir_sep (remoteurl[len])) + remoteurl[len] ='\0' ; + +if (!url_is_local_not_ssh (remoteurl) ||is_absolute_path (remoteurl)) + is_relative =0 ; +else { + is_relative =1 ; +/* +* Prepend a './' to ensure all relative +* remoteurls start with './' or '../' +*/ +if (!starts_with_dot_slash (remoteurl) && + !starts_with_dot_dot_slash (remoteurl)) { +strbuf_reset (&sb); +strbuf_addf (&sb,"./%s" , remoteurl); +free (remoteurl); + remoteurl =strbuf_detach (&sb,NULL ); + } + } +/* +* When the url starts with '../', remove that and the +* last directory in remoteurl. +*/ +while (url) { +if (starts_with_dot_dot_slash (url)) { + url +=3 ; + colonsep |=chop_last_dir (&remoteurl, is_relative); + }else if (starts_with_dot_slash (url)) + url +=2 ; +else +break ; + } +strbuf_reset (&sb); +strbuf_addf (&sb,"%s%s%s" , remoteurl, colonsep ?":" :"/" , url); + +if (starts_with_dot_slash (sb.buf)) + out =xstrdup (sb.buf +2 ); +else + out =xstrdup (sb.buf); +strbuf_reset (&sb); + +free (remoteurl); +if (!up_path || !is_relative) +return out; + +strbuf_addf (&sb,"%s%s" , up_path, out); +free (out); +return strbuf_detach (&sb,NULL ); +} + +static int resolve_relative_url (int argc,const char **argv,const char *prefix) +{ +char *remoteurl =NULL ; +char *remote =get_default_remote (); +const char *up_path =NULL ; +char *res; +const char *url; +struct strbuf sb =STRBUF_INIT ; + +if (argc !=2 && argc !=3 ) +die ("resolve-relative-url only accepts one or two arguments" ); + + url = argv[1 ]; +strbuf_addf (&sb,"remote.%s.url" , remote); +free (remote); + +if (git_config_get_string (sb.buf, &remoteurl)) +/* the repository is its own authoritative upstream */ + remoteurl =xgetcwd (); + +if (argc ==3 ) + up_path = argv[2 ]; + + res =relative_url (remoteurl, url, up_path); +puts (res); +free (res); +return 0 ; +} + +static int resolve_relative_url_test (int argc,const char **argv,const char *prefix) +{ +char *remoteurl, *res; +const char *up_path, *url; + +if (argc !=4 ) +die ("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>" ); + + up_path = argv[1 ]; + remoteurl =xstrdup (argv[2 ]); + url = argv[3 ]; + +if (!strcmp (up_path,"(null)" )) + up_path =NULL ; + + res =relative_url (remoteurl, url, up_path); +puts (res); +free (res); +return 0 ; +}struct module_list {const struct cache_entry **entries;
@@ -502,7 +704,9 @@ static struct cmd_struct commands[] = { {"list", module_list}, {"name", module_name}, {"clone", module_clone}, - {"update-clone", update_clone} + {"update-clone", update_clone}, + {"resolve-relative-url", resolve_relative_url}, + {"resolve-relative-url-test", resolve_relative_url_test}, };int cmd_submodule__helper (int argc,const char **argv,const char *prefix)
git-submodule.sh +4 -77
106 lines not on this page · raw message
t/t0060-path-utils.sh +42 -0
52 lines not on this page · raw message
Signature · 1 line
2.7.0.rc0.45.g0dba895.dirty
PATCH v3 2/2 submodule: port init from shell to C
In reply to Stefan BellerBy having the init functionality in C, we can reference it easier from other parts in the code.
--- builtin/submodule--helper.c | 115 ++++++++++++++++++++++++++++++++++++++++++-- git-submodule.sh | 39 +-------------- 2 files changed, 111 insertions(+), 43 deletions(-)
builtin/submodule--helper.c +110 -5
@@ -212,6 +212,115 @@ static int resolve_relative_url_test(int argc, const char **argv, const char *prreturn 0 ; } +static int git_submodule_config (const char *var,const char *value,void *cb) +{ +return parse_submodule_config_option (var, value); +} + +static void init_submodule (const char *path,const char *prefix,int quiet) +{ +const struct submodule *sub; +struct strbuf sb =STRBUF_INIT ; +char *url =NULL ; +const char *upd =NULL ; +const char *displaypath =relative_path (xgetcwd (), prefix, &sb); + +/* Only loads from .gitmodules, no overlay with .git/config */ +gitmodules_config (); + + sub =submodule_from_path (null_sha1, path); + +/* +* Copy url setting when it is not set yet. +* To look up the url in .git/config, we must not fall back to +* .gitmodules, so look it up directly. +*/ +strbuf_reset (&sb); +strbuf_addf (&sb,"submodule.%s.url" , sub->name); +if (git_config_get_string (sb.buf, &url)) { + url =xstrdup (sub->url); + +if (!url) +die (_ ("No url found for submodule path '%s' in .gitmodules" ), + displaypath); + +/* Possibly a url relative to parent */ +if (starts_with_dot_dot_slash (url) || +starts_with_dot_slash (url)) { +char *remoteurl; +char *remote =get_default_remote (); +struct strbuf remotesb =STRBUF_INIT ; +strbuf_addf (&remotesb,"remote.%s.url" , remote); +free (remote); + +if (git_config_get_string (remotesb.buf, &remoteurl)) +/* +* The repository is its own +* authoritative upstream +*/ + remoteurl =xgetcwd (); + url =relative_url (remoteurl, url,NULL ); +strbuf_release (&remotesb); + } + +if (git_config_set (sb.buf, url)) +die (_ ("Failed to register url for submodule path '%s'" ), + displaypath); +if (!quiet) +printf (_ ("Submodule '%s' (%s) registered for path '%s'\n" ), + sub->name, url, displaypath); +free (url); + } + +/* Copy "update" setting when it is not set yet */ +strbuf_reset (&sb); +strbuf_addf (&sb,"submodule.%s.update" , sub->name); +if (git_config_get_string_const (sb.buf, &upd) && sub->update) { + upd = sub->update; +if (strcmp (sub->update,"checkout" ) && +strcmp (sub->update,"rebase" ) && +strcmp (sub->update,"merge" ) && +strcmp (sub->update,"none" )) { +fprintf (stderr,_ ("warning: unknown update mode '%s' suggested for submodule '%s'\n" ), + upd, sub->name); + upd ="none" ; + } +if (git_config_set (sb.buf, upd)) +die (_ ("Failed to register update mode for submodule path '%s'" ), displaypath); + } +strbuf_release (&sb); +} + +static int module_init (int argc,const char **argv,const char *prefix) +{ +int quiet =0 ; +int i; + +struct option module_init_options[] = { +OPT_STRING (0 ,"prefix" , &prefix, +N_ ("path" ), +N_ ("alternative anchor for relative paths" )), +OPT__QUIET (&quiet,"Suppress output for initialzing a submodule" ), +OPT_END () + }; + +const char *const git_submodule_helper_usage[] = { +N_ ("git submodule--helper init [<path>]" ), +NULL + }; + + argc =parse_options (argc, argv, prefix, module_init_options, + git_submodule_helper_usage,0 ); + +if (argc ==0 ) +die (_ ("Pass at least one submodule" )); + +for (i =0 ; i < argc; i++) +init_submodule (argv[i], prefix, quiet); + +return 0 ; +} +struct module_list {const struct cache_entry **entries;int alloc, nr;
@@ -457,11 +566,6 @@ static int module_clone(int argc, const char **argv, const char *prefix)return 0 ; } -static int git_submodule_config (const char *var,const char *value,void *cb) -{ -return parse_submodule_config_option (var, value); -} -struct submodule_update_clone {/* states */ int count;
@@ -707,6 +811,7 @@ static struct cmd_struct commands[] = { {"update-clone", update_clone}, {"resolve-relative-url", resolve_relative_url}, {"resolve-relative-url-test", resolve_relative_url_test}, + {"init", module_init} };int cmd_submodule__helper (int argc,const char **argv,const char *prefix)
git-submodule.sh +1 -38
@@ -398,45 +398,8 @@ cmd_init() while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(git submodule--helper name "$sm_path") || exit - - displaypath=$(relative_path "$sm_path") - - # Copy url setting when it is not set yet - if test -z "$(git config "submodule.$name.url")" - then - url=$(git config -f .gitmodules submodule."$name".url) - test -z "$url" && - die "$(eval_gettext "No url found for submodule path '\$displaypath' in .gitmodules")" - - # Possibly a url relative to parent - case "$url" in - ./*|../*) - url=$(git submodule--helper resolve-relative-url "$url") || exit - ;; - esac - git config submodule."$name".url "$url" || - die "$(eval_gettext "Failed to register url for submodule path '\$displaypath'")" - say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$displaypath'")" - fi - - # Copy "update" setting when it is not set yet - if upd="$(git config -f .gitmodules submodule."$name".update)" && - test -n "$upd" && - test -z "$(git config submodule."$name".update)" - then - case "$upd" in - checkout | rebase | merge | none) - ;; # known modes of updating - *) - echo >&2 "warning: unknown update mode '$upd' suggested for submodule '$name'" - upd=none - ;; - esac - git config submodule."$name".update "$upd" || - die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")" - fi + git submodule--helper init ${GIT_QUIET:+--quiet} "$sm_path" || exit done }
Signature · 1 line
2.7.0.rc0.45.g0dba895.dirty
Re: [PATCHv4 2/2] submodule: port init from shell to C
In reply to Junio C HamanoQuoted 8 lines · Junio C Hamano
Quoted 6 lines · Stefan Beller
By having the
initfunctionality in C, we can reference it easier from other parts in the code.Signed-off-by Stefan Beller <[email protected]>---
Quoted 6 lines
Quoted 3 lines
Hi Junio,
please replace the top-most patch of sb/submodule-init with this patch.
Will do, but it seems that you'd have to be replacing 1/2 as well, so perhaps send both when that happens?
I'll resend the series later tonight indeed, so no need to pickup that patch.
Quoted 1 line
Quoted 5 lines · Stefan Beller
This applies on top of sb/submodule-parallel-update, replacing sb/submodule-init.
Fixes:
Quoted 7 lines
* a more faithful conversion by staying on stdout (We switch to stderr later in another series)
* use the existingfind_last_dir_sepinstead of reinventing the wheel.Stefan Beller (2):
submodule: portresolve_relative_urlfrom shell to C
submodule: port init from shell to C
Thanks, will replace.
v4 cover Port `git submodule init` from shell to C
Hi,
I looked at this patch series again and fixed all memory leaks as found by coverity scan.
This applies on top of sb/submodule-parallel-update
Thanks,
Stefan
Interdiff to v3:
builtin/submodule--helper.c +13 -7
@@ -35,7 +35,7 @@ static char *get_default_remote(void)if (git_config_get_string (sb.buf, &dest)) ret =xstrdup ("origin" );else - ret =xstrdup (dest); + ret = dest;strbuf_release (&sb);return ret;
@@ -188,6 +188,7 @@ static int resolve_relative_url(int argc, const char **argv, const char *prefix) res =relative_url (remoteurl, url, up_path);puts (res);free (res); +free (remoteurl);return 0 ; }
@@ -209,6 +210,7 @@ static int resolve_relative_url_test(int argc, const char **argv, const char *pr res =relative_url (remoteurl, url, up_path);puts (res);free (res); +free (remoteurl);return 0 ; }
@@ -222,8 +224,9 @@ static void init_submodule(const char *path, const char *prefix, int quiet)const struct submodule *sub;struct strbuf sb =STRBUF_INIT ;char *url =NULL ; -const char *upd =NULL ; -const char *displaypath =relative_path (xgetcwd (), prefix, &sb); +char *upd =NULL ; +char *cwd =xgetcwd (); +const char *displaypath =relative_path (cwd, prefix, &sb);/* Only loads from .gitmodules, no overlay with .git/config */ gitmodules_config ();
@@ -261,6 +264,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet) remoteurl =xgetcwd (); url =relative_url (remoteurl, url,NULL );strbuf_release (&remotesb); +free (remoteurl); }if (git_config_set (sb.buf, url))
@@ -269,26 +273,27 @@ static void init_submodule(const char *path, const char *prefix, int quiet)if (!quiet)printf (_ ("Submodule '%s' (%s) registered for path '%s'\n" ), sub->name, url, displaypath); -free (url); }/* Copy "update" setting when it is not set yet */ strbuf_reset (&sb);strbuf_addf (&sb,"submodule.%s.update" , sub->name); -if (git_config_get_string_const (sb.buf, &upd) && sub->update) { - upd = sub->update; +if (git_config_get_string (sb.buf, &upd) && sub->update) { + upd =xstrdup (sub->update);if (strcmp (sub->update,"checkout" ) &&strcmp (sub->update,"rebase" ) &&strcmp (sub->update,"merge" ) &&strcmp (sub->update,"none" )) {fprintf (stderr,_ ("warning: unknown update mode '%s' suggested for submodule '%s'\n" ), upd, sub->name); - upd ="none" ; + upd =xstrdup ("none" ); }if (git_config_set (sb.buf, upd))die (_ ("Failed to register update mode for submodule path '%s'" ), displaypath); }strbuf_release (&sb); +free (cwd); +free (upd); +free (url); }
static int module_init(int argc, const char **argv, const char *prefix)
Stefan Beller (2):
submodule: port resolve_relative_url from shell to C
submodule: port init from shell to C
builtin/submodule--helper.c | 326 +++++++++++++++++++++++++++++++++++++++++++- git-submodule.sh | 118 +--------------- t/t0060-path-utils.sh | 42 ++++++ 3 files changed, 366 insertions(+), 120 deletions(-)
Signature · 1 line
2.7.0.rc0.42.ge5f5e2d
PATCH v4 1/2 submodule: port resolve_relative_url from shell to C
Later on we want to automatically call git submodule init from other commands, such that the users don't have to initialize the submodule themselves. As these other commands are written in C already, we'd need the init functionality in C, too. The resolve_relative_url function is a large part of that init functionality, so start by porting this function to C.
To create the tests in t0060, the function resolve_relative_url was temporarily enhanced to write all inputs and output to disk when running the test suite. The added tests in this patch are a small selection thereof.
--- builtin/submodule--helper.c | 208 +++++++++++++++++++++++++++++++++++++++++++- git-submodule.sh | 81 +---------------- t/t0060-path-utils.sh | 42 +++++++++ 3 files changed, 253 insertions(+), 78 deletions(-)
builtin/submodule--helper.c +207 -1
@@ -9,6 +9,210 @@#include "submodule-config.h" #include "string-list.h" #include "run-command.h" +#include "remote.h" +#include "refs.h" +#include "connect.h" + +static char *get_default_remote (void ) +{ +char *dest =NULL , *ret; +unsigned char sha1[20 ]; +int flag =0 ; +struct strbuf sb =STRBUF_INIT ; +const char *refname =resolve_ref_unsafe ("HEAD" ,0 , sha1, &flag); + +if (!refname) +die ("No such ref: HEAD" ); + +/* detached HEAD */ +if (!strcmp (refname,"HEAD" )) +return xstrdup ("origin" ); + +if (!skip_prefix (refname,"refs/heads/" , &refname)) +die (_ ("Expecting a full ref name, got %s" ), refname); + +strbuf_addf (&sb,"branch.%s.remote" , refname); +if (git_config_get_string (sb.buf, &dest)) + ret =xstrdup ("origin" ); +else + ret = dest; + +strbuf_release (&sb); +return ret; +} + +static int starts_with_dot_slash (const char *str) +{ +return str[0 ] =='.' &&is_dir_sep (str[1 ]); +} + +static int starts_with_dot_dot_slash (const char *str) +{ +return str[0 ] =='.' && str[1 ] =='.' &&is_dir_sep (str[2 ]); +} + +/* +* Returns 1 if it was the last chop before ':'. +*/ +static int chop_last_dir (char **remoteurl,int is_relative) +{ +char *rfind =find_last_dir_sep (*remoteurl); +if (rfind) { + *rfind ='\0' ; +return 0 ; + } + + rfind =strrchr (*remoteurl,':' ); +if (rfind) { + *rfind ='\0' ; +return 1 ; + } + +if (is_relative || !strcmp ("." , *remoteurl)) +die (_ ("cannot strip one component off url '%s'" ), + *remoteurl); + +free (*remoteurl); + *remoteurl =xstrdup ("." ); +return 0 ; +} + +/* +* The `url` argument is the URL that navigates to the submodule origin +* repo. When relative, this URL is relative to the superproject origin +* URL repo. The `up_path` argument, if specified, is the relative +* path that navigates from the submodule working tree to the superproject +* working tree. Returns the origin URL of the submodule. +* +* Return either an absolute URL or filesystem path (if the superproject +* origin URL is an absolute URL or filesystem path, respectively) or a +* relative file system path (if the superproject origin URL is a relative +* file system path). +* +* When the output is a relative file system path, the path is either +* relative to the submodule working tree, if up_path is specified, or to +* the superproject working tree otherwise. +* +* NEEDSWORK: This works incorrectly on the domain and protocol part. +* remote_url url outcome correct +* http://a.com/b ../c http://a.com/c yes +* http://a.com/b ../../c http://c no (domain should be kept) +* http://a.com/b ../../../c http:/c no +* http://a.com/b ../../../../c http:c no +* http://a.com/b ../../../../../c .:c no +*/ +static char *relative_url (const char *remote_url, +const char *url, +const char *up_path) +{ +int is_relative =0 ; +int colonsep =0 ; +char *out; +char *remoteurl =xstrdup (remote_url); +struct strbuf sb =STRBUF_INIT ; +size_t len =strlen (remoteurl); + +if (is_dir_sep (remoteurl[len])) + remoteurl[len] ='\0' ; + +if (!url_is_local_not_ssh (remoteurl) ||is_absolute_path (remoteurl)) + is_relative =0 ; +else { + is_relative =1 ; +/* +* Prepend a './' to ensure all relative +* remoteurls start with './' or '../' +*/ +if (!starts_with_dot_slash (remoteurl) && + !starts_with_dot_dot_slash (remoteurl)) { +strbuf_reset (&sb); +strbuf_addf (&sb,"./%s" , remoteurl); +free (remoteurl); + remoteurl =strbuf_detach (&sb,NULL ); + } + } +/* +* When the url starts with '../', remove that and the +* last directory in remoteurl. +*/ +while (url) { +if (starts_with_dot_dot_slash (url)) { + url +=3 ; + colonsep |=chop_last_dir (&remoteurl, is_relative); + }else if (starts_with_dot_slash (url)) + url +=2 ; +else +break ; + } +strbuf_reset (&sb); +strbuf_addf (&sb,"%s%s%s" , remoteurl, colonsep ?":" :"/" , url); + +if (starts_with_dot_slash (sb.buf)) + out =xstrdup (sb.buf +2 ); +else + out =xstrdup (sb.buf); +strbuf_reset (&sb); + +free (remoteurl); +if (!up_path || !is_relative) +return out; + +strbuf_addf (&sb,"%s%s" , up_path, out); +free (out); +return strbuf_detach (&sb,NULL ); +} + +static int resolve_relative_url (int argc,const char **argv,const char *prefix) +{ +char *remoteurl =NULL ; +char *remote =get_default_remote (); +const char *up_path =NULL ; +char *res; +const char *url; +struct strbuf sb =STRBUF_INIT ; + +if (argc !=2 && argc !=3 ) +die ("resolve-relative-url only accepts one or two arguments" ); + + url = argv[1 ]; +strbuf_addf (&sb,"remote.%s.url" , remote); +free (remote); + +if (git_config_get_string (sb.buf, &remoteurl)) +/* the repository is its own authoritative upstream */ + remoteurl =xgetcwd (); + +if (argc ==3 ) + up_path = argv[2 ]; + + res =relative_url (remoteurl, url, up_path); +puts (res); +free (res); +free (remoteurl); +return 0 ; +} + +static int resolve_relative_url_test (int argc,const char **argv,const char *prefix) +{ +char *remoteurl, *res; +const char *up_path, *url; + +if (argc !=4 ) +die ("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>" ); + + up_path = argv[1 ]; + remoteurl =xstrdup (argv[2 ]); + url = argv[3 ]; + +if (!strcmp (up_path,"(null)" )) + up_path =NULL ; + + res =relative_url (remoteurl, url, up_path); +puts (res); +free (res); +free (remoteurl); +return 0 ; +}struct module_list {const struct cache_entry **entries;
@@ -502,7 +706,9 @@ static struct cmd_struct commands[] = { {"list", module_list}, {"name", module_name}, {"clone", module_clone}, - {"update-clone", update_clone} + {"update-clone", update_clone}, + {"resolve-relative-url", resolve_relative_url}, + {"resolve-relative-url-test", resolve_relative_url_test}, };int cmd_submodule__helper (int argc,const char **argv,const char *prefix)
git-submodule.sh +4 -77
106 lines not on this page · raw message
t/t0060-path-utils.sh +42 -0
52 lines not on this page · raw message
Signature · 1 line
2.7.0.rc0.42.ge5f5e2d
PATCH v4 2/2 submodule: port init from shell to C
In reply to Stefan BellerBy having the init functionality in C, we can reference it easier from other parts in the code.
--- builtin/submodule--helper.c | 119 ++++++++++++++++++++++++++++++++++++++++++-- git-submodule.sh | 39 +-------------- 2 files changed, 115 insertions(+), 43 deletions(-)
builtin/submodule--helper.c +114 -5
@@ -214,6 +214,119 @@ static int resolve_relative_url_test(int argc, const char **argv, const char *prreturn 0 ; } +static int git_submodule_config (const char *var,const char *value,void *cb) +{ +return parse_submodule_config_option (var, value); +} + +static void init_submodule (const char *path,const char *prefix,int quiet) +{ +const struct submodule *sub; +struct strbuf sb =STRBUF_INIT ; +char *url =NULL ; +char *upd =NULL ; +char *cwd =xgetcwd (); +const char *displaypath =relative_path (cwd, prefix, &sb); + +/* Only loads from .gitmodules, no overlay with .git/config */ +gitmodules_config (); + + sub =submodule_from_path (null_sha1, path); + +/* +* Copy url setting when it is not set yet. +* To look up the url in .git/config, we must not fall back to +* .gitmodules, so look it up directly. +*/ +strbuf_reset (&sb); +strbuf_addf (&sb,"submodule.%s.url" , sub->name); +if (git_config_get_string (sb.buf, &url)) { + url =xstrdup (sub->url); + +if (!url) +die (_ ("No url found for submodule path '%s' in .gitmodules" ), + displaypath); + +/* Possibly a url relative to parent */ +if (starts_with_dot_dot_slash (url) || +starts_with_dot_slash (url)) { +char *remoteurl; +char *remote =get_default_remote (); +struct strbuf remotesb =STRBUF_INIT ; +strbuf_addf (&remotesb,"remote.%s.url" , remote); +free (remote); + +if (git_config_get_string (remotesb.buf, &remoteurl)) +/* +* The repository is its own +* authoritative upstream +*/ + remoteurl =xgetcwd (); + url =relative_url (remoteurl, url,NULL ); +strbuf_release (&remotesb); +free (remoteurl); + } + +if (git_config_set (sb.buf, url)) +die (_ ("Failed to register url for submodule path '%s'" ), + displaypath); +if (!quiet) +printf (_ ("Submodule '%s' (%s) registered for path '%s'\n" ), + sub->name, url, displaypath); + } + +/* Copy "update" setting when it is not set yet */ +strbuf_reset (&sb); +strbuf_addf (&sb,"submodule.%s.update" , sub->name); +if (git_config_get_string (sb.buf, &upd) && sub->update) { + upd =xstrdup (sub->update); +if (strcmp (sub->update,"checkout" ) && +strcmp (sub->update,"rebase" ) && +strcmp (sub->update,"merge" ) && +strcmp (sub->update,"none" )) { +fprintf (stderr,_ ("warning: unknown update mode '%s' suggested for submodule '%s'\n" ), + upd, sub->name); + upd =xstrdup ("none" ); + } +if (git_config_set (sb.buf, upd)) +die (_ ("Failed to register update mode for submodule path '%s'" ), displaypath); + } +strbuf_release (&sb); +free (cwd); +free (upd); +free (url); +} + +static int module_init (int argc,const char **argv,const char *prefix) +{ +int quiet =0 ; +int i; + +struct option module_init_options[] = { +OPT_STRING (0 ,"prefix" , &prefix, +N_ ("path" ), +N_ ("alternative anchor for relative paths" )), +OPT__QUIET (&quiet,"Suppress output for initialzing a submodule" ), +OPT_END () + }; + +const char *const git_submodule_helper_usage[] = { +N_ ("git submodule--helper init [<path>]" ), +NULL + }; + + argc =parse_options (argc, argv, prefix, module_init_options, + git_submodule_helper_usage,0 ); + +if (argc ==0 ) +die (_ ("Pass at least one submodule" )); + +for (i =0 ; i < argc; i++) +init_submodule (argv[i], prefix, quiet); + +return 0 ; +} +struct module_list {const struct cache_entry **entries;int alloc, nr;
@@ -459,11 +572,6 @@ static int module_clone(int argc, const char **argv, const char *prefix)return 0 ; } -static int git_submodule_config (const char *var,const char *value,void *cb) -{ -return parse_submodule_config_option (var, value); -} -struct submodule_update_clone {/* states */ int count;
@@ -709,6 +817,7 @@ static struct cmd_struct commands[] = { {"update-clone", update_clone}, {"resolve-relative-url", resolve_relative_url}, {"resolve-relative-url-test", resolve_relative_url_test}, + {"init", module_init} };int cmd_submodule__helper (int argc,const char **argv,const char *prefix)
git-submodule.sh +1 -38
@@ -398,45 +398,8 @@ cmd_init() while read mode sha1 stage sm_path do die_if_unmatched "$mode" - name=$(git submodule--helper name "$sm_path") || exit - - displaypath=$(relative_path "$sm_path") - - # Copy url setting when it is not set yet - if test -z "$(git config "submodule.$name.url")" - then - url=$(git config -f .gitmodules submodule."$name".url) - test -z "$url" && - die "$(eval_gettext "No url found for submodule path '\$displaypath' in .gitmodules")" - - # Possibly a url relative to parent - case "$url" in - ./*|../*) - url=$(git submodule--helper resolve-relative-url "$url") || exit - ;; - esac - git config submodule."$name".url "$url" || - die "$(eval_gettext "Failed to register url for submodule path '\$displaypath'")" - say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$displaypath'")" - fi - - # Copy "update" setting when it is not set yet - if upd="$(git config -f .gitmodules submodule."$name".update)" && - test -n "$upd" && - test -z "$(git config submodule."$name".update)" - then - case "$upd" in - checkout | rebase | merge | none) - ;; # known modes of updating - *) - echo >&2 "warning: unknown update mode '$upd' suggested for submodule '$name'" - upd=none - ;; - esac - git config submodule."$name".update "$upd" || - die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")" - fi + git submodule--helper init ${GIT_QUIET:+--quiet} "$sm_path" || exit done }
Signature · 1 line
2.7.0.rc0.42.ge5f5e2d
Re: [PATCHv4 1/2] submodule: port resolve_relative_url from shell to C
In reply to Stefan BellerQuoted 43 lines · Stefan Beller
builtin/submodule--helper.c +43
-0+static char *relative_url (const char *remote_url, +const char *url, +const char *up_path) +{ +int is_relative =0 ; +int colonsep =0 ; +char *out; +char *remoteurl =xstrdup (remote_url); +struct strbuf sb =STRBUF_INIT ; +size_t len =strlen (remoteurl); + +if (is_dir_sep (remoteurl[len])) + remoteurl[len] ='\0' ; + +if (!url_is_local_not_ssh (remoteurl) ||is_absolute_path (remoteurl)) + is_relative =0 ; +else { + is_relative =1 ; +/* +* Prepend a './' to ensure all relative +* remoteurls start with './' or '../' +*/ +if (!starts_with_dot_slash (remoteurl) && + !starts_with_dot_dot_slash (remoteurl)) { +strbuf_reset (&sb); +strbuf_addf (&sb,"./%s" , remoteurl); +free (remoteurl); + remoteurl =strbuf_detach (&sb,NULL ); + } + } +/* +* When the url starts with '../', remove that and the +* last directory in remoteurl. +*/ +while (url) { +if (starts_with_dot_dot_slash (url)) { + url +=3 ; + colonsep |=chop_last_dir (&remoteurl, is_relative); + }else if (starts_with_dot_slash (url)) + url +=2 ; +else +break ; + }
builtin/submodule--helper.c line 156
Quoted 10 lines
builtin/submodule--helper.c +10
-0+strbuf_reset (&sb); +strbuf_addf (&sb,"%s%s%s" , remoteurl, colonsep ?":" :"/" , url); + +if (starts_with_dot_slash (sb.buf)) + out =xstrdup (sb.buf +2 ); +else + out =xstrdup (sb.buf); +strbuf_reset (&sb); + +free (remoteurl);
This is a rather strange place to put this free(), as you are done with it a bit earlier, but it's OK. I briefly wondered if the code becomes easier to follow with fewer free(remoteurl) if this local variable is made into a strbuf, but I didn't seriously think it through.
Otherwise looking good.
Thanks.
Re: [PATCHv4 1/2] submodule: port resolve_relative_url from shell to C
Quoted 53 lines · Junio C Hamano
Quoted 51 lines · Stefan Beller
builtin/submodule--helper.c +51
-0+static char *relative_url (const char *remote_url, +const char *url, +const char *up_path) +{ +int is_relative =0 ; +int colonsep =0 ; +char *out; +char *remoteurl =xstrdup (remote_url); +struct strbuf sb =STRBUF_INIT ; +size_t len =strlen (remoteurl); + +if (is_dir_sep (remoteurl[len])) + remoteurl[len] ='\0' ; + +if (!url_is_local_not_ssh (remoteurl) ||is_absolute_path (remoteurl)) + is_relative =0 ; +else { + is_relative =1 ; +/* +* Prepend a './' to ensure all relative +* remoteurls start with './' or '../' +*/ +if (!starts_with_dot_slash (remoteurl) && + !starts_with_dot_dot_slash (remoteurl)) { +strbuf_reset (&sb); +strbuf_addf (&sb,"./%s" , remoteurl); +free (remoteurl); + remoteurl =strbuf_detach (&sb,NULL ); + } + } +/* +* When the url starts with '../', remove that and the +* last directory in remoteurl. +*/ +while (url) { +if (starts_with_dot_dot_slash (url)) { + url +=3 ; + colonsep |=chop_last_dir (&remoteurl, is_relative); + }else if (starts_with_dot_slash (url)) + url +=2 ; +else +break ; + } +strbuf_reset (&sb); +strbuf_addf (&sb,"%s%s%s" , remoteurl, colonsep ?":" :"/" , url); + +if (starts_with_dot_slash (sb.buf)) + out =xstrdup (sb.buf +2 ); +else + out =xstrdup (sb.buf); +strbuf_reset (&sb);
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + free(remoteurl);This is a rather strange place to put this
free(), as you are done with it a bit earlier, but it's OK. I briefly wondered if the code becomes easier to follow with fewer free(remoteurl) if this local variable is made into a strbuf, but I didn't seriously think it through.
Right. As I did not touch that particular free with the resend, I wondered how it came there, too. And I think I had it at the end of the function and then realized the return just after the current position would leak it, so I moved it minimally up. If I'll resend again, I'll move it up to where it was last used.
Quoted 4 lines
Otherwise looking good.
Thanks.
Thanks,
Stefan
Re: [PATCHv4 2/2] submodule: port init from shell to C
In reply to Stefan BellerQuoted 4 lines · Stefan Beller
builtin/submodule--helper.c +0
-0
builtin/submodule--helper.c static int resolve_relative_url_test(int argc, const char **argv, const char *pr · line 309
Quoted 10 lines
builtin/submodule--helper.c +10
-0+static int module_init (int argc,const char **argv,const char *prefix) +{ +int quiet =0 ; +int i; + +struct option module_init_options[] = { +OPT_STRING (0 ,"prefix" , &prefix, +N_ ("path" ), +N_ ("alternative anchor for relative paths" )), +OPT__QUIET (&quiet,"Suppress output for initialzing a submodule" ),
I think we need N_() around this "Suppress..." string. By the way, typo in "initializing"
Signature · 1 line
Duy