Repository: python-versioneer/python-versioneer

Issue:
Error using old Git versions
Versioneer appears to make some assumptions about the Git version being used. In particular, the version 1.7.1 included in CentOS 6 causes errors like the following. Ideally, this would be fixed so that I don't have to give my users special instructions on how to update their Git. However, at a minimum, versioneer should detect the error so that I get a `0+unknown` version instead of a hard error. Right now I can't even install the software on such machines.

This is using versioneer 0.18 from conda-forge.

Repro steps at bottom of post.

```
Begin: setup.py build_ext --xtcdata=/lcls2/install -f --inplace
usage: git rev-list [OPTION] <commit-id>... [ -- paths... ]
  limiting output:
    --max-count=nr
    --max-age=epoch
    --min-age=epoch
    --sparse
    --no-merges
    --remove-empty
    --all
    --branches
    --tags
    --remotes
    --stdin
    --quiet
  ordering output:
    --topo-order
    --date-order
    --reverse
  formatting output:
    --parents
    --children
    --objects | --objects-edge
    --unpacked
    --header | --pretty
    --abbrev=nr | --no-abbrev
    --abbrev-commit
    --left-right
  special purpose:
    --bisect
    --bisect-vars
    --bisect-all
Traceback (most recent call last):
  File "setup.py", line 45, in <module>
    version=versioneer.get_version(),
  File "/lcls2/psana/versioneer.py", line 1480, in get_version
    return get_versions()["version"]
  File "/lcls2/psana/versioneer.py", line 1453, in get_versions
    pieces = from_vcs_f(cfg.tag_prefix, root, verbose)
  File "/lcls2/psana/versioneer.py", line 1110, in git_pieces_from_vcs
    pieces["distance"] = int(count_out)  # total number of commits
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
```

Reproduction steps:

```
docker run -ti slaclcls/travis:centos6-py3.6-versioneer-bug187
```

From inside this container, run:

```
source activate myrel
git clone https://github.com/slac-lcls/lcls2.git
cd lcls2
./build_travis.sh -p install
```
failure with git 1.7.1
I have packages using versioneer that works fine with git 2.5.0 but that I cannot install using easy_install (`python setup.py install` or `python setup.py develop`).

```
$ python setup.py develop
usage: git rev-list [OPTION] <commit-id>... [ -- paths... ]
 limiting output:
   --max-count=nr
   --max-age=epoch
   --min-age=epoch
   --sparse
   --no-merges
   --remove-empty
   --all
   --branches
   --tags
   --remotes
   --stdin
   --quiet
 ordering output:
   --topo-order
   --date-order
   --reverse
 formatting output:
   --parents
   --children
   --objects | --objects-edge
   --unpacked
   --header | --pretty
   --abbrev=nr | --no-abbrev
   --abbrev-commit
   --left-right
 special purpose:
   --bisect
   --bisect-vars
   --bisect-all
Traceback (most recent call last):
 File "setup.py", line 6, in <module>
   version=versioneer.get_version(),
 File "/mypackage/versioneer.py", line 1405, in get_version
   return get_versions()["version"]
 File "/mypackage/versioneer.py", line 1380, in get_versions
   pieces = from_vcs_f(cfg.tag_prefix, root, verbose)
 File "/mypackage/versioneer.py", line 1076, in git_pieces_from_vcs
   pieces["distance"] = int(count_out)  # total number of commits
TypeError: int() argument must be a string or a number, not 'NoneType'
```

Is 1.7.1 supported? If not, how can I workaround this issue on systems using this old version?


Discussion:

Patch:
diff --git a/src/git/from_vcs.py b/src/git/from_vcs.py
index eabbf04..1be3fca 100644
--- a/src/git/from_vcs.py
+++ b/src/git/from_vcs.py
@@ -136,8 +136,8 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command):
     else:
         # HEX: no tags
         pieces["closest-tag"] = None
-        count_out, rc = runner(GITS, ["rev-list", "HEAD", "--count"], cwd=root)
-        pieces["distance"] = int(count_out)  # total number of commits
+        out, rc = runner(GITS, ["rev-list", "HEAD", "--left-right"], cwd=root)
+        pieces["distance"] = len(out.split())  # total number of commits
 
     # commit date: see ISO-8601 comment in git_versions_from_keywords()
     date = runner(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[0].strip()


Test patch:
diff --git a/test/git/test_git.py b/test/git/test_git.py
index a8ee5ad..5f8683a 100644
--- a/test/git/test_git.py
+++ b/test/git/test_git.py
@@ -40,7 +40,7 @@ class ParseGitDescribe(unittest.TestCase):
                     else:
                         return "longlong\n", 0
                 if args[0] == "rev-list":
-                    return "42\n", 0
+                    return ">hashhashhashhashhashhashhashhash\n" * 42, 0
                 if args[0] == "show":
                     if do_error == "show":
                         return "gpg: signature\n12345\n", 0