diff mbox series

[6/7] xdiff: simplify style assignments

Message ID 20210609192842.696646-7-felipe.contreras@gmail.com (mailing list archive)
State New, archived
Headers show
Series Make diff3 the default conflict style | expand

Commit Message

Felipe Contreras June 9, 2021, 7:28 p.m. UTC
There is little value in checking that git_xmerge_style isn't 0 before
changing it's default value.

Most of the time it isn't 0 anyway, so just assign the value directly.

Also, add the missing constant for the default value: XDL_MERGE_STYLE_MERGE.

Additionally this change has the benefit that it gets rid of a Yoda
condition.

No functional changes.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 builtin/merge-file.c | 3 +--
 ll-merge.c           | 3 +--
 xdiff-interface.c    | 4 ++--
 xdiff/xdiff.h        | 1 +
 4 files changed, 5 insertions(+), 6 deletions(-)

Comments

Phillip Wood June 10, 2021, 9:26 a.m. UTC | #1
On 09/06/2021 20:28, Felipe Contreras wrote:

I don't find the commit message explains this change very well

> There is little value in checking that git_xmerge_style isn't 0 before
> changing it's default value.

I think the check is actually that git_xmerge_style isn't -1. Why is 
there little value in the check?

> Most of the time it isn't 0 anyway, so just assign the value directly.

Why to the times when it is zero (or -1) not matter?

> Also, add the missing constant for the default value: XDL_MERGE_STYLE_MERGE.
> 
> Additionally this change has the benefit that it gets rid of a Yoda
> condition.
> 
> No functional changes.

I think that is probably correct but it would be helpful if the commit 
message offered a bit more explanation.

Best Wishes

Phillip

> 
> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> ---
>   builtin/merge-file.c | 3 +--
>   ll-merge.c           | 3 +--
>   xdiff-interface.c    | 4 ++--
>   xdiff/xdiff.h        | 1 +
>   4 files changed, 5 insertions(+), 6 deletions(-)
> 
> diff --git a/builtin/merge-file.c b/builtin/merge-file.c
> index a4097a596f..01951762ec 100644
> --- a/builtin/merge-file.c
> +++ b/builtin/merge-file.c
> @@ -55,8 +55,7 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
>   	if (startup_info->have_repository) {
>   		/* Read the configuration file */
>   		git_config(git_xmerge_config, NULL);
> -		if (0 <= git_xmerge_style)
> -			xmp.style = git_xmerge_style;
> +		xmp.style = git_xmerge_style;
>   	}
>   
>   	argc = parse_options(argc, argv, prefix, options, merge_file_usage, 0);
> diff --git a/ll-merge.c b/ll-merge.c
> index 9a8a2c365c..4ce8d3f9cc 100644
> --- a/ll-merge.c
> +++ b/ll-merge.c
> @@ -124,8 +124,7 @@ static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
>   	xmp.level = XDL_MERGE_ZEALOUS;
>   	xmp.favor = opts->variant;
>   	xmp.xpp.flags = opts->xdl_opts;
> -	if (git_xmerge_style >= 0)
> -		xmp.style = git_xmerge_style;
> +	xmp.style = git_xmerge_style;
>   	if (marker_size > 0)
>   		xmp.marker_size = marker_size;
>   	xmp.ancestor = orig_name;
> diff --git a/xdiff-interface.c b/xdiff-interface.c
> index 64e2c4e301..19a030fbe2 100644
> --- a/xdiff-interface.c
> +++ b/xdiff-interface.c
> @@ -299,7 +299,7 @@ int xdiff_compare_lines(const char *l1, long s1,
>   	return xdl_recmatch(l1, s1, l2, s2, flags);
>   }
>   
> -int git_xmerge_style = -1;
> +int git_xmerge_style = XDL_MERGE_STYLE_MERGE;
>   
>   int git_xmerge_config(const char *var, const char *value, void *cb)
>   {
> @@ -309,7 +309,7 @@ int git_xmerge_config(const char *var, const char *value, void *cb)
>   		if (!strcmp(value, "diff3"))
>   			git_xmerge_style = XDL_MERGE_STYLE_DIFF3;
>   		else if (!strcmp(value, "merge"))
> -			git_xmerge_style = 0;
> +			git_xmerge_style = XDL_MERGE_STYLE_MERGE;
>   		/*
>   		 * Please update _git_checkout() in
>   		 * git-completion.bash when you add new merge config
> diff --git a/xdiff/xdiff.h b/xdiff/xdiff.h
> index 45883f5eb3..d24cd9f6ae 100644
> --- a/xdiff/xdiff.h
> +++ b/xdiff/xdiff.h
> @@ -64,6 +64,7 @@ extern "C" {
>   #define XDL_MERGE_FAVOR_UNION 3
>   
>   /* merge output styles */
> +#define XDL_MERGE_STYLE_MERGE 0
>   #define XDL_MERGE_STYLE_DIFF3 1
>   
>   typedef struct s_mmfile {
>
Felipe Contreras June 10, 2021, 1:50 p.m. UTC | #2
Phillip Wood wrote:
> On 09/06/2021 20:28, Felipe Contreras wrote:
> 
> I don't find the commit message explains this change very well
> 
> > There is little value in checking that git_xmerge_style isn't 0 before
> > changing it's default value.
> 
> I think the check is actually that git_xmerge_style isn't -1.

Actually I meant "< 0", but yeah, that's mainly to check the -1 case.

> Why is there little value in the check?

It's explained in the very next sentence.

> > Most of the time it isn't 0 anyway, so just assign the value directly.
> 
> Why to the times when it is zero (or -1) not matter?

When it's 0 it's a no-op, and now it can't be -1.

By default structures are zeroed in git, so the defaults of integers
are 0, and in the case of xmparam_t.style that is no exception.

These are all the same:

	static xmparam_t xmp;

	static xmparam_t xmp;
	xmp.style = 0;

	static xmparam_t xmp;
	if (1)
		xmp.style = 0;

But of course as it's explained most of the time that's not what
happens, what happens is:

  if (1)
    xml.style = 1;

Perhaps this is clearer:

  There is little value in checking that git_xmerge_style isn't < 0
  before changing its value to xmp.style. If it's 0 then assigning 0 to
  xmp.style is a no-op, and if it's 1 (as it usually is), we are going
  to assign the value anyway.

  The only exception is when git_xmerge_style is -1, but there is no
  value in having that as default, so we just don't, and set the default
  to 0.

Cheers.
diff mbox series

Patch

diff --git a/builtin/merge-file.c b/builtin/merge-file.c
index a4097a596f..01951762ec 100644
--- a/builtin/merge-file.c
+++ b/builtin/merge-file.c
@@ -55,8 +55,7 @@  int cmd_merge_file(int argc, const char **argv, const char *prefix)
 	if (startup_info->have_repository) {
 		/* Read the configuration file */
 		git_config(git_xmerge_config, NULL);
-		if (0 <= git_xmerge_style)
-			xmp.style = git_xmerge_style;
+		xmp.style = git_xmerge_style;
 	}
 
 	argc = parse_options(argc, argv, prefix, options, merge_file_usage, 0);
diff --git a/ll-merge.c b/ll-merge.c
index 9a8a2c365c..4ce8d3f9cc 100644
--- a/ll-merge.c
+++ b/ll-merge.c
@@ -124,8 +124,7 @@  static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
 	xmp.level = XDL_MERGE_ZEALOUS;
 	xmp.favor = opts->variant;
 	xmp.xpp.flags = opts->xdl_opts;
-	if (git_xmerge_style >= 0)
-		xmp.style = git_xmerge_style;
+	xmp.style = git_xmerge_style;
 	if (marker_size > 0)
 		xmp.marker_size = marker_size;
 	xmp.ancestor = orig_name;
diff --git a/xdiff-interface.c b/xdiff-interface.c
index 64e2c4e301..19a030fbe2 100644
--- a/xdiff-interface.c
+++ b/xdiff-interface.c
@@ -299,7 +299,7 @@  int xdiff_compare_lines(const char *l1, long s1,
 	return xdl_recmatch(l1, s1, l2, s2, flags);
 }
 
-int git_xmerge_style = -1;
+int git_xmerge_style = XDL_MERGE_STYLE_MERGE;
 
 int git_xmerge_config(const char *var, const char *value, void *cb)
 {
@@ -309,7 +309,7 @@  int git_xmerge_config(const char *var, const char *value, void *cb)
 		if (!strcmp(value, "diff3"))
 			git_xmerge_style = XDL_MERGE_STYLE_DIFF3;
 		else if (!strcmp(value, "merge"))
-			git_xmerge_style = 0;
+			git_xmerge_style = XDL_MERGE_STYLE_MERGE;
 		/*
 		 * Please update _git_checkout() in
 		 * git-completion.bash when you add new merge config
diff --git a/xdiff/xdiff.h b/xdiff/xdiff.h
index 45883f5eb3..d24cd9f6ae 100644
--- a/xdiff/xdiff.h
+++ b/xdiff/xdiff.h
@@ -64,6 +64,7 @@  extern "C" {
 #define XDL_MERGE_FAVOR_UNION 3
 
 /* merge output styles */
+#define XDL_MERGE_STYLE_MERGE 0
 #define XDL_MERGE_STYLE_DIFF3 1
 
 typedef struct s_mmfile {