diff options
Diffstat (limited to 't/t3200-branch.sh')
-rwxr-xr-x | t/t3200-branch.sh | 525 |
1 files changed, 403 insertions, 122 deletions
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index e575ffb..d3bbd00 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -8,6 +8,7 @@ test_description='git branch assorted tests' GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh . "$TEST_DIRECTORY"/lib-rebase.sh @@ -24,10 +25,10 @@ test_expect_success 'prepare a trivial repository' ' test_expect_success 'git branch --help should not have created a bogus branch' ' test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 && - test_path_is_missing .git/refs/heads/--help + test_ref_missing refs/heads/--help ' -test_expect_success 'branch -h in broken repository' ' +test_expect_success REFFILES 'branch -h in broken repository' ' mkdir broken && ( cd broken && @@ -35,39 +36,59 @@ test_expect_success 'branch -h in broken repository' ' >.git/refs/heads/main && test_expect_code 129 git branch -h >usage 2>&1 ) && - test_i18ngrep "[Uu]sage" broken/usage + test_grep "[Uu]sage" broken/usage ' test_expect_success 'git branch abc should create a branch' ' - git branch abc && test_path_is_file .git/refs/heads/abc + git branch abc && + test_ref_exists refs/heads/abc +' + +test_expect_success 'git branch abc should fail when abc exists' ' + test_must_fail git branch abc +' + +test_expect_success 'git branch --force abc should fail when abc is checked out' ' + test_when_finished git switch main && + git switch abc && + test_must_fail git branch --force abc HEAD~1 +' + +test_expect_success 'git branch --force abc should succeed when abc exists' ' + git rev-parse HEAD~1 >expect && + git branch --force abc HEAD~1 && + git rev-parse abc >actual && + test_cmp expect actual ' test_expect_success 'git branch a/b/c should create a branch' ' - git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c + git branch a/b/c && + test_ref_exists refs/heads/a/b/c ' test_expect_success 'git branch mb main... should create a branch' ' - git branch mb main... && test_path_is_file .git/refs/heads/mb + git branch mb main... && + test_ref_exists refs/heads/mb ' test_expect_success 'git branch HEAD should fail' ' test_must_fail git branch HEAD ' -cat >expect <<EOF -$ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main -EOF test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' ' GIT_COMMITTER_DATE="2005-05-26 23:30" \ git -c core.logallrefupdates=false branch --create-reflog d/e/f && - test_path_is_file .git/refs/heads/d/e/f && - test_path_is_file .git/logs/refs/heads/d/e/f && - test_cmp expect .git/logs/refs/heads/d/e/f + test_ref_exists refs/heads/d/e/f && + cat >expect <<-EOF && + $HEAD refs/heads/d/e/f@{0}: branch: Created from main + EOF + git reflog show --no-abbrev-commit refs/heads/d/e/f >actual && + test_cmp expect actual ' test_expect_success 'git branch -d d/e/f should delete a branch and a log' ' git branch -d d/e/f && - test_path_is_missing .git/refs/heads/d/e/f && + test_ref_missing refs/heads/d/e/f && test_must_fail git reflog exists refs/heads/d/e/f ' @@ -85,7 +106,7 @@ test_expect_success 'git branch l should work after branch l/m has been deleted' test_expect_success 'git branch -m dumps usage' ' test_expect_code 128 git branch -m 2>err && - test_i18ngrep "branch name required" err + test_grep "branch name required" err ' test_expect_success 'git branch -m m broken_symref should work' ' @@ -168,6 +189,13 @@ test_expect_success 'git branch -M foo bar should fail when bar is checked out' test_must_fail git branch -M bar foo ' +test_expect_success 'git branch -M foo bar should fail when bar is checked out in worktree' ' + git branch -f bar && + test_when_finished "git worktree remove wt && git branch -D wt" && + git worktree add wt && + test_must_fail git branch -M bar wt +' + test_expect_success 'git branch -M baz bam should succeed when baz is checked out' ' git checkout -b baz && git branch bam && @@ -175,10 +203,9 @@ test_expect_success 'git branch -M baz bam should succeed when baz is checked ou test $(git rev-parse --abbrev-ref HEAD) = bam ' -test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD' ' - msg="Branch: renamed refs/heads/baz to refs/heads/bam" && - grep " 0\{40\}.*$msg$" .git/logs/HEAD && - grep "^0\{40\}.*$msg$" .git/logs/HEAD +test_expect_success 'git branch -M baz bam should add entries to HEAD reflog' ' + git reflog show HEAD >actual && + grep "HEAD@{0}: Branch: renamed refs/heads/baz to refs/heads/bam" actual ' test_expect_success 'git branch -M should leave orphaned HEAD alone' ' @@ -187,17 +214,20 @@ test_expect_success 'git branch -M should leave orphaned HEAD alone' ' cd orphan && test_commit initial && git checkout --orphan lonely && - grep lonely .git/HEAD && - test_path_is_missing .git/refs/head/lonely && + git symbolic-ref HEAD >expect && + echo refs/heads/lonely >actual && + test_cmp expect actual && + test_ref_missing refs/head/lonely && git branch -M main mistress && - grep lonely .git/HEAD + git symbolic-ref HEAD >expect && + test_cmp expect actual ) ' test_expect_success 'resulting reflog can be shown by log -g' ' oid=$(git rev-parse HEAD) && cat >expect <<-EOF && - HEAD@{0} $oid $msg + HEAD@{0} $oid Branch: renamed refs/heads/baz to refs/heads/bam HEAD@{2} $oid checkout: moving from foo to baz EOF git log -g --format="%gd %H %gs" -2 HEAD >actual && @@ -215,15 +245,34 @@ test_expect_success 'git branch -M baz bam should succeed when baz is checked ou git worktree prune ' +test_expect_success REFFILES 'git branch -M fails if updating any linked working tree fails' ' + git worktree add -b baz bazdir1 && + git worktree add -f bazdir2 baz && + touch .git/worktrees/bazdir1/HEAD.lock && + test_must_fail git branch -M baz bam && + test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam && + git branch -M bam baz && + rm .git/worktrees/bazdir1/HEAD.lock && + touch .git/worktrees/bazdir2/HEAD.lock && + test_must_fail git branch -M baz bam && + test $(git -C bazdir1 rev-parse --abbrev-ref HEAD) = bam && + rm -rf bazdir1 bazdir2 && + git worktree prune +' + test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' ' git checkout -b baz && git worktree add -f bazdir baz && ( cd bazdir && git branch -M baz bam && - test $(git rev-parse --abbrev-ref HEAD) = bam + echo bam >expect && + git rev-parse --abbrev-ref HEAD >actual && + test_cmp expect actual ) && - test $(git rev-parse --abbrev-ref HEAD) = bam && + echo bam >expect && + git rev-parse --abbrev-ref HEAD >actual && + test_cmp expect actual && rm -r bazdir && git worktree prune ' @@ -244,6 +293,67 @@ test_expect_success 'git branch -M topic topic should work when main is checked git branch -M topic topic ' +test_expect_success 'git branch -M and -C fail on detached HEAD' ' + git checkout HEAD^{} && + test_when_finished git checkout - && + echo "fatal: cannot rename the current branch while not on any" >expect && + test_must_fail git branch -M must-fail 2>err && + test_cmp expect err && + echo "fatal: cannot copy the current branch while not on any" >expect && + test_must_fail git branch -C must-fail 2>err && + test_cmp expect err +' + +test_expect_success 'git branch -m should work with orphan branches' ' + test_when_finished git checkout - && + test_when_finished git worktree remove -f wt && + git worktree add wt --detach && + # rename orphan in another worktreee + git -C wt checkout --orphan orphan-foo-wt && + git branch -m orphan-foo-wt orphan-bar-wt && + test orphan-bar-wt=$(git -C orphan-worktree branch --show-current) && + # rename orphan in the current worktree + git checkout --orphan orphan-foo && + git branch -m orphan-foo orphan-bar && + test orphan-bar=$(git branch --show-current) +' + +test_expect_success 'git branch -d on orphan HEAD (merged)' ' + test_when_finished git checkout main && + git checkout --orphan orphan && + test_when_finished "rm -rf .git/objects/commit-graph*" && + git commit-graph write --reachable && + git branch --track to-delete main && + git branch -d to-delete +' + +test_expect_success 'git branch -d on orphan HEAD (merged, graph)' ' + test_when_finished git checkout main && + git checkout --orphan orphan && + git branch --track to-delete main && + git branch -d to-delete +' + +test_expect_success 'git branch -d on orphan HEAD (unmerged)' ' + test_when_finished git checkout main && + git checkout --orphan orphan && + test_when_finished "git branch -D to-delete" && + git branch to-delete main && + test_must_fail git branch -d to-delete 2>err && + grep "not fully merged" err +' + +test_expect_success 'git branch -d on orphan HEAD (unmerged, graph)' ' + test_when_finished git checkout main && + git checkout --orphan orphan && + test_when_finished "git branch -D to-delete" && + git branch to-delete main && + test_when_finished "rm -rf .git/objects/commit-graph*" && + git commit-graph write --reachable && + test_must_fail git branch -d to-delete 2>err && + grep "not fully merged" err +' + test_expect_success 'git branch -v -d t should work' ' git branch t && git rev-parse --verify refs/heads/t && @@ -282,6 +392,7 @@ test_expect_success 'deleting checked-out branch from repo that is a submodule' git init repo1 && git init repo1/sub && test_commit -C repo1/sub x && + test_config_global protocol.file.allow always && git -C repo1 submodule add ./sub && git -C repo1 commit -m "adding sub" && @@ -329,10 +440,10 @@ test_expect_success 'git branch --list -v with --abbrev' ' test_expect_success 'git branch --column' ' COLUMNS=81 git branch --column=column >actual && - cat >expect <<\EOF && - a/b/c bam foo l * main n o/p r - abc bar j/k m/m mb o/o q topic -EOF + cat >expect <<-\EOF && + a/b/c bam foo l * main n o/p r + abc bar j/k m/m mb o/o q topic + EOF test_cmp expect actual ' @@ -342,25 +453,25 @@ test_expect_success 'git branch --column with an extremely long branch name' ' test_when_finished "git branch -d $long" && git branch $long && COLUMNS=80 git branch --column=column >actual && - cat >expect <<EOF && - a/b/c - abc - bam - bar - foo - j/k - l - m/m -* main - mb - n - o/o - o/p - q - r - topic - $long -EOF + cat >expect <<-EOF && + a/b/c + abc + bam + bar + foo + j/k + l + m/m + * main + mb + n + o/o + o/p + q + r + topic + $long + EOF test_cmp expect actual ' @@ -370,10 +481,10 @@ test_expect_success 'git branch with column.*' ' COLUMNS=80 git branch >actual && git config --unset column.branch && git config --unset column.ui && - cat >expect <<\EOF && - a/b/c bam foo l * main n o/p r - abc bar j/k m/m mb o/o q topic -EOF + cat >expect <<-\EOF && + a/b/c bam foo l * main n o/p r + abc bar j/k m/m mb o/o q topic + EOF test_cmp expect actual ' @@ -385,39 +496,36 @@ test_expect_success 'git branch -v with column.ui ignored' ' git config column.ui column && COLUMNS=80 git branch -v | cut -c -8 | sed "s/ *$//" >actual && git config --unset column.ui && - cat >expect <<\EOF && - a/b/c - abc - bam - bar - foo - j/k - l - m/m -* main - mb - n - o/o - o/p - q - r - topic -EOF + cat >expect <<-\EOF && + a/b/c + abc + bam + bar + foo + j/k + l + m/m + * main + mb + n + o/o + o/p + q + r + topic + EOF test_cmp expect actual ' -mv .git/config .git/config-saved - -test_expect_success SHA1 'git branch -m q q2 without config should succeed' ' +test_expect_success DEFAULT_REPO_FORMAT 'git branch -m q q2 without config should succeed' ' + test_when_finished mv .git/config-saved .git/config && + mv .git/config .git/config-saved && git branch -m q q2 && git branch -m q2 q ' -mv .git/config-saved .git/config - -git config branch.s/s.dummy Hello - test_expect_success 'git branch -m s/s s should work when s/t is deleted' ' + git config branch.s/s.dummy Hello && git branch --create-reflog s/s && git reflog exists refs/heads/s/s && git branch --create-reflog s/t && @@ -468,19 +576,19 @@ EOF # ...and that the comments for those sections are also # preserved. - cat config.branch | sed "s/\"source\"/\"dest\"/" >expect && + sed "s/\"source\"/\"dest\"/" config.branch >expect && sed -n -e "/Note the lack/,\$p" .git/config >actual && test_cmp expect actual ' test_expect_success 'git branch -c dumps usage' ' test_expect_code 128 git branch -c 2>err && - test_i18ngrep "branch name required" err + test_grep "branch name required" err ' test_expect_success 'git branch --copy dumps usage' ' test_expect_code 128 git branch --copy 2>err && - test_i18ngrep "branch name required" err + test_grep "branch name required" err ' test_expect_success 'git branch -c d e should work' ' @@ -590,7 +698,8 @@ test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' test_expect_success 'git branch -C c1 c2 should never touch HEAD' ' msg="Branch: copied refs/heads/c1 to refs/heads/c2" && - ! grep "$msg$" .git/logs/HEAD + git reflog HEAD >actual && + ! grep "$msg$" actual ' test_expect_success 'git branch -C main should work when main is checked out' ' @@ -693,26 +802,26 @@ test_expect_success 'deleting a symref' ' git symbolic-ref refs/heads/symref refs/heads/target && echo "Deleted branch symref (was refs/heads/target)." >expect && git branch -d symref >actual && - test_path_is_file .git/refs/heads/target && - test_path_is_missing .git/refs/heads/symref && + test_ref_exists refs/heads/target && + test_ref_missing refs/heads/symref && test_cmp expect actual ' test_expect_success 'deleting a dangling symref' ' git symbolic-ref refs/heads/dangling-symref nowhere && - test_path_is_file .git/refs/heads/dangling-symref && + git symbolic-ref --no-recurse refs/heads/dangling-symref && echo "Deleted branch dangling-symref (was nowhere)." >expect && git branch -d dangling-symref >actual && - test_path_is_missing .git/refs/heads/dangling-symref && + test_ref_missing refs/heads/dangling-symref && test_cmp expect actual ' test_expect_success 'deleting a self-referential symref' ' git symbolic-ref refs/heads/self-reference refs/heads/self-reference && - test_path_is_file .git/refs/heads/self-reference && + test_ref_exists refs/heads/self-reference && echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect && git branch -d self-reference >actual && - test_path_is_missing .git/refs/heads/self-reference && + test_ref_missing refs/heads/self-reference && test_cmp expect actual ' @@ -720,15 +829,8 @@ test_expect_success 'renaming a symref is not allowed' ' git symbolic-ref refs/heads/topic refs/heads/main && test_must_fail git branch -m topic new-topic && git symbolic-ref refs/heads/topic && - test_path_is_file .git/refs/heads/main && - test_path_is_missing .git/refs/heads/new-topic -' - -test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' ' - git branch --create-reflog u && - mv .git/logs/refs/heads/u real-u && - ln -s real-u .git/logs/refs/heads/u && - test_must_fail git branch -m u v + test_ref_exists refs/heads/main && + test_ref_missing refs/heads/new-topic ' test_expect_success 'test tracking setup via --track' ' @@ -814,7 +916,19 @@ test_expect_success 'test deleting branch without config' ' test_expect_success 'deleting currently checked out branch fails' ' git worktree add -b my7 my7 && test_must_fail git -C my7 branch -d my7 && - test_must_fail git branch -d my7 && + test_must_fail git branch -d my7 2>actual && + grep "^error: cannot delete branch .my7. used by worktree at " actual && + rm -r my7 && + git worktree prune +' + +test_expect_success 'deleting in-use branch fails' ' + git worktree add my7 && + test_commit -C my7 bt7 && + git -C my7 bisect start HEAD HEAD~2 && + test_must_fail git -C my7 branch -d my7 && + test_must_fail git branch -d my7 2>actual && + grep "^error: cannot delete branch .my7. used by worktree at " actual && rm -r my7 && git worktree prune ' @@ -840,6 +954,41 @@ test_expect_success 'branch from tag w/--track causes failure' ' test_must_fail git branch --track my11 foobar ' +test_expect_success 'simple tracking works when remote branch name matches' ' + test_when_finished "rm -rf otherserver" && + git init otherserver && + test_commit -C otherserver my_commit 1 && + git -C otherserver branch feature && + test_config branch.autosetupmerge simple && + test_config remote.otherserver.url otherserver && + test_config remote.otherserver.fetch refs/heads/*:refs/remotes/otherserver/* && + git fetch otherserver && + git branch feature otherserver/feature && + test_cmp_config otherserver branch.feature.remote && + test_cmp_config refs/heads/feature branch.feature.merge +' + +test_expect_success 'simple tracking skips when remote branch name does not match' ' + test_config branch.autosetupmerge simple && + test_config remote.local.url . && + test_config remote.local.fetch refs/heads/*:refs/remotes/local/* && + git fetch local && + git branch my-other local/main && + test_cmp_config "" --default "" branch.my-other.remote && + test_cmp_config "" --default "" branch.my-other.merge +' + +test_expect_success 'simple tracking skips when remote ref is not a branch' ' + test_config branch.autosetupmerge simple && + test_config remote.localtags.url . && + test_config remote.localtags.fetch refs/tags/*:refs/remotes/localtags/* && + git tag mytag12 main && + git fetch localtags && + git branch mytag12 localtags/mytag12 && + test_cmp_config "" --default "" branch.mytag12.remote && + test_cmp_config "" --default "" branch.mytag12.merge +' + test_expect_success '--set-upstream-to fails on multiple branches' ' echo "fatal: too many arguments to set new upstream" >expect && test_must_fail git branch --set-upstream-to main a b c 2>err && @@ -849,7 +998,7 @@ test_expect_success '--set-upstream-to fails on multiple branches' ' test_expect_success '--set-upstream-to fails on detached HEAD' ' git checkout HEAD^{} && test_when_finished git checkout - && - echo "fatal: could not set upstream of HEAD to main when it does not point to any branch." >expect && + echo "fatal: could not set upstream of HEAD to main when it does not point to any branch" >expect && test_must_fail git branch --set-upstream-to main 2>err && test_cmp expect err ' @@ -862,11 +1011,11 @@ test_expect_success '--set-upstream-to fails on a missing dst branch' ' test_expect_success '--set-upstream-to fails on a missing src branch' ' test_must_fail git branch --set-upstream-to does-not-exist main 2>err && - test_i18ngrep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err + test_grep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err ' test_expect_success '--set-upstream-to fails on a non-ref' ' - echo "fatal: Cannot setup tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch." >expect && + echo "fatal: cannot set up tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch" >expect && test_must_fail git branch --set-upstream-to HEAD^{} 2>err && test_cmp expect err ' @@ -876,7 +1025,7 @@ test_expect_success '--set-upstream-to fails on locked config' ' >.git/config.lock && git branch locked && test_must_fail git branch --set-upstream-to locked 2>err && - test_i18ngrep "could not lock config file .git/config" err + test_grep "could not lock config file .git/config" err ' test_expect_success 'use --set-upstream-to modify HEAD' ' @@ -897,7 +1046,7 @@ test_expect_success 'use --set-upstream-to modify a particular branch' ' ' test_expect_success '--unset-upstream should fail if given a non-existent branch' ' - echo "fatal: Branch '"'"'i-dont-exist'"'"' has no upstream information" >expect && + echo "fatal: branch '"'"'i-dont-exist'"'"' has no upstream information" >expect && test_must_fail git branch --unset-upstream i-dont-exist 2>err && test_cmp expect err ' @@ -907,7 +1056,7 @@ test_expect_success '--unset-upstream should fail if config is locked' ' git branch --set-upstream-to locked && >.git/config.lock && test_must_fail git branch --unset-upstream 2>err && - test_i18ngrep "could not lock config file .git/config" err + test_grep "could not lock config file .git/config" err ' test_expect_success 'test --unset-upstream on HEAD' ' @@ -919,7 +1068,7 @@ test_expect_success 'test --unset-upstream on HEAD' ' test_must_fail git config branch.main.remote && test_must_fail git config branch.main.merge && # fail for a branch without upstream set - echo "fatal: Branch '"'"'main'"'"' has no upstream information" >expect && + echo "fatal: branch '"'"'main'"'"' has no upstream information" >expect && test_must_fail git branch --unset-upstream 2>err && test_cmp expect err ' @@ -933,7 +1082,7 @@ test_expect_success '--unset-upstream should fail on multiple branches' ' test_expect_success '--unset-upstream should fail on detached HEAD' ' git checkout HEAD^{} && test_when_finished git checkout - && - echo "fatal: could not unset upstream of HEAD when it does not point to any branch." >expect && + echo "fatal: could not unset upstream of HEAD when it does not point to any branch" >expect && test_must_fail git branch --unset-upstream 2>err && test_cmp expect err ' @@ -950,26 +1099,26 @@ test_expect_success 'disabled option --set-upstream fails' ' test_must_fail git branch --set-upstream origin/main ' -test_expect_success '--set-upstream-to notices an error to set branch as own upstream' ' +test_expect_success '--set-upstream-to notices an error to set branch as own upstream' " git branch --set-upstream-to refs/heads/my13 my13 2>actual && cat >expect <<-\EOF && - warning: Not setting branch my13 as its own upstream. + warning: not setting branch 'my13' as its own upstream EOF test_expect_code 1 git config branch.my13.remote && test_expect_code 1 git config branch.my13.merge && test_cmp expect actual -' +" -# Keep this test last, as it changes the current branch -cat >expect <<EOF -$ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main -EOF test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' ' + test_when_finished git checkout main && GIT_COMMITTER_DATE="2005-05-26 23:30" \ git checkout -b g/h/i -l main && - test_path_is_file .git/refs/heads/g/h/i && - test_path_is_file .git/logs/refs/heads/g/h/i && - test_cmp expect .git/logs/refs/heads/g/h/i + test_ref_exists refs/heads/g/h/i && + cat >expect <<-EOF && + $HEAD refs/heads/g/h/i@{0}: branch: Created from main + EOF + git reflog show --no-abbrev-commit refs/heads/g/h/i >actual && + test_cmp expect actual ' test_expect_success 'checkout -b makes reflog by default' ' @@ -993,13 +1142,27 @@ test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates git rev-parse --verify gamma@{0} ' -test_expect_success 'avoid ambiguous track' ' +test_expect_success 'avoid ambiguous track and advise' ' git config branch.autosetupmerge true && git config remote.ambi1.url lalala && git config remote.ambi1.fetch refs/heads/lalala:refs/heads/main && git config remote.ambi2.url lilili && git config remote.ambi2.fetch refs/heads/lilili:refs/heads/main && - test_must_fail git branch all1 main && + cat <<-EOF >expected && + fatal: not tracking: ambiguous information for ref '\''refs/heads/main'\'' + hint: There are multiple remotes whose fetch refspecs map to the remote + hint: tracking ref '\''refs/heads/main'\'': + hint: ambi1 + hint: ambi2 + hint: '' + hint: This is typically a configuration error. + hint: '' + hint: To support setting up tracking branches, ensure that + hint: different remotes'\'' fetch refspecs map into different + hint: tracking namespaces. + EOF + test_must_fail git branch all1 main 2>actual && + test_cmp expected actual && test -z "$(git config branch.all1.merge)" ' @@ -1286,6 +1449,9 @@ test_expect_success 'branch --delete --force removes dangling branch' ' ' test_expect_success 'use --edit-description' ' + EDITOR=: git branch --edit-description && + test_expect_code 1 git config branch.main.description && + write_script editor <<-\EOF && echo "New contents" >"$1" EOF @@ -1326,7 +1492,7 @@ test_expect_success '--list during rebase' ' set_fake_editor && git rebase -i HEAD~2 && git branch --list >actual && - test_i18ngrep "rebasing main" actual + test_grep "rebasing main" actual ' test_expect_success '--list during rebase from detached HEAD' ' @@ -1338,7 +1504,7 @@ test_expect_success '--list during rebase from detached HEAD' ' set_fake_editor && git rebase -i HEAD~2 && git branch --list >actual && - test_i18ngrep "rebasing detached HEAD $oid" actual + test_grep "rebasing detached HEAD $oid" actual ' test_expect_success 'tracking with unexpected .fetch refspec' ' @@ -1378,9 +1544,10 @@ test_expect_success 'tracking with unexpected .fetch refspec' ' test_expect_success 'configured committerdate sort' ' git init -b main sort && + test_config -C sort branch.sort "committerdate" && + ( cd sort && - git config branch.sort committerdate && test_commit initial && git checkout -b a && test_commit a && @@ -1400,9 +1567,10 @@ test_expect_success 'configured committerdate sort' ' ' test_expect_success 'option override configured sort' ' + test_config -C sort branch.sort "committerdate" && + ( cd sort && - git config branch.sort committerdate && git branch --sort=refname >actual && cat >expect <<-\EOF && a @@ -1414,12 +1582,125 @@ test_expect_success 'option override configured sort' ' ) ' +test_expect_success '--no-sort cancels config sort keys' ' + test_config -C sort branch.sort "-refname" && + + ( + cd sort && + + # objecttype is identical for all of them, so sort falls back on + # default (ascending refname) + git branch \ + --no-sort \ + --sort="objecttype" >actual && + cat >expect <<-\EOF && + a + * b + c + main + EOF + test_cmp expect actual + ) + +' + +test_expect_success '--no-sort cancels command line sort keys' ' + ( + cd sort && + + # objecttype is identical for all of them, so sort falls back on + # default (ascending refname) + git branch \ + --sort="-refname" \ + --no-sort \ + --sort="objecttype" >actual && + cat >expect <<-\EOF && + a + * b + c + main + EOF + test_cmp expect actual + ) +' + +test_expect_success '--no-sort without subsequent --sort prints expected branches' ' + ( + cd sort && + + # Sort the results with `sort` for a consistent comparison + # against expected + git branch --no-sort | sort >actual && + cat >expect <<-\EOF && + a + c + main + * b + EOF + test_cmp expect actual + ) +' + test_expect_success 'invalid sort parameter in configuration' ' + test_config -C sort branch.sort "v:notvalid" && + ( cd sort && - git config branch.sort "v:notvalid" && - test_must_fail git branch + + # this works in the "listing" mode, so bad sort key + # is a dying offence. + test_must_fail git branch && + + # these do not need to use sorting, and should all + # succeed + git branch newone main && + git branch -c newone newerone && + git branch -m newone newestone && + git branch -d newerone newestone ) ' +test_expect_success 'tracking info copied with --track=inherit' ' + git branch --track=inherit foo2 my1 && + test_cmp_config local branch.foo2.remote && + test_cmp_config refs/heads/main branch.foo2.merge +' + +test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' + test_unconfig branch.autoSetupMerge && + # default config does not copy tracking info + git branch foo-no-inherit my1 && + test_cmp_config "" --default "" branch.foo-no-inherit.remote && + test_cmp_config "" --default "" branch.foo-no-inherit.merge && + # with autoSetupMerge=inherit, we copy tracking info from my1 + test_config branch.autoSetupMerge inherit && + git branch foo3 my1 && + test_cmp_config local branch.foo3.remote && + test_cmp_config refs/heads/main branch.foo3.merge && + # no tracking info to inherit from main + git branch main2 main && + test_cmp_config "" --default "" branch.main2.remote && + test_cmp_config "" --default "" branch.main2.merge +' + +test_expect_success '--track overrides branch.autoSetupMerge' ' + test_config branch.autoSetupMerge inherit && + git branch --track=direct foo4 my1 && + test_cmp_config . branch.foo4.remote && + test_cmp_config refs/heads/my1 branch.foo4.merge && + git branch --no-track foo5 my1 && + test_cmp_config "" --default "" branch.foo5.remote && + test_cmp_config "" --default "" branch.foo5.merge +' + +test_expect_success 'errors if given a bad branch name' ' + cat <<-\EOF >expect && + fatal: '\''foo..bar'\'' is not a valid branch name + hint: See `man git check-ref-format` + hint: Disable this message with "git config advice.refSyntax false" + EOF + test_must_fail git branch foo..bar >actual 2>&1 && + test_cmp expect actual +' + test_done |