Is there implementation of Git in pure Python?

后端 未结 3 959
甜味超标
甜味超标 2021-02-13 22:50

Is there implementation of Git in pure Python?

3条回答
  •  别那么骄傲
    2021-02-13 23:43

    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="samy@friendco.de", 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.

提交回复
热议问题