@@ -50,7 +50,7 @@ static int fast_forward_only;
50
50
static int allow_trivial = 1 , have_message ;
51
51
static struct strbuf merge_msg ;
52
52
static struct commit_list * remoteheads ;
53
- static unsigned char head [20 ], stash [ 20 ] ;
53
+ static unsigned char head [20 ];
54
54
static struct strategy * * use_strategies ;
55
55
static size_t use_strategies_nr , use_strategies_alloc ;
56
56
static const char * * xopts ;
@@ -217,7 +217,7 @@ static void drop_save(void)
217
217
unlink (git_path ("MERGE_MODE" ));
218
218
}
219
219
220
- static void save_state (void )
220
+ static int save_state (unsigned char * stash )
221
221
{
222
222
int len ;
223
223
struct child_process cp ;
@@ -236,11 +236,12 @@ static void save_state(void)
236
236
237
237
if (finish_command (& cp ) || len < 0 )
238
238
die (_ ("stash failed" ));
239
- else if (!len )
240
- return ;
239
+ else if (!len ) /* no changes */
240
+ return -1 ;
241
241
strbuf_setlen (& buffer , buffer .len - 1 );
242
242
if (get_sha1 (buffer .buf , stash ))
243
243
die (_ ("not a valid object: %s" ), buffer .buf );
244
+ return 0 ;
244
245
}
245
246
246
247
static void read_empty (unsigned const char * sha1 , int verbose )
@@ -278,7 +279,7 @@ static void reset_hard(unsigned const char *sha1, int verbose)
278
279
die (_ ("read-tree failed" ));
279
280
}
280
281
281
- static void restore_state (void )
282
+ static void restore_state (const unsigned char * stash )
282
283
{
283
284
struct strbuf sb = STRBUF_INIT ;
284
285
const char * args [] = { "stash" , "apply" , NULL , NULL };
@@ -1010,6 +1011,7 @@ static int setup_with_upstream(const char ***argv)
1010
1011
int cmd_merge (int argc , const char * * argv , const char * prefix )
1011
1012
{
1012
1013
unsigned char result_tree [20 ];
1014
+ unsigned char stash [20 ];
1013
1015
struct strbuf buf = STRBUF_INIT ;
1014
1016
const char * head_arg ;
1015
1017
int flag , head_invalid = 0 , i ;
@@ -1320,21 +1322,18 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1320
1322
* sync with the head commit. The strategies are responsible
1321
1323
* to ensure this.
1322
1324
*/
1323
- if (use_strategies_nr != 1 ) {
1324
- /*
1325
- * Stash away the local changes so that we can try more
1326
- * than one.
1327
- */
1328
- save_state ();
1329
- } else {
1330
- memcpy (stash , null_sha1 , 20 );
1331
- }
1325
+ if (use_strategies_nr == 1 ||
1326
+ /*
1327
+ * Stash away the local changes so that we can try more than one.
1328
+ */
1329
+ save_state (stash ))
1330
+ hashcpy (stash , null_sha1 );
1332
1331
1333
1332
for (i = 0 ; i < use_strategies_nr ; i ++ ) {
1334
1333
int ret ;
1335
1334
if (i ) {
1336
1335
printf (_ ("Rewinding the tree to pristine...\n" ));
1337
- restore_state ();
1336
+ restore_state (stash );
1338
1337
}
1339
1338
if (use_strategies_nr != 1 )
1340
1339
printf (_ ("Trying merge strategy %s...\n" ),
@@ -1395,7 +1394,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1395
1394
* it up.
1396
1395
*/
1397
1396
if (!best_strategy ) {
1398
- restore_state ();
1397
+ restore_state (stash );
1399
1398
if (use_strategies_nr > 1 )
1400
1399
fprintf (stderr ,
1401
1400
_ ("No merge strategy handled the merge.\n" ));
@@ -1407,7 +1406,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1407
1406
; /* We already have its result in the working tree. */
1408
1407
else {
1409
1408
printf (_ ("Rewinding the tree to pristine...\n" ));
1410
- restore_state ();
1409
+ restore_state (stash );
1411
1410
printf (_ ("Using the %s to prepare resolving by hand.\n" ),
1412
1411
best_strategy );
1413
1412
try_merge_strategy (best_strategy , common , head_arg );
0 commit comments