git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 3/6] Refactor --dirstat parsing; deprecate --cumulative and --dirstat-by-file

From
Johan Herland <johan@herland.net>
Date
Apr 27, 2011, 02:02 UTC
Message-ID
<201104270402.10658.johan@herland.net>
In-Reply-To
<7vy62xezqy.fsf@alter.siamese.dyndns.org>
On Tuesday 26 April 2011, Junio C Hamano wrote:
Show 11 quoted lines
> Johan Herland <johan@herland.net> writes:
> > +--dirstat[=<arg1,arg2,...>]::
> > +	Output the distribution of relative amount of changes for each
> > +	sub-directory. The behavior of `--dirstat` can be customized by
> > +	passing it a comma separated list of arguments. The defaults
> > +	are controlled by the `diff.dirstat` configuration variable (see
> > +	linkgit:git-config[1]). The following arguments are available:
>
> These "arguments" feel more like "options" (or "parameters"), no?  Your
> code in diff.c also calls it "opt".  The second line of the proposed log
> message has the same issue.

I have tried to consistently use "option" for referring to the entire "--dirstat=whatever" entity, and then use "argument" for referring to each comma-separated token following "--dirstat=". I based this on the function naming in diff.c, which uses "diff_opt_parse()" to parse diff options, "stat_opt()" to parse the '--stat*' options, and "opt_arg()" to parse arguments to options (i.e. "--option=argument").

To me, "argument" and "parameter" are synonyms, but English is not my first language. I'll replace "argument" with "parameter" in the re-roll. I.e. "option" refers to the option name AND the option parameters, while "parameters" refers to the option parameters only.

Show 9 quoted lines
> > +--
> > +`changes`;;
> > +	Compute the dirstat numbers by counting the lines that have been
> > +	removed from the source, or added to the destination. This ignores
> > +	the amount of pure code movements within a file.  In other words,
> > +	rearranging lines in a file is not counted as much as other changes.
> > +	This is the default `--dirstat` behavior.
> 
> "default behavior when no option is given"?
"default behavior when no parameter is given"?
Show 13 quoted lines
> > +`cumulative`;;
> > +	Count changes in a child directory for the parent directory as well.
> > +	Note that when using `cumulative`, the sum of the percentages
> > +	reported may exceed 100%. The default (non-cumulative) behavior can
> > +	be specified with the `noncumulative` argument.
> 
> So the later one wins?  I.e. --dirstat=cumulative,noncumulative from the
> command line (which seems silly), or more importantly with
> 
>     [alias]
>     	dstat = diff --dirstat=cumulative
> 
> and you can say "git dstat --dirstat=noncumulative A..B"?

Indeed. The intention is that dirstat parameters are parsed in order (first from config, then from command line), and the later parameters override earlier (conflicting) parameters.

Show 17 quoted lines
> > diff --git a/diff.c b/diff.c
> > index cfbfa92..08aaa47 100644
> > --- a/diff.c
> > +++ b/diff.c
> > @@ -3144,6 +3144,72 @@ static int stat_opt(struct diff_options
> > *options, const char **av)
> > 
> >  	return argcount;
> >  
> >  }
> 
> /*
>  * Document what the return value from this function means here.
>  */
> > +static int dirstat_opt(struct diff_options *options, const char **av)
> 
> Do you have to pass "const char **av", or just "const char *arg"?

dirstat_opt() was modeled on stat_opt(). dirstat_opt() obviously needs just "const char *arg". Will fix.

Show 39 quoted lines
> > +{
> > +	const char *p, *arg = av[0];
> > +	char *mangled = NULL;
> > +	char sep = '=';
> > +
> > +	if (!strcmp(arg, "--cumulative")) /* deprecated */
> > +		/* handle '--cumulative' like '--dirstat=cumulative' */
> > +		p = "=cumulative";
> > +	else if (!strcmp(arg, "--dirstat-by-file") ||
> > +		 !prefixcmp(arg, "--dirstat-by-file=")) { /* deprecated */
> > +		/* handle '--dirstat-by-file=*' like '--dirstat=files,*' */
> > +		mangled = xstrdup(arg + 2);
> > +		memcpy(mangled, "--dirstat=files", 15);
> > +		if (mangled[15]) {
> > +			assert(mangled[15] == '=');
> > +			mangled[15] = ',';
> > +		}
> > +		arg = mangled;
> > +		p = mangled + 9;
> 
> I understand you wanted to reuse the while() loop below, but I do not
> think it is worth it.  Isn't it easier to read if you handled the above
> cases in their if/else body and return?
> 
> 	if (--cumulative) {
> 		options->output_format |= DIFF_FORMAT_DIRSTAT;
>         	DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
>                 return 1;
> 	}
>         if (--dirstat-by-file) {
> 		options->output_format |= DIFF_FORMAT_DIRSTAT;
> 		DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
> 		return 1;
> 	}
> 	...
> 
> Even better, probably they can be left to diff_opt_parse() without
> calling this function, as you are deprecating them and do not have to
> allow them to take the opt1,opt2,... form of parameter.

I understand, but politely disagree: Patch 6/6 complicates the logic that DIFF_OPT_SET()/CLR() various bits in the diff options. I'd rather keep that logic in one place, than duplicate it into diff_opt_parse().

Show 14 quoted lines
> > +	}
> > +	else if (!prefixcmp(arg, "-X"))
> > +		p = arg + 2;
> > +	else if (!prefixcmp(arg, "--dirstat"))
> > +		p = arg + 9;
> > +	else
> > +		return 0;
> > +
> > +	options->output_format |= DIFF_FORMAT_DIRSTAT;
> > +
> > +	while (*p) {
> > +		if (*p != sep)
> 
> What happens to "diff -X3 A..B"?
Oops. Will fix, and add testcases verifying the fix.
> > +			die("Missing argument separator ('%c'), at index %lu of '%s'",
> > +			    sep, p - arg, arg);
> 
> Don't you need to cast (p-arg) for %lu from ptrdiff type here?
Copied PD_FMT from builtin/mktag.c instead.
> It probably is more common to say s/index/char/;
Indeed.
Show 30 quoted lines
> > +		if (!prefixcmp(p, "changes")) {
> > +			p += 7;
> > +			DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
> > +		}
> > +		else if (!prefixcmp(p, "files")) {
> > +			p += 5;
> > +			DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
> > +		}
> > +		else if (!prefixcmp(p, "noncumulative")) {
> > +			p += 13;
> > +			DIFF_OPT_CLR(options, DIRSTAT_CUMULATIVE);
> > +		}
> > +		else if (!prefixcmp(p, "cumulative")) {
> > +			p += 10;
> > +			DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
> > +		}
> > +		else if (isdigit(*p)) {
> > +			char *end;
> > +			options->dirstat_percent = strtoul(p, &end, 10);
> > +			assert(end > p);
> > +			p = end;
> > +		}
> 
> That's a senseless assert(), isn't it?
> 
> You already know the first letter is a digit, so assert(p < end) will
> always be true.  You may want to check that this particular option is all
> digit by checking (*end == '\0' || *end == ',') but that is done at the
> beginning of this loop anyway, so I don't think there is anything to
> check here.

True. I guess I just wanted a sanity check that aborts, rather than entering an infinite loop in case I got my logic wrong somewhere... Removed in the re-roll.

> > +		else
> > +			die("Unknown --dirstat argument '%s'", p);
> 
> The function parses dirstat_OPT, but this says argument?

Again, the "option" refers to the option name ("--dirstat") AND its s/arguments/parameters/ ("changes,noncumulative,3")

Your other comments (that I felt no need to comment on) will also be incorporated in the re-roll.

Thanks for the feedback!
...Johan
-- 
Johan Herland, <johan@herland.net>
www.herland.net
Previous: Junio C HamanoNext: Junio C Hamano
Message 34 of 91 in “BUG? in --dirstat when rearranging lines in a file”
  1. Johan HerlandApr 7, 2011
  2. Linus TorvaldsApr 7, 2011
  3. Junio C HamanoApr 7, 2011
  4. Linus TorvaldsApr 7, 2011
  5. Johan HerlandApr 8, 2011
  6. 1/3 --dirstat: Document shortcomings compared to --stat or regular diffJohan Herland, Apr 8, 2011
  7. Junio C HamanoApr 8, 2011
  8. 2/3 --dirstat-by-file: Make it faster and more correctJohan Herland, Apr 8, 2011
  9. 3/3 Teach --dirstat to not completely ignore rearranged linesJohan Herland, Apr 8, 2011
  10. Linus TorvaldsApr 8, 2011
  11. Junio C HamanoApr 8, 2011
  12. 0/3 --dirstat fixesJohan Herland, Apr 10, 2011
  13. 1/3 --dirstat: Describe non-obvious differences relative to --stat or regular diffJohan Herland, Apr 10, 2011
  14. 2/3 --dirstat-by-file: Make it faster and more correctJohan Herland, Apr 10, 2011
  15. Junio C HamanoApr 11, 2011
  16. 3/3 Teach --dirstat to not completely ignore rearranged lines within a fileJohan Herland, Apr 10, 2011
  17. Junio C HamanoApr 11, 2011
  18. Johan HerlandApr 11, 2011
  19. Junio C HamanoApr 11, 2011
  20. Johan HerlandApr 12, 2011
  21. 4/3 --dirstat: In case of renames, use target filename instead of source filenameJohan Herland, Apr 12, 2011
  22. Linus TorvaldsApr 12, 2011
  23. 5/3 Alternative --dirstat implementation, based on diffstat analysisJohan Herland, Apr 12, 2011
  24. Linus TorvaldsApr 12, 2011
  25. Linus TorvaldsApr 12, 2011
  26. Johan HerlandApr 12, 2011
  27. Linus TorvaldsApr 12, 2011
  28. Junio C HamanoApr 12, 2011
  29. 0/6 --dirstat fixes, part 2Johan Herland, Apr 26, 2011
  30. 1/6 Add several testcases for --dirstat and friendsJohan Herland, Apr 26, 2011
  31. 2/6 Make --dirstat=0 output directories that contribute < 0.1% of changesJohan Herland, Apr 26, 2011
  32. 3/6 Refactor --dirstat parsing; deprecate --cumulative and --dirstat-by-fileJohan Herland, Apr 26, 2011
  33. Junio C HamanoApr 26, 2011
  34. Johan HerlandApr 27, 2011
  35. Junio C HamanoApr 27, 2011
  36. Junio C HamanoApr 27, 2011
  37. Junio C HamanoApr 27, 2011
  38. 4/6 Add config variable for specifying default --dirstat behaviorJohan Herland, Apr 26, 2011
  39. Junio C HamanoApr 26, 2011
  40. Johan HerlandApr 27, 2011
  41. 5/6 Use floating point for --dirstat percentagesJohan Herland, Apr 26, 2011
  42. Junio C HamanoApr 26, 2011
  43. Johan HerlandApr 27, 2011
  44. Junio C HamanoApr 27, 2011
  45. Linus TorvaldsApr 27, 2011
  46. Junio C HamanoApr 27, 2011
  47. 6/6 New --dirstat=lines mode, doing dirstat analysis based on diffstatJohan Herland, Apr 26, 2011
  48. Junio C HamanoApr 26, 2011
  49. Johan HerlandApr 27, 2011
  50. Linus TorvaldsApr 26, 2011
  51. 0/6 --dirstat fixes, part 2Johan Herland, Apr 27, 2011
  52. 1/6 Add several testcases for --dirstat and friendsJohan Herland, Apr 27, 2011
  53. 2/6 Make --dirstat=0 output directories that contribute < 0.1% of changesJohan Herland, Apr 27, 2011
  54. 3/6 Refactor --dirstat parsing; deprecate --cumulative and --dirstat-by-fileJohan Herland, Apr 27, 2011
  55. 4/6 Add config variable for specifying default --dirstat behaviorJohan Herland, Apr 27, 2011
  56. 5/6 Use floating point for --dirstat percentagesJohan Herland, Apr 27, 2011
  57. Linus TorvaldsApr 27, 2011
  58. 6/6 New --dirstat=lines mode, doing dirstat analysis based on diffstatJohan Herland, Apr 27, 2011
  59. 0/6 --dirstat fixes, part 2Johan Herland, Apr 27, 2011
  60. 1/6 Add several testcases for --dirstat and friendsJohan Herland, Apr 27, 2011
  61. 2/6 Make --dirstat=0 output directories that contribute < 0.1% of changesJohan Herland, Apr 27, 2011
  62. 3/6 Refactor --dirstat parsing; deprecate --cumulative and --dirstat-by-fileJohan Herland, Apr 27, 2011
  63. 4/6 Add config variable for specifying default --dirstat behaviorJohan Herland, Apr 27, 2011
  64. 5/6 Allow specifying --dirstat cut-off percentage as a floating point numberJohan Herland, Apr 27, 2011
  65. Linus TorvaldsApr 27, 2011
  66. 5/6 Allow specifying --dirstat cut-off percentage as a floating point numberJohan Herland, Apr 27, 2011
  67. 6/6 New --dirstat=lines mode, doing dirstat analysis based on diffstatJohan Herland, Apr 27, 2011
  68. 0/7 --dirstat fixes, part 2Johan Herland, Apr 28, 2011
  69. 1/7 Add several testcases for --dirstat and friendsJohan Herland, Apr 28, 2011
  70. 2/7 Make --dirstat=0 output directories that contribute < 0.1% of changesJohan Herland, Apr 28, 2011
  71. 3/7 Refactor --dirstat parsing; deprecate --cumulative and --dirstat-by-fileJohan Herland, Apr 28, 2011
  72. 4/7 Add config variable for specifying default --dirstat behaviorJohan Herland, Apr 28, 2011
  73. 5/7 Allow specifying --dirstat cut-off percentage as a floating point numberJohan Herland, Apr 28, 2011
  74. 6/7 New --dirstat=lines mode, doing dirstat analysis based on diffstatJohan Herland, Apr 28, 2011
  75. 7/7 Improve error handling when parsing dirstat parametersJohan Herland, Apr 28, 2011
  76. Junio C HamanoApr 28, 2011
  77. Junio C HamanoApr 28, 2011
  78. Johan HerlandApr 28, 2011
  79. Johan HerlandApr 28, 2011
  80. Junio C HamanoApr 29, 2011
  81. 0/8 --dirstat fixes, part 2Johan Herland, Apr 29, 2011
  82. 1/8 Add several testcases for --dirstat and friendsJohan Herland, Apr 29, 2011
  83. 2/8 Make --dirstat=0 output directories that contribute < 0.1% of changesJohan Herland, Apr 29, 2011
  84. 3/8 Refactor --dirstat parsing; deprecate --cumulative and --dirstat-by-fileJohan Herland, Apr 29, 2011
  85. 4/8 Add config variable for specifying default --dirstat behaviorJohan Herland, Apr 29, 2011
  86. 5/8 Allow specifying --dirstat cut-off percentage as a floating point numberJohan Herland, Apr 29, 2011
  87. 6/8 New --dirstat=lines mode, doing dirstat analysis based on diffstatJohan Herland, Apr 29, 2011
  88. 7/8 Improve error handling when parsing dirstat parametersJohan Herland, Apr 29, 2011
  89. 8/8 Mark dirstat error messages for translationJohan Herland, Apr 29, 2011
  90. Junio C HamanoApr 12, 2011
  91. Linus TorvaldsApr 10, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.