Skip to content

Fetching a remote without a configured refspec raises ValueError #296

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
earwig opened this issue Jun 6, 2015 · 3 comments
Closed

Fetching a remote without a configured refspec raises ValueError #296

earwig opened this issue Jun 6, 2015 · 3 comments

Comments

@earwig
Copy link

earwig commented Jun 6, 2015

I found this easiest to reproduce by creating a bare clone of another repository.

Set up like this:

ben@earwig:~/test $ git init foo
Initialized empty Git repository in /Users/ben/test/foo/.git/
ben@earwig:~/test $ cd foo
ben@earwig:~/test/foo [master] $ echo "test" > test && git add test && git commit -m "test"
[master (root-commit) 269c498] test
 1 file changed, 1 insertion(+)
 create mode 100644 test
ben@earwig:~/test/foo [master] $ cd ..
ben@earwig:~/test $ git clone --bare foo bar
Cloning into bare repository 'bar'...
done.

Note the structure of the config file and that an ordinary git fetch origin works.

ben@earwig:~/test $ cd bar
ben@earwig:~/test/bar [BARE:master] $ cat config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = /Users/ben/test/foo
ben@earwig:~/test/bar [BARE:master] $ git fetch origin
From /Users/ben/test/foo
 * branch            HEAD       -> FETCH_HEAD

Now, here's the bug:

ben@earwig:~/test/bar [BARE:master] $ python
Python 2.7.10 (default, May 29 2015, 21:05:23)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import git
>>> git.Repo().remotes.origin.fetch()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/git/remote.py", line 651, in fetch
    res = self._get_fetch_info_from_stderr(proc, progress or RemoteProgress())
  File "/usr/local/lib/python2.7/site-packages/git/remote.py", line 588, in _get_fetch_info_from_stderr
    for err_line, fetch_line in zip(fetch_info_lines, fetch_head_info))
  File "/usr/local/lib/python2.7/site-packages/git/remote.py", line 588, in <genexpr>
    for err_line, fetch_line in zip(fetch_info_lines, fetch_head_info))
  File "/usr/local/lib/python2.7/site-packages/git/remote.py", line 249, in _from_line
    raise ValueError("Failed to parse FETCH_HEAD line: %r" % fetch_line)
ValueError: Failed to parse FETCH_HEAD line: u'269c498e56feb93e408ed4558c8138d750de8893\t\t/Users/ben/test/foo\n'
>>>

If I add a valid refspec to the config, it works:

ben@earwig:~/test/bar [BARE:master] $ git config --add remote.origin.fetch +refs/heads/*:refs/heads/*
ben@earwig:~/test/bar [BARE:master] $ python
Python 2.7.10 (default, May 29 2015, 21:05:23)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import git
>>> git.Repo().remotes.origin.fetch()
[<git.remote.FetchInfo object at 0x11094a0a8>]
>>>
@Byron Byron added this to the v1.0.2 - Fixes milestone Jun 10, 2015
@Byron Byron closed this as completed in 4a771ad Jun 10, 2015
@Byron
Copy link
Member

Byron commented Jun 10, 2015

Thanks for the report !

I fixed the issue by explicitly checking for the case and tell the user a refspec is required for gitpython to work.

@Byron
Copy link
Member

Byron commented Jun 10, 2015

You can watch the development stream on youtube.

GitPython #8 [issue 296 - handle missing fetch-refspec gracefully]

thumb

@kshpytsya
Copy link

For me, a new user of GitPython, it is unclear, why refspec is required in this situation while it is unneeded by vanilla git. Maybe a couple of words in the docs are due, or have I missed them?

My current use case for GitPython is very simple: I need to be able to access specific files from specific revision of some repository.

import git
import pathlib

repo_path = pathlib.Path(REPO_PATH)

if repo_path.exists():
  repo = git.Repo(repo_path)
  repo.remote().fetch()
else:
  repo = git.Repo.clone_from(REPO_URL, repo_path, bare=True)

commit = repo.commit(REVISION)
for blob in commit.tree.blobs:
  process(blob.name, blob.data_stream.read())

On the second run, this fails with

AssertionError: Remote 'origin' has no refspec set.

I have fixed this by replacing

repo.fetch()

with

repo.remote().fetch("+refs/heads/*:refs/heads/*")

however, I am not sure if it is correct.

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

No branches or pull requests

3 participants