Repository: pypa/setuptools_scm

Issue:
7.0.0+ Regression: Calling GitWorkdir.git_branch inserts a newline
We use a version scheme for development that adds a branch name when the package is built on a git branch.

Recently we found 7.0.0+ changed the behavior of git_branch with the trailing `--` (apparently to add Hg support?)... Now, the "branch" variable consistently adds a newline and a trailing `--`, which breaks compatibility with PEP and causes other surprising failures downstream.

The code traces to this line here which changed in the 7.0.0 merge: https://github.com/pypa/setuptools_scm/blob/main/src/setuptools_scm/git.py#L85

Running raw git command with Ubuntu 20.04, on a branch called "test-branch" results in:

```
$ git --version
git version 2.25.1
$ git rev-parse --abbrev-ref HEAD --
test-branch
--
$
```

And then inside a python environment:

```
>>> from setuptools_scm import git
>>> git.parse(".")
<ScmVersion 7.0.2 dist=None node=gf1f35bd dirty=False branch=test-branch
-->
>>> 
```


Discussion:

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 997d10b..4c9bd24 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -1,5 +1,10 @@
+v7.0.3
+=======
+* fix mercurial usage when pip primes a isolated environment
+* fix regression for branch names on git + add a test
+
 v7.0.2
-======
+=======
 
 * fix #723 and #722: remove bootstrap dependencies
 * bugfix: ensure we read the distribution name from setup.cfg
diff --git a/src/setuptools_scm/git.py b/src/setuptools_scm/git.py
index 1f8984b..47e2a9e 100644
--- a/src/setuptools_scm/git.py
+++ b/src/setuptools_scm/git.py
@@ -82,10 +82,10 @@ class GitWorkdir(Workdir):
         return bool(out)
 
     def get_branch(self) -> str | None:
-        branch, err, ret = self.do_ex_git(["rev-parse", "--abbrev-ref", "HEAD", "--"])
+        branch, err, ret = self.do_ex_git(["rev-parse", "--abbrev-ref", "HEAD"])
         if ret:
             trace("branch err", branch, err, ret)
-            branch, err, ret = self.do_ex_git(["symbolic-ref", "--short", "HEAD", "--"])
+            branch, err, ret = self.do_ex_git(["symbolic-ref", "--short", "HEAD"])
             if ret:
                 trace("branch err (symbolic-ref)", branch, err, ret)
                 return None


Test patch:
diff --git a/testing/test_git.py b/testing/test_git.py
index 661a5ee..cb54f61 100644
--- a/testing/test_git.py
+++ b/testing/test_git.py
@@ -378,6 +378,14 @@ def test_git_archive_run_from_subdirectory(
     assert integration.find_files(".") == [opj(".", "test1.txt")]
 
 
+@pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/728")
+def test_git_branch_names_correct(wd: WorkDir) -> None:
+    wd.commit_testfile()
+    wd("git checkout -b test/fun")
+    wd_git = git.GitWorkdir(os.fspath(wd.cwd))
+    assert wd_git.get_branch() == "test/fun"
+
+
 def test_git_feature_branch_increments_major(wd: WorkDir) -> None:
     wd.commit_testfile()
     wd("git tag 1.0.0")