Improving merge failure message
I often see my students confused after a failed merge and they can't figure out what to do next. Two typical error messages they get are
[1]% git merge master
Auto-merging cool
CONFLICT (content): Merge conflict in cool
Automatic merge failed; fix conflicts and then commit the result.
[2]% git merge feature
error: Entry 'cool' not uptodate. Cannot merge.
fatal: merging of trees 8ec1d96451ff05451720e4e8968812c46b35e5e4 and aad8d5cef3915ab78b3227abaaac99b62db9eb54 failed
In the former case, the merge command gives a helpful message that automatic merge failed because it found a conflict and tells enough instruction to the user.
But in the latter case, the messages look unnecessarily scary, with two "error" and "fatal" comments, and long sha1 commit names.
Those of us who used git for some time can tell what it wants to say. The merge checked the files in the working tree before doing anything, found that the user has uncommitted change to a file that is involved in the merge, and it stopped. And it didn't change anything. It may be "fatal" but the user has much less reason to be scared about this failure than the conflicting case.
It would be nice if the message in the latter case can be toned down.
Signature · 2 lines
Nanako Shiraishi http://ivory.ap.teacup.com/nanako3/
Quoted 6 lines · Nanako Shiraishi
[2]% git merge feature
error: Entry 'cool' not uptodate. Cannot merge.
fatal: merging of trees8ec1d96451ff05451720e4e8968812c46b35e5e4andaad8d5cef3915ab78b3227abaaac99b62db9eb54failed... the messages look unnecessarily scary, with two "error" and "fatal" comments, and long sha1 commit names.
Just a technical nit. I think these are tree object names.
Quoted 8 lines
Those of us who used git for some time can tell what it wants to say. The merge checked the files in the working tree before doing anything, found that the user has uncommitted change to a file that is involved in the merge, and it stopped. And it didn't change anything. It may be "fatal" but the user has much less reason to be scared about this failure than the conflicting case.
It would be nice if the message in the latter case can be toned down.
Yeah, it would be nice. This actually was something that bothered me as well while trying to explain the recovery procedure for these two cases. Give me half an hour or so to cook up something...
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Nanako Shiraishi
[2]% git merge feature
error: Entry 'cool' not uptodate. Cannot merge.
fatal: merging of trees8ec1d96451ff05451720e4e8968812c46b35e5e4andaad8d5cef3915ab78b3227abaaac99b62db9eb54failed
Quoted 8 lines
Quoted 4 lines
... the messages look unnecessarily scary, with two "error" and "fatal" comments, and long sha1 commit names.
It would be nice if the message in the latter case can be toned down.
Yeah, it would be nice. This actually was something that bothered me as well while trying to explain the recovery procedure for these two cases. Give me half an hour or so to cook up something...
I think that this is a symptom of a much more involved issue about error handling.
Currently, it is customary in the code to report an error at each location where an exceptional condition is detected:
if (frotz())
ret = error("frotz messed up");If frotz() is a "low-level" routine, like a C library function, then this is the right thing to do. However, if frotz() is our own function ("high-level") which itself calls low-level functions with the same pattern, then this is obviously the wrong thing to do, because it results into two error messages.
We need a guideline how errors are reported. This guideline could be:
(1) Low-level functions do not print error messages, but set an error code and leave the reporting to the caller.
(2) High-level functions must print an error message (through error() or die()) when they detect a failure of a low-level function that was called directly.
(3) High-level functions must not print an error message when they detect a failure of another high-level function that was called directly.
There remains to classify our functions into high-level or low-level. But I think we won't have a lot of low-level functions.
BTW, I applied this guideline when I worked on {start,finish,run}_command recently, and IMHO it worked out quite nicely because exactly one error is reported after a failure.
-- Hannes
Quoted 18 lines · Junio C Hamano
Quoted 6 lines · Nanako Shiraishi
[2]% git merge feature
error: Entry 'cool' not uptodate. Cannot merge.
fatal: merging of trees8ec1d96451ff05451720e4e8968812c46b35e5e4andaad8d5cef3915ab78b3227abaaac99b62db9eb54failed... the messages look unnecessarily scary, with two "error" and "fatal" comments, and long sha1 commit names.
Just a technical nit. I think these are tree object names.
Quoted 7 lines
Those of us who used git for some time can tell what it wants to say. The merge checked the files in the working tree before doing anything, found that the user has uncommitted change to a file that is involved in the merge, and it stopped. And it didn't change anything. It may be "fatal" but the user has much less reason to be scared about this failure than the conflicting case.
Quoted 5 lines
Quoted 1 line
It would be nice if the message in the latter case can be toned down.
Yeah, it would be nice. This actually was something that bothered me as well while trying to explain the recovery procedure for these two cases. Give me half an hour or so to cook up something...
It turns out to be a lot simpler than I thought, because 8ccba00 (unpack-trees: allow Porcelain to give different error messages, 2008-05-17) already laid enough groundwork for doing this kind of thing easily.
Notable points are:
- End the messages with "Aborting."; they are given when the three-way merge stops without harming the work tree;
- Do not give the extra message after unpack_trees() already errored out. This "merging of trees failed" message was primarily for debugging merge-recursive itself, and the end user cannot do much with the object names given in the message anyway.
But do give it under higher verbosity level, or when it happens during the inner merge (the "recursive" one), as unpack_trees() should not fail for the inner merge under normal conditions.
We could later add instructions on how to recover (i.e. "stash changes away or commit on a side branch and retry") instead of the silent exit(128) I have down there, and then use Peff's advice.* mechanism to squelch it (e.g. "advice.mergeindirtytree"), but they are separate topics.
merge-recursive.c | 25 +++++++++++++++++++++---- 1 files changed, 21 insertions(+), 4 deletions(-)
merge-recursive.c +21 -4
@@ -170,6 +170,18 @@ static int git_merge_trees(int index_only,int rc;struct tree_desc t[3 ];struct unpack_trees_options opts; +static struct unpack_trees_error_msgs msgs = { +/* would_overwrite */ +"Your local changes to '%s' will be clobbered by merge. Aborting." , +/* not_uptodate_file */ +"Your local changes to '%s' will be clobbered by merge. Aborting." , +/* not_uptodate_dir */ +"Updating '%s' would lose untracked files in it. Aborting." , +/* would_lose_untracked */ +"Untracked working tree file '%s' would be %s by merge. Aborting" , +/* bind_overlap -- will not happen here */ +NULL , + };memset (&opts,0 ,sizeof (opts));if (index_only)
@@ -181,6 +193,7 @@ static int git_merge_trees(int index_only, opts.fn = threeway_merge; opts.src_index = &the_index; opts.dst_index = &the_index; + opts.msgs = msgs;init_tree_desc_from_tree (t+0 , common);init_tree_desc_from_tree (t+1 , head);
@@ -1188,10 +1201,14 @@ int merge_trees(struct merge_options *o, code =git_merge_trees (o->call_depth, common, head, merge); -if (code !=0 ) -die ("merging of trees %s and %s failed" , -sha1_to_hex (head->object.sha1), -sha1_to_hex (merge->object.sha1)); +if (code !=0 ) { +if (show (o,4 ) || o->call_depth) +die ("merging of trees %s and %s failed" , +sha1_to_hex (head->object.sha1), +sha1_to_hex (merge->object.sha1)); +else +exit (128 ); + }if (unmerged_cache ()) {struct string_list *entries, *re_head, *re_merge;
Nanako Shiraishi
Tested-byQuoting Junio C Hamano <[email protected]>
Quoted 10 lines
Notable points are:
- End the messages with "Aborting."; they are given when the three-way merge stops without harming the work tree;
- Do not give the extra message after
unpack_trees()already errored out. This "merging of trees failed" message was primarily for debugging merge-recursive itself, and the end user cannot do much with the object names given in the message anyway.
Quoted 8 lines
But do give it under higher verbosity level, or when it happens during the inner merge (the "recursive" one), as
unpack_trees()should not fail for the inner merge under normal conditions.We could later add instructions on how to recover (i.e. "stash changes away or commit on a side branch and retry") instead of the silent exit(128) I have down there, and then use Peff's advice.* mechanism to squelch it (e.g. "advice.mergeindirtytree"), but they are separate topics.
Thank you for a quick response. The patch works fine here, so if you want please add:
Signature · 2 lines
Nanako Shiraishi http://ivory.ap.teacup.com/nanako3/
merge-recursive.c static int git_merge_trees(int index_only, · line 173
Quoted 5 lines · Junio C Hamano
merge-recursive.c +1
-0@@ -170,6 +170,18 @@ static int git_merge_trees(int index_only, int rc; struct tree_desc t[3]; struct unpack_trees_options opts; + static struct unpack_trees_error_msgs msgs = {
You can make these const, the struct is copied anyway:
merge-recursive.c static int git_merge_trees(int index_only, · line 196
Quoted 1 line
merge-recursive.c +1
-0+ opts.msgs = msgs;
Here --^
Heya,
Quoted 2 lines · Junio C Hamano
+ /*
would_overwrite*/
+ "Your local changes to '%s' will be clobbered by merge. Aborting.",
Still scary, shouldn't that be s/will be/would be/ ?
Quoted 2 lines
+ /*
not_uptodate_file*/
+ "Your local changes to '%s' will be clobbered by merge. Aborting.",
Ditto.
Quoted 2 lines
+ /*
not_uptodate_dir*/
+ "Updating '%s' would lose untracked files in it. Aborting.",
Not sure, but maybe s/in it//
Signature · 3 lines
Cheers, Sverre Rabbelier
Quoted 5 lines · Sverre Rabbelier
Review
Quoted 2 lines · Junio C Hamano
(quoted diff) +2
-0+ /* would_overwrite */ + "Your local changes to '%s' will be clobbered by merge. Aborting.",Still scary, shouldn't that be s/will be/would be/ ?
Thanks, very true indeed. "It would be clobbered if we were to continue hence we abort." is how we want to explain our behaviour, so "would" is definitely better here.
Quoted 2 lines
merge-recursive.c static int git_merge_trees(int index_only, · line 179
Quoted 2 lines
merge-recursive.c +2
-0+/* not_uptodate_dir */ + "Updating '%s' would lose untracked files in it. Aborting.",
This is "merge would resolve to have a file X, but you have a directory X in your work tree and it is not empty" case.
I'll leave the exact wording up to other people. My primary focus was to end all of these messages with "Aborting."
This turns out to be a continuation of an older discussion thread back in May 2008, and I do not know if anybody took up the challenge back then. I wouldn't be surprised if "checkout", which was the topic of the old thread, has some other scary plumbing message still seeping through to the UI layer. Perhaps there are some other commands that needs similar kind of love.
Quoted 3 lines · Junio C Hamano
Thanks, very true indeed. "It would be clobbered if we were to continue hence we abort." is how we want to explain our behaviour, so "would" is definitely better here.
While we're picking apart your wording, is "clobbered" the word we want to use? Everywhere else that is user-facing we tend to use the term "overwritten".
-Peff
2009/9/8 Junio C Hamano <[email protected]>:
Quoted 9 lines
I'll leave the exact wording up to other people. My primary focus was to end all of these messages with "Aborting."
This turns out to be a continuation of an older discussion thread back in May 2008, and I do not know if anybody took up the challenge back then. I wouldn't be surprised if "checkout", which was the topic of the old thread, has some other scary plumbing message still seeping through to the UI layer. Perhaps there are some other commands that needs similar kind of love.
Just a note that Documentation/git-checkout.txt references this message in an example and should be kept in step with the final wording change. It would be ideal to be able to regression-test the examples in the documentation somehow but that might involve abusing the asciidoc markup somewhat.
2009/9/8 Jeff King <[email protected]>:
Quoted 2 lines
While we're picking apart your wording, is "clobbered" the word we want to use?
If we're debating 'clobbered', then maybe the non-word 'uptodate' is fair game too? 8-)
Quoted 11 lines · Mike Ralphson
2009/9/8 Junio C Hamano <[email protected]>:
Quoted 9 lines
I'll leave the exact wording up to other people. My primary focus was to end all of these messages with "Aborting."
This turns out to be a continuation of an older discussion thread back in May 2008, and I do not know if anybody took up the challenge back then. I wouldn't be surprised if "checkout", which was the topic of the old thread, has some other scary plumbing message still seeping through to the UI layer. Perhaps there are some other commands that needs similar kind of love.
Quoted 5 lines
Just a note that
Documentation/git-checkout.txtreferences this message in an example and should be kept in step with the final wording change. It would be ideal to be able to regression-test the examples in the documentation somehow but that might involve abusing the asciidoc markup somewhat.
Thanks.
Quoted 6 lines
2009/9/8 Jeff King <[email protected]>:
Quoted 2 lines
While we're picking apart your wording, is "clobbered" the word we want to use?
If we're debating 'clobbered', then maybe the non-word 'uptodate' is fair game too? 8-)
I think you added smiley because you already knew the answer, but if that is not the case, please see
http://thread.gmane.org/gmane.comp.version-control.git/81100/focus=82358