Subject: [RFC/PATCH jn/merge-renormalize] merge-recursive: expose merge options for builtin merge

From: Jonathan Nieder <[email-masked]>
Date: Mon, 23 Aug 2010 22:39:22 -0500

There are two very similar blocks of code that recognize options for
the "recursive" merge strategy.  Unify them.

No functional change intended.

Cc: Avery Pennarun <[email-masked]>
Signed-off-by: Jonathan Nieder <[email-masked]>
---
Jonathan Nieder wrote:
> Justin Frankel wrote:

>> --- a/builtin/merge.c
>> +++ b/builtin/merge.c
>> @@ -647,6 +648,14 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
>>  				o.renormalize = 1;
>>  			else if (!strcmp(xopts[x], "no-renormalize"))
>>  				o.renormalize = 0;
>> +			else if (!strcmp(xopts[x], "ignore-all-space"))
>> +				o.xdl_opts |= XDF_IGNORE_WHITESPACE;
[...]
> It's tempting to fix this code duplication once and for all.

 builtin/merge-recursive.c |   14 +-------------
 builtin/merge.c           |   20 ++------------------
 merge-recursive.c         |   21 +++++++++++++++++++++
 merge-recursive.h         |    2 ++
 4 files changed, 26 insertions(+), 31 deletions(-)

diff --git a/builtin/merge-recursive.c b/builtin/merge-recursive.c
index c2d4677..a610b68 100644
--- a/builtin/merge-recursive.c
+++ b/builtin/merge-recursive.c
@@ -37,19 +37,7 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
 		if (!prefixcmp(arg, "--")) {
 			if (!arg[2])
 				break;
-			if (!strcmp(arg+2, "ours"))
-				o.recursive_variant = MERGE_RECURSIVE_OURS;
-			else if (!strcmp(arg+2, "theirs"))
-				o.recursive_variant = MERGE_RECURSIVE_THEIRS;
-			else if (!strcmp(arg+2, "subtree"))
-				o.subtree_shift = "";
-			else if (!prefixcmp(arg+2, "subtree="))
-				o.subtree_shift = arg + 10;
-			else if (!strcmp(arg+2, "renormalize"))
-				o.renormalize = 1;
-			else if (!strcmp(arg+2, "no-renormalize"))
-				o.renormalize = 0;
-			else
+			if (parse_merge_opt(&o, arg + 2) <= 0)
 				die("Unknown option %s", arg);
 			continue;
 		}
diff --git a/builtin/merge.c b/builtin/merge.c
index 037cd47..8dd81bf 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -629,25 +629,9 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
 
 		o.renormalize = option_renormalize;
 
-		/*
-		 * NEEDSWORK: merge with table in builtin/merge-recursive
-		 */
-		for (x = 0; x < xopts_nr; x++) {
-			if (!strcmp(xopts[x], "ours"))
-				o.recursive_variant = MERGE_RECURSIVE_OURS;
-			else if (!strcmp(xopts[x], "theirs"))
-				o.recursive_variant = MERGE_RECURSIVE_THEIRS;
-			else if (!strcmp(xopts[x], "subtree"))
-				o.subtree_shift = "";
-			else if (!prefixcmp(xopts[x], "subtree="))
-				o.subtree_shift = xopts[x]+8;
-			else if (!strcmp(xopts[x], "renormalize"))
-				o.renormalize = 1;
-			else if (!strcmp(xopts[x], "no-renormalize"))
-				o.renormalize = 0;
-			else
+		for (x = 0; x < xopts_nr; x++)
+			if (parse_merge_opt(&o, xopts[x]) <= 0)
 				die("Unknown option for merge-recursive: -X%s", xopts[x]);
-		}
 
 		o.branch1 = head_arg;
 		o.branch2 = remoteheads->item->util;
diff --git a/merge-recursive.c b/merge-recursive.c
index ee52581..063d623 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1499,3 +1499,24 @@ void init_merge_options(struct merge_options *o)
 	memset(&o->current_directory_set, 0, sizeof(struct string_list));
 	o->current_directory_set.strdup_strings = 1;
 }
+
+int parse_merge_opt(struct merge_options *o, const char *s)
+{
+	if (!s || !*s)
+		return 0;
+	if (!strcmp(s, "ours"))
+		o->recursive_variant = MERGE_RECURSIVE_OURS;
+	else if (!strcmp(s, "theirs"))
+		o->recursive_variant = MERGE_RECURSIVE_THEIRS;
+	else if (!strcmp(s, "subtree"))
+		o->subtree_shift = "";
+	else if (!prefixcmp(s, "subtree="))
+		o->subtree_shift = s + strlen("subtree=");
+	else if (!strcmp(s, "renormalize"))
+		o->renormalize = 1;
+	else if (!strcmp(s, "no-renormalize"))
+		o->renormalize = 0;
+	else
+		return 0;
+	return 1;
+}
diff --git a/merge-recursive.h b/merge-recursive.h
index c5fbe79..37ff99a 100644
--- a/merge-recursive.h
+++ b/merge-recursive.h
@@ -55,6 +55,8 @@ int merge_recursive_generic(struct merge_options *o,
 void init_merge_options(struct merge_options *o);
 struct tree *write_tree_from_memory(struct merge_options *o);
 
+int parse_merge_opt(struct merge_options *out, const char *s);
+
 /* builtin/merge.c */
 int try_merge_command(const char *strategy, struct commit_list *common, const char *head_arg, struct commit_list *remotes);
 
-- 
1.7.2.2