← Back to team overview

dulwich-users team mailing list archive

Re: Dulwich Pull/Push command?

 

On Mon, Jun 28, 2010 at 18:31, Seth Shelnutt <shelnutt2@xxxxxxxxx> wrote:

> I'm actually just looking to use the modules. I want to integrate git
> support into my python script. I would be happy to help and add more
> functionality but first I'd like to finish my current project and second I'm
> familiar enough with Git. I'm in the process of learning how it works. It
> seems pretty straight forward, clone, pull, push, commit, all the basics
> don't seem to bad.
>
> What I am doing is moving wine's appinstall to python and expanding it.
> Appinstall was first developed by Austin English for the purpose of
> automating testing of software packages. What I need git support for is when
> a person runs apptester.py, the first thing it does is do a git status (or I
> might have it simply look for .git folder), and determine if the git
> repository has already been established.


The easiest way to do this in dulwich would be:
try:
  r = repo.Repo(dirname)
except errors.NotGitRepository:
  r = clone_repo()  # You'll have to define this function yourself; see
below.


> If it has, it'll do a git pull to make sure it's up to date.


Fetching is unfortunately a bit circuitous. I'll let Augie or Jelmer respond
as they're much more familiar with the use of GitClient. However, once
you've seen the difficult way, feel free to offer any interface suggestions
that would make it easier.

Once you've fetched and have the SHA of a commit, you can "merge" the
changes into your working tree using something like the following
(untested):

tree = r[commit_sha].tree
for filename, mode, sha in store.iter_tree_contents(tree):
    with open(os.path.join(r.root, filename), 'wb') as f:
        f.write(r[sha].data)
    # Honestly not sure if this works on Windows.
    os.chmod(os.path.join(r.root), filename), mode)

I say "merge" in quotes because this just naively overwrites anything in
your working directory rather than doing any sort of merging.

If there is no git repository it'll do a git clone of my git hub repository
> (http://github.com/Shelnutt2/apptester ). Apptester.py then runs the
> commands it's given and after it's done I need to commit the new files it
> produces


To commit a set of files, do r.stage(filenames), followed by
r.do_commit(message, ...). The list of filenames can be a superset of the
changed filenames; staging/committing

(The stage() method puts the files into the repo's staging area. If you're
not familiar with this git concept, see
http://www.gitready.com/beginner/2009/01/18/the-staging-area.html.)


> and then push everything back to the main repo. I'm thinking that this
> needs to be done with the TCP or SSHGitClient and using fetch_pack and
> send_pack?


send_pack is what you want, but again I'll defer to people who know this
piece better.


> If you could just give me a quick example or something on how I would use
> the pack commands that would be wonderful. Also would I not run into the
> problem of only have the pack files and not being able to manipulate the
> files in the packs? (I.e. read the files, and/or add files?)
>

The git data model is such that you can refer to any object by ID regardless
of whether it is stored loose (as a single file on the filesystem) or in a
packfile. Dulwich should be able to handle this transparently as well.


> I know that Dulwich might not be complete or up to par with C git yet, but
> it has a lot of the core parts implemented it seems, and I applaud your guys
> effort. It's making my life a whole lot easier being able to import and use
> the python modules then having to use GitPython or wright my own code to
> wrap around a git binary. This saves me from having to distribute the binary
> and have to worry about linux vs windows.


Glad to hear it. This is exactly the goal of the project :)


> Now I can just have a small python script that they download that first
> gets my apptester.py and then the dulwich files I need and then I can get to
> git.
>
> Thanks for the help,
>
> Seth Shelnutt
>
> On Mon, Jun 28, 2010 at 11:16 PM, David Borowitz <dborowitz@xxxxxxxxxx>wrote:
>
>> Dulwich as it is right now is primarily a Python library you can use to
>> operate on Git repositories; there is much more functionality in the various
>> modules than is exposed through the dulwich command-line tool. If you'd be
>> interested in helping us add more commands, we always appreciate
>> contributions.
>>
>> With regard to the specific functionality you're looking for, fetching,
>> committing, and pushing are currently implemented; see GitClient in
>> client.py for the client implementation. If you would like more detail on
>> how to write a script that uses this functionality we'd be happy to help.
>>
>> There is currently no merge implementation, though when that does get
>> implemented it should have the same merge semantics as C git.
>>
>> I apologize if these answers aren't that helpful. We do have the
>> admittedly ambitious goal of achieving parity with C git, but we are a very
>> small development team and have been focusing our work recently in other
>> areas.
>>
>> _______________________________________________
>>> Mailing list: https://launchpad.net/~dulwich-users<https://launchpad.net/%7Edulwich-users>
>>> Post to     : dulwich-users@xxxxxxxxxxxxxxxxxxx
>>> Unsubscribe : https://launchpad.net/~dulwich-users<https://launchpad.net/%7Edulwich-users>
>>> More help   : https://help.launchpad.net/ListHelp
>>>
>>>
>>
>

Follow ups

References