Skip to content

Commit

Permalink
fix --version crash on missing git/refs/heads
Browse files Browse the repository at this point in the history
- fixes #635
  • Loading branch information
casperdcl committed Jul 21, 2020
1 parent 030ea9d commit bf00034
Showing 1 changed file with 40 additions and 37 deletions.
77 changes: 40 additions & 37 deletions tqdm/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,48 @@
scriptdir = os.path.dirname(__file__)
gitdir = os.path.abspath(os.path.join(scriptdir, "..", ".git"))
if os.path.isdir(gitdir): # pragma: nocover
extra = None
# Open config file to check if we are in tqdm project
with io_open(os.path.join(gitdir, "config"), 'r') as fh_config:
if 'tqdm' in fh_config.read():
# Open the HEAD file
with io_open(os.path.join(gitdir, "HEAD"), 'r') as fh_head:
extra = fh_head.readline().strip()
# in a branch => HEAD points to file containing last commit
if 'ref:' in extra:
# reference file path
ref_file = extra[5:]
branch_name = ref_file.rsplit('/', 1)[-1]

ref_file_path = os.path.abspath(os.path.join(gitdir, ref_file))
# check that we are in git folder
# (by stripping the git folder from the ref file path)
if os.path.relpath(
ref_file_path, gitdir).replace('\\', '/') != ref_file:
# out of git folder
extra = None
try:
extra = None
# Open config file to check if we are in tqdm project
with io_open(os.path.join(gitdir, "config"), 'r') as fh_config:
if 'tqdm' in fh_config.read():
# Open the HEAD file
with io_open(os.path.join(gitdir, "HEAD"), 'r') as fh_head:
extra = fh_head.readline().strip()
# in a branch => HEAD points to file containing last commit
if 'ref:' in extra:
# reference file path
ref_file = extra[5:]
branch_name = ref_file.rsplit('/', 1)[-1]

ref_file_path = os.path.abspath(os.path.join(
gitdir, ref_file))
# check that we are in git folder
# (by stripping the git folder from the ref file path)
if os.path.relpath(ref_file_path, gitdir).replace(
'\\', '/') != ref_file:
# out of git folder
extra = None
else:
# open the ref file
with io_open(ref_file_path, 'r') as fh_branch:
commit_hash = fh_branch.readline().strip()
extra = commit_hash[:8]
if branch_name != "master":
extra += '.' + branch_name

# detached HEAD mode, already have commit hash
else:
# open the ref file
with io_open(ref_file_path, 'r') as fh_branch:
commit_hash = fh_branch.readline().strip()
extra = commit_hash[:8]
if branch_name != "master":
extra += '.' + branch_name

# detached HEAD mode, already have commit hash
else:
extra = extra[:8]

# Append commit hash (and branch) to version string if not tagged
if extra is not None:
try:
extra = extra[:8]

# Append commit hash (and branch) to version string if not tagged
if extra is not None:
with io_open(os.path.join(gitdir, "refs", "tags",
'v' + __version__)) as fdv:
if fdv.readline().strip()[:8] != extra[:8]:
__version__ += '-' + extra
except Exception as e:
if "No such file" not in str(e):
raise
except Exception as e:
if "No such file" in str(e):
__version__ += "-git.UNKNOWN"
else:
raise

0 comments on commit bf00034

Please sign in to comment.