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

Reproducibility info fails for git repo with no remote #99

Open
ndryden opened this issue Feb 28, 2023 · 0 comments · May be fixed by #145
Open

Reproducibility info fails for git repo with no remote #99

ndryden opened this issue Feb 28, 2023 · 0 comments · May be fixed by #145
Labels
bug Something isn't working

Comments

@ndryden
Copy link

ndryden commented Feb 28, 2023

If I have a git repository that does not have a remote, get_reproducibility_info will fail with a CalledProcessError exception. It appears that if a git repo is present, Tap assumes it has an origin remote, yet this need not be the case. I often use local git repos for initial development before pushing them somewhere.

Reproducer:

$ mkdir test
$ cd test
# Write test.py appropriately...
$ cat test.py
from tap import Tap

class ArgumentParser(Tap):
    foo: bool = False

if __name__ == '__main__':
    args = ArgumentParser().parse_args()
    print(args.get_reproducibility_info())

# Run with no git repo and it works fine:
$ python test.py           
{'command_line': 'python test.py', 'time': 'Tue Feb 28 13:03:00 2023'}

# Set up a git repo to break it:
$ git init
$ python test.py
Traceback (most recent call last):
  File "/home/ndryden/envs/test/lib/python3.9/site-packages/tap/utils.py", line 82, in get_git_url
    url = check_output(['git', 'remote', 'get-url', 'origin'], cwd=self.repo_path)
  File "/home/ndryden/envs/test/lib/python3.9/site-packages/tap/utils.py", line 45, in check_output
    output = subprocess.check_output(command, stderr=devnull, **kwargs).decode('utf-8').strip()
  File "/home/ndryden/envs/test/lib/python3.9/subprocess.py", line 424, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/home/ndryden/envs/test/lib/python3.9/subprocess.py", line 528, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['git', 'remote', 'get-url', 'origin']' returned non-zero exit status 2.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ndryden/test/test.py", line 8, in <module>
    print(args.get_reproducibility_info())
  File "/home/ndryden/envs/test/lib/python3.9/site-packages/tap/tap.py", line 392, in get_reproducibility_info
    reproducibility['git_url'] = git_info.get_git_url(commit_hash=True)
  File "/home/ndryden/envs/test/lib/python3.9/site-packages/tap/utils.py", line 85, in get_git_url
    url = check_output(['git', 'config', '--get', 'remote.origin.url'], cwd=self.repo_path)
  File "/home/ndryden/envs/test/lib/python3.9/site-packages/tap/utils.py", line 45, in check_output
    output = subprocess.check_output(command, stderr=devnull, **kwargs).decode('utf-8').strip()
  File "/home/ndryden/envs/test/lib/python3.9/subprocess.py", line 424, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/home/ndryden/envs/test/lib/python3.9/subprocess.py", line 528, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['git', 'config', '--get', 'remote.origin.url']' returned non-zero exit status 1.

This is because the remote origin does not exist, and git remote get-url origin returns status 2 in this case:

$ git remote get-url origin 
error: No such remote 'origin'

(Likewise, git config --get remote.origin.url returns status 1.)

You might be able to solve this by checking whether origin exists with git remote -v before executing this.

Incidentally, after adding a fake origin remote this reveals another bug, where git rev-parse HEAD fails when there are no commits, leading to a similar exception. Not sure if this deserves a separate issue (and seems like even more of an edge-case).

@swansonk14 swansonk14 added the bug Something isn't working label Jun 25, 2023
arnaud-ma added a commit to arnaud-ma/typed-argument-parser that referenced this issue Jul 11, 2024
@arnaud-ma arnaud-ma linked a pull request Jul 11, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants