Repository: conda/conda-build

Issue:
problems with GIT_DESCRIBE_NUMBER
Hello, my recipe is like:

```
package:
  name: pkg
  version: {{ GIT_DESCRIBE_TAG }}

source:
  git_url: .
  git_rev: conda_pkg

build:
  number: {{ GIT_DESCRIBE_NUMBER }}
...
```

and is located in a git repo.

```
user@machine:pkg_git $ git status
On branch conda_pkg
Your branch is up-to-date with 'origin/conda_pkg'.
nothing to commit, working directory clean
```

If I perform a `conda build .`, this error is produced:

```
Traceback (most recent call last):
  File "/home/user/anaconda3/bin/conda-build", line 5, in <module>
    sys.exit(main())
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/main_build.py", line 144, in main
    args_func(args, p)
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/main_build.py", line 389, in args_func
    args.func(args, p)
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/main_build.py", line 287, in execute
    verbose=False, dirty=args.dirty)
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/render.py", line 135, in render_recipe
    verbose=verbose, dirty=dirty)
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/render.py", line 87, in parse_or_try_download
    metadata.parse_again(permit_undefined_jinja=False)
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/metadata.py", line 377, in parse_again
    self.meta = parse(self._get_contents(permit_undefined_jinja), path=self.meta_path)
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/metadata.py", line 666, in _get_contents
    env.globals.update(context_processor(self, path))
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/jinja_context.py", line 130, in context_processor
    ctx = get_environ(m=initial_metadata)
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/environ.py", line 174, in get_dict
    d.update(meta_vars(m))
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/environ.py", line 276, in meta_vars
    d['PKG_BUILDNUM'] = str(meta.build_number())
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/metadata.py", line 454, in build_number
    return int(self.get_value('build/number', 0))
ValueError: invalid literal for int() with base 10: ''
```

If I substitute:

```
build:
  number: {{ GIT_DESCRIBE_NUMBER }}
```

with

```
build:
  number: 0
```

it works and the package created is something like pkg-v0.2.2-76_g869cb67.tar.bz2

Stepping into `/home/user/anaconda3/lib/python3.5/site-packages/conda_build/metadata.py:454` it seems that GIT_DESCRIBE_NUMBER is `None`...

Do you have any idea on why `GIT_DESCRIBE_NUMBER` is not working?
Thanks

---

```
$ conda build --version
conda-build 1.21.3

$ conda --version
conda 4.1.6
```



Discussion:
What does `git describe --tag` show?

How do you create your tags?  If you are not annotating them, there might be issues.  Note that tags created via the github web interface are lightweight, not annotated tags.

https://git-scm.com/book/en/v2/Git-Basics-Tagging#Annotated-Tags

To retroactively annotate tags:

```
git checkout <lightweight tag tagged via github>
git tag -a <same tag name>
<enter description/notes>
git push <github repo> --tags
```

If this does not work, then we have a bug, but I think this is the issue - we test this functionality in our integration tests, and have not seen an issue with the 1.21.3 release.

```
$ git describe --tag
v0.2.2-78-g3845222
```

v0.2.2 is annotated

OK, are these public repos (the source, and the recipe) that I can clone for debugging purposes?

No, I've created a repo here: https://github.com/elehcim/conda_git_dbg.git for which I can reproduce the issue.

```
$ conda create -n git_tag python
$ source activate git_tag
$ cd conda_git_dbg
$ git checkout conda_dbg
$ git describe --tag
ttt-1-g628dfb0
$ conda build .
Cloning into '/home/user/anaconda3/conda-bld/work'...
done.
Switched to a new branch 'conda_pkg'
An unexpected error has occurred, please consider sending the
following traceback to the conda GitHub issue tracker at:

    https://github.com/conda/conda-build/issues

Include the output of the command 'conda info' in your report.


Traceback (most recent call last):
  File "/home/user/anaconda3/bin/conda-build", line 5, in <module>
    sys.exit(main())
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/main_build.py", line 144, in main
    args_func(args, p)
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/main_build.py", line 389, in args_func
    args.func(args, p)
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/main_build.py", line 287, in execute
    verbose=False, dirty=args.dirty)
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/render.py", line 135, in render_recipe
    verbose=verbose, dirty=dirty)
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/render.py", line 87, in parse_or_try_download
    metadata.parse_again(permit_undefined_jinja=False)
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/metadata.py", line 377, in parse_again
    self.meta = parse(self._get_contents(permit_undefined_jinja), path=self.meta_path)
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/metadata.py", line 666, in _get_contents
    env.globals.update(context_processor(self, path))
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/jinja_context.py", line 130, in context_processor
    ctx = get_environ(m=initial_metadata)
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/environ.py", line 174, in get_dict
    d.update(meta_vars(m))
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/environ.py", line 276, in meta_vars
    d['PKG_BUILDNUM'] = str(meta.build_number())
  File "/home/user/anaconda3/lib/python3.5/site-packages/conda_build/metadata.py", line 454, in build_number
    return int(self.get_value('build/number', 0))
ValueError: invalid literal for int() with base 10: ''
```

---

```
$ conda info
Current conda install:

             platform : linux-64
        conda version : 4.1.6
    conda-env version : 2.5.1
  conda-build version : 1.21.3
       python version : 3.5.1.final.0
     requests version : 2.9.1
     root environment : /astri03/data1/mastropietro/anaconda3  (writable)
  default environment : /astri03/data1/mastropietro/anaconda3/envs/git_tag
     envs directories : /astri03/data1/mastropietro/anaconda3/envs
        package cache : /astri03/data1/mastropietro/anaconda3/pkgs
         channel URLs : https://repo.continuum.io/pkgs/free/linux-64/
                        https://repo.continuum.io/pkgs/free/noarch/
                        https://repo.continuum.io/pkgs/pro/linux-64/
                        https://repo.continuum.io/pkgs/pro/noarch/
          config file : None
         offline mode : False
    is foreign system : False
```

I've found a workaround. Changing to:

```
build:
  number: {{ environ.get('GIT_DESCRIBE_NUMBER', 0)}}
```

works.

OK, thanks.  That's helpful for debugging, though it should work either way.  I'll update the tests and make sure.

maybe related to #357 ?


Resolving patch:
diff --git a/conda_build/metadata.py b/conda_build/metadata.py
--- a/conda_build/metadata.py
+++ b/conda_build/metadata.py
@@ -451,7 +451,9 @@ def version(self):
         return res
 
     def build_number(self):
-        return int(self.get_value('build/number', 0))
+        number = self.get_value('build/number', 0)
+        # build number can come back as None if no setting (or jinja intermediate)
+        return int(number) if number else 0
 
     def ms_depends(self, typ='run'):
         res = []


Test patch:
diff --git a/tests/test-recipes/metadata/_git_describe_number_branch/meta.yaml b/tests/test-recipes/metadata/_git_describe_number_branch/meta.yaml
new file mode 100644
--- /dev/null
+++ b/tests/test-recipes/metadata/_git_describe_number_branch/meta.yaml
@@ -0,0 +1,11 @@
+package:
+  name: git_describe_number_branch
+  version: {{ GIT_DESCRIBE_TAG }}
+
+source:
+  git_url: https://github.com/conda/conda_build_test_recipe
+  git_branch: 1.20.2+1
+
+build:
+  number: {{ GIT_DESCRIBE_NUMBER }}
+  string: {{ GIT_BUILD_STR }}
diff --git a/tests/test_build_recipes.py b/tests/test_build_recipes.py
--- a/tests/test_build_recipes.py
+++ b/tests/test_build_recipes.py
@@ -377,3 +377,15 @@ def test_patch():
                 lines = modified.readlines()
                 assert lines[0] == '43770\n'
         os.chdir(basedir)
+
+
+def test_git_describe_info_on_branch():
+    cmd = 'conda build --output {}'.format(os.path.join(metadata_dir, "_git_describe_number_branch"))
+    process = subprocess.Popen(cmd.split(),
+                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+    output, error = process.communicate()
+    test_path = os.path.join(sys.prefix, "conda-bld", subdir,
+                        "git_describe_number_branch-1.20.2-1_g82c6ba6.tar.bz2")
+    output = output.decode('utf-8').rstrip()
+    error = error.decode('utf-8')
+    assert test_path == output, error