t2024: Add tests verifying current DWIM behavior of 'git checkout <branch>'

The DWIM mode of checkout allows you to run "git checkout foo" when there is no existing local ref or path called "foo", and there is exactly one remote with a remote-tracking branch called "foo". Git will then automatically create a new local branch called "foo" using the remote-tracking "foo" as its starting point and configured upstream. Improved-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Apr 21, 2013 at 23:51 UTC 399e4a1c560ae8caf338e13a73851089c384230a
1 file changed +99
t/t2024-checkout-dwim.sh new
+99
@@ -0,0 +1,99 @@
1 +#!/bin/sh
2 +
3 +test_description='checkout <branch>
4 +
5 +Ensures that checkout on an unborn branch does what the user expects'
6 +
7 +. ./test-lib.sh
8 +
9 +# Is the current branch "refs/heads/$1"?
10 +test_branch () {
11 + printf "%s\n" "refs/heads/$1" >expect.HEAD &&
12 + git symbolic-ref HEAD >actual.HEAD &&
13 + test_cmp expect.HEAD actual.HEAD
14 +}
15 +
16 +# Is branch "refs/heads/$1" set to pull from "$2/$3"?
17 +test_branch_upstream () {
18 + printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
19 + {
20 + git config "branch.$1.remote" &&
21 + git config "branch.$1.merge"
22 + } >actual.upstream &&
23 + test_cmp expect.upstream actual.upstream
24 +}
25 +
26 +test_expect_success 'setup' '
27 + git init repo_a &&
28 + (
29 + cd repo_a &&
30 + test_commit a_master &&
31 + git checkout -b foo &&
32 + test_commit a_foo &&
33 + git checkout -b bar &&
34 + test_commit a_bar
35 + ) &&
36 + git init repo_b &&
37 + (
38 + cd repo_b &&
39 + test_commit b_master &&
40 + git checkout -b foo &&
41 + test_commit b_foo &&
42 + git checkout -b baz &&
43 + test_commit b_baz
44 + ) &&
45 + git remote add repo_a repo_a &&
46 + git remote add repo_b repo_b &&
47 + git config remote.repo_b.fetch \
48 + "+refs/heads/*:refs/remotes/other_b/*" &&
49 + git fetch --all
50 +'
51 +
52 +test_expect_success 'checkout of non-existing branch fails' '
53 + git checkout -B master &&
54 + test_might_fail git branch -D xyzzy &&
55 +
56 + test_must_fail git checkout xyzzy &&
57 + test_must_fail git rev-parse --verify refs/heads/xyzzy &&
58 + test_branch master
59 +'
60 +
61 +test_expect_success 'checkout of branch from multiple remotes fails' '
62 + git checkout -B master &&
63 + test_might_fail git branch -D foo &&
64 +
65 + test_must_fail git checkout foo &&
66 + test_must_fail git rev-parse --verify refs/heads/foo &&
67 + test_branch master
68 +'
69 +
70 +test_expect_success 'checkout of branch from a single remote succeeds #1' '
71 + git checkout -B master &&
72 + test_might_fail git branch -D bar &&
73 +
74 + git checkout bar &&
75 + test_branch bar &&
76 + test_cmp_rev remotes/repo_a/bar HEAD &&
77 + test_branch_upstream bar repo_a bar
78 +'
79 +
80 +test_expect_success 'checkout of branch from a single remote succeeds #2' '
81 + git checkout -B master &&
82 + test_might_fail git branch -D baz &&
83 +
84 + git checkout baz &&
85 + test_branch baz &&
86 + test_cmp_rev remotes/other_b/baz HEAD &&
87 + test_branch_upstream baz repo_b baz
88 +'
89 +
90 +test_expect_success '--no-guess suppresses branch auto-vivification' '
91 + git checkout -B master &&
92 + test_might_fail git branch -D bar &&
93 +
94 + test_must_fail git checkout --no-guess bar &&
95 + test_must_fail git rev-parse --verify refs/heads/bar &&
96 + test_branch master
97 +'
98 +
99 +test_done