Skip to contentSkip to conversation

Add git-unresolve <paths>...

RFC/PATCH New Junio C Hamano 2 participants 11 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

This is an attempt to address the issue raised on #git channel recently by Carl Worth.

After a conflicted automerge, "git diff" shows a combined diff to give you how the tentative automerge result differs from what came from each branch. During a complex merge, it is tempting to be able to resolve a few paths at a time, mark them "I've dealt with them" with git-update-index to unclutter the next "git diff" output, and keep going. However, when the final result does not compile or otherwise found to be a mismerge, the workflow to fix the mismerged paths suddenly changes to "git diff HEAD -- path" (to get a diff from our HEAD before merging) and "git diff MERGE_HEAD -- path" (to get a diff from theirs), and it cannot show the combined anymore.

With git-unresolve <paths>..., the versions from our branch and their branch for specified blobs are placed in stage #2 and stage #3, without touching the working tree files. This gives you the combined diff back for easier review, along with "diff --ours" and "diff --theirs".

One thing it does not do is to place the base in stage #1; this means "diff --base" would behave differently between the run immediately after a conflicted three-way merge, and the run after an update-index by mistake followed by a git-unresolve.

We could theoretically run merge-base between HEAD and MERGE_HEAD to find which tree to place in stage #1, but reviewing "diff --base" is not that useful so....

---
* comes on top of the previous two "extended sha1" cleanups.

 .gitignore  |    1 
 Makefile    |    3 +
 cache.h     |    1 
 sha1_name.c |    6 +-
 unresolve.c |  145 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 151 insertions(+), 5 deletions(-)
.gitignore +1 -0
index b5959d6..1e4ba7b 100644
@@ -111,6 +111,7 @@ git-tag
 git-tar-tree
 git-unpack-file
 git-unpack-objects
+git-unresolve
 git-update-index
 git-update-ref
 git-update-server-info
Makefile +2 -1
index 8aed3af..85938c0 100644
@@ -165,7 +165,8 @@ PROGRAMS = \
 	git-upload-pack$X git-verify-pack$X git-write-tree$X \
 	git-update-ref$X git-symbolic-ref$X git-check-ref-format$X \
 	git-name-rev$X git-pack-redundant$X git-repo-config$X git-var$X \
-	git-describe$X git-merge-tree$X git-blame$X git-imap-send$X
+	git-describe$X git-merge-tree$X git-blame$X git-imap-send$X \
+	git-unresolve$X
 
 BUILT_INS = git-log$X
 
cache.h +1 -0
index 69801b0..a5f1eb3 100644
@@ -235,6 +235,7 @@ #define DEFAULT_ABBREV 7
 extern int get_sha1(const char *str, unsigned char *sha1);
 extern int get_sha1_hex(const char *hex, unsigned char *sha1);
 extern char *sha1_to_hex(const unsigned char *sha1);	/* static buffer result! */
+extern int get_tree_entry(const unsigned char *, const char *, unsigned char *, unsigned *);
 extern int read_ref(const char *filename, unsigned char *sha1);
 extern const char *resolve_ref(const char *path, unsigned char *sha1, int);
 extern int create_symref(const char *git_HEAD, const char *refs_heads_master);
sha1_name.c +2 -4
index 7ad20b5..68b1275 100644
@@ -450,8 +450,6 @@ static int get_sha1_1(const char *name, 
 	return get_short_sha1(name, len, sha1, 0);
 }
 
-static int get_tree_entry(const unsigned char *, const char *, unsigned char *, unsigned *);
-
 static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char *result, unsigned *mode)
 {
 	int namelen = strlen(name);
@@ -487,13 +485,13 @@ static int find_tree_entry(struct tree_d
 	return -1;
 }
 
-static int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned char *sha1, unsigned *mode)
+int get_tree_entry(const unsigned char *ent_sha1, const char *name, unsigned char *sha1, unsigned *mode)
 {
 	int retval;
 	void *tree;
 	struct tree_desc t;
 
-	tree = read_object_with_reference(tree_sha1, tree_type, &t.size, NULL);
+	tree = read_object_with_reference(ent_sha1, tree_type, &t.size, NULL);
 	if (!tree)
 		return -1;
 	t.buf = tree;
unresolve.c new file +145 -0

145 lines not on this page · raw message

Signature · 1 line
1.3.0.g2c4a
#1 lore raw
Quoted 3 lines · Junio C Hamano

This is an attempt to address the issue raised on #git channel recently by Carl Worth.

...

Quoted 5 lines

With git-unresolve <paths>..., the versions from our branch and their branch for specified blobs are placed in stage #2 and stage #3, without touching the working tree files. This gives you the combined diff back for easier review, along with "diff --ours" and "diff --theirs".

Thanks. This looks quite interesting.

The name of git-unresolve seems reasonable on its own. In fact, git-merge already uses "resolve" to describe manual conflict resolution. For example:

echo "Using the $best_strategy to prepare resolving by hand."

But this does introduce an unfortunate semantic clash with the existing git-resolve, (which is an automated merge tool of some sort). I don't know much about the existing git-resolve, but a recent thread suggests it is a non-useful relic and people shouldn't be using it.

It would be nice if the complementary operations of manually resolving and unresolving a merge conflict had complementary command names. Would it be feasible to cop-opt the git-resolve name as a synonym for update-index? (I certainly wouldn't mind a shorter name for that operation.)

In fact, git-resolve could even go beyond being a synonym for update-index. For example, it might also complain if it notices conflict markers in the file. That seems like it would be a useful convenience, (and no functionality would be robbed, since update-index would still be available for anyone who needs to commit content that looks like conflict markers).

Another thing I've been meaning to suggest soon is better output for when an automatic merge fails. Currently, the final message is:

Automatic merge failed; fix up by hand

That's not much guidance for a new user that perhaps is only used to "git commit -a" and "git pull" that usually works. Without getting too verbose, this might be improved with something like:

Automatic merge failed; fix conflicts by hand, then commit the result

In addition, if git-commit is run with unmerged files, it could also provide some extra guidance. Perhaps something like this, (just after the list of "needs merge" files mentioned by git-update-index):

Fix any conflicts in these files, then git-resolve each and commit again

-Carl

#2 lore raw
Quoted 4 lines · Carl Worth

But this does introduce an unfortunate semantic clash with the existing git-resolve, (which is an automated merge tool of some sort). I don't know much about the existing git-resolve, but a recent thread suggests it is a non-useful relic and people shouldn't be using it.

It is useful in a quick-and-dirty way, but does the same thing as 'git merge -s resolve' and that is why people discussed about removing it. It has semantics quite different from update-index, so I'd ignore the synonym part of your discussion.

Time for a quick raise-hand. Does anybody still use 'git resolve'? Maybe we could remove it by 1.4?

Quoted 3 lines

It would be nice if the complementary operations of manually resolving and unresolving a merge conflict had complementary command names.

True. I considered two other possibilities.

* "git unmerge", because it creates unmerged index entries, and

* "git update-index --unmerge", because this is just a special kind of updates to the index file.

Quoted 3 lines

... For example, it might also complain if it notices conflict markers in the file. That seems like it would be a useful convenience.

Since you should compile test the merge result before going anywhere, and the primary target of git is to manage sources, it might not matter in most of the case, but for non-sources and non compiled languages, that certainly is an issue.

When it _does_ matter, you can have a customized pre-commit hook to look for the conflict markers, like this:

templates/hooks--pre-commit +3 -0
index 43d3b6e..723a9ef 100644
@@ -61,6 +61,9 @@ perl -e '
 	    if (/^\s* 	/) {
 		bad_line("indent SP followed by a TAB", $_);
 	    }
+	    if (/^(?:[<>=]){7}/) {
+		bad_line("unresolved merge conflict", $_);
+	    }
 	}
     }
     exit($found_bad);

As usual, you can defeat the check with "git commit --no-verify" for unlikely false matches.

Quoted 5 lines

That's not much guidance for a new user that perhaps is only used to "git commit -a" and "git pull" that usually works. Without getting too verbose, this might be improved with something like:

Automatic merge failed; fix conflicts by hand, then commit the result

That should be an easy patch ;-).

git-merge.sh +1 -1
index 78ab422..b834e79 100755
@@ -335,5 +335,5 @@ Conflicts:
 	then
 		git-rerere
 	fi
-	die "Automatic merge failed; fix up by hand"
+	die "Automatic merge failed; fix conflicts and then commit the result."
 fi
#3 lore raw

PATCH 1/1 pre-commit hook: complain about conflict markers.

Several <<< or === or >>> characters at the beginning of a line is very likely to be leftover conflict markers from a failed automerge the user resolved incorrectly, so detect them.

As usual, this can be defeated with "git commit --no-verify" if you really do want to have those files, just like changes that introduce trailing whitespaces.

---

 templates/hooks--pre-commit |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)

6be63d37efc766756922a9b96ed8fe7a332f133a

templates/hooks--pre-commit +3 -0
index 43d3b6e..723a9ef 100644
@@ -61,6 +61,9 @@ perl -e '
 	    if (/^\s* 	/) {
 		bad_line("indent SP followed by a TAB", $_);
 	    }
+	    if (/^(?:[<>=]){7}/) {
+		bad_line("unresolved merge conflict", $_);
+	    }
 	}
     }
     exit($found_bad);
Signature · 1 line
1.3.0.g2c4a
#4 lore raw

PATCH 1/1 git-merge: a bit more readable user guidance.

In reply to Junio C Hamano

We said "fix up by hand" after failed automerge, which was a big "Huh? Now what?". Be a bit more explicit without being too verbose. Suggested by Carl Worth.

---

 git-merge.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

aec8e89ca3d38f9d0d4d287d874e8b3859d68787

git-merge.sh +1 -1
index 78ab422..b834e79 100755
@@ -335,5 +335,5 @@ Conflicts:
 	then
 		git-rerere
 	fi
-	die "Automatic merge failed; fix up by hand"
+	die "Automatic merge failed; fix conflicts and then commit the result."
 fi
Signature · 1 line
1.3.0.g2c4a
#5 lore raw
In reply to Junio C Hamano
Quoted 8 lines · Junio C Hamano
Quoted 4 lines

It would be nice if the complementary operations of manually resolving and unresolving a merge conflict had complementary command names.

True. I considered two other possibilities.

* "git unmerge", because it creates unmerged index entries, and

This has the same problem as git-unresolve as compared to the old git-resolve. Namely, it would provide an "unmerge" command that syntactically looks like a complement to "merge" but in fact is an entirely separate, (operating on different object types, etc.)

Quoted 2 lines

* "git update-index --unmerge", because this is just a special kind of updates to the index file.

That seems like a very reasonable place to have this functionality (if needed). Compared to things like "update-index --add" and "update-index --remove" the desire for this operation is likely much more rare, so perhaps doesn't merit its own command.

Meanwhile, I still think it's worth re-considering the original problem.

After a failed merge, I get a multi-parent diff from "git diff". However, after updating the index, I can't find any way to get multi-parent diffs anymore.

I'd still like to be able to do that, even when I know that what I have in the index is good, and I don't want to undo it. So the proposed unresolve (or update-index --unmerge, or whatever) is still not totally satisfactory.

In this state, "git commit" is still multi-parent aware and will be making a merged commit, (by examining .git/MERGE_HEAD). Wouldn't it be reasonable to make "git diff --cached" also be aware of this mid-merge state and display a multi-parent diff? at least as an option?

-Carl

PS. As a more bizarre suggestion, I've been idly wondering for some time if it would make sense to provide names to allow the working tree and the index to be treated as pseudo, in-progress objects. For example, much of my initial (and occasionally residual) confusion regarding "git diff" would be alleviated if I could run commands something like:

git diff HEAD TREE
git diff HEAD INDEX
git diff INDEX TREE

For this usage, perhaps INDEX and TREE are not the actual names that git supports, but just some convenient refs that a user has set up. And I don't have any proposal for what would be appropriate low-level names for git to use for these pseudo-objects.

But I would sure be happy if I could just use refs like the above.

Then, if a concept like that existed, it should be rather straightforward to achieve the multi-parent HEAD,MERGE_HEAD -> INDEX diff that I'm after. It could probably come in handy in quite a few other situations as well.

But I can imagine the idea might also break down quite badly. So just let me know where it is totally stupid/infeasible.

#6 lore raw
Quoted 2 lines · Carl Worth

Meanwhile, I still think it's worth re-considering the original problem.

Maybe you are misunderstanding something in a major way,...

Quoted 3 lines

After a failed merge, I get a multi-parent diff from "git diff". However, after updating the index, I can't find any way to get multi-parent diffs anymore.

... and that is exactly what this "unmerge to re-populate higher order stages" does. The multi-parent diff you have been looking at failed merge _is_ generated by comparing the higher order stages and your working tree file.

Quoted 2 lines

I'd still like to be able to do that, even when I know that what I have in the index is good, and I don't want to undo it.

You cannot it have both ways. Either you want to have the unmerged entries (so that you *can* view the combined diff), or you do not want to have them (then you obviously cannot view the combined diff between the working tree file and these stages).

#7 lore raw
Quoted 6 lines · Junio C Hamano
Quoted 2 lines · Carl Worth

Meanwhile, I still think it's worth re-considering the original problem.

Maybe you are misunderstanding something in a major way,...

It's possible, but it's also possible I'm just failing to express myself clearly.

Quoted 8 lines
Quoted 3 lines

After a failed merge, I get a multi-parent diff from "git diff". However, after updating the index, I can't find any way to get multi-parent diffs anymore.

... and that is exactly what this "unmerge to re-populate higher order stages" does. The multi-parent diff you have been looking at failed merge _is_ generated by comparing the higher order stages and your working tree file.

But unmerging puts my index back into a not-ready-to-commit state, right? If so, then that's what I don't like from this proposal.

Quoted 7 lines
Quoted 2 lines

I'd still like to be able to do that, even when I know that what I have in the index is good, and I don't want to undo it.

You cannot it have both ways. Either you want to have the unmerged entries (so that you *can* view the combined diff), or you do not want to have them (then you obviously cannot view the combined diff between the working tree file and these stages).

I don't understand. Why would the entire diff operation have to happen inside the index? As soon as I'm done with the merge commit I can do "git show" and see a multi-parent diff. That doesn't have to muck with my index, does it?

By, example, (using the merge from the git tutorial), just before updating the index I get a lovely multi-parent diff:

	$ git diff
	diff --cc hello
	index 06fa6a2,cc44c73..0000000
	--- a/hello
	+++ b/hello
	@@@ -1,3 -1,3 +1,4 @@@
	  Hello World
	  It's a new day for git
	 +Play, play, play
	+ Work, work, work

then I can update the index:

	$ git update-index hello

and at this point I know I can see a single-parent diff before committing:

	$ git diff --cached
	diff --git a/hello b/hello
	index 06fa6a2..8798bdc 100644
	--- a/hello
	+++ b/hello
	@@ -1,3 +1,4 @@
	 Hello World
	 It's a new day for git
	 Play, play, play
	+Work, work, work

But what I want to do instead is at this point get the multi-parent diff.

Finally, after I commit, it's easy to get multi-parent diff again:

	$ git commit -m "Resolved merge conflict"
	$ PAGER= git show
	commit 547599946cf5dfb04af804e9d5451beb17662dca
	Merge: 35e0e9b... 698ac13...
	Author: Carl Worth <[email protected]>
	Date:   Wed Apr 19 16:51:55 2006 -0700
	    Resolved merge conflict
	
	diff --cc hello
	index 06fa6a2,cc44c73..8798bdc
	--- a/hello
	+++ b/hello
	@@@ -1,3 -1,3 +1,4 @@@
	  Hello World
	  It's a new day for git
	 +Play, play, play
	+ Work, work, work

So why can't I get that same result before committing without changing my index?

If the answer is that current implementation doesn't provide for that, (that is, it provides either an in-index multi-diff for files in multiple stages, OR it provides a multi-parent diff for a multi-parent commit object), then that's fine. But that doesn't seem like any fundamental limitation preventing what I'd like to have.

Am I making more sense now? Or am I still missing something?

-Carl

#8 lore raw
In reply to Carl Worth
Quoted 11 lines · Carl Worth

Meanwhile, I still think it's worth re-considering the original problem.

After a failed merge, I get a multi-parent diff from "git diff". However, after updating the index, I can't find any way to get multi-parent diffs anymore.

I'd still like to be able to do that, even when I know that what I have in the index is good, and I don't want to undo it. So the proposed unresolve (or update-index --unmerge, or whatever) is still not totally satisfactory.

I suspect this is just a misunderstanding caused by insufficient explanation, so let's try this a bit differently.

With the patch applied (or use "next" branch I'll be pushing out shortly), let's try the core-tutorial example up to the point where we need to make a merge commit and get conflict.

The easiest way to do it is to interrupt the t/t1200-tutorial.sh sequence, like this:

------------

t/t1200-tutorial.sh +2 -0
index 16b3ea9..a51bb50 100755
@@ -105,6 +105,8 @@ git commit -m 'Some fun.' -i hello examp
 
 test_expect_failure 'git resolve now fails' 'git resolve HEAD mybranch "Merge work in mybranch"'
 
+test_done
+
 cat > hello << EOF
 Hello World
 It's a new day for git

------------

Now, go to t/ directory and run this:

------------

$ cd t
$ ./t1200-tutorial.sh
------------

... which will stop after saying:

------------
* ok 13: git resolve now fails
* passed all 13 test(s)
------------

Then go to t/trash directory to find the initial part of Tutorial already run for us. We have created a repository with two files (example and hello), with two branches (master and mybranch), and we are on master. Both branches touched hello in a conflicting way, and we have just pulled the other branch into master, which conflicted, and hand resolved.

We can see the conflict like this:

------------

$ git diff
diff --cc hello
index ba42a2a,cc44c73..0000000
--- a/hello
+++ b/hello
@@@ -1,2 -1,3 +1,7 @@@
  Hello World
++<<<<<<< .merge_file_4wwUQv
 +Play, play, play
++=======
+ It's a new day for git
+ Work, work, work
++>>>>>>> .merge_file_6Ie4jx
------------

and we follow the tutorial to resolve it to see how the combined diff looks like:

------------

$ cat >hello <<\EOF
Hello World
Play, play, play
It's a new day for git
Work, work, work
EOF
$ git diff
diff --cc hello
index ba42a2a,cc44c73..0000000
--- a/hello
+++ b/hello
@@@ -1,2 -1,3 +1,4 @@@
  Hello World
 +Play, play, play
+ It's a new day for git
+ Work, work, work
------------

Still with me so? Now, let's say there was something wrong with the above merge, but at this point we did not know about it. For now, we are satisfied with this and run update-index:

------------

$ git update-index hello
$ git diff
------------

It was conflicting before, we hand-resolved and we said we are done. We do not see "git diff" output anymore, so we can concentrate on other files.

But later (much later) we find out that there was something wrong with this hand-resolve and now we would want to fix it. The new command, "git unresolve" is designed to help us exactly in this situation:

------------

$ git unresolve hello
$ git diff
diff --cc hello
index ba42a2a,cc44c73..0000000
--- a/hello
+++ b/hello
@@@ -1,2 -1,3 +1,4 @@@
  Hello World
 +Play, play, play
+ It's a new day for git
+ Work, work, work
------------

Then we realize that the "Play" line is not something we would want in the merge result, because we are in serious business. So let's drop it from the result and view the diff again:

------------

$ mv hello hellooo
$ sed -e '2d' <hellooo >hello
$ rm hellooo 
$ git diff
diff --cc hello
index ba42a2a,cc44c73..0000000
--- a/hello
+++ b/hello
------------

Oops, what happened? There is no diff?

The default option "dense combined" (--cc) is stronger than "combined" in that it omits hunks that have differences from only one parent, and in this case we took "their" version, so there is no interesting combined merge remaining to be seen.

If you want to view such not-so-interesting hunks, you could give different option to "git diff", like so:

------------

$ git diff -c -p
diff --combined hello
index ba42a2a,cc44c73..0000000
--- a/hello
+++ b/hello
@@@ -1,2 -1,3 +1,3 @@@
  Hello World
- Play, play, play
+ It's a new day for git
+ Work, work, work
------------

Also we can use different options like '--ours' (view diff from our branch) and '--theirs' (diff from their branch):

------------

$ git diff --ours hello
* Unmerged path hello
hello +2 -1
index ba42a2a..cc44c73 100644
@@ -1,2 +1,3 @@
 Hello World
-Play, play, play
+It's a new day for git
+Work, work, work
$ git diff --theirs hello
* Unmerged path hello
hello +0 -0

------------

#9 lore raw
Quoted 3 lines · Junio C Hamano

I suspect this is just a misunderstanding caused by insufficient explanation, so let's try this a bit differently.

Junio,

Thanks for the careful explanation.

Quoted 3 lines

With the patch applied (or use "next" branch I'll be pushing out shortly), let's try the core-tutorial example up to the point where we need to make a merge commit and get conflict.

Heh. We dropped back to the identical example with our crossed-in-flight messages.

Quoted 4 lines

But later (much later) we find out that there was something wrong with this hand-resolve and now we would want to fix it. The new command, "git unresolve" is designed to help us exactly in this situation:

Yes. And this was in fact the question I had asked in IRC. How to get the diff again when I realize the file I updated is wrong.

And as you point out, git-unresolve does just the trick here.

The question I asked in my latest message is basically just "Shouldn't there be a way to get the diff from the several parents to the index?". That's slightly different, but it is the functionality I was looking for when I was trying to recover from my update-of-botched-merge.

And its useful separately as part of the pre-commit-review I've said I always want to be able to do, (and as you designed "git status -v" to provide).

So there's the final piece I'd like here. I think "git status -a -v" should provide a multi-parent diff when merging, as should "git status -v" after manually doing an update-index while merging.

-Carl

#10 lore raw
Quoted 1 line · Carl Worth

Thanks for the careful explanation.

You're welcome. You owe me a patch that typo-fixes and pretty-prints my message, and drop it somewhere in Documentation/howto hierarchy for future reference ;-).

Quoted 3 lines

So there's the final piece I'd like here. I think "git status -a -v" should provide a multi-parent diff when merging, as should "git status -v" after manually doing an update-index while merging.

I'll keep that in mind, but honestly I am not very interested in that particular use case, if only because you can already do that by committing, and running "git show".

If you do not like how the resulting merge commit looks like, we have "git commit --amend" and "git reset --hard HEAD^" for you already.

I (or Linus) _may_ end up doing "git diff tree1 tree2 tree3..." as part of the planned diff rewrite, but that would be only if/when doing things in such a generic way is not much more trouble than the current "we only do one of index-vs-files, tree-vs-index, tree-vs-files-via-index or tree-vs-tree and nothing else" model.