Piotr Dobrogost
Piotr Dobrogost

Reputation: 42425

Is there implementation of Git in pure Python?

Is there implementation of Git in pure Python?

Upvotes: 17

Views: 5600

Answers (2)

CharlesB
CharlesB

Reputation: 90336

Found Dulwich:

Dulwich is a pure-Python implementation of the Git file formats and protocols.

The project is named after the village in which Mr. and Mrs. Git live in the Monty Python sketch.

Looks like a low-level library, the API did not appear friendly to my eyes, but there's a tutorial on the Github page

Upvotes: 19

iLoveTux
iLoveTux

Reputation: 3625

I know that this question is rather old, but I just thought I would add this for the next guy. The accepted answer mentions Dulwich and mentions that it is rather low-level (which is also my opinion). I found gittle which is a high-level wrapper around Dulwich. It's rather easy to use.

Install

$ pip install gittle

Examples (taken from the project's README.md):

Clone a repository

from gittle import Gittle

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

repo = Gittle.clone(repo_url, repo_path)

Init repository from a path

repo = Gittle.init(path)

Get repository information

# 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')

Commit

# 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="[email protected]", message="This is a commit")

Pull

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()

Push

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()

Branch

# 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)

These are just the parts (again pulled from the project's README.md) which I think would be most common use-cases. You should check out the project yourself if you need more than this.

Upvotes: 8

Related Questions