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 <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Thomas Rast committed
Jan 17, 2009 at 17:09 UTC
696acf45f9638b014c7132508de26d1a571c8a33
3 files changed
+57
Documentation/git-checkout.txt
+4
@@ -133,6 +133,10 @@ the conflicted merge in the specified paths.
133
+
134
When this parameter names a non-branch (but still a valid commit object),
135
your HEAD becomes 'detached'.
136
++
137
+As a special case, the "`@\{-N\}`" syntax for the N-th last branch
138
+checks out the branch (instead of detaching). You may also specify
139
+"`-`" which is synonymous with "`@\{-1\}`".
140
141
142
Detached HEAD
builtin-checkout.c
+3
@@ -679,6 +679,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
679
arg = argv[0];
680
has_dash_dash = (argc > 1) && !strcmp(argv[1], "--");
681
682
+ if (!strcmp(arg, "-"))
683
+ arg = "@{-1}";
684
+
685
if (get_sha1(arg, rev)) {
686
if (has_dash_dash) /* case (1) */
687
die("invalid reference: %s", arg);
t/t2012-checkout-last.sh
new
+50
@@ -0,0 +1,50 @@
1
+#!/bin/sh
2
+
3
+test_description='checkout can switch to last branch'
4
+
5
+. ./test-lib.sh
6
+
7
+test_expect_success 'setup' '
8
+ echo hello >world &&
9
+ git add world &&
10
+ git commit -m initial &&
11
+ git branch other &&
12
+ echo "hello again" >>world &&
13
+ git add world &&
14
+ git commit -m second
15
+'
16
+
17
+test_expect_success '"checkout -" does not work initially' '
18
+ test_must_fail git checkout -
19
+'
20
+
21
+test_expect_success 'first branch switch' '
22
+ git checkout other
23
+'
24
+
25
+test_expect_success '"checkout -" switches back' '
26
+ git checkout - &&
27
+ test "z$(git symbolic-ref HEAD)" = "zrefs/heads/master"
28
+'
29
+
30
+test_expect_success '"checkout -" switches forth' '
31
+ git checkout - &&
32
+ test "z$(git symbolic-ref HEAD)" = "zrefs/heads/other"
33
+'
34
+
35
+test_expect_success 'detach HEAD' '
36
+ git checkout $(git rev-parse HEAD)
37
+'
38
+
39
+test_expect_success '"checkout -" attaches again' '
40
+ git checkout - &&
41
+ test "z$(git symbolic-ref HEAD)" = "zrefs/heads/other"
42
+'
43
+
44
+test_expect_success '"checkout -" detaches again' '
45
+ git checkout - &&
46
+ test "z$(git rev-parse HEAD)" = "z$(git rev-parse other)" &&
47
+ test_must_fail git symbolic-ref HEAD
48
+'
49
+
50
+test_done