6

How to do something like git pull in python dulwich library.

Determinant
  • 3,886
  • 7
  • 31
  • 47

2 Answers2

6

I haven't used dulwich, but from these doc's, possibly something like:

from dulwich.repo import Repo
from dulwich.client import HttpGitClient
local = Repo.init("local", mkdir=True)
client = HttpGitClient('http://github.com/adammorris/')
remote_refs = client.fetch("history.js.git",local)
local["HEAD"] = remote_refs["refs/heads/master"]

At this point, it didn't load the files, but I could do "git checkout" from the local path, and it updated the files.

Also, saw these:

Community
  • 1
  • 1
Adam Morris
  • 8,265
  • 12
  • 45
  • 68
  • Yes, the fetch function will pull in a pack file under `.git` directory. And I just don't know how to merge it into master branch. – Determinant Aug 15 '12 at 07:50
  • It sounds like fetch() should import the pack into same branch as the repo. Is possible to use do_commit() to merge it into the master branch? http://stackoverflow.com/questions/6904734/in-dulwich-how-do-i-commit-to-a-branch-instead-of-to-head – Adam Morris Aug 15 '12 at 08:13
  • Tried this and found I needed to set the refs first, like local["HEAD"] = remote_refs["refs/heads/master"] - saw this also: http://stackoverflow.com/questions/6640546/programmatically-git-checkout-with-dulwich – Adam Morris Aug 15 '12 at 13:01
3

Full example. Works with Bitbucket.

from dulwich import index
from dulwich.client import HttpGitClient
from dulwich.repo import Repo

local_repo = Repo.init(LOCAL_FOLDER, mkdir=True)
remote_repo = HttpGitClient(REMOTE_URL, username=USERNAME, password=PASSWORD)
remote_refs = remote_repo.fetch(REMOTE_URL, local_repo)
local_repo[b"HEAD"] = remote_refs[b"refs/heads/master"]

index_file = local_repo.index_path()
tree = local_repo[b"HEAD"].tree
index.build_index_from_tree(local_repo.path, index_file, local_repo.object_store, tree)

Replace LOCAL_FOLDER, REMOTE_URL, USERNAME, PASSWORD with your data.

Maxim Korobov
  • 2,574
  • 1
  • 26
  • 44
  • if remote-repo uninitialized, remote_refs[b"refs/heads/master"] raise KeyError: b'refs/heads/master' – CS QGB May 21 '21 at 10:13