bashgitbats-core

Git: "Not a valid object name HEAD"


I'm a Git novice and am testing some Git tool scripts via BATS (Bash Automated Testing System).

I'm getting the following error:

fatal: Not a valid object name HEAD

The test does the following setup before running the tooling script:

git remote add origin my_repo/.git  # Create repository
git switch --create main  # Create main branch
git commit --allow-empty -m "Initial commit"  # Initial commit on main

git tag v11.0.0  # Create a tag on main

git switch --create release/12.0.0  # Create release branch
git commit --allow-empty -m "Commit for v12.0.0"  # Initial commit on release branch

git merge tags/v11.0.0  # Merge tag onto the release branch

git switch main  # Switch back to main
git commit --allow-empty -m "Subsequent commit"  # Another commit on main
git config --add receive.denyCurrentBranch ignore  # Allow direct pushes to the checked-out branch

The test then runs the tooling script, which tries to get the latest tag in the main branch:

LATEST_TAG=$(git describe --tags $(git rev-list --tags --max-count=1))

... which is where the error ("fatal: Not a valid object name HEAD") occurs.

Would anyone be able to advise what the issue is, please?


Solution

  • Not shown in the question above, but the problem was to do with how I invoked Git in the following command (specifically, the "git rev-list" part):

    LATEST_TAG=$(git describe --tags $(git rev-list --tags --max-count=1))
    

    Once I had fixed this, the BATS test worked correctly.