From 7457014be5d095aac55cabd24c82bf0e42641f3b Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Thu, 14 Mar 2024 17:05:03 +0000 Subject: [PATCH 1/5] xdiff-interface: refactor parsing of merge.conflictstyle Factor out the code that parses of conflict style name so it can be reused in a later commit that wants to parse the name given on the command line. Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano --- xdiff-interface.c | 29 ++++++++++++++++++----------- xdiff-interface.h | 1 + 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/xdiff-interface.c b/xdiff-interface.c index 3162f51743..16ed8ac492 100644 --- a/xdiff-interface.c +++ b/xdiff-interface.c @@ -305,6 +305,22 @@ int xdiff_compare_lines(const char *l1, long s1, return xdl_recmatch(l1, s1, l2, s2, flags); } +int parse_conflict_style_name(const char *value) +{ + if (!strcmp(value, "diff3")) + return XDL_MERGE_DIFF3; + else if (!strcmp(value, "zdiff3")) + return XDL_MERGE_ZEALOUS_DIFF3; + else if (!strcmp(value, "merge")) + return 0; + /* + * Please update _git_checkout() in git-completion.bash when + * you add new merge config + */ + else + return -1; +} + int git_xmerge_style = -1; int git_xmerge_config(const char *var, const char *value, @@ -313,17 +329,8 @@ int git_xmerge_config(const char *var, const char *value, if (!strcmp(var, "merge.conflictstyle")) { if (!value) return config_error_nonbool(var); - if (!strcmp(value, "diff3")) - git_xmerge_style = XDL_MERGE_DIFF3; - else if (!strcmp(value, "zdiff3")) - git_xmerge_style = XDL_MERGE_ZEALOUS_DIFF3; - else if (!strcmp(value, "merge")) - git_xmerge_style = 0; - /* - * Please update _git_checkout() in - * git-completion.bash when you add new merge config - */ - else + git_xmerge_style = parse_conflict_style_name(value); + if (git_xmerge_style == -1) return error(_("unknown style '%s' given for '%s'"), value, var); return 0; diff --git a/xdiff-interface.h b/xdiff-interface.h index e6f80df046..38537169b7 100644 --- a/xdiff-interface.h +++ b/xdiff-interface.h @@ -51,6 +51,7 @@ int buffer_is_binary(const char *ptr, unsigned long size); void xdiff_set_find_func(xdemitconf_t *xecfg, const char *line, int cflags); void xdiff_clear_find_func(xdemitconf_t *xecfg); struct config_context; +int parse_conflict_style_name(const char *value); int git_xmerge_config(const char *var, const char *value, const struct config_context *ctx, void *cb); extern int git_xmerge_style; From 412aff7b3379b182e3331ec66c0657ae4e39d576 Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Thu, 14 Mar 2024 17:05:04 +0000 Subject: [PATCH 2/5] merge-ll: introduce LL_MERGE_OPTIONS_INIT Introduce a macro to initialize `struct ll_merge_options` in preparation for the next commit that will add a new member that needs to be initialized to a non-zero value. Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano --- builtin/checkout.c | 3 +-- merge-ll.c | 2 +- merge-ll.h | 2 ++ merge-ort.c | 2 +- merge-recursive.c | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/builtin/checkout.c b/builtin/checkout.c index a6e30931b5..e9859a1980 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -262,7 +262,7 @@ static int checkout_merged(int pos, const struct checkout *state, mmbuffer_t result_buf; struct object_id threeway[3]; unsigned mode = 0; - struct ll_merge_options ll_opts; + struct ll_merge_options ll_opts = LL_MERGE_OPTIONS_INIT; int renormalize = 0; memset(threeway, 0, sizeof(threeway)); @@ -284,7 +284,6 @@ static int checkout_merged(int pos, const struct checkout *state, read_mmblob(&ours, &threeway[1]); read_mmblob(&theirs, &threeway[2]); - memset(&ll_opts, 0, sizeof(ll_opts)); git_config_get_bool("merge.renormalize", &renormalize); ll_opts.renormalize = renormalize; merge_status = ll_merge(&result_buf, path, &ancestor, "base", diff --git a/merge-ll.c b/merge-ll.c index 5ffb045efb..ef200e4497 100644 --- a/merge-ll.c +++ b/merge-ll.c @@ -401,7 +401,7 @@ enum ll_merge_result ll_merge(mmbuffer_t *result_buf, const struct ll_merge_options *opts) { struct attr_check *check = load_merge_attributes(); - static const struct ll_merge_options default_opts; + static const struct ll_merge_options default_opts = LL_MERGE_OPTIONS_INIT; const char *ll_driver_name = NULL; int marker_size = DEFAULT_CONFLICT_MARKER_SIZE; const struct ll_merge_driver *driver; diff --git a/merge-ll.h b/merge-ll.h index e4a20e81a3..af1ee36abd 100644 --- a/merge-ll.h +++ b/merge-ll.h @@ -82,6 +82,8 @@ struct ll_merge_options { long xdl_opts; }; +#define LL_MERGE_OPTIONS_INIT {0} + enum ll_merge_result { LL_MERGE_ERROR = -1, LL_MERGE_OK = 0, diff --git a/merge-ort.c b/merge-ort.c index 8617babee4..4a02c3ecd9 100644 --- a/merge-ort.c +++ b/merge-ort.c @@ -1956,7 +1956,7 @@ static int merge_3way(struct merge_options *opt, mmbuffer_t *result_buf) { mmfile_t orig, src1, src2; - struct ll_merge_options ll_opts = {0}; + struct ll_merge_options ll_opts = LL_MERGE_OPTIONS_INIT; char *base, *name1, *name2; enum ll_merge_result merge_status; diff --git a/merge-recursive.c b/merge-recursive.c index a0c3e7a2d9..02b7b584f9 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1047,7 +1047,7 @@ static int merge_3way(struct merge_options *opt, const int extra_marker_size) { mmfile_t orig, src1, src2; - struct ll_merge_options ll_opts = {0}; + struct ll_merge_options ll_opts = LL_MERGE_OPTIONS_INIT; char *base, *name1, *name2; enum ll_merge_result merge_status; From 135cc712c39fa7ccf25b9b2b55d1d07fc85c85a4 Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Thu, 14 Mar 2024 17:05:05 +0000 Subject: [PATCH 3/5] merge options: add a conflict style member Add a conflict_style member to `struct merge_options` and `struct ll_merge_options` to allow callers to override the default conflict style. This will be used in the next commit. Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano --- merge-ll.c | 4 +++- merge-ll.h | 5 ++++- merge-ort.c | 1 + merge-recursive.c | 3 +++ merge-recursive.h | 1 + 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/merge-ll.c b/merge-ll.c index ef200e4497..34514e8bc6 100644 --- a/merge-ll.c +++ b/merge-ll.c @@ -128,7 +128,9 @@ static enum ll_merge_result ll_xdl_merge(const struct ll_merge_driver *drv_unuse xmp.level = XDL_MERGE_ZEALOUS; xmp.favor = opts->variant; xmp.xpp.flags = opts->xdl_opts; - if (git_xmerge_style >= 0) + if (opts->conflict_style >= 0) + xmp.style = opts->conflict_style; + else if (git_xmerge_style >= 0) xmp.style = git_xmerge_style; if (marker_size > 0) xmp.marker_size = marker_size; diff --git a/merge-ll.h b/merge-ll.h index af1ee36abd..d038ee0c1e 100644 --- a/merge-ll.h +++ b/merge-ll.h @@ -78,11 +78,14 @@ struct ll_merge_options { */ unsigned extra_marker_size; + /* Override the global conflict style. */ + int conflict_style; + /* Extra xpparam_t flags as defined in xdiff/xdiff.h. */ long xdl_opts; }; -#define LL_MERGE_OPTIONS_INIT {0} +#define LL_MERGE_OPTIONS_INIT { .conflict_style = -1 } enum ll_merge_result { LL_MERGE_ERROR = -1, diff --git a/merge-ort.c b/merge-ort.c index 4a02c3ecd9..a9ab403145 100644 --- a/merge-ort.c +++ b/merge-ort.c @@ -1966,6 +1966,7 @@ static int merge_3way(struct merge_options *opt, ll_opts.renormalize = opt->renormalize; ll_opts.extra_marker_size = extra_marker_size; ll_opts.xdl_opts = opt->xdl_opts; + ll_opts.conflict_style = opt->conflict_style; if (opt->priv->call_depth) { ll_opts.virtual_ancestor = 1; diff --git a/merge-recursive.c b/merge-recursive.c index 02b7b584f9..33b5f9384e 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1054,6 +1054,7 @@ static int merge_3way(struct merge_options *opt, ll_opts.renormalize = opt->renormalize; ll_opts.extra_marker_size = extra_marker_size; ll_opts.xdl_opts = opt->xdl_opts; + ll_opts.conflict_style = opt->conflict_style; if (opt->priv->call_depth) { ll_opts.virtual_ancestor = 1; @@ -3899,6 +3900,8 @@ void init_merge_options(struct merge_options *opt, opt->renormalize = 0; + opt->conflict_style = -1; + merge_recursive_config(opt); merge_verbosity = getenv("GIT_MERGE_VERBOSITY"); if (merge_verbosity) diff --git a/merge-recursive.h b/merge-recursive.h index 3d3b3e3c29..e67d38c303 100644 --- a/merge-recursive.h +++ b/merge-recursive.h @@ -31,6 +31,7 @@ struct merge_options { /* xdiff-related options (patience, ignore whitespace, ours/theirs) */ long xdl_opts; + int conflict_style; enum { MERGE_VARIANT_NORMAL = 0, MERGE_VARIANT_OURS, From dbeaf8e8c0ada5ba6a8c379e2ad7bab7e815ba79 Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Thu, 14 Mar 2024 17:05:06 +0000 Subject: [PATCH 4/5] checkout: cleanup --conflict=