| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='clone --branch option' |
| 4 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 5 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 6 | |
| 7 | . ./test-lib.sh |
| 8 | |
| 9 | check_HEAD() { |
| 10 | echo refs/heads/"$1" >expect && |
| 11 | git symbolic-ref HEAD >actual && |
| 12 | test_cmp expect actual |
| 13 | } |
| 14 | |
| 15 | check_file() { |
| 16 | echo "$1" >expect && |
| 17 | test_cmp expect file |
| 18 | } |
| 19 | |
| 20 | test_expect_success 'setup' ' |
| 21 | mkdir parent && |
| 22 | (cd parent && git init && |
| 23 | echo one >file && git add file && git commit -m one && |
| 24 | git checkout -b two && |
| 25 | echo two >file && git add file && git commit -m two && |
| 26 | git checkout main) && |
| 27 | mkdir empty && |
| 28 | (cd empty && git init) |
| 29 | ' |
| 30 | |
| 31 | test_expect_success 'vanilla clone chooses HEAD' ' |
| 32 | git clone parent clone && |
| 33 | (cd clone && |
| 34 | check_HEAD main && |
| 35 | check_file one |
| 36 | ) |
| 37 | ' |
| 38 | |
| 39 | test_expect_success 'clone -b chooses specified branch' ' |
| 40 | git clone -b two parent clone-two && |
| 41 | (cd clone-two && |
| 42 | check_HEAD two && |
| 43 | check_file two |
| 44 | ) |
| 45 | ' |
| 46 | |
| 47 | test_expect_success 'clone -b sets up tracking' ' |
| 48 | (cd clone-two && |
| 49 | echo origin >expect && |
| 50 | git config branch.two.remote >actual && |
| 51 | echo refs/heads/two >>expect && |
| 52 | git config branch.two.merge >>actual && |
| 53 | test_cmp expect actual |
| 54 | ) |
| 55 | ' |
| 56 | |
| 57 | test_expect_success 'clone -b does not munge remotes/origin/HEAD' ' |
| 58 | (cd clone-two && |
| 59 | echo refs/remotes/origin/main >expect && |
| 60 | git symbolic-ref refs/remotes/origin/HEAD >actual && |
| 61 | test_cmp expect actual |
| 62 | ) |
| 63 | ' |
| 64 | |
| 65 | test_expect_success 'clone -b with bogus branch' ' |
| 66 | test_must_fail git clone -b bogus parent clone-bogus |
| 67 | ' |
| 68 | |
| 69 | test_expect_success 'clone -b not allowed with empty repos' ' |
| 70 | test_must_fail git clone -b branch empty clone-branch-empty |
| 71 | ' |
| 72 | |
| 73 | test_done |