Skip to contentSkip to conversation

checkout --conflict=<style>: recreate merge in a non-default style

PATCH In review Junio C Hamano 2 participants 3 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 checkout --conflict=<style>: recreate merge in a non-default style

This new option does essentially the same thing as -m option when checking unmerged paths out of the index, but it uses the specified style instead of configured merge.conflictstyle.

Setting "merge.conflictstyle" to "diff3" is usually less useful than using the default "merge" style, because the latter allows a conflict that results by both sides changing the same region in a very similar way to get simplified substancially by reducing the common lines. However, when one side removed a group of lines (perhaps a function was moved to some other file) while the other side modified it, the default "merge" style does not give any clue as to why the hunk is left conflicting. You would need the original to understand what is going on.

The recommended use would be not to set merge.conflictstyle variable so that you would usually use the default "merge" style conflict, and when the result in a path in a particular merge is too hard to understand, use "git checkout --conflict=diff3 $path" to check it out with the original to review what is going on.

---
* This comes on top of the previous series (the series got reordered in my tree before it was merged to 'next', but it is essentially the same).

 Documentation/git-checkout.txt |   10 ++++-
 builtin-checkout.c             |   10 ++++-
 t/t7201-co.sh                  |  100 +++++++++++++++++++++------------------
 3 files changed, 72 insertions(+), 48 deletions(-)
Documentation/git-checkout.txt +9 -1
index c884862..13b106d 100644
@@ -9,7 +9,7 @@ SYNOPSIS
 --------
 [verse]
 'git checkout' [-q] [-f] [[--track | --no-track] -b <new_branch> [-l]] [-m] [<branch>]
-'git checkout' [-f|--ours|--theirs|-m] [<tree-ish>] [--] <paths>...
+'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>...
 
 DESCRIPTION
 -----------
@@ -84,6 +84,7 @@ entries; instead, unmerged entries are ignored.
 	based sha1 expressions such as "<branchname>@\{yesterday}".
 
 -m::
+--merge::
 	When switching branches,
 	if you have local modifications to one or more files that
 	are different between the current branch and the branch to
@@ -101,6 +102,13 @@ should result in deletion of the path).
 When checking out paths from the index, this option lets you recreate
 the conflicted merge in the specified paths.
 
+--conflict=<style>::
+	The same as --merge option above, but changes the way the
+	conflicting hunks are presented, overriding the
+	merge.conflictstyle configuration variable.  Possible values are
+	"merge" (default) and "diff3" (in addition to what is shown by
+	"merge" style, shows the original contents).
+
 <new_branch>::
 	Name for the new branch.
 
builtin-checkout.c +9 -1
index b957193..7921432 100644
@@ -580,6 +580,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	const char *arg;
 	struct branch_info new;
 	struct tree *source_tree = NULL;
+	char *conflict_style = NULL;
 	struct option options[] = {
 		OPT__QUIET(&opts.quiet),
 		OPT_STRING('b', NULL, &opts.new_branch, "new branch", "branch"),
@@ -591,7 +592,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 		OPT_SET_INT('3', "theirs", &opts.writeout_stage, "stage",
 			    3),
 		OPT_BOOLEAN('f', NULL, &opts.force, "force"),
-		OPT_BOOLEAN('m', NULL, &opts.merge, "merge"),
+		OPT_BOOLEAN('m', "merge", &opts.merge, "merge"),
+		OPT_STRING(0, "conflict", &conflict_style, "style",
+			   "conflict style (merge or diff3)"),
 		OPT_END(),
 	};
 	int has_dash_dash;
@@ -606,6 +609,11 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, options, checkout_usage,
 			     PARSE_OPT_KEEP_DASHDASH);
 
+	if (conflict_style) {
+		opts.merge = 1; /* implied */
+		git_xmerge_config("merge.conflictstyle", conflict_style, NULL);
+	}
+
 	if (!opts.new_branch && (opts.track != git_branch_track))
 		die("git checkout: --track and --no-track require -b");
 
t/t7201-co.sh +54 -46

140 lines not on this page · raw message

Signature · 1 line
1.6.0.1.260.g7aa8
#1 lore raw

Documentation/git-checkout.txt should result in deletion of the path). · line 111

Quoted 7 lines · Junio C Hamano

+--conflict=<style>::

Documentation/git-checkout.txt +6 -0
+	The same as --merge option above, but changes the way the
+	conflicting hunks are presented, overriding the
+	merge.conflictstyle configuration variable.  Possible values are
+	"merge" (default) and "diff3" (in addition to what is shown by
+	"merge" style, shows the original contents).
+

If --conflict is basically the same as --merge, couldn't we then pass style argument to --merge or omit it to get the default style?

-- Hannes

#2 lore raw
Quoted 2 lines · Johannes Sixt

If --conflict is basically the same as --merge, couldn't we then pass style argument to --merge or omit it to get the default style?

I originally was thinking about doing it that way, but the option parsing would become somewhat hairly.

    $ git checkout --merge=diff3 foo
    $ git checkout --merge diff3 foo
    $ git checkout --merge diff3

The first one is clearly "switch to branch 'foo'; simple 2-way read-tree may find the changes in my working tree conflicting and refuse, so use 3-way merge to resolve such conflicts and express the conflicts in diff3 style".

The third one is "switch to branch 'diff3'; don't barf on conflicting working tree changes; 3-way merge them".

What about the second one? Is it "switch to 'foo', express conflicts in diff3 style", or "checkout path foo from tree-ish diff3"?

It is the former, but that is only because the latter is an error (-m is defined only for checking things out of the index). That is not something parse-options can easily/cleanly do.