Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid fail on git error #110

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions cosmosis/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,34 +314,41 @@ def get_git_revision_dulwich(directory):


def get_git_revision(directory):
# Turn e.g. $COSMOSIS_SRC_DIR into a proper path
directory = os.path.expandvars(directory)
if not os.path.isdir(directory):
return ""
try:
# Turn e.g. $COSMOSIS_SRC_DIR into a proper path
directory = os.path.expandvars(directory)
if not os.path.isdir(directory):
return ""

if dulwich is not None:
return get_git_revision_dulwich(directory)
if dulwich is not None:
return get_git_revision_dulwich(directory)

if os.environ.get("COSMOSIS_NO_SUBPROCESS", "") not in ["", "0"]:
return ""
if os.environ.get("COSMOSIS_NO_SUBPROCESS", "") not in ["", "0"]:
return ""

# this git command gives the current commit ID of the
# directory it is run from
cmd = "git rev-parse HEAD".split()
# this git command gives the current commit ID of the
# directory it is run from
cmd = "git rev-parse HEAD".split()

# run, capturing stderr and stdout to read the hash from
p = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE,
cwd=directory)
# run, capturing stderr and stdout to read the hash from
p = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE,
cwd=directory)

# Read stdout. Discard stderr, which will be None
rev, _ = p.communicate()
# Read stdout. Discard stderr, which will be None
rev, _ = p.communicate()

# If there are any errors then we ignore everything
if p.returncode:
# If there are any errors then we ignore everything
if p.returncode:
return ""
# There shouldn't be any newlines here, but in case there are in future
# we replace them with spaces to avoid messing up output file formats
return rev.decode('utf-8').strip().replace("\n", " ")

# It's usually bad practice to catch all exceptions, but we really don't
# want to crash if we can't get the metadata.
except Exception as e:
print(f"Unable to get metadata on git revision for {directory}: error {e}")
return ""
# There shouldn't be any newlines here, but in case there are in future
# we replace them with spaces to avoid messing up output file formats
return rev.decode('utf-8').strip().replace("\n", " ")



Expand Down