pythongitdulwich

How to fetch using dulwich in python


I'm trying to do the equivalent of git fetch -a using the dulwich library within python.

Using the docs at https://www.dulwich.io/docs/tutorial/remote.html I created the following script:

from dulwich.client import LocalGitClient
from dulwich.repo import Repo
import os

home = os.path.expanduser('~')

local_folder = os.path.join(home, 'temp/local'
local = Repo(local_folder)

remote = os.path.join(home, 'temp/remote')

remote_refs = LocalGitClient().fetch(remote, local)
local_refs = LocalGitClient().get_refs(local_folder)

print(remote_refs)
print(local_refs)

with an existing git repository at ~/temp/remote and a newly initialised repo at ~/temp/local

remote_refs shows everything I would expect, but local_refs is an empty dictionary and git branch -a on the local repo returns nothing.

Am I missing something obvious?

This is on dulwich 0.12.0 and Python 3.5

EDIT #1

Following a discussion on the python-uk irc channel, I updated my script to include the use of determine_wants_all:

from dulwich.client import LocalGitClient
from dulwich.repo import Repo

home = os.path.expanduser('~')

local_folder = os.path.join(home, 'temp/local'
local = Repo(local_folder)

remote = os.path.join(home, 'temp/remote')

wants = local.object_store.determine_wants_all
remote_refs = LocalGitClient().fetch(remote, local, wants)
local_refs = LocalGitClient().get_refs(local_folder)

print(remote_refs)
print(local_refs)

but this had no effect :-(

EDIT #2

Again, following discussion on the python-uk irc channel, I tried running dulwich fetch from within the local repo. It gave the same result as my script i.e. the remote refs were printed to the console correctly, but git branch -a showed nothing.

EDIT - Solved

A simple loop to update the local refs did the trick:

from dulwich.client import LocalGitClient
from dulwich.repo import Repo
import os

home = os.path.expanduser('~')

local_folder = os.path.join(home, 'temp/local')
local = Repo(local_folder)

remote = os.path.join(home, 'temp/remote')
remote_refs = LocalGitClient().fetch(remote, local)

for key, value in remote_refs.items():
    local.refs[key] = value

local_refs = LocalGitClient().get_refs(local_folder)

print(remote_refs)
print(local_refs)

Solution

  • LocalGitClient.fetch() does not update refs, it just fetches objects and then returns the remote refs so you can use that to update the target repository refs.