EzDevInfo.com

dulwich

Pure-Python Git implementation Dulwich

Check which branches contain a specific git commit sha using Python dulwich?

I would like to do the following command from Python script using dulwich:

$ git branch --contains <myCommitSha> | wc -l

What I intend is to check if a particular commit (sha) is placed in more than one branches.

Of course I thought that I can execute the above command from Python and parse the output (parse the number of branches), but that's the last resort solution.

Any other ideas/comments? Thanks in advance.


Source: (StackOverflow)

Dulwich Remote Repo Authentication

Are there any good examples out there for accessing and fetching a remote repository over HTTPS? I have a Git repository that I can clone from the command line with my username and password, but I want to be able to do this using Dulwich and just provide my username and password when I make my HTTPS client.

Another requirement is that this be done using the MemoryRepo option rather than writing to the file system.


Source: (StackOverflow)

Advertisements

python datetime for git commit time?

I'm having a (django) db that wants datetime objects, and I'd like to feed it data from git commits.

Now git commits have that "not really a timezone" offset thingie.

What's the correct way to get a python datetime? Any luck with getting timezones right?

I'm thinking about

datetime.datetime.utcfromtimestamp(commit.commit_time)

I'm using dulwich right now, but that seems to be a side note.


Source: (StackOverflow)

How to pull from the remote using dulwich?

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


Source: (StackOverflow)

In Dulwich, how do I commit to a branch instead of to HEAD?

Apparently repo.do_commit(message='test commit', committer='Name ') only commits to refs/heads/master.

Is there a way to set the current commit ref to another one than refs/heads/master?

Or is the only way to commit to a branch by creating a Commit object (as shown in the tutorial in the documentation) and setting it's parent to be the one of the branches commit id?

Should this be true, which would then be the use of repo.do_commit other than committing to refs/heads/master?


Source: (StackOverflow)

Dulwich: How to detect fast-forward?

As the title suggests, namely, how can I know if the remote branch is an ancestor of local branch and vice versa?


Source: (StackOverflow)

How do I correctly install dulwich to get hg-git working on Windows?

I'm trying to use the hg-git Mercurial extension on Windows (Windows 7 64-bit, to be specific). I have Mercurial and Git installed. I have Python 2.5 (32-bit) installed.

I followed the instructions on http://hg-git.github.com/ to install the extension. The initial easy_install failed because it was unable to compile dulwich without Visual Studio 2003.

I installed dulwich manually by:

  • git clone git://git.samba.org/jelmer/dulwich.git
  • cd dulwich
  • c:\Python25\python setup.py --pure install

Now when I run easy_install hg-git, it succeeds (since the dulwich dependency is satisfied).

In my C:\Users\username\Mercurial.ini, I have:

[extensions]
hgext.bookmarks =
hggit =

When I type 'hg' at a command prompt, I see: "* failed to import extension hggit: No module named hggit"

Looking under my c:\Python25 folder, the only reference to hggit I see is Lib\site-packages\hg_git-0.2.1-py2.5.egg. Is this supposed to be extracted somewhere, or should it work as-is?

Since that failed, I attempted the "more involved" instructions from the hg-git page that suggested cloning git://github.com/schacon/hg-git.git and referencing the path in my Mercurial configuration. I cloned the repo, and changed my extensions file to look like:

[extensions]
hgext.bookmarks =
hggit = c:\code\hg-git\hggit

Now when I run hg, I see: * failed to import extension hggit from c:\code\hg-git\hggit: No module named dulwich.errors.

Ok, so that tells me that it is finding hggit now, because I can see in hg-git\hggit\git_handler.py that it calls

from dulwich.errors import HangupException

That makes me think dulwich is not installed correctly, or not in the path.

Update:

From Python command line:

import dulwich

yields Import Error: No module named dulwich

However, under C:\Python25\Lib\site-packages, I do have a dulwich-0.5.0-py2.5.egg folder which appears to be populated. This was created by the steps mentioned above. Is there an additional step I need to take to make it part of the Python "path"?

From Python command line (as suggested in one of the answers):

import pkg_resources
pkg_resources.require('dulwich')

yields [dulwich 0.5.0 (c:\python25\lib\site-packages\dulwich-0.5.0-py2.5.egg)]

So what does that tell me? Importing dulwich fails, but apparently pkg_resources can find it. What can I do with that information?


Source: (StackOverflow)

Work with GitHub Wiki repository using Python

Is there any way to programmatically (using libraries like PyGithub, GitPython or dulwich) load any file right into MyRepo.wiki.git repository? Using Python, of course.

I can easily upload a file right into MyRepo.git repository with a help of PyGithub, but, unfortunately, this library has no API or ways to work with MyRepo.wiki.git repository.

Here is how I can upload a file to MyRepo.git repository:

github_repo = github_account.get_user().get_repo('MyRepo')

head_ref = gh_repo.get_git_ref("heads/%s" % github_branch)
latest_commit = gh_repo.get_git_commit(head_ref.object.sha)

base_tree = latest_commit.tree

new_tree = gh_repo.create_git_tree(
    [github.InputGitTreeElement(
        path="test.txt",
        mode='100755' if github_executable else '100644',
        type='blob',
        content="test"
    )],
    base_tree)

new_commit = gh_repo.create_git_commit(
    message="test commit message",
    parents=[latest_commit],
    tree=new_tree)

head_ref.edit(sha=new_commit.sha, force=False)

So, how can I do the same but with MyRepo.wiki.git repository? If you can provide an example using PyGithub library - it would be really great.

P.S. Can I do this using Gollum API?

P.P.S. Has not anybody worked with *.wiki.git using any kind of python library? I do not believe :(

P.P.P.S. If I was not clear enough: I DO NOT want to create a local repository in any way. All I want to modify repo structure on-the-fly - just how my example does. But with *.wiki.git repository.

Thank you!


Source: (StackOverflow)

Programmatically `git checkout .` with dulwich

Having this code

from dulwich.objects import Blob, Tree, Commit, parse_timezone
from dulwich.repo import Repo
from time import time

repo = Repo.init("myrepo", mkdir=True)
blob = Blob.from_string("my file content\n")
tree = Tree()
tree.add("spam", 0100644, blob.id)
commit = Commit()
commit.tree = tree.id


author = "Flav <foo@bar.com>"
commit.author = commit.committer = author
commit.commit_time = commit.author_time = int(time())
tz = parse_timezone('+0200')[0]
commit.commit_timezone = commit.author_timezone = tz
commit.encoding = "UTF-8"
commit.message = "initial commit"

o_sto = repo.object_store
o_sto.add_object(blob)
o_sto.add_object(tree)
o_sto.add_object(commit)

repo.refs["HEAD"] = commit.id

I end up with the commit in the history, BUT the created file is pending for deletion (git status says so).

A git checkout . fixes it.

My question is: how to do git checkout . programmatically with dulwich?


Source: (StackOverflow)

Getting started with Git Python

My goal is to access existing Git repos from Python. I want to get repo history and on demand diffs.

In order to do that I started with dulwich. So I tried:

from dulwich.repo import Repo
Repo.init('/home/umpirsky/Projects/my-exising-git-repo')

and got OSError: [Errno 17] File exists: '/home/umpirsky/Projects/my-exising-git-repo/.git

The doc says You can open an existing repository or you can create a new one..

Any idea how to do that? Can I fetch history and diffs with dulwich? Can you recommand any other lib for Git access? I am developing Ubuntu app, so it would be appriciated to have ubuntu package for easier deployment.

I will also check periodically to detect new changes in repo, so I would rather work with remote so I can detect changes that are not pulled to local yet. I'm not sure how this should work, so any help will be appriciated.

Thanks in advance.


Source: (StackOverflow)

How to get a list of tags and create new tags with python and dulwich in git?

I am having problems to retrieve the following information of a git repo using python:

  1. I would like to get a list of all tags of this repository.
  2. I would like to checkout another branch and also create a new branch for staging.
  3. I would like to tag a commit with an annotated tag.

I have looked into the dulwich's documentation and the way it works seems very bare-bones. Are there also alternatives which are easier to use?


Source: (StackOverflow)

Programmatically `git status` with dulwich

I'm wondering how I can perform the equivalent of git status with dulwich?

I tried this:

After adding/changing/renaming some files and staging them for commit, this is what I've tried doing:

from dulwich.repo  import Repo
from dulwich.index import changes_from_tree
r = Repo('my-git-repo')
index = r.open_index()
changes = index.changes_from_tree(r.object_store, r['HEAD'].tree)

Outputs the following:

>>> list(changes)
(('Makefile', None), (33188, None), ('9b20...', None))
(('test/README.txt', 'test/README.txt'), (33188, 33188), ('484b...', '4f89...'))
((None, 'Makefile.mk'), (None, 33188), (None, '9b20...'))
((None, 'TEST.txt'), (None, 33188), (None, '2a02...'))

But this output requires that I further process it to detect:

  1. I modified README.txt.
  2. I renamed Makefile to Makefile.mk.
  3. I added TEST.txt to the repository.

The functions in dulwich.diff_tree provide a much nicer interface to tree changes... is this not possible before actually committing?


Source: (StackOverflow)

How to add something to the index, commit it, then push the master branch to a named remote with dulwich?

How can I add something to the index, as in

git add .

then

git commit -m "message"

then

git push origin master

using dulwich?

So far I've found this http://www.samba.org/~jelmer/dulwich/apidocs/dulwich.index.Index.html but it doesn't say much, does it?

Thanks


Source: (StackOverflow)

dulwich's NotCommitError when trying to resolve tags

I'm working with dulwich on a project where I need to clone repositories sometimes by commit ID, sometimes by tag, sometimes by branch name. I'm having trouble with the tag case which seems to work for some repositories, but not for others.

Here's the "clone" helper function I wrote:

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


def clone(repo_url, ref, folder):
    is_commit = False
    if not ref.startswith('refs/'):
        is_commit = True
    rep = Repo.init(folder)
    client, relative_path = get_transport_and_path(repo_url)

    remote_refs = client.fetch(relative_path, rep)
    for k, v in remote_refs.iteritems():
        try:
            rep.refs.add_if_new(k, v)
        except:
            pass

    if ref.startswith('refs/tags'):
        ref = rep.ref(ref)
        is_commit = True

    if is_commit:
        rep['HEAD'] = rep.commit(ref)
    else:
        rep['HEAD'] = remote_refs[ref]
    indexfile = rep.index_path()
    tree = rep["HEAD"].tree
    index.build_index_from_tree(rep.path, indexfile, rep.object_store, tree)
    return rep, folder

Oddly, I am able to do

 clone('git://github.com/dotcloud/docker-py', 'refs/tags/0.2.0', '/tmp/a')

But

clone('git://github.com/dotcloud/docker-registry', 'refs/tags/0.6.0', '/tmp/b')

fails with

NotCommitError: object debd567e95df51f8ac91d0bb69ca35037d957ee6
type commit
[...]
 is not a commit

Both refs are tags, so I'm not sure what I'm doing wrong, or why the code behaves differently on both repositories. Would appreciate any help to sort this out!


Source: (StackOverflow)

How to get last commit for specified file with python(dulwich)?

I need author name and last commit time for a specified file with python. Currentrly, I'm trying to use dulwich.

There're plenty of apis to retrieve objects for a specific SHA like:

repo = Repo("myrepo")
head = repo.head()
object = repo.get_object(head)
author = object.author
time = object.commit_time

But, how do i know the recent commit for the specific file? Is there a way to retrieve it like:

repo = Repo("myrepo")
commit = repo.get_commit('a.txt')
author = commit.author
time = commit.commit_time

or

repo = Repo("myrepo")
sha = repo.get_sha_for('a.txt')
object = repo.get_object(sha)
author = object.author
time = object.commit_time

Thank you.


Source: (StackOverflow)