Skip to content

repo.git.rev_list called in the wrong dir context #1772

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

Closed
random-feedback opened this issue Dec 13, 2023 · 2 comments
Closed

repo.git.rev_list called in the wrong dir context #1772

random-feedback opened this issue Dec 13, 2023 · 2 comments

Comments

@random-feedback
Copy link

problem

If I do git.Repo("../other_repo").git.rev_list('test ^master') when the pwd is a repo too, it runs it against the current repo not the specified one.

tested version

dunno if it's already been fixed, all my Unices are broken and I'm stuck on python 3.9 and can't check, hence the throwaway account. here's the output:

$ python --version
Python 3.9.5
$ pip freeze | grep GitPy
GitPython==3.1.40

code to reproduce

Test framework made me glaze over so here's a bash script:

#!/bin/bash

set -e

# Install GitPython (you might want to use a venv here I guess)
pip install GitPython

# (re)create directories
rm -rf repo1 repo2
mkdir repo1 repo2

# Create empty README.md in each directory
touch repo1/README.md repo2/README.md

# Initialize and commit in repo1
cd repo1
git init  # broken by woken folken? should have forking spoken
git add README.md
git commit -m "Initial commit"

# Create and checkout to a new branch 'test', modify README.md, and commit
git checkout -b test
echo "Some text for the test branch" >> README.md
git add README.md
git commit -m "Update README on test branch"

# Go back to the parent directory
cd ..

# Initialize and commit in repo2
cd repo2
git init
git add README.md
git commit -m "Initial commit"

# Run Python one-liner to get unique commits in the test branch compared to master in repo1 using rev_list
python -c "import git; repo = git.Repo('../repo1'); print(repo.git.rev_list('test ^master').split('\n'))"

expected result

return code of 0, no stack trace

actual result

...
git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
  cmdline: git rev-list test ^master
  stderr: 'fatal: ambiguous argument 'test ^master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]''

$ echo $?
1
@Byron
Copy link
Member

Byron commented Dec 13, 2023

Thanks a lot for reporting, and for providing a reproducer!

I took a quick look at the code but it wasn't immediately obvious why the git object would pick up the CWD.

Doing the same with git -C ../repo1 rev-list test ^main works, but doing git -C ../repo1 rev-list 'test ^main' produces exactly the message provided here.

Does the following work for you?

python -c "import git; repo = git.Repo('../repo1'); print(repo.git.rev_list('test',  '^master').split('\n'))"

I assume it does and recommend taking a look at the docs specific to invoking Git directly.

I hope that helps.

@Byron Byron closed this as completed Dec 13, 2023
@random-feedback
Copy link
Author

Ugh! Sorry for wasting your time there, yeah it looks like me ignorantly using copypasta from gpt was the main issue here!

Thanks for the quick investigation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants