git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCHv2 1/8] t2024: Add tests verifying current DWIM behavior of 'git checkout <branch>'

From
Johan Herland <johan@herland.net>
Date
Apr 20, 2013, 15:05 UTC
Message-ID
<1366470363-22309-2-git-send-email-johan@herland.net>
In-Reply-To
<1366470363-22309-1-git-send-email-johan@herland.net>

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.

Signed-off-by: Johan Herland <johan@herland.net>
---
 t/t2024-checkout-dwim.sh | 66 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)
 create mode 100755 t/t2024-checkout-dwim.sh
diff --git a/t/t2024-checkout-dwim.sh b/t/t2024-checkout-dwim.sh
new file mode 100755
index 0000000..5650d21
--- /dev/null
+++ b/t/t2024-checkout-dwim.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+test_description='checkout <branch>
+
+Ensures that checkout on an unborn branch does what the user expects'
+
+. ./test-lib.sh
+
+# Arguments: <branch> <remote> <remote-tracking>
+#
+# Verify that we have checked out <branch>, and that it is at the same
+# commit as <remote-tracking>, and that has appropriate tracking config
+# setup against <remote>
+test_tracking_branch() {
+	branch=$1 &&
+	remote=$2 &&
+	remote_track=$3 &&
+	test "refs/heads/$branch" = "$(git rev-parse --symbolic-full-name HEAD)" &&
+	test "$(git rev-parse --verify HEAD)" = "$(git rev-parse --verify "$remote_track")" &&
+	test "$remote" = "$(git config "branch.$branch.remote")" &&
+	test "refs/heads/$branch" = "$(git config "branch.$branch.merge")"
+}
+
+test_expect_success 'setup' '
+	(git init repo_a &&
+	 cd repo_a &&
+	 test_commit a_master &&
+	 git checkout -b foo &&
+	 test_commit a_foo &&
+	 git checkout -b bar &&
+	 test_commit a_bar
+	) &&
+	(git init repo_b &&
+	 cd repo_b &&
+	 test_commit b_master &&
+	 git checkout -b foo &&
+	 test_commit b_foo &&
+	 git checkout -b baz &&
+	 test_commit b_baz
+	) &&
+	git remote add repo_a repo_a &&
+	git remote add repo_b repo_b &&
+	git config remote.repo_b.fetch \
+		"+refs/heads/*:refs/remotes/other_b/*" &&
+	git fetch --all
+'
+
+test_expect_success 'checkout of non-existing branch fails' '
+	test_must_fail git checkout xyzzy
+'
+
+test_expect_success 'checkout of branch from multiple remotes fails' '
+	test_must_fail git checkout foo
+'
+
+test_expect_success 'checkout of branch from a single remote succeeds #1' '
+	git checkout bar &&
+	test_tracking_branch bar repo_a refs/remotes/repo_a/bar
+'
+
+test_expect_success 'checkout of branch from a single remote succeeds #2' '
+	git checkout baz &&
+	test_tracking_branch baz repo_b refs/remotes/other_b/baz
+'
+
+test_done
-- 
1.8.1.3.704.g33f7d4f
Previous: Johan HerlandNext: Jonathan Nieder
Message 2 of 12 in “[PATCHv2 0/8] Improving the search for remote-tracking branches”
  1. Johan HerlandApr 20, 2013
  2. 1/8 t2024: Add tests verifying current DWIM behavior of 'git checkout <branch>'Johan Herland, Apr 20, 2013
  3. Jonathan NiederApr 20, 2013
  4. Johan HerlandApr 20, 2013
  5. 2/8 t2024: Show failure to use refspec when DWIMming remote branch namesJohan Herland, Apr 20, 2013
  6. 3/8 checkout: Use remote refspecs when DWIMming tracking branchesJohan Herland, Apr 20, 2013
  7. 4/8 t3200.39: tracking setup should fail if there is no matching refspec.Johan Herland, Apr 20, 2013
  8. 5/8 t7201.24: Add refspec to keep --track workingJohan Herland, Apr 20, 2013
  9. 6/8 t9114.2: Don't use --track option against "svn-remote"-tracking branchesJohan Herland, Apr 20, 2013
  10. Eric WongApr 21, 2013
  11. 7/8 branch.c: Validate tracking branches with refspecs instead of refs/remotes/*Johan Herland, Apr 20, 2013
  12. 8/8 glossary: Update and rephrase the definition of a remote-tracking branchJohan Herland, Apr 20, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.