gitpython将每次的commit导出项目(可以导出到指定文件夹)
| import git |
| import subprocess |
| 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) |
| |
| def export_archive(self,fp,file_path): |
| """ |
| 将commit导出作为一个项目文件 |
| """ |
| self.repo.git.archive(fp, output=file_path) |
| |
| |
| |
| if __name__ == '__main__': |
| |
| |
| local_path = '/Users/song/Code/threejs_哔站_汐月风尘/three-learner' |
| |
| out_path = os.path.join(local_path, 'out') |
| |
| repo_path = '' |
| |
| repo = GitRepository(local_path,repo_path) |
| |
| commit_list = repo.commits() |
| for commit in commit_list: |
| |
| time = commit['date'].replace(' ','_').replace(':','') |
| summary = commit['summary'].replace(':','_') |
| output_file = os.path.join(out_path,time+"_"+summary+'.zip') |
| commit_sha = commit['commit'] |
| |
| |
| subprocess.call(['git', 'archive', '--format=zip', '-o', output_file, commit_sha], cwd=local_path) |
gitpython导出(只能导出到当前文件夹)
| 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) |
| |
| def export_archive(self,fp,file_path): |
| """ |
| 将commit导出作为一个项目文件 |
| """ |
| self.repo.git.archive(fp, output=file_path) |
| |
| |
| |
| if __name__ == '__main__': |
| |
| local_path = '/Users/song/Code/threejs_哔站_汐月风尘/three-learner' |
| out_path = os.path.join(local_path, 'out') |
| repo_path = '' |
| repo = GitRepository(local_path,repo_path) |
| |
| commit_list = repo.commits() |
| for commit in commit_list: |
| |
| time = commit['date'].replace(' ','_').replace(':','') |
| summary = commit['summary'].replace(':','_') |
| commit_exp = repo.repo.commit(commit['commit']) |
| file_name = time+"_"+summary+'.zip' |
| |
| with open(file_name, 'wb') as fp: |
| |
| repo.repo.archive(fp,treeish=commit_exp,format='zip') |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战