Skip to contentSkip to conversation

checkout: add a test for creating a new branch with regexp as a starting point

PATCH In review Dmitry V. Levin 5 participants 17 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkout: add a test for creating a new branch with regexp as a starting point

---

This is just a regression test for the bug.

 t/t2018-checkout-new-branch-by-regexp.sh |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)
 create mode 100755 t/t2018-checkout-new-branch-by-regexp.sh
t/t2018-checkout-new-branch-by-regexp.sh new file +22 -0
new file mode 100755
index 0000000..78e54c5
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+test_description='checkout -b new_branch :/regexp'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+	echo a > a &&
+	git add a &&
+	test_tick &&
+	git commit -m first &&
+	echo b > b &&
+	git add b &&
+	test_tick &&
+	git commit -m second
+'
+
+test_expect_success checkout '
+	git checkout -b new_branch :/first
+'
+
+test_done
Signature · 1 line
ldv
#1 lore raw
Quoted 5 lines · Dmitry V. Levin

---

This is just a regression test for the bug.

Thanks, always good to have more tests.

Quoted 16 lines

 t/t2018-checkout-new-branch-by-regexp.sh |   22 ++++++++++++++++++++++

 1 files changed, 22 insertions(+), 0 deletions(-)

 create mode 100755 t/t2018-checkout-new-branch-by-regexp.sh

t/t2018-checkout-new-branch-by-regexp.sh new file +6 -0
new file mode 100755
index 0000000..78e54c5
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+test_description='checkout -b new_branch :/regexp'
+
+. ./test-lib.sh
+

t/t2018-checkout-new-branch-by-regexp.sh line 16

Quoted 10 lines
t/t2018-checkout-new-branch-by-regexp.sh new file +10 -0
+test_expect_success setup '
+       echo a > a &&
+       git add a &&
+       test_tick &&
+       git commit -m first &&
+       echo b > b &&
+       git add b &&
+       test_tick &&
+       git commit -m second
+'

This should use test_commit (see t/README), but...

t/t2018-checkout-new-branch-by-regexp.sh line 22

Quoted 5 lines
t/t2018-checkout-new-branch-by-regexp.sh new file +5 -0
+test_expect_success checkout '
+       git checkout -b new_branch :/first
+'
+
+test_done

...it looks like this can just be added to the end of t2018-checkout-branch.sh instead of creating a new test. Creating a new file just for a single test for such a simple feature is a bit of an overkill.

#2 lore raw
Quoted 11 lines · Ævar Arnfjörð Bjarmason

t/t2018-checkout-new-branch-by-regexp.sh line 22

Quoted 5 lines · Dmitry V. Levin
t/t2018-checkout-new-branch-by-regexp.sh new file +5 -0
+test_expect_success checkout '
+       git checkout -b new_branch :/first
+'
+
+test_done

...it looks like this can just be added to the end of t2018-checkout-branch.sh instead of creating a new test. Creating a new file just for a single test for such a simple feature is a bit of an overkill.

It should also use test_expect_failure unless you expect to have a fix soon, otherwise it would stop the test suite from running through.

Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.ch
#3 lore raw
Quoted 1 line · Ævar Arnfjörð Bjarmason

On Thu, Jul 29, 2010 at 22:01, Dmitry V. Levin wrote:

[...]

Quoted 11 lines
Quoted 10 lines

+test_expect_success setup '
+       echo a > a &&
+       git add a &&
+       test_tick &&
+       git commit -m first &&
+       echo b > b &&
+       git add b &&
+       test_tick &&
+       git commit -m second
+'

Quoted 1 line

This should use test_commit (see t/README), but...

The peculiarity of this bug makes it impossible, because test_commit() also creates a tag which spoils test conditions.

Quoted 10 lines

t/t2018-checkout-new-branch-by-regexp.sh line 22

Quoted 5 lines
t/t2018-checkout-new-branch-by-regexp.sh new file +5 -0
+test_expect_success checkout '
+       git checkout -b new_branch :/first
+'
+
+test_done

...it looks like this can just be added to the end of t2018-checkout-branch.sh instead of creating a new test. Creating a new file just for a single test for such a simple feature is a bit of an overkill.

Well, I see no t2018-checkout-branch.sh yet. What file do you suggest appending?

Quoted 2 lines · Thomas Rast

It should also use test_expect_failure unless you expect to have a fix soon, otherwise it would stop the test suite from running through.

Of course I'd like to have this bug fixed, but OK, let it be test_expect_failure() for now.

Signature · 1 line
ldv
#4 lore raw
Quoted 14 lines · Dmitry V. Levin
Quoted 1 line · Ævar Arnfjörð Bjarmason

On Thu, Jul 29, 2010 at 22:01, Dmitry V. Levin wrote:

[...]

Quoted 11 lines
Quoted 10 lines

+test_expect_success setup '
+       echo a > a &&
+       git add a &&
+       test_tick &&
+       git commit -m first &&
+       echo b > b &&
+       git add b &&
+       test_tick &&
+       git commit -m second
+'

Quoted 4 lines
Quoted 1 line

This should use test_commit (see t/README), but...

The peculiarity of this bug makes it impossible, because test_commit() also creates a tag which spoils test conditions.

I didn't know that. It'd be good if the commit message or a comment indicated that. And actually, we should probably have a test_commit_notag() then.

Quoted 11 lines
Quoted 10 lines

t/t2018-checkout-new-branch-by-regexp.sh line 22

Quoted 5 lines
t/t2018-checkout-new-branch-by-regexp.sh new file +5 -0
+test_expect_success checkout '
+       git checkout -b new_branch :/first
+'
+
+test_done

...it looks like this can just be added to the end of t2018-checkout-branch.sh instead of creating a new test. Creating a new file just for a single test for such a simple feature is a bit of an overkill.

Quoted 2 lines

Well, I see no t2018-checkout-branch.sh yet.  What file do you suggest appending?

Ah, t2018-checkout-branch.sh only exists on the pu branch, not master/next. It's probably worthwhile to patch it anyway rather than adding a new one.

#5 lore raw

When to use test_commit (Re: [PATCH] checkout: add a test for creating a new branch with regexp as a starting point)

Quoted 2 lines · Ævar Arnfjörð Bjarmason

And actually, we should probably have a
test_commit_notag() then.

I don’t know. What’s so bad about using "git commit" directly?

I often find myself using "git commit" in tests because test_commit imposes all the usual restrictions for a ref name on the commit message. I would happily use an abbreviation for

test_tick &&
git commit -m "something" &&
git tag something-else

if available because I don’t like typing, but would that help the reader and test runner any?

(That’s not a rhetorical question. A patch that answers in the positive would be fine by me. ;-))

#6 lore raw

Re: When to use test_commit (Re: [PATCH] checkout: add a test for creating a new branch with regexp as a starting point)

Quoted 6 lines · Jonathan Nieder
Quoted 2 lines · Ævar Arnfjörð Bjarmason

And actually, we should probably have a
test_commit_notag() then.

I don’t know.  What’s so bad about using "git commit" directly?

Nothing, it's just easier to write and read the tests when we have and use functions for these common operations.

Quoted 10 lines

I often find myself using "git commit" in tests because test_commit imposes all the usual restrictions for a ref name on the commit message.  I would happily use an abbreviation for

 test_tick &&
 git commit -m "something" &&
 git tag something-else

if available because I don’t like typing, but would that help the reader and test runner any?

Maybe just introduce a fourth argument to test_commit, to make it <message> [<file> [<contents> [<tagname>]] instead of <message> [<file> [<contents>]] ?

#7 lore raw

PATCH 1/2 test-lib.sh: introduce 4th argument to test_commit() specifying a tag name

When fourth argument to test_commit() is specified, use it as a tag name, unless it equals to empty string. In the latter case, skip tag creation.

---
 t/test-lib.sh |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
t/test-lib.sh +5 -3
index 367f053..a203383 100644
@@ -277,10 +277,12 @@ test_tick () {
 	export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
 }
 
-# Call test_commit with the arguments "<message> [<file> [<contents>]]"
+# Call test_commit with the arguments
+# "<message> [<file> [<contents> [<tagname>]]]"
 #
 # This will commit a file with the given contents and the given commit
-# message.  It will also add a tag with <message> as name.
+# message.  It will also add a tag with the given name unless the latter
+# is the empty string.
 #
 # Both <file> and <contents> default to <message>.
 
@@ -290,7 +292,7 @@ test_commit () {
 	git add "$file" &&
 	test_tick &&
 	git commit -m "$1" &&
-	git tag "$1"
+	if test -n "${4-$1}"; then git tag "${4-$1}"; fi
 }
 
 # Call test_merge with the arguments "<message> <commit>", where <commit>
Signature · 1 line
ldv
#8 lore raw

Re: [PATCH 1/2] test-lib.sh: introduce 4th argument to test_commit() specifying a tag name

Quoted 3 lines · Dmitry V. Levin

When fourth argument to test_commit() is specified, use it as a tag name, unless it equals to empty string.  In the latter case, skip tag creation.

Looks good, mostly.

Quoted 1 line

Seems like you sent a ISO-8859-1 E-Mail with UTF-8 content.

My name also doubles as a UUID and a UTF-8 canary, you see.

Quoted 18 lines

---
 t/test-lib.sh |    8 +++++---

 1 files changed, 5 insertions(+), 3 deletions(-)
t/test-lib.sh +0 -0
index 367f053..a203383 100644
@@ -277,10 +277,12 @@ test_tick () {

       export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
 }

t/test-lib.sh +2 -1
-# Call test_commit with the arguments "<message> [<file> [<contents>]]"
+# Call test_commit with the arguments
+# "<message> [<file> [<contents> [<tagname>]]]"

 #
 # This will commit a file with the given contents and the given commit

t/test-lib.sh test_tick () { · line 285

Quoted 3 lines
t/test-lib.sh +2 -1
-# message.  It will also add a tag with <message> as name.
+# message.  It will also add a tag with the given name unless the latter
+# is the empty string.

You should also patch t/README, which documents this function.

Quoted 8 lines

 # Both <file> and <contents> default to <message>.

t/test-lib.sh +0 -0
@@ -290,7 +292,7 @@ test_commit () {

       git add "$file" &&
       test_tick &&
       git commit -m "$1" &&
-       git tag "$1"
+       if test -n "${4-$1}"; then git tag "${4-$1}"; fi

This is just a nit, but I'd write this on 4 lines:

if test..
then
git tag
fi

#9 lore raw

PATCH v2 1/2 test-lib.sh: introduce 4th argument to test_commit() specifying a tag name

When fourth argument to test_commit() is specified, use it as a tag name, unless it equals to empty string. In the latter case, skip tag creation.

---
 t/README      |    8 ++++----
 t/test-lib.sh |   11 ++++++++---
 2 files changed, 12 insertions(+), 7 deletions(-)
t/README +4 -4
index 0d1183c..18f5810 100644
@@ -388,13 +388,13 @@ library for your script to use.
    committer times to defined stated.  Subsequent calls will
    advance the times by a fixed amount.
 
- - test_commit <message> [<filename> [<contents>]]
+ - test_commit <message> [<filename> [<contents> [<tagname>]]]
 
    Creates a commit with the given message, committing the given
    file with the given contents (default for both is to reuse the
-   message string), and adds a tag (again reusing the message
-   string as name).  Calls test_tick to make the SHA-1s
-   reproducible.
+   message string), and adds a tag with the given name (default again
+   is to reuse the message string as name) unless this name is the
+   empty string.  Calls test_tick to make the SHA-1s reproducible.
 
  - test_merge <message> <commit-or-tag>
 
t/test-lib.sh +8 -3
index e5523dd..ebb6215 100644
@@ -277,10 +277,12 @@ test_tick () {
 	export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
 }
 
-# Call test_commit with the arguments "<message> [<file> [<contents>]]"
+# Call test_commit with the arguments
+# "<message> [<file> [<contents> [<tagname>]]]"
 #
 # This will commit a file with the given contents and the given commit
-# message.  It will also add a tag with <message> as name.
+# message.  It will also add a tag with the given name unless the latter
+# is the empty string.
 #
 # Both <file> and <contents> default to <message>.
 
@@ -290,7 +292,10 @@ test_commit () {
 	git add "$file" &&
 	test_tick &&
 	git commit -m "$1" &&
-	git tag "$1"
+	if test -n "${4-$1}"
+	then
+		git tag "${4-$1}"
+	fi
 }
 
 # Call test_merge with the arguments "<message> <commit>", where <commit>
Signature · 1 line
ldv
Acked-by on v2 1/2
#10 lore raw

Re: [PATCH 1/2 v2] test-lib.sh: introduce 4th argument to test_commit() specifying a tag name

Quoted 63 lines · Dmitry V. Levin

When fourth argument to test_commit() is specified, use it as a tag name, unless it equals to empty string.  In the latter case, skip tag creation.

---
 t/README      |    8 ++++----
 t/test-lib.sh |   11 ++++++++---

 2 files changed, 12 insertions(+), 7 deletions(-)
t/README +0 -0
index 0d1183c..18f5810 100644
@@ -388,13 +388,13 @@ library for your script to use.

   committer times to defined stated.  Subsequent calls will advance the times by a fixed amount.

- - test_commit <message> [<filename> [<contents>]]
+ - test_commit <message> [<filename> [<contents> [<tagname>]]]

   Creates a commit with the given message, committing the given file with the given contents (default for both is to reuse the
-   message string), and adds a tag (again reusing the message
-   string as name).  Calls test_tick to make the SHA-1s
-   reproducible.
+   message string), and adds a tag with the given name (default again
+   is to reuse the message string as name) unless this name is the
+   empty string.  Calls test_tick to make the SHA-1s reproducible.

 - test_merge <message> <commit-or-tag>

t/test-lib.sh +0 -0
index e5523dd..ebb6215 100644
@@ -277,10 +277,12 @@ test_tick () {

       export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
 }

t/test-lib.sh +2 -1
-# Call test_commit with the arguments "<message> [<file> [<contents>]]"
+# Call test_commit with the arguments
+# "<message> [<file> [<contents> [<tagname>]]]"

 #
 # This will commit a file with the given contents and the given commit

t/test-lib.sh +2 -1
-# message.  It will also add a tag with <message> as name.
+# message.  It will also add a tag with the given name unless the latter
+# is the empty string.

 #
 # Both <file> and <contents> default to <message>.

t/test-lib.sh +0 -0
@@ -290,7 +292,10 @@ test_commit () {

       git add "$file" &&
       test_tick &&
       git commit -m "$1" &&
-       git tag "$1"
+       if test -n "${4-$1}"
+       then
+               git tag "${4-$1}"
+       fi
 }

Quoted 1 line

 # Call test_merge with the arguments "<message> <commit>", where <commit>

This one looks good, thanks.

#11 lore raw

PATCH 2/2 checkout: add a test for creating a new branch with regexp as a starting point

In reply to Ævar Arnfjörð Bjarmason
---
 t/t2018-checkout-branch.sh |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
t/t2018-checkout-branch.sh +13 -0
index 1caffea..4d26b2a 100755
@@ -118,7 +118,20 @@ test_expect_success 'checkout -b to an existing branch fails' '
 	test_must_fail do_checkout branch2 $HEAD2
 '
 
+test_expect_failure 'checkout -b new_branch :/regexp' '
+	# clean up from previous test
+	git reset --hard &&
+
+	do_checkout old_regexp_branch branch1 &&
+	test_commit first '' '' '' &&
+	test_commit second '' '' '' &&
+
+	do_checkout new_regexp_branch :/first
+'
+
 test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
+	# clean up from previous test
+	git reset --hard &&
 	git checkout branch1 &&
 
 	do_checkout branch2 "" -B
Signature · 1 line
ldv
#12 lore raw
Quoted 14 lines · Dmitry V. Levin

---
 t/t2018-checkout-branch.sh |   13 +++++++++++++

 1 files changed, 13 insertions(+), 0 deletions(-)
t/t2018-checkout-branch.sh +0 -0
index 1caffea..4d26b2a 100755
@@ -118,7 +118,20 @@ test_expect_success 'checkout -b to an existing branch fails' '

       test_must_fail do_checkout branch2 $HEAD2
 '

t/t2018-checkout-branch.sh test_expect_success 'checkout -b to an existing branch fails' ' · line 127

Quoted 7 lines
t/t2018-checkout-branch.sh +7 -0
+test_expect_failure 'checkout -b new_branch :/regexp' '
+       # clean up from previous test
+       git reset --hard &&
+
+       do_checkout old_regexp_branch branch1 &&
+       test_commit first '' '' '' &&
+       test_commit second '' '' '' &&

Should note in the commit message or a comment that this test needs to not create a tag, in case someone wonders down the line what the '' '' '' ASCII art is all about :)

Looks good otherwise, thanks for patching test_commit.

#13 lore raw

PATCH v2 2/2 checkout: add a test for creating a new branch with regexp as a starting point

---
 t/t2018-checkout-branch.sh |   25 +++++++++++++++++++++----
 1 files changed, 21 insertions(+), 4 deletions(-)
t/t2018-checkout-branch.sh +21 -4
index 1caffea..6ada870 100755
@@ -4,12 +4,13 @@ test_description='checkout '
 
 . ./test-lib.sh
 
-# Arguments: <branch> <sha> [<checkout options>]
+# Arguments: <branch> <start_point> [<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.
+#   2) HEAD points to the same commit as <start_point> does;
+#      if <start_point> is not specified, the old HEAD is used.
 #
 # If <checkout options> is not specified, "git checkout" is run with -b.
 do_checkout() {
@@ -17,7 +18,8 @@ do_checkout() {
 	exp_ref="refs/heads/$exp_branch" &&
 
 	# if <sha> is not specified, use HEAD.
-	exp_sha=${2:-$(git rev-parse --verify HEAD)} &&
+	start_point=${2:-HEAD}
+	exp_sha=$(git rev-parse --verify $start_point) &&
 
 	# default options for git checkout: -b
 	if [ -z "$3" ]; then
@@ -26,7 +28,7 @@ do_checkout() {
 		opts="$3"
 	fi
 
-	git checkout $opts $exp_branch $exp_sha &&
+	git checkout $opts $exp_branch $start_point &&
 
 	test $exp_ref = $(git rev-parse --symbolic-full-name HEAD) &&
 	test $exp_sha = $(git rev-parse --verify HEAD)
@@ -118,7 +120,22 @@ test_expect_success 'checkout -b to an existing branch fails' '
 	test_must_fail do_checkout branch2 $HEAD2
 '
 
+test_expect_failure 'checkout -b new_branch :/regexp' '
+	# clean up from previous test
+	git reset --hard &&
+
+	do_checkout old_regexp_branch branch1 &&
+	# The first commit in this test should not be referenced by
+	# other branches or tags.
+	test_commit first a a "" &&
+	test_commit second &&
+
+	do_checkout new_regexp_branch :/first
+'
+
 test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
+	# clean up from previous test
+	git reset --hard &&
 	git checkout branch1 &&
 
 	do_checkout branch2 "" -B
Signature · 1 line
1.7.2.1
Acked-by on v2 2/2
#14 lore raw
Quoted 67 lines · Dmitry V. Levin

---
 t/t2018-checkout-branch.sh |   25 +++++++++++++++++++++----

 1 files changed, 21 insertions(+), 4 deletions(-)
t/t2018-checkout-branch.sh +0 -0
index 1caffea..6ada870 100755
@@ -4,12 +4,13 @@ test_description='checkout '
 

 . ./test-lib.sh

t/t2018-checkout-branch.sh +1 -1
-# Arguments: <branch> <sha> [<checkout options>]
+# Arguments: <branch> <start_point> [<checkout options>]

 #
 # Runs "git checkout" to switch to <branch>, testing that #
 #   1) we are on the specified branch, <branch>;

t/t2018-checkout-branch.sh +2 -1
-#   2) HEAD is <sha>; if <sha> is not specified, the old HEAD is used.
+#   2) HEAD points to the same commit as <start_point> does;
+#      if <start_point> is not specified, the old HEAD is used.

 #
 # If <checkout options> is not specified, "git checkout" is run with -b. do_checkout() {

t/t2018-checkout-branch.sh +0 -0
@@ -17,7 +18,8 @@ do_checkout() {

       exp_ref="refs/heads/$exp_branch" &&

       # if <sha> is not specified, use HEAD.
-       exp_sha=${2:-$(git rev-parse --verify HEAD)} &&
+       start_point=${2:-HEAD}
+       exp_sha=$(git rev-parse --verify $start_point) &&
       # default options for git checkout: -b
       if [ -z "$3" ]; then
t/t2018-checkout-branch.sh +0 -0
@@ -26,7 +28,7 @@ do_checkout() {

               opts="$3"
       fi

-       git checkout $opts $exp_branch $exp_sha &&
+       git checkout $opts $exp_branch $start_point &&

       test $exp_ref = $(git rev-parse --symbolic-full-name HEAD) && test $exp_sha = $(git rev-parse --verify HEAD)

t/t2018-checkout-branch.sh +0 -0
@@ -118,7 +120,22 @@ test_expect_success 'checkout -b to an existing branch fails' '

       test_must_fail do_checkout branch2 $HEAD2
 '

t/t2018-checkout-branch.sh +13 -0
+test_expect_failure 'checkout -b new_branch :/regexp' '
+       # clean up from previous test
+       git reset --hard &&
+
+       do_checkout old_regexp_branch branch1 &&
+       # The first commit in this test should not be referenced by
+       # other branches or tags.
+       test_commit first a a "" &&
+       test_commit second &&
+
+       do_checkout new_regexp_branch :/first
+'
+

 test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
+       # clean up from previous test
+       git reset --hard &&
       git checkout branch1 &&

Quoted 3 lines

       do_checkout branch2 "" -B
--
1.7.2.1

Looks good, thanks.

author Acked-by Ævar Arnfjörð Bjarmason
#15 lore raw

---

The only difference between v2 and v3 editions of this patch is that test_expect_failure has been replaced with test_expect_success, because the fix (jc/sha1-name-find-fix) is already available.

 t/t2018-checkout-branch.sh |   25 +++++++++++++++++++++----
 1 files changed, 21 insertions(+), 4 deletions(-)
t/t2018-checkout-branch.sh +21 -4
index 1caffea..13f7194 100755
@@ -4,12 +4,13 @@ test_description='checkout '
 
 . ./test-lib.sh
 
-# Arguments: <branch> <sha> [<checkout options>]
+# Arguments: <branch> <start_point> [<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.
+#   2) HEAD points to the same commit as <start_point> does;
+#      if <start_point> is not specified, the old HEAD is used.
 #
 # If <checkout options> is not specified, "git checkout" is run with -b.
 do_checkout() {
@@ -17,7 +18,8 @@ do_checkout() {
 	exp_ref="refs/heads/$exp_branch" &&
 
 	# if <sha> is not specified, use HEAD.
-	exp_sha=${2:-$(git rev-parse --verify HEAD)} &&
+	start_point=${2:-HEAD}
+	exp_sha=$(git rev-parse --verify $start_point) &&
 
 	# default options for git checkout: -b
 	if [ -z "$3" ]; then
@@ -26,7 +28,7 @@ do_checkout() {
 		opts="$3"
 	fi
 
-	git checkout $opts $exp_branch $exp_sha &&
+	git checkout $opts $exp_branch $start_point &&
 
 	test $exp_ref = $(git rev-parse --symbolic-full-name HEAD) &&
 	test $exp_sha = $(git rev-parse --verify HEAD)
@@ -118,7 +120,22 @@ test_expect_success 'checkout -b to an existing branch fails' '
 	test_must_fail do_checkout branch2 $HEAD2
 '
 
+test_expect_success 'checkout -b new_branch :/regexp' '
+	# clean up from previous test
+	git reset --hard &&
+
+	do_checkout old_regexp_branch branch1 &&
+	# The first commit in this test should not be referenced by
+	# other branches or tags.
+	test_commit first a a "" &&
+	test_commit second &&
+
+	do_checkout new_regexp_branch :/first
+'
+
 test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
+	# clean up from previous test
+	git reset --hard &&
 	git checkout branch1 &&
 
 	do_checkout branch2 "" -B
Signature · 1 line
ldv
#16 lore raw
In reply to Thomas Rast
Quoted 11 lines · Thomas Rast
Quoted 10 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+test_expect_success checkout '
+       git checkout -b new_branch :/first
+'
+
+test_done

...it looks like this can just be added to the end of t2018-checkout-branch.sh instead of creating a new test. Creating a new file just for a single test for such a simple feature is a bit of an overkill.

Quoted 2 lines

It should also use test_expect_failure unless you expect to have a fix soon, otherwise it would stop the test suite from running through.

I think this is an ancient bug from the very beginning of the ":/" notation. Because it temporarily uses one bit in the object flags, but the get_sha1_oneline() parser can be called multiple times, it tries to clear the flag bits it smudged at the end, but fails.

The basic pattern for traversing the ancestry while avoiding duplication is:

    prepare commits you traverse from in "list";
    while (list) {
        commit = pop_most_recent_commit(&list, MARK);
        try to use commit;
    }

pop_most_recent_commit() removes a commit from the list, pushes parents of the commit that are _not_ marked with any of the bits in the MARK to the given list, and returns that commit.

While introducing the feature ':/', however, 28a4d94 (object name:
introduce ':/<oneline prefix>' notation, 2007-02-24) wanted to avoid leaving the mark used in the loop before leaving the function (which is a right thing to do), by doing this:

    prepare commits you traverse from in "list";
+   keep a copy of "list" in "backup";
    while (list) {
        commit = pop_most_recent_commit(&list, MARK);
        try to use commit;
    }

+ for (commit in backup)
+ clear_commit_marks(commit, MARK);

which is wrong, as clear_commit_marks() will ignore if the starting commit does not have the MARK.

I think the fix should be just as simple as this. Essentially, X_SEEN mark means "we have marked and placed this item on the list (so do not put it again back on the list when it appears as a parent of some other item)" so marking commits we initially put on the list with the bit should be the right thing to do regardless of this clean-up business.

 sha1_name.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
sha1_name.c +3 -1
index 4f2af8d..b935688 100644
@@ -704,8 +704,10 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
 		die("Invalid search pattern: %s", prefix);
 
 	for_each_ref(handle_one_ref, &list);
-	for (l = list; l; l = l->next)
+	for (l = list; l; l = l->next) {
 		commit_list_insert(l->item, &backup);
+		l->item->object.flags |= ONELINE_SEEN;
+	}
 	while (list) {
 		char *p;
 		struct commit *commit;