从内部git中使用Python从URL获取最新的git提交id
从内部git中使用Python从URL获取最新的git提交id
使用以下函数,我提取了最新的git提交ID,并以简短的形式将其写入文本文件中。
from os.path import exists from subprocess import Popen, PIPE def get_git_commit_id(txt_file: str) -> str: """ 获取最新的git提交ID,如果不存在则将其放入文本文件中 参数: ========== :param txt_file: 文本文件的名称 返回值: ========== :return: 最新的git提交ID """ if not exists(txt_file): print(f"'{txt_file}' 之前不存在") # 用于日志记录 try: process = Popen("git rev-parse --short HEAD", stdout=PIPE) output = process.communicate()[0] except Exception as error: output = bytes("latest", "utf-8") print("无法读取 .git", error) # 用于日志记录 with open(txt_file, "w", encoding="utf8") as file: file.write(output.decode("utf-8")) file = open(txt_file, "r", encoding="utf8") git_commit_id = file.readline().strip() return git_commit_id get_git_commit_id("git_commit_id.txt")
然而,这段代码仅在项目中存在.git
目录时才有效。
如何从我的项目所在的内部git的URL中提取最新的git提交ID?
在这段代码中,我能够得到所需的输出:
# 导入 from os import devnull from os.path import exists from logging import getLogger from urllib.parse import parse_qsl from urllib.request import urlopen from ssl import create_default_context from xml.dom.minidom import parseString from subprocess import call, Popen, STDOUT, PIPE logger = getLogger(__name__) project_git_url = "https://path/to/your/git.com/?p=your-project;a=summary" cert = None def get_git_commit_id(txt_file: str) -> str: """ 获取最新的 git 提交 ID,并将其放入文本文件中(如果不存在) 参数: ========== :param txt_file: 文本文件的名称 返回: ========== :return: 最新的 git 提交 ID """ if not exists(txt_file): logger.info( msg=f"The following text file does not exist : '{txt_file}'. Now it was created.", exc_info=False, ) output = bytes("latest", "utf-8") git_check = call(["git", "master"], stderr=STDOUT, stdout=open(devnull, 'w')) if git_check != 0: process = Popen("git rev-parse --short HEAD", stdout=PIPE) output = process.communicate()[0] else: logger.debug( msg=f"It could not read the .git repository to extract the latest git commit id.", exc_info=False, ) try: url = urlopen(project_git_url, context=create_default_context(cafile=cert)) url_as_xml = url.read().decode("utf-8") docs = parseString(url_as_xml) html = docs.getElementsByTagName("html")[0] body = html.getElementsByTagName("body")[0] for div_element in body.getElementsByTagName("div"): if 'page_nav' in div_element.attributes.items()[0]: for href in div_element.getElementsByTagName("a"): href_content = href.attributes.items()[0][1] if 'a=commit;' in href_content: parsed_href_content = parse_qsl(href_content, encoding='utf-8', separator=';') output = bytes(parsed_href_content[2][1][:6], "utf-8") except Exception as error: logger.debug( msg=f"It could not get the latest git commit id from the git : {project_git_url}.\nThe following error occurred : {error}", exc_info=False, ) with open(txt_file, "w", encoding="utf8") as file: file.write(output.decode("utf-8")) file = open(txt_file, "r", encoding="utf8") git_commit_id = file.readline().strip() return git_commit_id
它还包括解析内部 git 位置的 URL。
参考文献:
从给定的URL获得内部git的最新提交ID,可以通过以下步骤实现:
首先,使用pip安装gitpython库:
pip install gitpython
然后,导入git库:
import git
接下来,指定仓库的URL:
repo_url = 'https://your-repository-url.com/your-repo.git'
然后,克隆仓库到本地指定路径(这里假设为'/path/to/local/repo'):
repo = git.Repo.clone_from(repo_url, '/path/to/local/repo', depth=1)
获取最新的提交:
latest_commit = repo.head.commit
打印提交ID:
print(latest_commit.hexsha)
通过上述步骤,可以从给定URL的内部git获取到最新的提交ID。