git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Stefan Beller <sbeller@google.com>
Cc: sunshine@sunshineco.com, git@vger.kernel.org, jrnieder@gmail.com,
	johannes.schindelin@gmail.com, Jens.Lehmann@web.de,
	peff@peff.net
Subject: Re: [PATCHv4 1/3] submodule: implement `list` as a builtin helper
Date: Tue, 01 Sep 2015 12:55:51 -0700	[thread overview]
Message-ID: <xmqqtwrdrk1k.fsf@gitster.mtv.corp.google.com> (raw)
In-Reply-To: <1441131869-29474-2-git-send-email-sbeller@google.com> (Stefan Beller's message of "Tue, 1 Sep 2015 11:24:27 -0700")

Stefan Beller <sbeller@google.com> writes:

> +static int module_list_compute(int argc, const char **argv,
> +				const char *prefix,
> +				struct pathspec *pathspec)
> +{
> +	int i, result = 0;
> +	char *max_prefix, *ps_matched = NULL;
> +	int max_prefix_len;
> +	parse_pathspec(pathspec, 0,
> +		       PATHSPEC_PREFER_FULL |
> +		       PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
> +		       prefix, argv);
> +
> +	/* Find common prefix for all pathspec's */
> +	max_prefix = common_prefix(pathspec);
> +	max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
> +
> +	if (pathspec->nr)
> +		ps_matched = xcalloc(pathspec->nr, 1);
> +
> +	if (read_cache() < 0)
> +		die(_("index file corrupt"));
> +
> +	for (i = 0; i < active_nr; i++) {
> +		const struct cache_entry *ce = active_cache[i];
> +
> +		if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
> +				    max_prefix_len, ps_matched,
> +				    S_ISGITLINK(ce->ce_mode) | S_ISDIR(ce->ce_mode)))
> +			continue;
> +
> +		if (S_ISGITLINK(ce->ce_mode)) {
> +			ALLOC_GROW(ce_entries, ce_used + 1, ce_alloc);
> +			ce_entries[ce_used++] = ce;
> +		}
> +
> +		while (i + 1 < active_nr && !strcmp(ce->name, active_cache[i + 1]->name))
> +			/*
> +			 * Skip entries with the same name in different stages
> +			 * to make sure an entry is returned only once.
> +			 */
> +			i++;
> +	}

I hate myself not catching this earlier, but I suspect that this is
not quite a faithful rewrite of the original.  It changes behaviour
when a conflicted path records a non submodule in stage #1 and a
submodule in stage #2 (or stage #3), doesn't it?

The original scripted version will see the stage #1 entry and skips
it because it is not a submodule, then will see the stage #2 entry
and because the path is not yet in the %unmerged hash, and it will
push it to @out.

This loop instead sees a non-submodule entry at stage #1, skips it
(because it is not a submodule), then goes on to skip the entries
with the same name, including the stage #2 entry that _is_ a
submodule.

I think the clever "we are done with this entry, so let's skip all
others that have the same name" optimization should be done only
when we did handle an entry with the same name.  I.e. something like
this squashed in.
	
--------------------------------------------------

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 16d9abe..c4aff0c 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -39,12 +39,13 @@ static int module_list_compute(int argc, const char **argv,
 				    S_ISGITLINK(ce->ce_mode) | S_ISDIR(ce->ce_mode)))
 			continue;
 
-		if (S_ISGITLINK(ce->ce_mode)) {
-			ALLOC_GROW(ce_entries, ce_used + 1, ce_alloc);
-			ce_entries[ce_used++] = ce;
-		}
+		if (!S_ISGITLINK(ce->ce_mode))
+			continue;
 
-		while (i + 1 < active_nr && !strcmp(ce->name, active_cache[i + 1]->name))
+		ALLOC_GROW(ce_entries, ce_used + 1, ce_alloc);
+		ce_entries[ce_used++] = ce;
+		while (i + 1 < active_nr &&
+		       !strcmp(ce->name, active_cache[i + 1]->name))
 			/*
 			 * Skip entries with the same name in different stages
 			 * to make sure an entry is returned only once.

--------------------------------------------------

> +static int module_list(int argc, const char **argv, const char *prefix)
> +{
> +	int i;
> +	struct pathspec pathspec;
> +	const char *alternative_path;
> +
> +	struct option module_list_options[] = {
> +		OPT_STRING(0, "prefix", &alternative_path,
> +			   N_("path"),
> +			   N_("alternative anchor for relative paths")),
> +		OPT_END()
> +	};

Do we really need this alternative_path variable?  The "--prefix"
option that overrides the passed-in variable prefix would be easier
to understand if it touched the same variable, i.e.

--------------------------------------------------

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 16d9abe..387539f 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -65,10 +66,9 @@ static int module_list(int argc, const char **argv, const char *prefix)
 {
 	int i;
 	struct pathspec pathspec;
-	const char *alternative_path;
 
 	struct option module_list_options[] = {
-		OPT_STRING(0, "prefix", &alternative_path,
+		OPT_STRING(0, "prefix", &prefix,
 			   N_("path"),
 			   N_("alternative anchor for relative paths")),
 		OPT_END()
@@ -82,9 +82,7 @@ static int module_list(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, module_list_options,
 			     git_submodule_helper_usage, 0);
 
-	if (module_list_compute(argc, argv, alternative_path
-					    ? alternative_path
-					    : prefix, &pathspec) < 0) {
+	if (module_list_compute(argc, argv, prefix, &pathspec) < 0) {
 		printf("#unmatched\n");
 		return 1;
 	}

--------------------------------------------------

  reply	other threads:[~2015-09-01 19:55 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-01 18:24 [PATCHv4 0/3] submodule--helper: Have some refactoring only patches first Stefan Beller
2015-09-01 18:24 ` [PATCHv4 1/3] submodule: implement `list` as a builtin helper Stefan Beller
2015-09-01 19:55   ` Junio C Hamano [this message]
2015-09-02  7:20     ` Junio C Hamano
2015-09-01 23:17   ` Eric Sunshine
2015-09-01 18:24 ` [PATCHv4 2/3] submodule: implement `name` " Stefan Beller
2015-09-01 19:01   ` Eric Sunshine
2015-09-01 18:24 ` [PATCHv4 3/3] submodule: implement `clone` " Stefan Beller
2015-09-01 18:52   ` Eric Sunshine
2015-09-01 19:05     ` Jeff King
2015-09-01 19:15       ` Eric Sunshine
2015-09-01 19:13     ` Junio C Hamano
     [not found] <1441148863-9139-1-git-send-email-sbeller@google.com>
2015-09-01 23:07 ` [PATCHv4 1/3] submodule: implement `list` " Stefan Beller
2015-09-01 23:08   ` Stefan Beller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqtwrdrk1k.fsf@gitster.mtv.corp.google.com \
    --to=gitster@pobox.com \
    --cc=Jens.Lehmann@web.de \
    --cc=git@vger.kernel.org \
    --cc=johannes.schindelin@gmail.com \
    --cc=jrnieder@gmail.com \
    --cc=peff@peff.net \
    --cc=sbeller@google.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).