0

I have my own package I would like to be able to have the git hash for logging purposes when outside the repo e.g. when calling the package on a remote server or keeping track of frequent updates and distributed servers to see which version of the code ran what.

I know I can get the git hash as such however if I call this outside the repo I get an error InvalidGitRepositoryError:

I tried setting the version in my setup.py as: '1.0-{}'.format(GIT_REVISION) using the gitpython package however when I build using pip it falls back to setuptools instead of using a wheel because of the following:

  WARNING: Built wheel for mypackage is invalid: Metadata 1.2 mandates PEP 440 version, but '1.0-d180ffe3fc30a3920edfcbace5ab00e934f15e3b' is not
Failed to build mypackage
Installing collected packages: mypackage
    Running setup.py install for mypackage ... done
  DEPRECATION: mypackage was installed using the legacy 'setup.py install' method, because a wheel could not be built for it. A possible replacement is to fix the wheel build issue reported above. Discussion can be found at https://github.com/pypa/pip/issues/8368

setuptools doe not provide a mypackage.__version__ to obtain the git hash.

Is there a way I can get the githash within the mypackage module or version while outside the repo?

torek
  • 448,244
  • 59
  • 642
  • 775
pyCthon
  • 11,746
  • 20
  • 73
  • 135
  • The standard practice is that when *exporting* something, so that the user won't have the repository in the first place, you put a unique identifier—not necessarily a Git hash ID; `v1.2.3` is often more reasonable for instance—in the exported files. There are many ways to do this depending on the build process, language(s), and so on. If you're guaranteed to be working with a Git repository, though, all (all?!) you have to do is *find* it. – torek Nov 04 '21 at 06:41
  • To put in an exported tag, consider using Git's annotated tags and the `git describe` command. Note that some hosting sites can generate tarballs on demand, and can use `git archive` to do that, and `git archive` can insert the output of `git describe` into a particular file using the mechanism described in [the gitattributes documentation](https://git-scm.com/docs/gitattributes) (search for `export-subst`). Whether this is a good choice for your case depends on too many things I don't know. – torek Nov 04 '21 at 06:43
  • I'm not exporting my package to external users so I don't care about standard practice, I want the git hash so i can see which exact commit something went wrong. Changing the version for 100s of commits a day doesn't make sense. As mentioned in the post about I want it outside of the repo and within the python package. @torek – pyCthon Nov 04 '21 at 07:33
  • Well, then, come up with a way to locate the repository. That's a problem you can solve. How you solve it is up to you: consider a default path and an environment variable that can override it, or a search path, or something along those lines. (Note that `git.Repo` lets you specify where the repository is, rather than having it search "from here upwards".) – torek Nov 04 '21 at 07:37

0 Answers0