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

Re: git checkout creates strange '(null)'-branch

From
Erik Faye-Lund <kusmabite@gmail.com>
Date
May 8, 2012, 11:50 UTC
Message-ID
<CABPQNSZjdpz8zqYOwBQ_SPeh0JqyxQ1bjrghG1vzxt0GiLR22w@mail.gmail.com>
In-Reply-To
<4FA904C3.7070208@viscovery.net>
On Tue, May 8, 2012 at 1:34 PM, Johannes Sixt <j.sixt@viscovery.net> wrote:
Show 11 quoted lines
> Am 5/8/2012 13:24, schrieb Erik Faye-Lund:
>> +             git show-ref | !grep "(null)"
> ...
>> Now, doing this doesn't make my test above pass,
>
> It is unlikely to pass if you used this test script literally, because
>
>   $ !grep
>   !grep: command not found
>
> "!" is not an operator in a shell script. Insert a space.

Whoops, you are of course right, my bad. Actually, inserting a space doesn't quite do the trick, but doing "! (git show-ref | grep "(null)")" instead seems to work. I'm no shell-script wizard ;)

The test still fails, though :)

I didn't actually fix up the code so the test failed, so I guess that's why I didn't see a false negative. Instead, I fixed up the code so it failed earlier.

But if I apply the following patch, the test passes. I'm not saying it's the right thing to do, though.

(Warning: white-space damaged because of copying diffs between terminals)
---8<---
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 23fc56d..d70e819 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1096,8 +1096,11 @@ int cmd_checkout(int argc, const char **argv,
const char *prefix)
                int flag;

                if (!read_ref_full("HEAD", rev, 0, &flag) &&
-                   (flag & REF_ISSYMREF) && is_null_sha1(rev))
+                   (flag & REF_ISSYMREF) && is_null_sha1(rev)) {
+                       if (!opts.new_branch)
+                               return 0;
                        return switch_unborn_to_new_branch(&opts);
+               }
        }
        return switch_branches(&opts, &new);
 }
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index 81827e6..5b1932c 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -520,4 +520,16 @@ test_expect_success 'moving the superproject does
not break submodules' '
        )
 '

+test_expect_failure 'committing to empty submodule does not create
(null) branch' '
+       test_create_repo empty-repo &&
+       git submodule add ./empty-repo empty-submodule &&
+       (
+               cd empty-submodule &&
+               echo "foo" > bar.txt &&
+               git add bar.txt &&
+               git commit -m. &&
+               ! (git show-ref | grep "(null)")
+       )
+'
+
 test_done
---8<---
Previous: Johannes SixtNext: Andreas Schwab
Message 3 of 12 in “git checkout creates strange '(null)'-branch”
  1. Erik Faye-LundMay 8, 2012
  2. Johannes SixtMay 8, 2012
  3. Erik Faye-LundMay 8, 2012
  4. Andreas SchwabMay 8, 2012
  5. Junio C HamanoMay 8, 2012
  6. Erik Faye-LundMay 10, 2012
  7. Junio C HamanoMay 10, 2012
  8. Junio C HamanoMay 8, 2012
  9. Junio C HamanoMay 8, 2012
  10. Junio C HamanoMay 8, 2012
  11. Erik Faye-LundMay 8, 2012
  12. Junio C HamanoMay 8, 2012

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.