Forums

Unable to get git commit hash

I've attempted to get the current git commit hash to use as a version number for sentry & django. So this happens in the django settings;

repo = git.Repo(search_parent_directories=True)
RELEASE_NUMBER = repo.head.object.hexsha
VERSION = RELEASE_NUMBER

As I understand it, the site for this simply pulls the latest changes from the repo & reloads, therefore I thought this would be an appropriate way of getting this value.

The above code causes an error however;

Error running WSGI application
2021-08-19 15:40:06,959: git.exc.InvalidGitRepositoryError
2021-08-19 15:40:06,959:   File "/var/www/djangogirls_org_wsgi.py", line 30, in <module>
2021-08-19 15:40:06,959:     application = get_wsgi_application()
2021-08-19 15:40:06,959: 
2021-08-19 15:40:06,959:   File "/home/djangogirls2/.virtualenvs/djangogirls3/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2021-08-19 15:40:06,960:     django.setup(set_prefix=False)
2021-08-19 15:40:06,960: 
2021-08-19 15:40:06,960:   File "/home/djangogirls2/.virtualenvs/djangogirls3/lib/python3.7/site-packages/django/__init__.py", line 19, in setup
2021-08-19 15:40:06,960:     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2021-08-19 15:40:06,960: 
2021-08-19 15:40:06,960:   File "/home/djangogirls2/.virtualenvs/djangogirls3/lib/python3.7/site-packages/django/conf/__init__.py", line 56, in __getattr__
2021-08-19 15:40:06,960:     self._setup(name)
2021-08-19 15:40:06,960: 
2021-08-19 15:40:06,960:   File "/home/djangogirls2/.virtualenvs/djangogirls3/lib/python3.7/site-packages/django/conf/__init__.py", line 43, in _setup
2021-08-19 15:40:06,961:     self._wrapped = Settings(settings_module)
2021-08-19 15:40:06,961: 
2021-08-19 15:40:06,961:   File "/home/djangogirls2/.virtualenvs/djangogirls3/lib/python3.7/site-packages/django/conf/__init__.py", line 106, in __init__
2021-08-19 15:40:06,961:     mod = importlib.import_module(self.SETTINGS_MODULE)
2021-08-19 15:40:06,961: 
2021-08-19 15:40:06,961:   File "/home/djangogirls2/djangogirls.com/djangogirls/settings.py", line 11, in <module>
2021-08-19 15:40:06,961:     repo = git.Repo(search_parent_directories=True)
2021-08-19 15:40:06,961: 
2021-08-19 15:40:06,961:   File "/home/djangogirls2/.virtualenvs/djangogirls3/lib/python3.7/site-packages/git/repo/base.py", line 220, in __init__
2021-08-19 15:40:06,961:     self.working_dir = self._working_tree_dir or self.common_dir  # type: Optional[PathLike]
2021-08-19 15:40:06,962: 
2021-08-19 15:40:06,962:   File "/home/djangogirls2/.virtualenvs/djangogirls3/lib/python3.7/site-packages/git/repo/base.py", line 303, in common_dir
2021-08-19 15:40:06,962:     raise InvalidGitRepositoryError()

I don't have access to the account where this is being executed, it's a github PR I've submitted & had this stacktrace back on.

https://github.com/DjangoGirls/djangogirls/pull/640

Looks like the working directory is not what you expect. Maybe check what it is first.