使用 Python 操作 Git 版本库 - GitPython

使用 Python 操作 Git 版本库 - GitPython

GitPython 是一个用于操作 Git 版本库的 python 包,
它提供了一系列的对象模型(库 - Repo、树 - Tree、提交 - Commit等)
用于操作版本库中的相应对象。

版本库对象 - Repo

首先,使用包含 .git 文件夹的版本库路径创建 git.Repo 对象

from git import Repo
# 创建版本库对象
repo = git.Repo(r'E:\Notes')

然后便可以使用这个 Repo 对象对版本库进行操作,如:

# 版本库是否为空版本库
repo.bare

# 当前工作区是否干净
repo.is_dirty()

# 版本库中未跟踪的文件列表
repo.untracked_files

# 克隆版本库
repo.clone('clone_path')

# 压缩版本库到 tar 文件
with open('repo.tar', 'wb') as fp:
    repo.archive(fp)

# 新建分支
repo.create_head('branchname')

# 查看当前分支
repo.active_branch

索引/暂存区对象 - Index

Git 术语中,index 表示暂存区,为下次将要提交到版本库里的文件,
GitPython 提供 Repo.Index 来操作暂存区,如添加、提交操作

index = repo.index
index.add(['new.txt'])
index.remove(['old.txt'])
index.commit('this is a test')

远程版本库操作 - Remotes

Remotes 用于操作远程版本库,可以通过 Repo.remote 方法获取远程版本库,
Repo.Remotes 属性获取远程版本库列表

# 获取默认版本库 origin
remote = repo.remote()
# 从远程版本库拉取分支
remote.pull()
# 推送本地分支到远程版本库
remote.push()
# 重命名远程分支
# remote.rename('new_origin')

直接执行 Git 命令

一般我们在工作目录做了改变之后,就会调用 git add 命令添加文件到暂存区,
然后调用 git commit 命令提交更改,Repo 虽然没有添加、提交方法,
但取而代之提供了一个 git.cmd.Git 对象实现对 Git 命令的调用,
通过 Repo.git 来进行 Git 命令操作。

git = repo.git
git.add('test1.txt') # git add test1.txt
git.commit('-m', 'this is a test') # git commit -m 'this is a test'

Repo.git.[command] 即相当于调用对应的 git 命令,而调用对应命令方法所用的参数,
会被转换成跟在命令后的参数。

而调用命令方法所用的命名参数会被转换成对应的完整参数,如:git.command(flag=True)
会被转换成 git command --flag 命令执行

总结

基本的 Git 操作可以概括如下:

# 新建版本库对象
repo = Repo(r'E:\Notes')

# 进行文件修改操作

# 获取版本库暂存区
index = repo.index
# 添加修改文件
index.add(['new.txt'])
# 提交修改到本地仓库
index.commit('this is a test')

# 获取远程仓库
remote = repo.remote()
# 推送本地修改到远程仓库
remote.push()

python操作git

安装模块

pip3 install gitpython

基本使用

import os
from git.repo import Repo

# 创建本地路径用来存放远程仓库下载的代码
download_path = os.path.join('NB')
# 拉取代码
Repo.clone_from('https://github.com/DominicJi/TeachTest.git',to_path=download_path,branch='master')

其他常见操作

# ############## 2. pull最新代码 ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
repo.git.pull()
# ############## 3. 获取所有分支 ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
 
branches = repo.remote().refs
for item in branches:
    print(item.remote_head)
    
# ############## 4. 获取所有版本 ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
 
for tag in repo.tags:
    print(tag.name)
# ############## 5. 获取所有commit ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
 
# 将所有提交记录结果格式成json格式字符串 方便后续反序列化操作
commit_log = repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}', max_count=50,
                          date='format:%Y-%m-%d %H:%M')
log_list = commit_log.split("\n")
real_log_list = [eval(item) for item in log_list]
print(real_log_list)
 
# ############## 6. 切换分支 ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
 
before = repo.git.branch()
print(before)
repo.git.checkout('master')
after = repo.git.branch()
print(after)
repo.git.reset('--hard', '854ead2e82dc73b634cbd5afcf1414f5b30e94a8')
# ############## 7. 打包代码 ##############
import os
from git.repo import Repo

local_path = os.path.join(NB')
repo = Repo(local_path)

with open(os.path.join('NB.tar'), 'wb') as fp:
    repo.archive(fp)

将上述所有的方法封装到类中以便后续的调用(后续如果你想要操作git直接拷贝使用即可)

import os
from git.repo import Repo
from git.repo.fun import is_git_dir


class GitRepository(object):
    """
    git仓库管理
    """
    def __init__(self, local_path, repo_url, branch='master'):
        self.local_path = local_path
        self.repo_url = repo_url
        self.repo = None
        self.initial(repo_url, branch)

    def initial(self, repo_url, branch):
        """
        初始化git仓库
        :param repo_url:
        :param branch:
        :return:
        """
        if not os.path.exists(self.local_path):
            os.makedirs(self.local_path)

        git_local_path = os.path.join(self.local_path, '.git')
        if not is_git_dir(git_local_path):
            self.repo = Repo.clone_from(repo_url, to_path=self.local_path, branch=branch)
        else:
            self.repo = Repo(self.local_path)

    def pull(self):
        """
        从线上拉最新代码
        :return:
        """
        self.repo.git.pull()

    def branches(self):
        """
        获取所有分支
        :return:
        """
        branches = self.repo.remote().refs
        return [item.remote_head for item in branches if item.remote_head not in ['HEAD', ]]

    def commits(self):
        """
        获取所有提交记录
        :return:
        """
        commit_log = self.repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}',
                                       max_count=50,
                                       date='format:%Y-%m-%d %H:%M')
        log_list = commit_log.split("\n")
        return [eval(item) for item in log_list]

    def tags(self):
        """
        获取所有tag
        :return:
        """
        return [tag.name for tag in self.repo.tags]

    def change_to_branch(self, branch):
        """
        切换分值
        :param branch:
        :return:
        """
        self.repo.git.checkout(branch)

    def change_to_commit(self, branch, commit):
        """
        切换commit
        :param branch:
        :param commit:
        :return:
        """
        self.change_to_branch(branch=branch)
        self.repo.git.reset('--hard', commit)

    def change_to_tag(self, tag):
        """
        切换tag
        :param tag:
        :return:
        """
        self.repo.git.checkout(tag)


if __name__ == '__main__':
    local_path = os.path.join('codes', 'luffycity')
    repo = GitRepository(local_path,remote_path)
    branch_list = repo.branches()
    print(branch_list)
    repo.change_to_branch('dev')
    repo.pull()

参考链接:

https://my.oschina.net/hopeMan/blog/141221

http://gitpython.readthedocs.io/en/stable/tutorial.html

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值