Skip to content

Commit b4fd940

Browse files
pcloudsgitster
authored andcommitted
merge: keep stash[] a local variable
A stash is created by save_state() and used by restore_state(). Pass SHA-1 explicitly for clarity and keep stash[] to cmd_merge(). Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 06bb643 commit b4fd940

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

builtin/merge.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static int fast_forward_only;
5050
static int allow_trivial = 1, have_message;
5151
static struct strbuf merge_msg;
5252
static struct commit_list *remoteheads;
53-
static unsigned char head[20], stash[20];
53+
static unsigned char head[20];
5454
static struct strategy **use_strategies;
5555
static size_t use_strategies_nr, use_strategies_alloc;
5656
static const char **xopts;
@@ -217,7 +217,7 @@ static void drop_save(void)
217217
unlink(git_path("MERGE_MODE"));
218218
}
219219

220-
static void save_state(void)
220+
static int save_state(unsigned char *stash)
221221
{
222222
int len;
223223
struct child_process cp;
@@ -236,11 +236,12 @@ static void save_state(void)
236236

237237
if (finish_command(&cp) || len < 0)
238238
die(_("stash failed"));
239-
else if (!len)
240-
return;
239+
else if (!len) /* no changes */
240+
return -1;
241241
strbuf_setlen(&buffer, buffer.len-1);
242242
if (get_sha1(buffer.buf, stash))
243243
die(_("not a valid object: %s"), buffer.buf);
244+
return 0;
244245
}
245246

246247
static void read_empty(unsigned const char *sha1, int verbose)
@@ -278,7 +279,7 @@ static void reset_hard(unsigned const char *sha1, int verbose)
278279
die(_("read-tree failed"));
279280
}
280281

281-
static void restore_state(void)
282+
static void restore_state(const unsigned char *stash)
282283
{
283284
struct strbuf sb = STRBUF_INIT;
284285
const char *args[] = { "stash", "apply", NULL, NULL };
@@ -1010,6 +1011,7 @@ static int setup_with_upstream(const char ***argv)
10101011
int cmd_merge(int argc, const char **argv, const char *prefix)
10111012
{
10121013
unsigned char result_tree[20];
1014+
unsigned char stash[20];
10131015
struct strbuf buf = STRBUF_INIT;
10141016
const char *head_arg;
10151017
int flag, head_invalid = 0, i;
@@ -1320,21 +1322,18 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13201322
* sync with the head commit. The strategies are responsible
13211323
* to ensure this.
13221324
*/
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);
13321331

13331332
for (i = 0; i < use_strategies_nr; i++) {
13341333
int ret;
13351334
if (i) {
13361335
printf(_("Rewinding the tree to pristine...\n"));
1337-
restore_state();
1336+
restore_state(stash);
13381337
}
13391338
if (use_strategies_nr != 1)
13401339
printf(_("Trying merge strategy %s...\n"),
@@ -1395,7 +1394,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13951394
* it up.
13961395
*/
13971396
if (!best_strategy) {
1398-
restore_state();
1397+
restore_state(stash);
13991398
if (use_strategies_nr > 1)
14001399
fprintf(stderr,
14011400
_("No merge strategy handled the merge.\n"));
@@ -1407,7 +1406,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
14071406
; /* We already have its result in the working tree. */
14081407
else {
14091408
printf(_("Rewinding the tree to pristine...\n"));
1410-
restore_state();
1409+
restore_state(stash);
14111410
printf(_("Using the %s to prepare resolving by hand.\n"),
14121411
best_strategy);
14131412
try_merge_strategy(best_strategy, common, head_arg);

0 commit comments

Comments
 (0)