【问题标题】:Git library for Ruby or Python?Ruby 或 Python 的 Git 库?
【发布时间】:2010-06-27 17:04:41
【问题描述】:

我正在寻找可用于在本地存储库中更新和提交更改的 Git 客户端的 Ruby 或 Python 实现。

如果库根本不使用 shell 命令,而是将所有内容保存在“纯代码”中,我更喜欢。

有吗?

提前谢谢你。

【问题讨论】:

    标签: python ruby git


    【解决方案1】:

    还有Dulwich,它是 Git 文件格式和协议的 Python 实现。

    【讨论】:

      【解决方案2】:

      Grit 让您可以通过 Ruby 对 Git 存储库进行面向对象的读/写访问。

      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">]
      
      ...
      

      【讨论】:

        【解决方案3】:

        您可以查看ruby-git gem

        【讨论】:

        • 这是目前(2015 年 10 月)Ruby 中使用最多的 Git 包装器。
        【解决方案4】:

        对于 Python,@RyanWilcox 已经提到了 Dulwich 库。

        对于 Ruby,遗憾的是没有 Git 库。有Grit,它在Ruby中实现了Git的一个子集,并为一些附加功能包装了命令行工具,但只支持GitHub需要的Git子集。您可以通过 JRuby 或 IronRuby 使用 JGitGit#

        【讨论】:

          【解决方案5】:

          现在有 libgit2:一个 C 库 sponsored by Githubmany bindings,包括 Ruby 和 Python。

          【讨论】:

            【解决方案6】:

            GitPython 有一个类似于 Grit 的面向对象的 API:

            >>> #$ 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">]
            

            【讨论】:

            • GitPython 的主要问题是它是实际 git 可执行文件之上的一个包装器,包含它所需要的一切。 Dulwich 和 libgit2 在纯 Python 或 C 库级别工作。
            • @GicomoLacava 是的,像 Dulwich 这样的纯 python 实现有很多依赖和跨平台的优势。但是,可执行文件的包装器的优势在于,您可以确保它正确地实现了可执行文件格式等的特定版本,以及该可执行文件的所有最新功能。它更干燥,更有未来的证明。也就是说,下次我在纯 python 模块中需要 git 时,我一定会尝试dulwich,这样我就不会因为依赖复杂性而污染我的模块。
            猜你喜欢
            • 2023-03-03
            • 2011-12-15
            • 1970-01-01
            • 2011-04-23
            • 2018-05-25
            • 1970-01-01
            • 2012-01-28
            • 1970-01-01
            • 2012-11-12
            相关资源
            最近更新 更多