【Python】快速判断两个commit 是否存在cherry-pick 关系

判断两个提交是否有 cherry-pick 关系的 Python 脚本,可以基于以下三种常见情况进行优化:

  1. Commit Hash 一致:如果两个提交的 hash 完全相同,那么它们是相同的提交。

  2. Commit Title 存在关联:如果两个提交的 commit message 提及了相同的原始提交,例如 cherry picked from commit <hash>,可以通过解析提交信息来确定关联。

  3. Commit 变更内容一致:即使 hashtitle 不同,如果两个提交的代码变更完全一致,则可能是 cherry-pick 关系。

另外,还有可能存在其他场景,比如提交的父节点不同,但内容一致,这也是一种 cherry-pick 的表现。

基于以上情况,下面是一个优化的 Python 脚本:

Python 脚本

V1版本
import subprocess
import redef run_git_command(args):"""运行 Git 命令并返回输出结果。"""try:result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)if result.returncode != 0:print(f"Error running command {args}: {result.stderr}")return Nonereturn result.stdout.strip()except Exception as e:print(f"An error occurred: {e}")return Nonedef get_commit_message(commit_hash):"""获取提交信息(commit message)。"""return run_git_command(["git", "log", "-1", "--pretty=%B", commit_hash])def get_diff(commit_hash):"""获取提交的代码差异(diff)。"""return run_git_command(["git", "diff-tree", "--no-commit-id", "--patch", "-r", commit_hash])def check_cherry_pick_message(commit1_message, commit2_message):"""检查提交信息中是否提到 cherry-pick 的关联。"""cherry_pick_pattern = re.compile(r'cherry\s+picked\s+from\s+commit\s+([a-f0-9]{40})')# 检查第一个提交信息是否提到 cherry-pick 的原始提交match1 = cherry_pick_pattern.search(commit1_message)match2 = cherry_pick_pattern.search(commit2_message)if match1 and match2 and match1.group(1) == match2.group(1):return True# 或者检查一个提交是否提到另一个提交if match1 and match1.group(1) in commit2_message:return Trueif match2 and match2.group(1) in commit1_message:return Truereturn Falsedef compare_commit_diffs(commit1, commit2):"""比较两个提交的代码变更是否一致。"""diff1 = get_diff(commit1)diff2 = get_diff(commit2)if diff1 is None or diff2 is None:return Falsereturn diff1 == diff2def compare_commits(commit1, commit2):"""综合比较两个提交是否有 cherry-pick 关系。"""# 1. 检查 commit hash 是否相同if commit1 == commit2:print(f"Commits {commit1} and {commit2} are identical (same hash).")return True# 2. 检查 commit message 是否提到 cherry-pick 关系commit1_message = get_commit_message(commit1)commit2_message = get_commit_message(commit2)if commit1_message is None or commit2_message is None:print("Failed to get commit messages.")return Falseif check_cherry_pick_message(commit1_message, commit2_message):print(f"Commits {commit1} and {commit2} have a cherry-pick relation based on commit message.")return True# 3. 检查代码变更是否完全一致if compare_commit_diffs(commit1, commit2):print(f"Commits {commit1} and {commit2} have identical code changes (cherry-pick relation likely).")return Trueprint(f"Commits {commit1} and {commit2} do not have a clear cherry-pick relation.")return Falseif __name__ == "__main__":# 输入需要比较的两个 commit hashcommit_hash_1 = input("Enter the first commit hash: ")commit_hash_2 = input("Enter the second commit hash: ")if compare_commits(commit_hash_1, commit_hash_2):print(f"Commits {commit_hash_1} and {commit_hash_2} are related (cherry-pick detected).")else:print(f"Commits {commit_hash_1} and {commit_hash_2} are not related (no cherry-pick detected).")

如果存在编码错误

可以使用下方脚本尝试

V2版本
import subprocess
import redef run_git_command(args):"""运行 Git 命令并返回输出结果。"""try:result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, encoding='utf-8')if result.returncode != 0:print(f"Error running command {args}: {result.stderr}")return Nonereturn result.stdout.strip()except Exception as e:print(f"An error occurred: {e}")return Nonedef get_commit_message(commit_hash):"""获取提交信息(commit message)。"""return run_git_command(["git", "log", "-1", "--pretty=%B", commit_hash])def get_diff(commit_hash):"""获取提交的代码差异(diff)。"""try:return run_git_command(["git", "diff-tree", "--no-commit-id", "--patch", "-r", commit_hash])except Exception as e:print(f"An error occurred while getting diff for {commit_hash}: {e}")return Nonedef check_cherry_pick_message(commit1_message, commit2_message):"""检查提交信息中是否提到 cherry-pick 的关联。"""cherry_pick_pattern = re.compile(r'cherry\s+picked\s+from\s+commit\s+([a-f0-9]{40})')match1 = cherry_pick_pattern.search(commit1_message)match2 = cherry_pick_pattern.search(commit2_message)if match1 and match2 and match1.group(1) == match2.group(1):return Trueif match1 and match1.group(1) in commit2_message:return Trueif match2 and match2.group(1) in commit1_message:return Truereturn Falsedef compare_commit_diffs(commit1, commit2):"""比较两个提交的代码变更是否一致。"""diff1 = get_diff(commit1)diff2 = get_diff(commit2)if diff1 is None or diff2 is None:return Falsereturn diff1 == diff2def compare_commits(commit1, commit2):"""综合比较两个提交是否有 cherry-pick 关系。"""if commit1 == commit2:print(f"Commits {commit1} and {commit2} are identical (same hash).")return Truecommit1_message = get_commit_message(commit1)commit2_message = get_commit_message(commit2)if commit1_message is None or commit2_message is None:print("Failed to get commit messages.")return Falseif check_cherry_pick_message(commit1_message, commit2_message):print(f"Commits {commit1} and {commit2} have a cherry-pick relation based on commit message.")return Trueif compare_commit_diffs(commit1, commit2):print(f"Commits {commit1} and {commit2} have identical code changes (cherry-pick relation likely).")return Trueprint(f"Commits {commit1} and {commit2} do not have a clear cherry-pick relation.")return Falsedef validate_commit_hash(commit_hash):"""验证提交 hash 是否是有效的 SHA-1 hash。"""if re.fullmatch(r'[a-fA-F0-9]{40}', commit_hash):return Trueelse:print(f"Invalid commit hash: {commit_hash}")return Falseif __name__ == "__main__":commit_hash_1 = input("Enter the first commit hash: ")commit_hash_2 = input("Enter the second commit hash: ")if validate_commit_hash(commit_hash_1) and validate_commit_hash(commit_hash_2):if compare_commits(commit_hash_1, commit_hash_2):print(f"Commits {commit_hash_1} and {commit_hash_2} are related (cherry-pick detected).")else:print(f"Commits {commit_hash_1} and {commit_hash_2} are not related (no cherry-pick detected).")else:print("Please enter valid commit hashes.")

脚本的工作原理

  1. Commit Hash 一致:脚本首先检查两个提交的 hash 是否完全相同。如果相同,它们肯定是相同的提交。

  2. Commit Title 存在关联:通过正则表达式匹配 commit message 中的 cherry picked from commit <hash> 字符串,检查一个提交是否从另一个提交进行了 cherry-pick。如果 commit message 中存在这样的引用,则说明它们有 cherry-pick 关系。

  1. Commit 变更内容一致:如果提交的 hashmessage 没有明显的关联,脚本通过获取两个提交的代码变更(git diff-tree)并对比差异内容是否完全相同。如果两个提交的代码变更完全一致,它们很有可能是 cherry-pick 关系。

使用说明

  1. 将脚本保存为 compare_commits.py

  2. 在 Git 仓库的根目录执行以下命令:

     

    python3 compare_commits.py

  3. 输入两个需要比较的提交哈希(commit hash)。

处理的场景

  • 两个提交的 commit hash 完全相同(直接相同的提交)。

  • commit message 提到 cherry-pick 的引用关系。

  • commit hashcommit message 不同,但代码变更内容完全相同。

这个脚本可以帮助识别 cherry-pick 关系的多种常见情况,并可以扩展以处理更多特殊情况。你可以尝试运行这个脚本,看看能否正确检测出 cherry-pick 关系。

运行示例

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1540768.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

如何下载ComfyUI开发版

看B站视频&#xff0c;见用绘世可以下载ComfyUI开发版&#xff0c;而我又不想在电脑里放太多东西&#xff0c;于是研究了一下&#xff0c;如何直接从GitHub网站下载。具体步骤看图示。 看压缩包内容&#xff0c;应该直接解压覆盖就可以了&#xff0c;暂未有时间测试。

科研绘图系列:R语言散点图和小提琴图(scatter plot violin plot)

文章目录 介绍加载R包导入数据数据预处理函数画图系统信息介绍 提取模型的结果并对模型的结果进行可视化。 加载R包 library(ggplot2) library(ggridges) library(patchwork) library(party) library(caret) library(dplyr

堆的向下调整算法和TOPK问题

目录 1.什么是堆&#xff1f; 1.1 向下调整建堆的时间复杂度计算 1.2 堆的结构体设计 2.堆的功能实现&#xff1a; 2.1 堆的插入&#xff1a; 2.2 堆的删除&#xff1a; 2.3 堆排序&#xff1a; 2.4 向下调整建堆&#xff1a; 2.5 TOPK问题&#xff1a; 2.6 向上调整算…

【Unity踩坑】UI Image的fillAmount不起作用

在游戏场景中&#xff0c;我们经常在界面上展示进度条&#xff0c;当然有各种形状的&#xff0c;线性的&#xff0c;长方形的&#xff0c;圆形&#xff0c;环形等等。 Unity中实现这种效果的话&#xff0c;最基本的方法说是改变Image的fillAmout属性。 如果你是初次使用UI Ima…

ubuntu安装SFML库+QT使用SFML库播放声音

(1)ubuntu安装SFML库 sudo apt-get install libsfml-dev (2)QT使用SFML库播放声音 在.pro文件中添加头文件路径和库文件路径 INCLUDEPATH /usr/include/SFML LIBS /usr/lib/x86_64-linux-gnu/libsfml*.so UI界面中创建一个pushbutton按钮&#xff0c;并且创建槽函数 加载…

国外大带宽服务器怎么连接

随着互联网技术的发展&#xff0c;企业和个人用户越来越依赖于高速的数据传输服务。国外的大带宽服务器因其高速度、稳定性及较低延迟等优势&#xff0c;成为了许多跨国公司、网站托管商以及数据密集型应用的选择。以下是连接国外大带宽服务器的一些常见方法及其注意事项。 选择…

STL-常用算法 遍历/查找/排序/拷贝和替换/算数生成/集合算法

STL常用算法 常用的遍历算法 for_each #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; #include<vector> #include<algorithm>void myPrint(int v) {cout << v << " "; }class MyPrint { public:void op…

切换到WDDM模式,Tesla M4可以用于本地显示输出了!

正文共&#xff1a;1333 字 21 图&#xff0c;预估阅读时间&#xff1a;2 分钟 上次安装完Tesla M4显卡之后&#xff08;HPE服务器通过显卡直通安装Tesla M4&#xff0c;这算亮机成功了吗&#xff1f;&#xff09;&#xff0c;系统识别正常&#xff0c;但是不能用于显示&#x…

C语言的文件基础知识

一、文件存在的意义 ① 文件的定义是什么&#xff1f; 文件是以单个名称在计算机上存储的信息集合。文件可以是文本文档、图片、程序等等。文件通常具有三个字母的文件扩展名&#xff0c;用于指示文件类型&#xff08;例如&#xff0c;图片文件常常以 JPEG 格式保存并且文件扩…

Hive企业级调优[4]——HQL语法优化之分组聚合优化

HQL语法优化之分组聚合优化 优化说明 在 Hive 中&#xff0c;未经优化的分组聚合通常通过一个 MapReduce Job 实现。Map 端负责读取数据&#xff0c;并按分组字段进行分区&#xff0c;通过 Shuffle 将数据发送至 Reduce 端&#xff0c;在 Reduce 端完成最终的聚合运算。 Hiv…

网页交互模拟:模拟用户输入、点击、选择、滚动等交互操作

目录 一、理论基础 1.1 网页交互模拟的重要性 1.2 网页交互的基本原理 二、常用工具介绍 2.1 Selenium 2.2 Puppeteer 2.3 Cypress 2.4 TestCafe 三、实战案例 3.1 模拟用户输入 3.2 模拟用户点击 3.3 模拟用户选择 3.4 模拟滚动操作 四、最佳实践与优化 4.1 代…

用 Pygame 实现一个乒乓球游戏

用 Pygame 实现一个乒乓球游戏 伸手需要一瞬间&#xff0c;牵手却要很多年&#xff0c;无论你遇见谁&#xff0c;他都是你生命该出现的人&#xff0c;绝非偶然。若无相欠&#xff0c;怎会相见。 引言 在这篇文章中&#xff0c;我将带领大家使用 Pygame 库开发一个简单的乒乓球…

系统优化工具 | Windows Manager v2.0.5 便携版

Windows Manager 是一款专为Microsoft Windows 10/11设计的系统优化和管理软件。它集成了多种实用程序&#xff0c;旨在帮助用户更好地管理和优化Windows操作系统。该软件的功能包括系统清理、系统优化、系统修复、硬件信息查看和系统设置调整等。 系统清理&#xff1a;Window…

Qt Creator项目模板介绍

在Qt Creator中创建项目时&#xff0c;用户可以从多个模板类别中进行选择&#xff0c;以满足不同的开发需求。 Application(Qt) 在Application(Qt)类别下&#xff0c;Qt Creator提供了多种用于创建不同类型Qt应用程序的模板。这些模板主要包括&#xff1a; Qt Widgets Applic…

前缀和与差分(二维)

二维前缀和 下面是一个二维数组&#xff0c;我们要求&#xff08;1&#xff0c;1&#xff09;到&#xff08;2&#xff0c;2&#xff09;区间内的所有元素的和&#xff0c;最原始的方法就是遍历每个元素然后一个一个加起来&#xff0c;此时时间复杂度为O(n*m)。 我们之前学过…

【计算机网络篇】电路交换,报文交换,分组交换

本文主要介绍计算机网络中的电路交换&#xff0c;报文交换&#xff0c;分组交换&#xff0c;文中的内容是我认为的重点内容&#xff0c;并非所有。参考的教材是谢希仁老师编著的《计算机网络》第8版。跟学视频课为河南科技大学郑瑞娟老师所讲计网。 目录 &#x1f3af;一.划分…

【实战篇】MySQL是怎么保证主备一致的?

MySQL 主备的基本原理 如图 1 所示就是基本的主备切换流程。 在状态 1 中&#xff0c;客户端的读写都直接访问节点 A&#xff0c;而节点 B 是 A 的备库&#xff0c;只是将 A 的更新都同步过来&#xff0c;到本地执行。这样可以保持节点 B 和 A 的数据是相同的。 当需要切换的…

PostgreSQL JAVA与SQL集成之PL/Java

PostgreSQL pljava PL/Java 作为 PostgreSQL 的编程语言扩展之一&#xff0c;与 PL/pgSQL&#xff08;PostgreSQL 原生的存储过程语言&#xff09;相比&#xff0c;提供了 Java 语言特有的面向对象功能&#xff0c;并支持 Java 的标准库和第三方库。由于 Java 是一种跨平台的语…

企业搭建VR虚拟展厅,如何选择搭建平台?

选择虚拟展厅搭建平台时&#xff0c;需要综合考虑多个因素以确保平台能够满足您的具体需求并提供高质量的展示效果。以下是一些关键的选择标准&#xff1a; 1. 技术实力与创新能力 技术平台选择&#xff1a;确保平台支持虚拟现实&#xff08;VR&#xff09;、增强现实&#xf…

Qt clicked()、clicked(bool)、toggled(bool)信号的区别和联系

clicked() 信号 所属控件&#xff1a;clicked()信号是QAbstractButton类&#xff08;及其子类&#xff0c;如QPushButton、QRadioButton、QCheckBox等&#xff09;的一个信号。clicked信号可以说是许多控件&#xff08;特别是按钮类控件&#xff0c;如QPushButton&#xff09;…