纯Python中是否有Git的实现?

Pio*_*ost 12 python git dvcs

纯Python中是否有Git的实现?

Cha*_*esB 13

发现德威:

Dulwich是Git文件格式和协议的纯Python实现.

该项目以Git先生和夫人居住在Monty Python草图中的村庄命名.

看起来像一个低级库,API对我的眼睛看起来并不友好,但是在Github页面上有一个教程

  • 这里有一个渲染版本:http://www.samba.org/~jelmer/dulwich/docs/tutorial/index.html (2认同)

iLo*_*Tux 7

我知道这个问题相当老了,但我只是想我会为下一个人添加这个问题。接受的答案提到了德威并提到它的水平相当低(这也是我的观点)。我发现吉特尔,它是 Dulwich 的高级包装。它相当容易使用。

安装

$ pip install gittle
Run Code Online (Sandbox Code Playgroud)

示例(取自项目的 README.md):

克隆存储库

from gittle import Gittle

repo_path = '/tmp/gittle_bare'
repo_url = 'git://github.com/FriendCode/gittle.git'

repo = Gittle.clone(repo_url, repo_path)
Run Code Online (Sandbox Code Playgroud)

从路径初始化存储库

repo = Gittle.init(path)
Run Code Online (Sandbox Code Playgroud)

获取存储库信息

# Get list of objects
repo.commits

# Get list of branches
repo.branches

# Get list of modified files (in current working directory)
repo.modified_files

# Get diff between latest commits
repo.diff('HEAD', 'HEAD~1')
Run Code Online (Sandbox Code Playgroud)

犯罪

# Stage single file
repo.stage('file.txt')

# Stage multiple files
repo.stage(['other1.txt', 'other2.txt'])

# Do the commit
repo.commit(name="Samy Pesse", email="samy@friendco.de", message="This is a commit")
Run Code Online (Sandbox Code Playgroud)

repo = Gittle(repo_path, origin_uri=repo_url)

# Authentication with RSA private key
key_file = open('/Users/Me/keys/rsa/private_rsa')
repo.auth(pkey=key_file)

# Do pull
repo.pull()
Run Code Online (Sandbox Code Playgroud)

repo = Gittle(repo_path, origin_uri=repo_url)

# Authentication with RSA private key
key_file = open('/Users/Me/keys/rsa/private_rsa')
repo.auth(pkey=key_file)

# Do push
repo.push()
Run Code Online (Sandbox Code Playgroud)

分支

# Create branch off master
repo.create_branch('dev', 'master')

# Checkout the branch
repo.switch_branch('dev')

# Create an empty branch (like 'git checkout --orphan')
repo.create_orphan_branch('NewBranchName')

# Print a list of branches
print(repo.branches)

# Remove a branch
repo.remove_branch('dev')

# Print a list of branches
print(repo.branches)
Run Code Online (Sandbox Code Playgroud)

这些只是我认为最常见用例的部分(再次从项目的 README.md 中提取)。如果您需要更多,您应该自己检查该项目。

  • 请注意,dulwich 现在还有一个“dulwich.porcelain”模块,它提供了更高级别的操作。 (2认同)