From ae5a6c3684c378bc32c1f6ecc0e6dc45300c14c1 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 17 Jan 2009 17:09:53 +0100 Subject: checkout: implement "@{-N}" shortcut name for N-th last branch Implement a shortcut @{-N} for the N-th last branch checked out, that works by parsing the reflog for the message added by previous git-checkout invocations. We expand the @{-N} to the branch name, so that you end up on an attached HEAD on that branch. Signed-off-by: Junio C Hamano --- builtin-checkout.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'builtin-checkout.c') diff --git a/builtin-checkout.c b/builtin-checkout.c index b5dd9c07b4..a3b69d6b94 100644 --- a/builtin-checkout.c +++ b/builtin-checkout.c @@ -361,8 +361,14 @@ struct branch_info { static void setup_branch_path(struct branch_info *branch) { struct strbuf buf = STRBUF_INIT; - strbuf_addstr(&buf, "refs/heads/"); - strbuf_addstr(&buf, branch->name); + + if (!interpret_nth_last_branch(branch->name, &buf)) { + branch->name = xstrdup(buf.buf); + strbuf_splice(&buf, 0, 0, "refs/heads/", 11); + } else { + strbuf_addstr(&buf, "refs/heads/"); + strbuf_addstr(&buf, branch->name); + } branch->path = strbuf_detach(&buf, NULL); } -- cgit v1.2.1 From a884d0cb71463c28d0329c593dce1ef9758f6177 Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Sat, 17 Jan 2009 17:09:54 +0100 Subject: sha1_name: tweak @{-N} lookup Have the lookup only look at "interesting" checkouts, meaning those that tell you "Already on ..." don't count even though they also cause a reflog entry. Let interpret_nth_last_branch() return the number of characters parsed, so that git-checkout can verify that the branch spec was @{-N}, not @{-1}^2 or something like that. (The latter will be added later.) Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- builtin-checkout.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'builtin-checkout.c') diff --git a/builtin-checkout.c b/builtin-checkout.c index a3b69d6b94..dc1de06279 100644 --- a/builtin-checkout.c +++ b/builtin-checkout.c @@ -361,8 +361,10 @@ struct branch_info { static void setup_branch_path(struct branch_info *branch) { struct strbuf buf = STRBUF_INIT; + int ret; - if (!interpret_nth_last_branch(branch->name, &buf)) { + if ((ret = interpret_nth_last_branch(branch->name, &buf)) + && ret == strlen(branch->name)) { branch->name = xstrdup(buf.buf); strbuf_splice(&buf, 0, 0, "refs/heads/", 11); } else { -- cgit v1.2.1 From 696acf45f9638b014c7132508de26d1a571c8a33 Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Sat, 17 Jan 2009 17:09:56 +0100 Subject: checkout: implement "-" abbreviation, add docs and tests Have '-' mean the same as '@{-1}', i.e., the last branch we were on. Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- builtin-checkout.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'builtin-checkout.c') diff --git a/builtin-checkout.c b/builtin-checkout.c index dc1de06279..b0a101bac7 100644 --- a/builtin-checkout.c +++ b/builtin-checkout.c @@ -679,6 +679,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix) arg = argv[0]; has_dash_dash = (argc > 1) && !strcmp(argv[1], "--"); + if (!strcmp(arg, "-")) + arg = "@{-1}"; + if (get_sha1(arg, rev)) { if (has_dash_dash) /* case (1) */ die("invalid reference: %s", arg); -- cgit v1.2.1