简体   繁体   English

开始使用Git Python

[英]Getting started with Git Python

My goal is to access existing Git repos from Python. 我的目标是从Python访问现有的Git repos。 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 得到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. 文档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? 你能推荐任何其他lib用于Git访问吗? I am developing Ubuntu app, so it would be appriciated to have ubuntu package for easier deployment. 我正在开发Ubuntu应用程序,因此可以使用ubuntu软件包以便于部署。

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. 我还会定期检查以检测repo中的新变化,所以我宁愿使用远程工具,这样我就可以检测到尚未转移到本地的更改。 I'm not sure how this should work, so any help will be appriciated. 我不确定这应该如何运作,所以任何帮助都会得到满足。

Thanks in advance. 提前致谢。

I think that init method is used to create a new repository, to open an existing one you just pass the path to it this way: 我认为init方法用于创建一个新的存储库,打开一个现有的存储库,你只需这样传递路径:

from dulwich.repo import Repo
repo = Repo(<path>)

For a summary of alternative libraries please have a look at this answer . 有关替代库的摘要,请查看此答案 Basically, it suggests that it's easier to use subprocess module because it's the best way to use the interface you already know. 基本上,它表明使用subprocess模块更容易,因为它是使用您已知的界面的最佳方式。

Most of Dulwich' documentation assumes a fair bit of knowledge of the Git file formats/protocols. 德威的大多数文档都假定您对Git文件格式/协议有相关的了解。

You should be able to open an existing repository with Repo : 您应该能够使用Repo打开现有存储库:

from dulwich.repo import Repo
x = Repo("/path/to/git/repo")

or create a new one: 或创建一个新的:

x = Repo.init("/path/to/new/repo")

To get the diff for a particular commit (the diff with its first parent) 获取特定提交的差异(具有第一个父级的差异)

from dulwich.patch import write_tree_diff
commit = x[commit_id]
parent_commit = x[commit.parents[0]]
write_tree_diff(sys.stdout, x.object_store, parent_commit.tree, commit.tree)

The Git protocol only allows fetching/sending packs, it doesn't allow direct access to specific objects in the database. Git协议只允许提取/发送包,它不允许直接访问数据库中的特定对象。 This means that to inspect a remote repository you first have to fetch the relevant commits from the remote repo and then you can view them: 这意味着要检查远程存储库,首先必须从远程仓库获取相关提交,然后您可以查看它们:

from dulwich.client import get_transport_and_path
client, path = get_transport_and_path(remote_url)
remote_refs = client.fetch(path, x)
print x[remote_refs["refs/heads/master"]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM