diff mbox series

[2/3] diff-merges: cleanup set_diff_merges()

Message ID 20220914193102.5275-3-sorganov@gmail.com (mailing list archive)
State New, archived
Headers show
Series diff-merges: minor cleanups | expand

Commit Message

Sergey Organov Sept. 14, 2022, 7:31 p.m. UTC
Get rid of special-casing of 'suppress' in set_diff_merges(). Instead
set 'merges_need_diff' flag correctly in every option handling
function.

Signed-off-by: Sergey Organov <sorganov@gmail.com>
---
 diff-merges.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)

Comments

Junio C Hamano Sept. 15, 2022, 8:41 p.m. UTC | #1
Sergey Organov <sorganov@gmail.com> writes:

> Get rid of special-casing of 'suppress' in set_diff_merges(). Instead
> set 'merges_need_diff' flag correctly in every option handling
> function.
>
> Signed-off-by: Sergey Organov <sorganov@gmail.com>
> ---
>  diff-merges.c | 30 +++++++++++++++++++-----------
>  1 file changed, 19 insertions(+), 11 deletions(-)

Looks OK to me.

Everybody else says set_X() but set_none() has nothing to do ther
than calling suppress(), so the change does not really make a
functional difference (as you said in the cover letter).

Is the idea that the original value in .merges_need_diff member does
not matter because in every case it is set to either 0 or 1?  Most
cases call common_setup() to set it to 1 like this here...

> +static void common_setup(struct rev_info *revs)
> +{
> +	suppress(revs);
> +	revs->merges_need_diff = 1;
> +}

... but this does not touch (in other words, it does not explicitly
clear) it, ...

> +static void set_none(struct rev_info *revs)
> +{
> +	suppress(revs);
> +}

 ... so we still rely on somebody to set the .merges_need_diff
to 0 initially, right?

> -
> -	/* NOTE: the merges_need_diff flag is cleared by func() call */
> -	if (func != suppress)
> -		revs->merges_need_diff = 1;
>  }

It is very good to see this one go.

>  /*
> @@ -115,6 +122,7 @@ int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
>  
>  	if (!suppress_m_parsing && !strcmp(arg, "-m")) {
>  		set_to_default(revs);
> +		revs->merges_need_diff = 0;

I am wondering how this becomes necessary?  Is it because
set_to_default() would flip the member to 1 unconditionally, or
something?  If it weren't for this hunk, the lossage of the previous
hunk is a very good clean-up, but if we need to do this, I cannot
shake the feeling that we mostly shifted the dirt around, without
really cleaning it?  I dunno.

> @@ -125,7 +133,7 @@ int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
>  		set_remerge_diff(revs);
>  		revs->merges_imply_patch = 1;
>  	} else if (!strcmp(arg, "--no-diff-merges")) {
> -		suppress(revs);
> +		set_none(revs);

We do not need to explicitly set .merges_need_diff to 0 here,
presumably because it is initialized to 0 and nobody touched it,
right?

>  	} else if (!strcmp(arg, "--combined-all-paths")) {
>  		revs->combined_all_paths = 1;
>  	} else if ((argcount = parse_long_opt("diff-merges", argv, &optarg))) {
> @@ -139,7 +147,7 @@ int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
>  
>  void diff_merges_suppress(struct rev_info *revs)
>  {
> -	suppress(revs);
> +	set_none(revs);
>  }
>  
>  void diff_merges_default_to_first_parent(struct rev_info *revs)
Sergey Organov Sept. 16, 2022, 1:38 p.m. UTC | #2
Junio C Hamano <gitster@pobox.com> writes:

> Sergey Organov <sorganov@gmail.com> writes:
>
>> Get rid of special-casing of 'suppress' in set_diff_merges(). Instead
>> set 'merges_need_diff' flag correctly in every option handling
>> function.
>>
>> Signed-off-by: Sergey Organov <sorganov@gmail.com>
>> ---
>>  diff-merges.c | 30 +++++++++++++++++++-----------
>>  1 file changed, 19 insertions(+), 11 deletions(-)
>
> Looks OK to me.
>
> Everybody else says set_X() but set_none() has nothing to do ther
> than calling suppress(), so the change does not really make a
> functional difference (as you said in the cover letter).
>
> Is the idea that the original value in .merges_need_diff member does
> not matter because in every case it is set to either 0 or 1?  Most
> cases call common_setup() to set it to 1 like this here...

Yes, exactly.

>
>> +static void common_setup(struct rev_info *revs)
>> +{
>> +	suppress(revs);
>> +	revs->merges_need_diff = 1;
>> +}
>
> ... but this does not touch (in other words, it does not explicitly
> clear) it, ...

>
>> +static void set_none(struct rev_info *revs)
>> +{
>> +	suppress(revs);
>> +}
>
>  ... so we still rely on somebody to set the .merges_need_diff
> to 0 initially, right?

No, the suppress() does clear everything, .merges_need_diff included.
The suppress() ensures every next diff-merges option on the command-line
actually overwrites and correctly sets everything.

>
>> -
>> -	/* NOTE: the merges_need_diff flag is cleared by func() call */
>> -	if (func != suppress)
>> -		revs->merges_need_diff = 1;
>>  }
>
> It is very good to see this one go.

Yep, that was a kludge that bothered me for a while indeed.

>
>>  /*
>> @@ -115,6 +122,7 @@ int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
>>  
>>  	if (!suppress_m_parsing && !strcmp(arg, "-m")) {
>>  		set_to_default(revs);
>> +		revs->merges_need_diff = 0;
>
> I am wondering how this becomes necessary?  Is it because
> set_to_default() would flip the member to 1 unconditionally, or
> something?

Yes, as all "native" -diff-merge= options set this bit to 1.

> If it weren't for this hunk, the lossage of the previous
> hunk is a very good clean-up, but if we need to do this, I cannot
> shake the feeling that we mostly shifted the dirt around, without
> really cleaning it?  I dunno.

Yes, I believe it does cleanup things in both these places.

The "-m" option indeed differs from the rest of the options in exactly
this thing: it wants .merges_need_diff=0 to suppress output unless '-p'
is given as well.

The -c/--cc are in fact similar, but they don't need this line as they
imply '-p' that in turn ignores .merges_need_diff, and generates output
anyway, so -m code has:

  revs->merges_need_diff = 0;

whereas -c and --cc code both have:

  revs->merges_imply_patch = 1;

Thanks,
-- Sergey Organov

>
>> @@ -125,7 +133,7 @@ int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
>>  		set_remerge_diff(revs);
>>  		revs->merges_imply_patch = 1;
>>  	} else if (!strcmp(arg, "--no-diff-merges")) {
>> -		suppress(revs);
>> +		set_none(revs);
>
> We do not need to explicitly set .merges_need_diff to 0 here,
> presumably because it is initialized to 0 and nobody touched it,
> right?

No, we rather do set it to 0 here, in suppress() called from set_none().

Thanks,
-- Sergey Organov
diff mbox series

Patch

diff --git a/diff-merges.c b/diff-merges.c
index 780ed08fc87f..85cbefa5afd7 100644
--- a/diff-merges.c
+++ b/diff-merges.c
@@ -20,9 +20,20 @@  static void suppress(struct rev_info *revs)
 	revs->remerge_diff = 0;
 }
 
+static void common_setup(struct rev_info *revs)
+{
+	suppress(revs);
+	revs->merges_need_diff = 1;
+}
+
+static void set_none(struct rev_info *revs)
+{
+	suppress(revs);
+}
+
 static void set_separate(struct rev_info *revs)
 {
-	suppress(revs);
+	common_setup(revs);
 	revs->separate_merges = 1;
 	revs->simplify_history = 0;
 }
@@ -35,21 +46,21 @@  static void set_first_parent(struct rev_info *revs)
 
 static void set_combined(struct rev_info *revs)
 {
-	suppress(revs);
+	common_setup(revs);
 	revs->combine_merges = 1;
 	revs->dense_combined_merges = 0;
 }
 
 static void set_dense_combined(struct rev_info *revs)
 {
-	suppress(revs);
+	common_setup(revs);
 	revs->combine_merges = 1;
 	revs->dense_combined_merges = 1;
 }
 
 static void set_remerge_diff(struct rev_info *revs)
 {
-	suppress(revs);
+	common_setup(revs);
 	revs->remerge_diff = 1;
 	revs->simplify_history = 0;
 }
@@ -57,7 +68,7 @@  static void set_remerge_diff(struct rev_info *revs)
 static diff_merges_setup_func_t func_by_opt(const char *optarg)
 {
 	if (!strcmp(optarg, "off") || !strcmp(optarg, "none"))
-		return suppress;
+		return set_none;
 	if (!strcmp(optarg, "1") || !strcmp(optarg, "first-parent"))
 		return set_first_parent;
 	if (!strcmp(optarg, "separate"))
@@ -81,10 +92,6 @@  static void set_diff_merges(struct rev_info *revs, const char *optarg)
 		die(_("invalid value for '%s': '%s'"), "--diff-merges", optarg);
 
 	func(revs);
-
-	/* NOTE: the merges_need_diff flag is cleared by func() call */
-	if (func != suppress)
-		revs->merges_need_diff = 1;
 }
 
 /*
@@ -115,6 +122,7 @@  int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
 
 	if (!suppress_m_parsing && !strcmp(arg, "-m")) {
 		set_to_default(revs);
+		revs->merges_need_diff = 0;
 	} else if (!strcmp(arg, "-c")) {
 		set_combined(revs);
 		revs->merges_imply_patch = 1;
@@ -125,7 +133,7 @@  int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
 		set_remerge_diff(revs);
 		revs->merges_imply_patch = 1;
 	} else if (!strcmp(arg, "--no-diff-merges")) {
-		suppress(revs);
+		set_none(revs);
 	} else if (!strcmp(arg, "--combined-all-paths")) {
 		revs->combined_all_paths = 1;
 	} else if ((argcount = parse_long_opt("diff-merges", argv, &optarg))) {
@@ -139,7 +147,7 @@  int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
 
 void diff_merges_suppress(struct rev_info *revs)
 {
-	suppress(revs);
+	set_none(revs);
 }
 
 void diff_merges_default_to_first_parent(struct rev_info *revs)