Skip to contentSkip to conversation

submodule: port subcommand 'set-url' from shell to C

PATCH Superseded Shourya Shukla 5 participants 22 messages May 6, 2020 – May 8, 2020 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 submodule: port subcommand 'set-url' from shell to C

Convert submodule subcommand 'set-url' to a builtin. Port 'set-url'to 'submodule--helper.c' and call the latter via 'git-submodule.sh'.

---
Thank you Junio for the review! :)
BTW, how detailed should the commit message be about the patch?

 builtin/submodule--helper.c | 39 +++++++++++++++++++++++++++++++++++++
 git-submodule.sh            | 22 +--------------------
 2 files changed, 40 insertions(+), 21 deletions(-)
builtin/submodule--helper.c +39 -0
index 1a4b391c88..f50745a03f 100644
@@ -2246,6 +2246,44 @@ static int module_config(int argc, const char **argv, const char *prefix)
 	usage_with_options(git_submodule_helper_usage, module_config_options);
 }
 
+static int module_set_url(int argc, const char **argv, const char *prefix)
+{
+	int quiet = 0;
+	const char *newurl;
+	const char *path;
+	struct strbuf config_name = STRBUF_INIT;
+
+	struct option set_url_options[] = {
+		OPT__QUIET(&quiet, N_("Suppress output for setting url of a submodule")),
+		OPT_END()
+	};
+
+	const char *const usage[] = {
+		N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
+		NULL
+	};
+
+	argc = parse_options(argc, argv, prefix, set_url_options,
+			     usage, 0);
+
+	if (argc!=2) {
+		usage_with_options(usage, set_url_options);
+		return 1;
+	}
+
+	path = argv[0];
+	newurl = argv[1];
+
+	strbuf_addf(&config_name, "submodule.%s.url", path);
+
+	config_set_in_gitmodules_file_gently(config_name.buf, newurl);
+	sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
+
+	strbuf_release(&config_name);
+
+	return 0;
+}
+
 #define SUPPORT_SUPER_PREFIX (1<<0)
 
 struct cmd_struct {
@@ -2276,6 +2314,7 @@ static struct cmd_struct commands[] = {
 	{"is-active", is_active, 0},
 	{"check-name", check_name, 0},
 	{"config", module_config, 0},
+	{"set-url", module_set_url, 0},
 };
 
 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
git-submodule.sh +1 -21
index 08e0439df0..39ebdf25b5 100755
@@ -805,27 +805,7 @@ cmd_set_url() {
 		shift
 	done
 
-	if test $# -ne 2
-	then
-		usage
-	fi
-
-	# we can't use `git submodule--helper name` here because internally, it
-	# hashes the path so a trailing slash could lead to an unintentional no match
-	name="$(git submodule--helper list "$1" | cut -f2)"
-	if test -z "$name"
-	then
-		exit 1
-	fi
-
-	url="$2"
-	if test -z "$url"
-	then
-		exit 1
-	fi
-
-	git submodule--helper config submodule."$name".url "$url"
-	git submodule--helper sync ${GIT_QUIET:+--quiet} "$name"
+	git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper set-url ${GIT_QUIET:+--quiet} -- "$@"
 }
 
 #
Signature · 1 line
2.26.2
#1 lore raw
Quoted 2 lines · Shourya Shukla

Convert submodule subcommand 'set-url' to a builtin. Port 'set-url'to

There is a space missing between "'set-url'" and "to".

Quoted 7 lines

'submodule--helper.c' and call the latter via 'git-submodule.sh'.

---
Thank you Junio for the review! :)
BTW, how detailed should the commit message be about the patch?

It looks good to me. Maybe it could more explicitely state that the larger goal is to convert shell code in 'git-submodule.sh' to C code in 'submodule--helper.c'. It can be guessed from the subject though.

Quoted 23 lines
 builtin/submodule--helper.c | 39 +++++++++++++++++++++++++++++++++++++
 git-submodule.sh            | 22 +--------------------
 2 files changed, 40 insertions(+), 21 deletions(-)
builtin/submodule--helper.c +11 -0
index 1a4b391c88..f50745a03f 100644
@@ -2246,6 +2246,44 @@ static int module_config(int argc, const char **argv, const char *prefix)
        usage_with_options(git_submodule_helper_usage, module_config_options);
 }
 
+static int module_set_url(int argc, const char **argv, const char *prefix)
+{
+       int quiet = 0;
+       const char *newurl;
+       const char *path;
+       struct strbuf config_name = STRBUF_INIT;
+
+       struct option set_url_options[] = {
+               OPT__QUIET(&quiet, N_("Suppress output for setting url of a submodule")),
+               OPT_END()
+       };

builtin/submodule--helper.c static int module_config(int argc, const char **argv, const char *prefix) · line 2269

Quoted 10 lines
builtin/submodule--helper.c +10 -0
+
+       const char *const usage[] = {
+               N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
+               NULL
+       };
+
+       argc = parse_options(argc, argv, prefix, set_url_options,
+                            usage, 0);
+
+       if (argc!=2) {

Please add space chars around "!=" like "argc != 2".

builtin/submodule--helper.c static int module_config(int argc, const char **argv, const char *prefix) · line 2282

Quoted 13 lines
builtin/submodule--helper.c +13 -0
+               usage_with_options(usage, set_url_options);
+               return 1;
+       }
+
+       path = argv[0];
+       newurl = argv[1];
+
+       strbuf_addf(&config_name, "submodule.%s.url", path);
+
+       config_set_in_gitmodules_file_gently(config_name.buf, newurl);
+       sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
+
+       strbuf_release(&config_name);

Nit: it might be a bit simpler to define config_name as a "char *", and then use xstrfmt() and free() instead of strbuf_addf() and strbuf_release().

Quoted 2 lines
builtin/submodule--helper.c +2 -0
+       return 0;
+}
#2 lore raw
Quoted 10 lines · Christian Couder

builtin/submodule--helper.c static int module_config(int argc, const char **argv, const char *prefix) · line 2282

Quoted 6 lines
builtin/submodule--helper.c +6 -0
+       strbuf_addf(&config_name, "submodule.%s.url", path);
+
+       config_set_in_gitmodules_file_gently(config_name.buf, newurl);
+       sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
+
+       strbuf_release(&config_name);

Nit: it might be a bit simpler to define config_name as a "char *", and then use xstrfmt() and free() instead of strbuf_addf() and strbuf_release().

Apart from the simplicity purposes, does doing this aid in performance in any way?

Quoted 2 lines
Quoted 2 lines
builtin/submodule--helper.c +2 -0
+       return 0;
+}
#3 lore raw
Quoted 12 lines · Shourya Shukla
Quoted 10 lines · Christian Couder

builtin/submodule--helper.c static int module_config(int argc, const char **argv, const char *prefix) · line 2282

Quoted 6 lines
builtin/submodule--helper.c +6 -0
+       strbuf_addf(&config_name, "submodule.%s.url", path);
+
+       config_set_in_gitmodules_file_gently(config_name.buf, newurl);
+       sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
+
+       strbuf_release(&config_name);

Nit: it might be a bit simpler to define config_name as a "char *", and then use xstrfmt() and free() instead of strbuf_addf() and strbuf_release().

Quoted 2 lines

Apart from the simplicity purposes, does doing this aid in performance in any way?

strbuf.c::xstrfmt() uses strbuf.c::xstrvfmt() that formats into a temporary strbuf and returns the detached buffer as the result.

Compare it with what strbuf.c::strbuf_addf() and you can draw a conclusion on your own ;-)

#4 lore raw
In reply to Shourya Shukla
Quoted 27 lines · Shourya Shukla

Convert submodule subcommand 'set-url' to a builtin. Port 'set-url'to 'submodule--helper.c' and call the latter via 'git-submodule.sh'.

---
Thank you Junio for the review! :)
BTW, how detailed should the commit message be about the patch?

 builtin/submodule--helper.c | 39 +++++++++++++++++++++++++++++++++++++
 git-submodule.sh            | 22 +--------------------
 2 files changed, 40 insertions(+), 21 deletions(-)
builtin/submodule--helper.c +6 -0
index 1a4b391c88..f50745a03f 100644
@@ -2246,6 +2246,44 @@ static int module_config(int argc, const char **argv, const char *prefix)
 	usage_with_options(git_submodule_helper_usage, module_config_options);
 }
 
+static int module_set_url(int argc, const char **argv, const char *prefix)
+{
+	int quiet = 0;
+	const char *newurl;
+	const char *path;
+	struct strbuf config_name = STRBUF_INIT;

builtin/submodule--helper.c static int module_config(int argc, const char **argv, const char *prefix) · line 2264

Quoted 10 lines
builtin/submodule--helper.c +10 -0
+
+	struct option set_url_options[] = {
+		OPT__QUIET(&quiet, N_("Suppress output for setting url of a submodule")),
+		OPT_END()
+	};
+
+	const char *const usage[] = {
+		N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
+		NULL
+	};

Hmph, do we really want all the blank lines in the above?

There is only one "struct option" the code in this function needs to be aware of and worried about. Isn't naming it set_url_options[] overly redundant? Calling it just options[] would save lines here ;-)

builtin/submodule--helper.c static int module_config(int argc, const char **argv, const char *prefix) · line 2267

Quoted 2 lines
builtin/submodule--helper.c +2 -0
+	argc = parse_options(argc, argv, prefix, set_url_options,
+			     usage, 0);
	argc = parse_options(argc, argv, prefix, options, usage, 0);

builtin/submodule--helper.c static int module_config(int argc, const char **argv, const char *prefix) · line 2269

Quoted 1 line
builtin/submodule--helper.c +1 -0
+	if (argc!=2) {

Style. SP around all binary operators like !=, i.e.

	if (argc != 2) {

By the way, looking at print_default_remote() that takes no arguments wants argc to be 1, and resolve_relative_url() that takes only one or two arguments checks for 2 or 3, shouldn't this be checking if argc is 3, not 2?

I thought I pointed it out in my very first review of this series.

... tries to go back and check, notices that this v4 is not ... a reply to v3 or earlier and feels somewhat irritated. ... then finally finds the following in the v2 review.

Quoted 12 lines
builtin/submodule--helper.c +0 -0
 Taking all these together,
 
        if (argc != 3) {
                usage_with_options(usage, options);
                return 1;
        }
        path = argv[0];
        newurl = argv[1];

If you feel paranoid, you can check these two are not NULL, too, i.e.

Quoted 8 lines
        if (argc != 3 || !(path = argv[0]) || !(newurl = argv[1])) {
                usage_with_options(usage, options);
                return 1;
        }

I have no strong preference either way. Perhaps the latter is more concise and more careful at the same time, so some people may prefer it.

Thanks.

#5 lore raw
Quoted 10 lines · Junio C Hamano
Quoted 10 lines
builtin/submodule--helper.c +10 -0
+static int module_set_url(int argc, const char **argv, const char *prefix)
+{
+	int quiet = 0;
+	const char *newurl;
+	const char *path;
+	struct strbuf config_name = STRBUF_INIT;
+
+	struct option set_url_options[] = {
+		OPT__QUIET(&quiet, N_("Suppress output for setting url of a submodule")),
+		OPT_END()
Quoted 8 lines

builtin/submodule--helper.c static int module_config(int argc, const char **argv, const char *prefix) · line 2264

Quoted 6 lines
builtin/submodule--helper.c +6 -0
+	};
+
+	const char *const usage[] = {
+		N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
+		NULL
+	};

Hmph, do we really want all the blank lines in the above?

Apologies,will amend.

Quoted 3 lines

There is only one "struct option" the code in this function needs to be aware of and worried about. Isn't naming it set_url_options[] overly redundant? Calling it just options[] would save lines here ;-)

I was actually following the format of the other subcommands, will surely change it.

Quoted 9 lines

builtin/submodule--helper.c static int module_config(int argc, const char **argv, const char *prefix) · line 2267

Quoted 2 lines
builtin/submodule--helper.c +2 -0
+	argc = parse_options(argc, argv, prefix, set_url_options,
+			     usage, 0);
	argc = parse_options(argc, argv, prefix, options, usage, 0);

builtin/submodule--helper.c static int module_config(int argc, const char **argv, const char *prefix) · line 2269

Quoted 1 line
builtin/submodule--helper.c +1 -0
+	if (argc!=2) {

Style. SP around all binary operators like !=, i.e.

Quoted 6 lines
	if (argc != 2) {

By the way, looking at print_default_remote() that takes no arguments wants argc to be 1, and resolve_relative_url() that takes only one or two arguments checks for 2 or 3, shouldn't this be checking if argc is 3, not 2?

Aren't path and newurl the only arguments we should worry about here as 'parse_options' will parse out the other arguments ('git submodule--helper' and the 'quiet' option) leaving us with only the aforementioned arguments. Am I missing something here?

To add on, checking for argc!=3 results in a failure of t7420. If we have anything but 2 arguments (either less or more) we should have a failure.

I think that we will do a check for 3 if we pass the macro PARSE_OPT_KEEP_ARGV0 in parse_options(). So the final code segment would look like:

	argc = parse_options(argc, argv, prefix, options,
			     usage, PARSE_OPT_KEEP_ARGV0);

	if (argc != 3) {
		usage_with_options(usage, options);
		return 1;
	}

	path = argv[1];
	newurl = argv[2];

which does pass t7420. Therefore a stricter check could be:

	argc = parse_options(argc, argv, prefix, options,
			     usage, 0);

	path = argv[0];
	newurl = argv[1];

	if (argc != 2 || path == NULL || newurl == NULL) {
		usage_with_options(usage, options);
		return 1;
	}

which passes t7420.

Quoted 5 lines

I thought I pointed it out in my very first review of this series.

... tries to go back and check, notices that this v4 is not ... a reply to v3 or earlier and feels somewhat irritated. ... then finally finds the following in the v2 review.

I am very very sorry for this. I undestand how this must feel. Will ensure this from the next version. :)

#6 lore raw
Quoted 9 lines · Shourya Shukla
Quoted 4 lines

By the way, looking at print_default_remote() that takes no arguments wants argc to be 1, and resolve_relative_url() that takes only one or two arguments checks for 2 or 3, shouldn't this be checking if argc is 3, not 2?

Aren't path and newurl the only arguments we should worry about here as 'parse_options' will parse out the other arguments ('git submodule--helper' and the 'quiet' option) leaving us with only the aforementioned arguments. Am I missing something here?

Ah, I misread those examples that suggested that you are supposed to
check for N+1 when you expect N arguments. They are *not* using
parse_options() and that is where that funny numbering comes from.

This one uses "argc = parse_options(...)" so we should check for N when we want N args. Thanks.

#7 lore raw
Quoted 6 lines · Junio C Hamano

Ah, I misread those examples that suggested that you are supposed to
check for N+1 when you expect N arguments. They are *not* using
parse_options() and that is where that funny numbering comes from.

This one uses "argc = parse_options(...)" so we should check for N when we want N args. Thanks.

No worries. BTW, should I include the path == NULL check in the if-statement? I think the argc check would suffice but I would still love to hear a final verdict from you and Christian :)

#8 lore raw
Quoted 2 lines · Shourya Shukla

No worries. BTW, should I include the path == NULL check in the if-statement?

If I were writing this code, I would probably write it like so:

	if (!path || !newurl)
		oops;

Specifically, I would write "!path", not "path == NULL". I thought a rule for that is in the CodingGuidelines (I didn't double check, though).

The comparison on argc is to see if we are even allowed to access argv[0] and/or argv[1]. In practice, if what main() got from the outside world in argv[] is passed directly to you, argv[n] would never be NULL as long as n < argc, but there are a few levels of callchain between main() and you (i.e. module_set_url()), so not counting on that would be sensible.

#9 lore raw
Quoted 5 lines · Junio C Hamano
Quoted 2 lines · Shourya Shukla

No worries. BTW, should I include the path == NULL check in the if-statement?

Quoted 8 lines

If I were writing this code, I would probably write it like so:

	if (!path || !newurl)
		oops;

Specifically, I would write "!path", not "path == NULL". I thought a rule for that is in the CodingGuidelines (I didn't double check, though).

I could not find a rule like that in the CodingGuidelines. Should I add it?
https://github.com/git/git/blob/master/Documentation/CodingGuidelines

Quoted 6 lines

The comparison on argc is to see if we are even allowed to access argv[0] and/or argv[1]. In practice, if what main() got from the outside world in argv[] is passed directly to you, argv[n] would never be NULL as long as n < argc, but there are a few levels of callchain between main() and you (i.e. module_set_url()), so not counting on that would be sensible.

Understood. I will add the NULL check as well.

#10 lore raw
Quoted 10 lines · Shourya Shukla
Quoted 3 lines · Junio C Hamano

Specifically, I would write "!path", not "path == NULL". I thought a rule for that is in the CodingGuidelines (I didn't double check, though).

I could not find a rule like that in the CodingGuidelines. Should I add it?
https://github.com/git/git/blob/master/Documentation/CodingGuidelines

Sure.

Quoted 8 lines
Quoted 6 lines

The comparison on argc is to see if we are even allowed to access argv[0] and/or argv[1]. In practice, if what main() got from the outside world in argv[] is passed directly to you, argv[n] would never be NULL as long as n < argc, but there are a few levels of callchain between main() and you (i.e. module_set_url()), so not counting on that would be sensible.

Understood. I will add the NULL check as well.

Thanks,
Christian.

#11 lore raw

Re* [PATCH v4] submodule: port subcommand 'set-url' from shell to C

Quoted 9 lines · Christian Couder
Quoted 7 lines · Shourya Shukla
Quoted 3 lines · Junio C Hamano

Specifically, I would write "!path", not "path == NULL". I thought a rule for that is in the CodingGuidelines (I didn't double check, though).

Quoted 5 lines
Quoted 3 lines

I could not find a rule like that in the CodingGuidelines. Should I add it?
https://github.com/git/git/blob/master/Documentation/CodingGuidelines

Sure.

I'd rather not see too many unrelated things piled up on Shourya's plate. Without guidance, the new entry we'll see would be only about comparing with NULL, and we'd need to spend review cycles correcting that, too.

How about something like this, perhaps?

-- >8 --
CodingGuidelines: do not ==/!= compare with 0/NULL

---
 Documentation/CodingGuidelines | 13 +++++++++++++
 1 file changed, 13 insertions(+)
Documentation/CodingGuidelines +13 -0
index 390ceece52..41a89dd845 100644
@@ -236,6 +236,19 @@ For C programs:
         while( condition )
 		func (bar+1);
 
+ - Do not explicitly compare an integral value with constant 0 or a
+   pointer value with constant NULL for equality; just say !value
+   instead.  To validate a counted array at ptr that has cnt elements
+   in it, write:
+
+	if (!ptr || !cnt)
+		BUG("array should not be empty at this point");
+
+   and not:
+
+	if (ptr == NULL || cnt == 0);
+		BUG("array should not be empty at this point");
+
  - We avoid using braces unnecessarily.  I.e.
 
 	if (bla) {
#12 lore raw

Re: Re* [PATCH v4] submodule: port subcommand 'set-url' from shell to C

Documentation/CodingGuidelines For C programs: · line 250

Quoted 12 lines · Junio C Hamano

+ - Do not explicitly compare an integral value with constant 0 or a

Documentation/CodingGuidelines +11 -0
+   pointer value with constant NULL for equality; just say !value
+   instead.  To validate a counted array at ptr that has cnt elements
+   in it, write:
+
+       if (!ptr || !cnt)
+               BUG("array should not be empty at this point");
+
+   and not:
+
+       if (ptr == NULL || cnt == 0);
+               BUG("array should not be empty at this point");

This talks only about '=='. People might still use 0 or NULL with '!='. I wonder if the example can include '!=', as well. Perhaps:

    if (!ptr)
        BUG("...");
    if (cnt)
        foo(ptr, cnt);

instead of:

    if (ptr == NULL)
        BUG("...");
    if (cnt != 0)
        foo(ptr, cnt);

or something.

Also, would you want to talk about not comparing against NUL character?

    if (*s)
        foo(s);

instead of:

    if (*s != '\0')
        foo(s);

Maybe that's overkill since NUL is an integral value which is already covered by your earlier statement (but perhaps some people would overlook that).

#13 lore raw

Re: Re* [PATCH v4] submodule: port subcommand 'set-url' from shell to C

Quoted 15 lines · Eric Sunshine

Documentation/CodingGuidelines For C programs: · line 250

Quoted 12 lines · Junio C Hamano

+ - Do not explicitly compare an integral value with constant 0 or a

Documentation/CodingGuidelines +11 -0
+   pointer value with constant NULL for equality; just say !value
+   instead.  To validate a counted array at ptr that has cnt elements
+   in it, write:
+
+       if (!ptr || !cnt)
+               BUG("array should not be empty at this point");
+
+   and not:
+
+       if (ptr == NULL || cnt == 0);
+               BUG("array should not be empty at this point");

This talks only about '=='.

Yup. The text would need a matching change, though.

Quoted 8 lines

People might still use 0 or NULL with
'!='. I wonder if the example can include '!=', as well. Perhaps:

    if (!ptr)
        BUG("...");
    if (cnt)
        foo(ptr, cnt);
Quoted 8 lines

instead of:

    if (ptr == NULL)
        BUG("...");
    if (cnt != 0)
        foo(ptr, cnt);

or something.

Or more succinctly:

	if (!ptr || cnt)
		BUG("we must have an empty array at this point");

perhaps?

Quoted 5 lines

Also, would you want to talk about not comparing against NUL character?

    if (*s)
        foo(s);
Quoted 8 lines

instead of:

    if (*s != '\0')
        foo(s);

Maybe that's overkill since NUL is an integral value which is already covered by your earlier statement (but perhaps some people would overlook that).

Yeah, it might be worth saying it explicitly. I dunno.

#14 lore raw

Re: Re* [PATCH v4] submodule: port subcommand 'set-url' from shell to C

Quoted 4 lines · Junio C Hamano
Quoted 1 line · Eric Sunshine

This talks only about '=='.

Yup. The text would need a matching change, though.

Here's a re-roll with the necessary changes.

Quoted 9 lines
Quoted 4 lines
    if (!ptr)
        BUG("...");
    if (cnt)
        foo(ptr, cnt);

Or more succinctly:

	if (!ptr || cnt)
		BUG("we must have an empty array at this point");

I considered that but thought it might be too "cute", however, seeing it written out, it looks fine, so I used it in the re-roll.

Quoted 3 lines
Quoted 1 line

Also, would you want to talk about not comparing against NUL character?

Yeah, it might be worth saying it explicitly. I dunno.

Rather than giving this a separate example in the re-roll, I just mentioned '\0' in the text.

--- >8 ---

From: Junio C Hamano <[email protected]>
Subject: [PATCH] CodingGuidelines: do not ==/!= compare with 0 or '\0' or NULL

---
 Documentation/CodingGuidelines | 12 ++++++++++++
 1 file changed, 12 insertions(+)
Documentation/CodingGuidelines +12 -0
index 390ceece52..6dfc47ed7d 100644
@@ -236,6 +236,18 @@ For C programs:
         while( condition )
 		func (bar+1);
 
+ - Do not explicitly compare an integral value with constant 0 or '\0',
+   or a pointer value with constant NULL.  For instance, to validate a
+   counted array ptr that has cnt elements, write:
+
+	if (!ptr || cnt)
+		BUG("empty array expected");
+
+   and not:
+
+	if (ptr == NULL || cnt != 0);
+		BUG("empty array expected");
+
  - We avoid using braces unnecessarily.  I.e.
 
 	if (bla) {
Signature · 1 line
2.26.2.717.g5cccb0e1a8
#15 lore raw

Re: Re* [PATCH v4] submodule: port subcommand 'set-url' from shell to C

Quoted 14 lines · Eric Sunshine

From: Junio C Hamano <[email protected]>
Subject: [PATCH] CodingGuidelines: do not ==/!= compare with 0 or '\0' or NULL

---
 Documentation/CodingGuidelines | 12 ++++++++++++
 1 file changed, 12 insertions(+)
Documentation/CodingGuidelines +0 -0
index 390ceece52..6dfc47ed7d 100644

Documentation/CodingGuidelines For C programs: · line 241

Quoted 6 lines
Documentation/CodingGuidelines +3 -0
@@ -236,6 +236,18 @@ For C programs:
         while( condition )
 		func (bar+1);
 
+ - Do not explicitly compare an integral value with constant 0 or '\0',
+   or a pointer value with constant NULL.  For instance, to validate a
+   counted array ptr that has cnt elements, write:

I think this should be

counted array <ptr, cnt> is initialized but has no elements, write:

Quoted 12 lines
Documentation/CodingGuidelines +9 -0
+
+	if (!ptr || cnt)
+		BUG("empty array expected");
+
+   and not:
+
+	if (ptr == NULL || cnt != 0);
+		BUG("empty array expected");
+
  - We avoid using braces unnecessarily.  I.e.
 
 	if (bla) {
#16 lore raw

Re: Re* [PATCH v4] submodule: port subcommand 'set-url' from shell to C

Quoted 8 lines · Junio C Hamano

Review

Quoted 3 lines · Eric Sunshine

+ - Do not explicitly compare an integral value with constant 0 or '\0',

(quoted diff) +2 -0
+   or a pointer value with constant NULL.  For instance, to validate a
+   counted array ptr that has cnt elements, write:

I think this should be

counted array <ptr, cnt> is initialized but has no elements, write:

You're right. Here's a corrected version. I also applied s/a/that/ in the second line to improve the grammar a bit.

--- >8 ---

From: Junio C Hamano <[email protected]>
Subject: [PATCH v3] CodingGuidelines: do not ==/!= compare with 0 or '\0' or
NULL

---
 Documentation/CodingGuidelines | 12 ++++++++++++
 1 file changed, 12 insertions(+)
Documentation/CodingGuidelines +12 -0
index 390ceece52..803a3b9bde 100644
@@ -236,6 +236,18 @@ For C programs:
         while( condition )
 		func (bar+1);
 
+ - Do not explicitly compare an integral value with constant 0 or '\0',
+   or a pointer value with constant NULL.  For instance, to validate that
+   counted array <ptr, cnt> is initialized but has no elements, write:
+
+	if (!ptr || cnt)
+		BUG("empty array expected");
+
+   and not:
+
+	if (ptr == NULL || cnt != 0);
+		BUG("empty array expected");
+
  - We avoid using braces unnecessarily.  I.e.
 
 	if (bla) {
Signature · 1 line
2.26.2.737.gf3227dd3d3
#17 lore raw

PATCH v5 1/1 submodule: port subcommand 'set-url' from shell to C

In reply to Shourya Shukla

Convert submodule subcommand 'set-url' to a builtin. Port 'set-url' to 'submodule--helper.c' and call the latter via 'git-submodule.sh'.

---
 builtin/submodule--helper.c | 37 +++++++++++++++++++++++++++++++++++++
 git-submodule.sh            | 22 +---------------------
 2 files changed, 38 insertions(+), 21 deletions(-)
builtin/submodule--helper.c +37 -0
index 1a4b391c88..8bc7b4cfa6 100644
@@ -2246,6 +2246,42 @@ static int module_config(int argc, const char **argv, const char *prefix)
 	usage_with_options(git_submodule_helper_usage, module_config_options);
 }
 
+static int module_set_url(int argc, const char **argv, const char *prefix)
+{
+	int quiet = 0;
+	const char *newurl;
+	const char *path;
+	char* config_name;
+
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("Suppress output for setting url of a submodule")),
+		OPT_END()
+	};
+	const char *const usage[] = {
+		N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
+		NULL
+	};
+
+	argc = parse_options(argc, argv, prefix, options, usage, 0);
+
+	path = argv[0];
+	newurl = argv[1];
+
+	if (argc != 2 || !path || !newurl) {
+		usage_with_options(usage, options);
+		return 1;
+	}
+
+	config_name = xstrfmt("submodule.%s.url", path);
+
+	config_set_in_gitmodules_file_gently(config_name, newurl);
+	sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
+
+	free(config_name);
+
+	return 0;
+}
+
 #define SUPPORT_SUPER_PREFIX (1<<0)
 
 struct cmd_struct {
@@ -2276,6 +2312,7 @@ static struct cmd_struct commands[] = {
 	{"is-active", is_active, 0},
 	{"check-name", check_name, 0},
 	{"config", module_config, 0},
+	{"set-url", module_set_url, 0},
 };
 
 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
git-submodule.sh +1 -21
index 08e0439df0..39ebdf25b5 100755
@@ -805,27 +805,7 @@ cmd_set_url() {
 		shift
 	done
 
-	if test $# -ne 2
-	then
-		usage
-	fi
-
-	# we can't use `git submodule--helper name` here because internally, it
-	# hashes the path so a trailing slash could lead to an unintentional no match
-	name="$(git submodule--helper list "$1" | cut -f2)"
-	if test -z "$name"
-	then
-		exit 1
-	fi
-
-	url="$2"
-	if test -z "$url"
-	then
-		exit 1
-	fi
-
-	git submodule--helper config submodule."$name".url "$url"
-	git submodule--helper sync ${GIT_QUIET:+--quiet} "$name"
+	git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper set-url ${GIT_QUIET:+--quiet} -- "$@"
 }
 
 #
Signature · 1 line
2.26.2
#18 lore raw

Hi Shourya,

It looks good to me except for one tiny nit:

Quoted 14 lines · Shourya Shukla

Convert submodule subcommand 'set-url' to a builtin. Port 'set-url' to 'submodule--helper.c' and call the latter via 'git-submodule.sh'.

---
 builtin/submodule--helper.c | 37 +++++++++++++++++++++++++++++++++++++
 git-submodule.sh            | 22 +---------------------
 2 files changed, 38 insertions(+), 21 deletions(-)
builtin/submodule--helper.c +0 -0
index 1a4b391c88..8bc7b4cfa6 100644

builtin/submodule--helper.c static int module_config(int argc, const char **argv, const char *prefix) · line 2254

Quoted 9 lines
builtin/submodule--helper.c +6 -0
@@ -2246,6 +2246,42 @@ static int module_config(int argc, const char **argv, const char *prefix)
 	usage_with_options(git_submodule_helper_usage, module_config_options);
 }
 
+static int module_set_url(int argc, const char **argv, const char *prefix)
+{
+	int quiet = 0;
+	const char *newurl;
+	const char *path;
+	char* config_name;

The asterisk should be stuck with the name, not the type, similar to how you wrote it above.

Quoted 33 lines
builtin/submodule--helper.c +30 -0
+
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("Suppress output for setting url of a submodule")),
+		OPT_END()
+	};
+	const char *const usage[] = {
+		N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
+		NULL
+	};
+
+	argc = parse_options(argc, argv, prefix, options, usage, 0);
+
+	path = argv[0];
+	newurl = argv[1];
+
+	if (argc != 2 || !path || !newurl) {
+		usage_with_options(usage, options);
+		return 1;
+	}
+
+	config_name = xstrfmt("submodule.%s.url", path);
+
+	config_set_in_gitmodules_file_gently(config_name, newurl);
+	sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
+
+	free(config_name);
+
+	return 0;
+}
+
 #define SUPPORT_SUPER_PREFIX (1<<0)
 
 struct cmd_struct {
#19 lore raw
Quoted 22 lines · Denton Liu

Hi Shourya,

It looks good to me except for one tiny nit:

Quoted 17 lines · Shourya Shukla

Convert submodule subcommand 'set-url' to a builtin. Port 'set-url' to 'submodule--helper.c' and call the latter via 'git-submodule.sh'.

---
 builtin/submodule--helper.c | 37 +++++++++++++++++++++++++++++++++++++
 git-submodule.sh            | 22 +---------------------
 2 files changed, 38 insertions(+), 21 deletions(-)
builtin/submodule--helper.c +0 -0
index 1a4b391c88..8bc7b4cfa6 100644
@@ -2246,6 +2246,42 @@ static int module_config(int argc, const char **argv, const char *prefix)
 	usage_with_options(git_submodule_helper_usage, module_config_options);
 }
 
Quoted 9 lines

builtin/submodule--helper.c static int module_config(int argc, const char **argv, const char *prefix) · line 2254

Quoted 6 lines
builtin/submodule--helper.c +6 -0
+static int module_set_url(int argc, const char **argv, const char *prefix)
+{
+	int quiet = 0;
+	const char *newurl;
+	const char *path;
+	char* config_name;

The asterisk should be stuck with the name, not the type, similar to how you wrote it above.

Right.

Quoted 8 lines
Quoted 8 lines
builtin/submodule--helper.c +8 -0
+
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("Suppress output for setting url of a submodule")),
+		OPT_END()
+	};
+	const char *const usage[] = {
+		N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
+		NULL
Quoted 8 lines

builtin/submodule--helper.c static int module_config(int argc, const char **argv, const char *prefix) · line 2270

Quoted 8 lines
builtin/submodule--helper.c +8 -0
+	};
+
+	argc = parse_options(argc, argv, prefix, options, usage, 0);
+
+	path = argv[0];
+	newurl = argv[1];
+
+	if (argc != 2 || !path || !newurl) {

Checking argc at this point is too late to protect against the potential out-of-bounds access we have already made to argv[0] and argv[1].

#20 lore raw
In reply to Denton Liu
Quoted 3 lines · Denton Liu

builtin/submodule--helper.c static int module_config(int argc, const char **argv, const char *prefix) · line 2272

Quoted 3 lines
builtin/submodule--helper.c +3 -0
+	if (argc != 2 || !path || !newurl) {
+		usage_with_options(usage, options);
+		return 1;

It is embarrassing that nobody noticed that usage_with_options() is NORETURN; return 1 has no effect here.

Quoted 15 lines
Quoted 15 lines
builtin/submodule--helper.c +12 -0
+	}
+
+	config_name = xstrfmt("submodule.%s.url", path);
+
+	config_set_in_gitmodules_file_gently(config_name, newurl);
+	sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
+
+	free(config_name);
+
+	return 0;
+}
+
 #define SUPPORT_SUPER_PREFIX (1<<0)
 
 struct cmd_struct {
#21 lore raw

PATCH v6 1/1 submodule: port subcommand 'set-url' from shell to C

In reply to Shourya Shukla

From: Shourya Shukla <[email protected]>

Convert submodule subcommand 'set-url' to a builtin. Port 'set-url' to 'submodule--helper.c' and call the latter via 'git-submodule.sh'.

---

* Here is what I'll queue with fixups for now.

 builtin/submodule--helper.c | 32 ++++++++++++++++++++++++++++++++
 git-submodule.sh            | 22 +---------------------
 2 files changed, 33 insertions(+), 21 deletions(-)
builtin/submodule--helper.c +32 -0
index 1a4b391c88..46c03d2a12 100644
@@ -2246,6 +2246,37 @@ static int module_config(int argc, const char **argv, const char *prefix)
 	usage_with_options(git_submodule_helper_usage, module_config_options);
 }
 
+static int module_set_url(int argc, const char **argv, const char *prefix)
+{
+	int quiet = 0;
+	const char *newurl;
+	const char *path;
+	char *config_name;
+
+	struct option options[] = {
+		OPT__QUIET(&quiet, N_("Suppress output for setting url of a submodule")),
+		OPT_END()
+	};
+	const char *const usage[] = {
+		N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
+		NULL
+	};
+
+	argc = parse_options(argc, argv, prefix, options, usage, 0);
+
+	if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
+		usage_with_options(usage, options);
+
+	config_name = xstrfmt("submodule.%s.url", path);
+
+	config_set_in_gitmodules_file_gently(config_name, newurl);
+	sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
+
+	free(config_name);
+
+	return 0;
+}
+
 #define SUPPORT_SUPER_PREFIX (1<<0)
 
 struct cmd_struct {
@@ -2276,6 +2307,7 @@ static struct cmd_struct commands[] = {
 	{"is-active", is_active, 0},
 	{"check-name", check_name, 0},
 	{"config", module_config, 0},
+	{"set-url", module_set_url, 0},
 };
 
 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
git-submodule.sh +1 -21
index 08e0439df0..39ebdf25b5 100755
@@ -805,27 +805,7 @@ cmd_set_url() {
 		shift
 	done
 
-	if test $# -ne 2
-	then
-		usage
-	fi
-
-	# we can't use `git submodule--helper name` here because internally, it
-	# hashes the path so a trailing slash could lead to an unintentional no match
-	name="$(git submodule--helper list "$1" | cut -f2)"
-	if test -z "$name"
-	then
-		exit 1
-	fi
-
-	url="$2"
-	if test -z "$url"
-	then
-		exit 1
-	fi
-
-	git submodule--helper config submodule."$name".url "$url"
-	git submodule--helper sync ${GIT_QUIET:+--quiet} "$name"
+	git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper set-url ${GIT_QUIET:+--quiet} -- "$@"
 }
 
 #
Signature · 1 line
2.26.2-561-g07d8ea56f2