Teach @{-1} to git merge
1.6.2 will have @{-1} syntax advertised as "usable anywhere you can use a branch name". However, "git merge @{-1}" did not work. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Feb 13, 2009 at 23:26 UTC
c9717ee97075bb333684fa2f63e9136c5f7d89bf
2 files changed
+35
-3
builtin-merge.c
+12
-3
@@ -356,9 +356,14 @@ static void merge_name(const char *remote, struct strbuf *msg)
356
struct object *remote_head;
357
unsigned char branch_head[20], buf_sha[20];
358
struct strbuf buf = STRBUF_INIT;
359
+ struct strbuf bname = STRBUF_INIT;
360
const char *ptr;
361
int len, early;
362
363
+ len = strlen(remote);
364
+ if (interpret_nth_last_branch(remote, &bname) == len)
365
+ remote = bname.buf;
366
+
367
memset(branch_head, 0, sizeof(branch_head));
368
remote_head = peel_to_type(remote, 0, NULL, OBJ_COMMIT);
369
if (!remote_head)
@@ -371,7 +376,7 @@ static void merge_name(const char *remote, struct strbuf *msg)
376
if (!hashcmp(remote_head->sha1, branch_head)) {
377
strbuf_addf(msg, "%s\t\tbranch '%s' of .\n",
378
sha1_to_hex(branch_head), remote);
374
- return;
379
+ goto cleanup;
380
}
381
382
/* See if remote matches <name>^^^.. or <name>~<number> */
@@ -411,7 +416,8 @@ static void merge_name(const char *remote, struct strbuf *msg)
416
sha1_to_hex(remote_head->sha1),
417
truname.buf + 11,
418
(early ? " (early part)" : ""));
414
- return;
419
+ strbuf_release(&truname);
420
+ goto cleanup;
421
}
422
}
423
@@ -432,10 +438,13 @@ static void merge_name(const char *remote, struct strbuf *msg)
438
strbuf_remove(&line, ptr-line.buf+1, 13);
439
strbuf_addbuf(msg, &line);
440
strbuf_release(&line);
435
- return;
441
+ goto cleanup;
442
}
443
strbuf_addf(msg, "%s\t\tcommit '%s'\n",
444
sha1_to_hex(remote_head->sha1), remote);
445
+cleanup:
446
+ strbuf_release(&buf);
447
+ strbuf_release(&bname);
448
}
449
450
static int git_merge_config(const char *k, const char *v, void *cb)
t/t0100-previous.sh
+23
@@ -22,5 +22,28 @@ test_expect_success 'branch -d @{-12} when there is not enough switches yet' '
22
git rev-parse --verify refs/heads/master
23
'
24
25
+test_expect_success 'merge @{-1}' '
26
+ git checkout A &&
27
+ test_commit B &&
28
+ git checkout A &&
29
+ test_commit C &&
30
+ git branch -f master B &&
31
+ git branch -f other &&
32
+ git checkout other &&
33
+ git checkout master &&
34
+ git merge @{-1} &&
35
+ git cat-file commit HEAD | grep "Merge branch '\''other'\''"
36
+'
37
+
38
+test_expect_success 'merge @{-1} when there is not enough switches yet' '
39
+ git reflog expire --expire=now &&
40
+ git checkout -f master &&
41
+ git reset --hard B &&
42
+ git branch -f other C &&
43
+ git checkout other &&
44
+ git checkout master &&
45
+ test_must_fail git merge @{-12}
46
+'
47
+
48
test_done
49