@@ -231,27 +231,30 @@ static void set_author_ident_env(const char *message)
231231 sha1_to_hex (commit -> object .sha1 ));
232232}
233233
234- static char * help_msg ( void )
234+ static void advise ( const char * advice , ... )
235235{
236- struct strbuf helpbuf = STRBUF_INIT ;
237- char * msg = getenv ("GIT_CHERRY_PICK_HELP" );
236+ va_list params ;
238237
239- if (msg )
240- return msg ;
238+ va_start (params , advice );
239+ vreportf ("hint: " , advice , params );
240+ va_end (params );
241+ }
241242
242- strbuf_addstr ( & helpbuf , " After resolving the conflicts,\n"
243- "mark the corrected paths with 'git add <paths>' or 'git rm <paths>'\n"
244- "and commit the result " );
243+ static void print_advice ( void )
244+ {
245+ char * msg = getenv ( "GIT_CHERRY_PICK_HELP " );
245246
246- if (action == CHERRY_PICK ) {
247- strbuf_addf (& helpbuf , " with: \n"
248- "\n"
249- " git commit -c %s\n" ,
250- sha1_to_hex (commit -> object .sha1 ));
247+ if (msg ) {
248+ fprintf (stderr , "%s\n" , msg );
249+ return ;
251250 }
252- else
253- strbuf_addch (& helpbuf , '.' );
254- return strbuf_detach (& helpbuf , NULL );
251+
252+ advise ("after resolving the conflicts, mark the corrected paths" );
253+ advise ("with 'git add <paths>' or 'git rm <paths>'" );
254+
255+ if (action == CHERRY_PICK )
256+ advise ("and commit the result with 'git commit -c %s'" ,
257+ find_unique_abbrev (commit -> object .sha1 , DEFAULT_ABBREV ));
255258}
256259
257260static void write_message (struct strbuf * msgbuf , const char * filename )
@@ -301,10 +304,9 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from)
301304 return write_ref_sha1 (ref_lock , to , "cherry-pick" );
302305}
303306
304- static void do_recursive_merge (struct commit * base , struct commit * next ,
305- const char * base_label , const char * next_label ,
306- unsigned char * head , struct strbuf * msgbuf ,
307- char * defmsg )
307+ static int do_recursive_merge (struct commit * base , struct commit * next ,
308+ const char * base_label , const char * next_label ,
309+ unsigned char * head , struct strbuf * msgbuf )
308310{
309311 struct merge_options o ;
310312 struct tree * result , * next_tree , * base_tree , * head_tree ;
@@ -347,14 +349,35 @@ static void do_recursive_merge(struct commit *base, struct commit *next,
347349 i ++ ;
348350 }
349351 }
350- write_message (msgbuf , defmsg );
351- fprintf (stderr , "Automatic %s failed.%s\n" ,
352- me , help_msg ());
353- rerere (allow_rerere_auto );
354- exit (1 );
355352 }
356- write_message (msgbuf , defmsg );
357- fprintf (stderr , "Finished one %s.\n" , me );
353+
354+ return !clean ;
355+ }
356+
357+ /*
358+ * If we are cherry-pick, and if the merge did not result in
359+ * hand-editing, we will hit this commit and inherit the original
360+ * author date and name.
361+ * If we are revert, or if our cherry-pick results in a hand merge,
362+ * we had better say that the current user is responsible for that.
363+ */
364+ static int run_git_commit (const char * defmsg )
365+ {
366+ /* 6 is max possible length of our args array including NULL */
367+ const char * args [6 ];
368+ int i = 0 ;
369+
370+ args [i ++ ] = "commit" ;
371+ args [i ++ ] = "-n" ;
372+ if (signoff )
373+ args [i ++ ] = "-s" ;
374+ if (!edit ) {
375+ args [i ++ ] = "-F" ;
376+ args [i ++ ] = defmsg ;
377+ }
378+ args [i ] = NULL ;
379+
380+ return run_command_v_opt (args , RUN_GIT_CMD );
358381}
359382
360383static int do_pick_commit (void )
@@ -365,6 +388,7 @@ static int do_pick_commit(void)
365388 struct commit_message msg = { NULL , NULL , NULL , NULL , NULL };
366389 char * defmsg = NULL ;
367390 struct strbuf msgbuf = STRBUF_INIT ;
391+ int res ;
368392
369393 if (no_commit ) {
370394 /*
@@ -460,63 +484,40 @@ static int do_pick_commit(void)
460484 }
461485 }
462486
463- if (!strategy || !strcmp (strategy , "recursive" ) || action == REVERT )
464- do_recursive_merge (base , next , base_label , next_label ,
465- head , & msgbuf , defmsg );
466- else {
467- int res ;
487+ if (!strategy || !strcmp (strategy , "recursive" ) || action == REVERT ) {
488+ res = do_recursive_merge (base , next , base_label , next_label ,
489+ head , & msgbuf );
490+ write_message ( & msgbuf , defmsg );
491+ } else {
468492 struct commit_list * common = NULL ;
469493 struct commit_list * remotes = NULL ;
494+
470495 write_message (& msgbuf , defmsg );
496+
471497 commit_list_insert (base , & common );
472498 commit_list_insert (next , & remotes );
473499 res = try_merge_command (strategy , common ,
474500 sha1_to_hex (head ), remotes );
475501 free_commit_list (common );
476502 free_commit_list (remotes );
477- if (res ) {
478- fprintf (stderr , "Automatic %s with strategy %s failed.%s\n" ,
479- me , strategy , help_msg ());
480- rerere (allow_rerere_auto );
481- exit (1 );
482- }
483503 }
484504
485- free_message (& msg );
486-
487- /*
488- *
489- * If we are cherry-pick, and if the merge did not result in
490- * hand-editing, we will hit this commit and inherit the original
491- * author date and name.
492- * If we are revert, or if our cherry-pick results in a hand merge,
493- * we had better say that the current user is responsible for that.
494- */
495-
496- if (!no_commit ) {
497- /* 6 is max possible length of our args array including NULL */
498- const char * args [6 ];
499- int res ;
500- int i = 0 ;
501-
502- args [i ++ ] = "commit" ;
503- args [i ++ ] = "-n" ;
504- if (signoff )
505- args [i ++ ] = "-s" ;
506- if (!edit ) {
507- args [i ++ ] = "-F" ;
508- args [i ++ ] = defmsg ;
509- }
510- args [i ] = NULL ;
511- res = run_command_v_opt (args , RUN_GIT_CMD );
512- free (defmsg );
513-
514- return res ;
505+ if (res ) {
506+ error ("could not %s %s... %s" ,
507+ action == REVERT ? "revert" : "apply" ,
508+ find_unique_abbrev (commit -> object .sha1 , DEFAULT_ABBREV ),
509+ msg .subject );
510+ print_advice ();
511+ rerere (allow_rerere_auto );
512+ } else {
513+ if (!no_commit )
514+ res = run_git_commit (defmsg );
515515 }
516516
517+ free_message (& msg );
517518 free (defmsg );
518519
519- return 0 ;
520+ return res ;
520521}
521522
522523static void prepare_revs (struct rev_info * revs )
0 commit comments