要获取当前 Git 堆栈的远程所在,可以使用 [code]subprocess[/code] 模块执行 Git 命令。下面是怎样做到这一点的示例代码: [code]import subprocess def get_git_remote_url(): try: # 获取远程 URL result = subprocess.run( ['git', 'config', '--get', 'remote.origin.url'], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) # 获取并返回输出 remote_url = result.stdout.strip() return remote_url except subprocess.CalledProcessError as e: print(f"An error occurred: {e}") return None # 使用示例 remote_url = get_git_remote_url() if remote_url: print(f"Remote URL: {remote_url}") else: print("Failed to retrieve the remote URL.") [/code]注意事项:
拓展:python利用git gitpython模块 安装模块 [code]pip3 install gitpython [/code]基本使用 [code]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') [/code]其他常见利用 [code]# ############## 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) [/code]全部的方法封装到类中 [code]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() [/code]到此这篇关于python获取当前git的repo所在的示例代码的文章就先容到这了,更多相干python获取git repo所在内容请搜索脚本之家从前的文章或继续浏览下面的相干文章盼望大家以后多多支持脚本之家! 来源:https://www.jb51.net/python/328128pmz.htm 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|手机版|小黑屋|梦想之都-俊月星空
( 粤ICP备18056059号 )|网站地图
GMT+8, 2025-7-1 19:08 , Processed in 0.030057 second(s), 20 queries .
Powered by Mxzdjyxk! X3.5
© 2001-2025 Discuz! Team.