开发者

Git library for Ruby or Python?

开发者 https://www.devze.com 2023-01-05 08:37 出处:网络
I\'m looking for a Ruby or Python implementation of the Git client that can be used to update and commit changes开发者_如何学运维 in a local repository.

I'm looking for a Ruby or Python implementation of the Git client that can be used to update and commit changes开发者_如何学运维 in a local repository.

I prefer if the library does not use shell commands at all but keeps everything in "pure code".

Are there any?

Thank you in advance.


There's also Dulwich, a Python implementation of the Git file formats and protocols.


Grit gives you object oriented read/write access to Git repositories via Ruby.

require 'grit'
include Grit
repo = Repo.new("/Users/tom/dev/grit")

repo.commits
# => [#<Grit::Commit "e80bbd2ce67651aa18e57fb0b43618ad4baf7750">,
      #<Grit::Commit "91169e1f5fa4de2eaea3f176461f5dc784796769">,
      #<Grit::Commit "038af8c329ef7c1bae4568b98bd5c58510465493">,
      #<Grit::Commit "40d3057d09a7a4d61059bca9dca5ae698de58cbe">,
      #<Grit::Commit "4ea50f4754937bf19461af58ce3b3d24c77311d9">]

...


You can check out ruby-git gem.


For Python, there is the Dulwich library which @RyanWilcox already mentioned.

For Ruby, there unfortunately is no Git library. There is Grit, which implements a subset of Git in Ruby and wraps the command line tools for some additional features, but only supports the subset of Git that GitHub needs. And you could use either JGit or Git# via JRuby or IronRuby.


There is now libgit2: a C library sponsored by Github with many bindings including Ruby and Python.


GitPython has an object-oriented API similar to Grit:

>>> #$ pip install GitPython
>>> import git
>>> repo = git.Repo('.')
>>> repo.git_dir
'/home/hobs/src/twip/.git'
>>> repo.bare
False
>>> repo.untracked_files
[u'twip/scripts.bak/__init__.py',
 u'twip/scripts.bak/cat_tweets.py',
 u'twip/scripts.bak/clean.py',
 u'twip/scripts.bak/explore.py',
 u'twip/scripts.bak/generate.py',
 u'twip/scripts.bak/plot_globe.py',
 u'twip/scripts.bak/skeleton.py']
>>> repo.head.ref
<git.Head "refs/heads/master">
>>> repo.tags
[<git.TagReference "refs/tags/0.0.1">,
 <git.TagReference "refs/tags/0.0.2">,
 <git.TagReference "refs/tags/0.0.3">]
0

精彩评论

暂无评论...
验证码 换一张
取 消