1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-06-24 10:38:35 +02:00
git/t/t3203-branch-output.sh

405 lines
9.4 KiB
Bash
Raw Normal View History

#!/bin/sh
test_description='git branch display tests'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
tests: mark tests relying on the current default for `init.defaultBranch` In addition to the manual adjustment to let the `linux-gcc` CI job run the test suite with `master` and then with `main`, this patch makes sure that GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME is set in all test scripts that currently rely on the initial branch name being `master by default. To determine which test scripts to mark up, the first step was to force-set the default branch name to `master` in - all test scripts that contain the keyword `master`, - t4211, which expects `t/t4211/history.export` with a hard-coded ref to initialize the default branch, - t5560 because it sources `t/t556x_common` which uses `master`, - t8002 and t8012 because both source `t/annotate-tests.sh` which also uses `master`) This trick was performed by this command: $ sed -i '/^ *\. \.\/\(test-lib\|lib-\(bash\|cvs\|git-svn\)\|gitweb-lib\)\.sh$/i\ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master\ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\ ' $(git grep -l master t/t[0-9]*.sh) \ t/t4211*.sh t/t5560*.sh t/t8002*.sh t/t8012*.sh After that, careful, manual inspection revealed that some of the test scripts containing the needle `master` do not actually rely on a specific default branch name: either they mention `master` only in a comment, or they initialize that branch specificially, or they do not actually refer to the current default branch. Therefore, the aforementioned modification was undone in those test scripts thusly: $ git checkout HEAD -- \ t/t0027-auto-crlf.sh t/t0060-path-utils.sh \ t/t1011-read-tree-sparse-checkout.sh \ t/t1305-config-include.sh t/t1309-early-config.sh \ t/t1402-check-ref-format.sh t/t1450-fsck.sh \ t/t2024-checkout-dwim.sh \ t/t2106-update-index-assume-unchanged.sh \ t/t3040-subprojects-basic.sh t/t3301-notes.sh \ t/t3308-notes-merge.sh t/t3423-rebase-reword.sh \ t/t3436-rebase-more-options.sh \ t/t4015-diff-whitespace.sh t/t4257-am-interactive.sh \ t/t5323-pack-redundant.sh t/t5401-update-hooks.sh \ t/t5511-refspec.sh t/t5526-fetch-submodules.sh \ t/t5529-push-errors.sh t/t5530-upload-pack-error.sh \ t/t5548-push-porcelain.sh \ t/t5552-skipping-fetch-negotiator.sh \ t/t5572-pull-submodule.sh t/t5608-clone-2gb.sh \ t/t5614-clone-submodules-shallow.sh \ t/t7508-status.sh t/t7606-merge-custom.sh \ t/t9302-fast-import-unpack-limit.sh We excluded one set of test scripts in these commands, though: the range of `git p4` tests. The reason? `git p4` stores the (foreign) remote branch in the branch called `p4/master`, which is obviously not the default branch. Manual analysis revealed that only five of these tests actually require a specific default branch name to pass; They were modified thusly: $ sed -i '/^ *\. \.\/lib-git-p4\.sh$/i\ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master\ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\ ' t/t980[0167]*.sh t/t9811*.sh Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-11-19 00:44:19 +01:00
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-terminal.sh
test_expect_success 'make commits' '
echo content >file &&
git add file &&
git commit -m one &&
git branch -M main &&
echo content >>file &&
git commit -a -m two
'
test_expect_success 'make branches' '
git branch branch-one &&
git branch branch-two HEAD^
'
test_expect_success 'make remote branches' '
git update-ref refs/remotes/origin/branch-one branch-one &&
git update-ref refs/remotes/origin/branch-two branch-two &&
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/branch-one
'
cat >expect <<'EOF'
branch-one
branch-two
* main
EOF
test_expect_success 'git branch shows local branches' '
git branch >actual &&
test_cmp expect actual
'
test_expect_success 'git branch --list shows local branches' '
git branch --list >actual &&
test_cmp expect actual
'
cat >expect <<'EOF'
branch-one
branch-two
EOF
test_expect_success 'git branch --list pattern shows matching local branches' '
git branch --list branch* >actual &&
test_cmp expect actual
'
cat >expect <<'EOF'
origin/HEAD -> origin/branch-one
origin/branch-one
origin/branch-two
EOF
test_expect_success 'git branch -r shows remote branches' '
git branch -r >actual &&
test_cmp expect actual
'
cat >expect <<'EOF'
branch-one
branch-two
* main
remotes/origin/HEAD -> origin/branch-one
remotes/origin/branch-one
remotes/origin/branch-two
EOF
test_expect_success 'git branch -a shows local and remote branches' '
git branch -a >actual &&
test_cmp expect actual
'
cat >expect <<'EOF'
two
one
two
EOF
test_expect_success 'git branch -v shows branch summaries' '
git branch -v >tmp &&
awk "{print \$NF}" <tmp >actual &&
test_cmp expect actual
'
cat >expect <<'EOF'
two
one
EOF
test_expect_success 'git branch --list -v pattern shows branch summaries' '
git branch --list -v branch* >tmp &&
awk "{print \$NF}" <tmp >actual &&
test_cmp expect actual
'
test_expect_success 'git branch --ignore-case --list -v pattern shows branch summaries' '
git branch --list --ignore-case -v BRANCH* >tmp &&
awk "{print \$NF}" <tmp >actual &&
test_cmp expect actual
'
test_expect_success 'git branch -v pattern does not show branch summaries' '
test_must_fail git branch -v branch*
'
test_expect_success 'git branch `--show-current` shows current branch' '
cat >expect <<-\EOF &&
branch-two
EOF
git checkout branch-two &&
git branch --show-current >actual &&
test_cmp expect actual
'
test_expect_success 'git branch `--show-current` is silent when detached HEAD' '
git checkout HEAD^0 &&
git branch --show-current >actual &&
test_must_be_empty actual
'
test_expect_success 'git branch `--show-current` works properly when tag exists' '
cat >expect <<-\EOF &&
branch-and-tag-name
EOF
test_when_finished "
git checkout branch-one
git branch -D branch-and-tag-name
" &&
git checkout -b branch-and-tag-name &&
test_when_finished "git tag -d branch-and-tag-name" &&
git tag branch-and-tag-name &&
git branch --show-current >actual &&
test_cmp expect actual
'
test_expect_success 'git branch `--show-current` works properly with worktrees' '
cat >expect <<-\EOF &&
branch-one
branch-two
EOF
git checkout branch-one &&
test_when_finished "
git worktree remove worktree_dir
" &&
git worktree add worktree_dir branch-two &&
{
git branch --show-current &&
git -C worktree_dir branch --show-current
} >actual &&
test_cmp expect actual
'
test_expect_success 'git branch shows detached HEAD properly' '
cat >expect <<EOF &&
* (HEAD detached at $(git rev-parse --short HEAD^0))
branch-one
branch-two
main
EOF
git checkout HEAD^0 &&
git branch >actual &&
test_cmp expect actual
'
test_expect_success 'git branch shows detached HEAD properly after checkout --detach' '
git checkout main &&
cat >expect <<EOF &&
* (HEAD detached at $(git rev-parse --short HEAD^0))
branch-one
branch-two
main
EOF
git checkout --detach &&
git branch >actual &&
test_cmp expect actual
'
test_expect_success 'git branch shows detached HEAD properly after moving' '
cat >expect <<EOF &&
* (HEAD detached from $(git rev-parse --short HEAD))
branch-one
branch-two
main
EOF
git reset --hard HEAD^1 &&
git branch >actual &&
test_cmp expect actual
'
test_expect_success 'git branch shows detached HEAD properly from tag' '
cat >expect <<EOF &&
* (HEAD detached at fromtag)
branch-one
branch-two
main
EOF
git tag fromtag main &&
git checkout fromtag &&
git branch >actual &&
test_cmp expect actual
'
test_expect_success 'git branch shows detached HEAD properly after moving from tag' '
cat >expect <<EOF &&
* (HEAD detached from fromtag)
branch-one
branch-two
main
EOF
git reset --hard HEAD^1 &&
git branch >actual &&
test_cmp expect actual
'
test_expect_success 'git branch `--sort=[-]objectsize` option' '
cat >expect <<-\EOF &&
* (HEAD detached from fromtag)
branch-two
branch-one
main
EOF
git branch --sort=objectsize >actual &&
test_cmp expect actual &&
cat >expect <<-\EOF &&
* (HEAD detached from fromtag)
branch-one
main
branch-two
EOF
git branch --sort=-objectsize >actual &&
test_cmp expect actual
'
test_expect_success 'git branch `--sort=[-]type` option' '
cat >expect <<-\EOF &&
* (HEAD detached from fromtag)
branch-one
branch-two
main
EOF
git branch --sort=type >actual &&
test_cmp expect actual &&
cat >expect <<-\EOF &&
* (HEAD detached from fromtag)
branch-one
branch-two
main
EOF
git branch --sort=-type >actual &&
test_cmp expect actual
'
test_expect_success 'git branch `--sort=[-]version:refname` option' '
cat >expect <<-\EOF &&
* (HEAD detached from fromtag)
branch-one
branch-two
main
EOF
git branch --sort=version:refname >actual &&
test_cmp expect actual &&
cat >expect <<-\EOF &&
* (HEAD detached from fromtag)
main
branch-two
branch-one
EOF
git branch --sort=-version:refname >actual &&
test_cmp expect actual
'
test_expect_success 'git branch --points-at option' '
cat >expect <<-\EOF &&
branch-one
main
EOF
git branch --points-at=branch-one >actual &&
test_cmp expect actual
'
tag: do not show ambiguous tag names as "tags/foo" Since b7cc53e9 (tag.c: use 'ref-filter' APIs, 2015-07-11), git-tag has started showing tags with ambiguous names (i.e., when both "heads/foo" and "tags/foo" exists) as "tags/foo" instead of just "foo". This is both: - pointless; the output of "git tag" includes only refs/tags, so we know that "foo" means the one in "refs/tags". and - ambiguous; in the original output, we know that the line "foo" means that "refs/tags/foo" exists. In the new output, it is unclear whether we mean "refs/tags/foo" or "refs/tags/tags/foo". The reason this happens is that commit b7cc53e9 switched git-tag to use ref-filter's "%(refname:short)" output formatting, which was adapted from for-each-ref. This more general code does not know that we care only about tags, and uses shorten_unambiguous_ref to get the short-name. We need to tell it that we care only about "refs/tags/", and it should shorten with respect to that value. In theory, the ref-filter code could figure this out by us passing FILTER_REFS_TAGS. But there are two complications there: 1. The handling of refname:short is deep in formatting code that does not even have our ref_filter struct, let alone the arguments to the filter_ref struct. 2. In git v2.7.0, we expose the formatting language to the user. If we follow this path, it will mean that "%(refname:short)" behaves differently for "tag" versus "for-each-ref" (including "for-each-ref refs/tags/"), which can lead to confusion. Instead, let's add a new modifier to the formatting language, "strip", to remove a specific set of prefix components. This fixes "git tag", and lets users invoke the same behavior from their own custom formats (for "tag" or "for-each-ref") while leaving ":short" with its same consistent meaning in all places. We introduce a test in t7004 for "git tag", which fails without this patch. We also add a similar test in t3203 for "git branch", which does not actually fail. But since it is likely that "branch" will eventually use the same formatting code, the test helps defend against future regressions. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-01-26 04:00:05 +01:00
test_expect_success 'ambiguous branch/tag not marked' '
git tag ambiguous &&
git branch ambiguous &&
echo " ambiguous" >expect &&
git branch --list ambiguous >actual &&
test_cmp expect actual
'
test_expect_success 'local-branch symrefs shortened properly' '
git symbolic-ref refs/heads/ref-to-branch refs/heads/branch-one &&
git symbolic-ref refs/heads/ref-to-remote refs/remotes/origin/branch-one &&
cat >expect <<-\EOF &&
ref-to-branch -> branch-one
2017-01-10 09:49:52 +01:00
ref-to-remote -> origin/branch-one
EOF
git branch >actual.raw &&
grep ref-to <actual.raw >actual &&
test_cmp expect actual
'
test_expect_success 'sort branches, ignore case' '
(
git init -b main sort-icase &&
cd sort-icase &&
test_commit initial &&
git branch branch-one &&
git branch BRANCH-two &&
git branch --list | awk "{print \$NF}" >actual &&
cat >expected <<-\EOF &&
BRANCH-two
branch-one
main
EOF
test_cmp expected actual &&
git branch --list -i | awk "{print \$NF}" >actual &&
cat >expected <<-\EOF &&
branch-one
BRANCH-two
main
EOF
test_cmp expected actual
)
'
test_expect_success 'git branch --format option' '
cat >expect <<-\EOF &&
Refname is (HEAD detached from fromtag)
Refname is refs/heads/ambiguous
Refname is refs/heads/branch-one
Refname is refs/heads/branch-two
Refname is refs/heads/main
Refname is refs/heads/ref-to-branch
Refname is refs/heads/ref-to-remote
EOF
git branch --format="Refname is %(refname)" >actual &&
test_cmp expect actual
'
test_expect_success 'worktree colors correct' '
cat >expect <<-EOF &&
* <GREEN>(HEAD detached from fromtag)<RESET>
ambiguous<RESET>
branch-one<RESET>
+ <CYAN>branch-two<RESET>
main<RESET>
ref-to-branch<RESET> -> branch-one
ref-to-remote<RESET> -> origin/branch-one
EOF
git worktree add worktree_dir branch-two &&
git branch --color >actual.raw &&
rm -r worktree_dir &&
git worktree prune &&
test_decode_color <actual.raw >actual &&
test_cmp expect actual
'
test_expect_success "set up color tests" '
echo "<RED>main<RESET>" >expect.color &&
echo "main" >expect.bare &&
color_args="--format=%(color:red)%(refname:short) --list main"
'
test_expect_success '%(color) omitted without tty' '
TERM=vt100 git branch $color_args >actual.raw &&
test_decode_color <actual.raw >actual &&
test_cmp expect.bare actual
'
test_expect_success TTY '%(color) present with tty' '
test_terminal git branch $color_args >actual.raw &&
test_decode_color <actual.raw >actual &&
test_cmp expect.color actual
'
test_expect_success '--color overrides auto-color' '
git branch --color $color_args >actual.raw &&
test_decode_color <actual.raw >actual &&
test_cmp expect.color actual
'
test_expect_success 'verbose output lists worktree path' '
one=$(git rev-parse --short HEAD) &&
two=$(git rev-parse --short main) &&
cat >expect <<-EOF &&
* (HEAD detached from fromtag) $one one
ambiguous $one one
branch-one $two two
+ branch-two $one ($(pwd)/worktree_dir) one
main $two two
ref-to-branch $two two
ref-to-remote $two two
EOF
git worktree add worktree_dir branch-two &&
git branch -vv >actual &&
rm -r worktree_dir &&
git worktree prune &&
test_cmp expect actual
'
test_done