git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tay Ray Chuan <rctay89@gmail.com>
To: Git Mailing List <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>, Jeff King <peff@peff.net>,
	Clemens Buchacher <drizzd@aon.at>,
	Michel Lespinasse <walken@google.com>,
	Erick Mattos <erick.mattos@gmail.com>
Subject: [PATCH 1/3] add tests for checkout -b
Date: Tue, 22 Jun 2010 01:19:40 +0800	[thread overview]
Message-ID: <1277140782-4064-2-git-send-email-rctay89@gmail.com> (raw)
In-Reply-To: <1277140782-4064-1-git-send-email-rctay89@gmail.com>

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---
 t/t2018-checkout-branch.sh |  121 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 121 insertions(+), 0 deletions(-)
 create mode 100755 t/t2018-checkout-branch.sh

diff --git a/t/t2018-checkout-branch.sh b/t/t2018-checkout-branch.sh
new file mode 100755
index 0000000..3c13065
--- /dev/null
+++ b/t/t2018-checkout-branch.sh
@@ -0,0 +1,121 @@
+#!/bin/sh
+
+test_description='checkout '
+
+. ./test-lib.sh
+
+# Arguments: <branch> <sha> [<checkout options>]
+#
+# Runs "git checkout" to switch to <branch>, testing that
+#
+#   1) we are on the specified branch, <branch>;
+#   2) HEAD is <sha>; if <sha> is not specified, the old HEAD is used.
+#
+# If <checkout options> is not specified, "git checkout" is run with -b.
+do_checkout() {
+	exp_branch=$1 &&
+	exp_ref="refs/heads/$exp_branch" &&
+
+	# if <sha> is not specified, use HEAD.
+	exp_sha=${2:-$(git rev-parse --verify HEAD)} &&
+
+	# default options for git checkout: -b
+	if [ -z "$3" ]; then
+		opts="-b"
+	else
+		opts="$3"
+	fi
+
+	git checkout $opts $exp_branch $exp_sha &&
+
+	test $exp_ref = $(git rev-parse --symbolic-full-name HEAD) &&
+	test $exp_sha = $(git rev-parse --verify HEAD)
+}
+
+test_dirty_unmergeable() {
+	! git diff --exit-code >/dev/null
+}
+
+setup_dirty_unmergeable() {
+	echo >>file1 change2
+}
+
+test_dirty_mergeable() {
+	! git diff --cached --exit-code >/dev/null
+}
+
+setup_dirty_mergeable() {
+	echo >file2 file2 &&
+	git add file2
+}
+
+test_expect_success 'setup' '
+	test_commit initial file1 &&
+	HEAD1=$(git rev-parse --verify HEAD) &&
+
+	test_commit change1 file1 &&
+	HEAD2=$(git rev-parse --verify HEAD) &&
+
+	git branch -m branch1
+'
+
+test_expect_success 'checkout -b to a new branch, set to HEAD' '
+	do_checkout branch2
+'
+
+test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
+	git checkout branch1 &&
+	git branch -D branch2 &&
+
+	do_checkout branch2 $HEAD1
+'
+
+test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
+	git checkout branch1 &&
+
+	# clean up from previous test
+	git branch -D branch2 &&
+
+	setup_dirty_unmergeable &&
+	test_must_fail do_checkout branch2 $HEAD1 &&
+	test_dirty_unmergeable
+'
+
+test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' '
+	# still dirty and on branch1
+	do_checkout branch2 $HEAD1 "-f -b" &&
+	test_must_fail test_dirty_unmergeable
+'
+
+test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
+	git checkout branch1 &&
+
+	# clean up from previous test
+	git branch -D branch2 &&
+
+	setup_dirty_mergeable &&
+	do_checkout branch2 $HEAD1 &&
+	test_dirty_mergeable
+'
+
+test_expect_success 'checkout -f -b to a new branch with mergeable changes discards changes' '
+	# clean up from previous test
+	git reset --hard &&
+
+	git checkout branch1 &&
+
+	# clean up from previous test
+	git branch -D branch2 &&
+
+	setup_dirty_mergeable &&
+	do_checkout branch2 $HEAD1 "-f -b" &&
+	test_must_fail test_dirty_mergeable
+'
+
+test_expect_success 'checkout -b to an existing branch fails' '
+	git reset --hard HEAD &&
+
+	test_must_fail do_checkout branch2 $HEAD2
+'
+
+test_done
-- 
1.7.1.513.g4f18

  reply	other threads:[~2010-06-21 17:20 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-20 16:33 [PATCH 0/3] DWIM with "git checkout -f -b" Tay Ray Chuan
2010-06-20 16:33 ` [PATCH 1/3] add tests for checkout -b Tay Ray Chuan
2010-06-20 16:33   ` [PATCH 2/3] t2018-checkout-branch.sh: show that checkout -f -b doesn't DWIM Tay Ray Chuan
2010-06-20 16:33     ` [PATCH 3/3] builtin/checkout: DWIM with -f -b Tay Ray Chuan
2010-06-20 19:12       ` Junio C Hamano
2010-06-20 20:11         ` Jeff King
2010-06-20 21:07           ` Clemens Buchacher
2010-06-20 21:10           ` Junio C Hamano
2010-06-20 21:16             ` Jeff King
2010-06-21  0:09               ` Junio C Hamano
2010-06-21  1:08                 ` Tay Ray Chuan
2010-06-21  5:15                   ` Junio C Hamano
2010-06-21 17:19                     ` [PATCH 0/3] teach git-checkout -B WAS " Tay Ray Chuan
2010-06-21 17:19                       ` Tay Ray Chuan [this message]
2010-06-21 17:19                         ` [PATCH 2/3] builtin/checkout: change -b from an OPTION_STRING to a OPTION_SET_INT Tay Ray Chuan
2010-06-21 17:19                           ` [PATCH 3/3] builtin/checkout: learn -B Tay Ray Chuan
2010-06-23 18:36                             ` Junio C Hamano
2010-06-23 19:13                               ` Tay Ray Chuan
2010-06-21 23:04                           ` [PATCH 2/3] builtin/checkout: change -b from an OPTION_STRING to a OPTION_SET_INT Erick Mattos
2010-06-23 19:04                             ` Tay Ray Chuan
2010-06-23 21:37                               ` Erick Mattos
2010-06-21 17:24                       ` [PATCH 0/3] teach git-checkout -B WAS builtin/checkout: DWIM with -f -b Tay Ray Chuan
2010-06-21 21:30                         ` Erick Mattos
2010-06-23 19:28                       ` [PATCH v2 " Tay Ray Chuan
2010-06-23 19:28                         ` [PATCH v2 1/3] add tests for checkout -b Tay Ray Chuan
2010-06-23 19:28                           ` [PATCH v2 2/3] builtin/checkout: reword hint for -b Tay Ray Chuan
2010-06-23 19:29                             ` [PATCH v2 3/3] builtin/checkout: learn -B Tay Ray Chuan
2010-06-21  2:58               ` [PATCH 3/3] builtin/checkout: DWIM with -f -b Michel Lespinasse
2010-06-21  4:09                 ` Michel Lespinasse

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1277140782-4064-2-git-send-email-rctay89@gmail.com \
    --to=rctay89@gmail.com \
    --cc=drizzd@aon.at \
    --cc=erick.mattos@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    --cc=walken@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).