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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Aug 19, 2011 at 21:50 UTC
b4fd94064de5cec7a45883684225b600d36208d9
1 file changed
+16
-17
builtin/merge.c
+16
-17
@@ -50,7 +50,7 @@ static int fast_forward_only;
50
static int allow_trivial = 1, have_message;
51
static struct strbuf merge_msg;
52
static struct commit_list *remoteheads;
53
-static unsigned char head[20], stash[20];
53
+static unsigned char head[20];
54
static struct strategy **use_strategies;
55
static size_t use_strategies_nr, use_strategies_alloc;
56
static const char **xopts;
@@ -217,7 +217,7 @@ static void drop_save(void)
217
unlink(git_path("MERGE_MODE"));
218
}
219
220
-static void save_state(void)
220
+static int save_state(unsigned char *stash)
221
{
222
int len;
223
struct child_process cp;
@@ -236,11 +236,12 @@ static void save_state(void)
236
237
if (finish_command(&cp) || len < 0)
238
die(_("stash failed"));
239
- else if (!len)
240
- return;
239
+ else if (!len) /* no changes */
240
+ return -1;
241
strbuf_setlen(&buffer, buffer.len-1);
242
if (get_sha1(buffer.buf, stash))
243
die(_("not a valid object: %s"), buffer.buf);
244
+ return 0;
245
}
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)
279
die(_("read-tree failed"));
280
}
281
281
-static void restore_state(void)
282
+static void restore_state(const unsigned char *stash)
283
{
284
struct strbuf sb = STRBUF_INIT;
285
const char *args[] = { "stash", "apply", NULL, NULL };
@@ -1010,6 +1011,7 @@ static int setup_with_upstream(const char ***argv)
1011
int cmd_merge(int argc, const char **argv, const char *prefix)
1012
{
1013
unsigned char result_tree[20];
1014
+ unsigned char stash[20];
1015
struct strbuf buf = STRBUF_INIT;
1016
const char *head_arg;
1017
int flag, head_invalid = 0, i;
@@ -1320,21 +1322,18 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1322
* sync with the head commit. The strategies are responsible
1323
* to ensure this.
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);
1331
1332
for (i = 0; i < use_strategies_nr; i++) {
1333
int ret;
1334
if (i) {
1335
printf(_("Rewinding the tree to pristine...\n"));
1337
- restore_state();
1336
+ restore_state(stash);
1337
}
1338
if (use_strategies_nr != 1)
1339
printf(_("Trying merge strategy %s...\n"),
@@ -1395,7 +1394,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1394
* it up.
1395
*/
1396
if (!best_strategy) {
1398
- restore_state();
1397
+ restore_state(stash);
1398
if (use_strategies_nr > 1)
1399
fprintf(stderr,
1400
_("No merge strategy handled the merge.\n"));
@@ -1407,7 +1406,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1406
; /* We already have its result in the working tree. */
1407
else {
1408
printf(_("Rewinding the tree to pristine...\n"));
1410
- restore_state();
1409
+ restore_state(stash);
1410
printf(_("Using the %s to prepare resolving by hand.\n"),
1411
best_strategy);
1412
try_merge_strategy(best_strategy, common, head_arg);