Unity git 获取当前修改或者新增的文件列表

server/2025/2/27 7:32:48/

直接上代码

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;public class GitFileStatusCheckerTools : MonoBehaviour
{// 获取Git变更文件列表(新增/修改)public static List<string> GetGitModifiedFiles(){var results = new List<string>();string projectRoot = GetGitRepositoryRoot();if (string.IsNullOrEmpty(projectRoot)){UnityEngine.Debug.LogWarning("Not a git repository");return results;}ProcessStartInfo startInfo = new ProcessStartInfo{FileName = "git",Arguments = "status --porcelain",WorkingDirectory = projectRoot,RedirectStandardOutput = true,UseShellExecute = false,CreateNoWindow = true};using (Process process = new Process()){process.StartInfo = startInfo;process.Start();while (!process.StandardOutput.EndOfStream){string line = process.StandardOutput.ReadLine();if (IsValidChange(line, out string filePath)){string unityPath = ConvertToUnityPath(filePath);results.Add(unityPath);}}process.WaitForExit();}return results;}// 获取Git仓库根目录private static string GetGitRepositoryRoot(){string currentPath = Directory.GetCurrentDirectory();DirectoryInfo directory = new DirectoryInfo(currentPath);while (directory != null){if (Directory.Exists(Path.Combine(directory.FullName, ".git"))){return directory.FullName;}directory = directory.Parent;}return null;}// 验证是否为有效变更(修改或新增)private static bool IsValidChange(string statusLine, out string filePath){filePath = "";if (string.IsNullOrEmpty(statusLine)) return false;bool isDebug = false;// 使用正则表达式匹配状态码var match = Regex.Match(statusLine, @"^(|M|A|\?\?)\s+(.*)");if (match.Success){string[] statusList = statusLine.TrimStart().Split(' ');if (statusList.Length < 2){UnityEngine.Debug.Log($"statusLine:{statusLine} 拆分路径错误 异常中间没有空格分割 split length < 2  length:{statusList.Length}");return false;}filePath = statusList[1];if(isDebug){UnityEngine.Debug.Log($"statusLine:{statusLine}\n filePath:{filePath}");}// 处理带空格的文件名(引号包裹)if (filePath.StartsWith("\"") && filePath.EndsWith("\"")){filePath = filePath.Substring(1, filePath.Length - 2);}return true;}return false;}// 转换为Unity相对路径private static string ConvertToUnityPath(string fullPath){string projectRoot = GetGitRepositoryRoot();string relativePath = fullPath.Replace(projectRoot, "").TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).Replace(Path.DirectorySeparatorChar, '/');return AddAssetPrefixSmart(relativePath);}private static string AddAssetPrefixSmart(string relativePath){const string assetsPrefix = "Assets/";// 情况1:已经是合法Unity路径if (relativePath.StartsWith(assetsPrefix, StringComparison.Ordinal)){return relativePath;}// 情况2:非Assets开头但确实在Assets目录下string[] pathSegments = relativePath.Split('/');if (pathSegments.Length > 0 && pathSegments[0] == "Assets"){return relativePath;}// 情况3:需要添加前缀return $"{assetsPrefix}{relativePath}";}
}

调用测试代码:

[MenuItem("CustomTools/测试获取git修改文件列表", false)]public static void TestGetModifiedFiles(){List<string> files = GitFileStatusCheckerTools.GetGitModifiedFiles();foreach (var path in files){Debug.Log($"有修改 path: {path}");}}

这样就获取到哪些文件新增或者修改 就可以做一些特殊处理 提交或者是修改 不用每次都记录全文件md5了 依赖git的对比差异


http://www.ppmy.cn/server/170973.html

相关文章

常见锁类型介绍

下面结合代码详细介绍 Mutex、RW Lock、Futex、自旋锁、信号量、条件变量 和 synchronized&#xff0c;并分析它们的适用场景、特点以及为什么这些锁适用于特定场景。我们将从锁的实现机制和性能特点出发&#xff0c;解释其适用性。 1. Mutex&#xff08;互斥锁&#xff09; 代…

智能升级、安全加倍,遨游防爆对讲机拉起通信安防线

在充斥着爆炸性气体和易燃物质的危险作业环境中&#xff0c;通信设备的选择关乎生命安全。一旦通信设备引发电火花&#xff0c;其后果将不堪设想。因此&#xff0c;专为防范易燃易爆环境而设计的防爆对讲机&#xff0c;凭借其独特的防爆技术和设计&#xff0c;成为了这些高风险…

Win7库显示*.library-ms,恢复到正常

Win7库里的文件全部变成***.library-ms了&#xff0c;怎么弄都没法恢复 库文件只显示*..library-ms用管理员身份运行CMD输入ASSOC.library-msLibraryFolder 库文件只显示*…library-ms 用管理员身份运行CMD输入ASSOC.library-msLibraryFolder 参考 [1]: https://tieba.baidu.…

JUC并发—14.Future模式和异步编程分析二

大纲 1.FutureTask(Future/Callable)的使用例子 2.FutureTask(Future/Callable)的实现原理 3.FutureTask(Future/Callable)的源码分析 4.CompletableFuture的基本介绍 5.CompletionStage方法及作用说明 6.CompletableFuture的实现原理分析 7.CompletableFuture的核心源码…

Vulhub靶机 Apereo CAS 4.1 反序列化 RCE(渗透测试详解)

一、开启vulhub环境 docker-compose up -d 启动 docker ps 查看开放的端口 漏洞范围 Apereo CAS < 4.1.7 二、访问靶机IP 8080端口 1、账号密码任意输入&#xff0c;利用burpsuite工具抓包 2、下载apereo-cas-attack工具 利用apereo-cas-attack生成生成一个加密的ysose…

基于Python socket库构建的基于 P2P 的文件共享系统示例

基于 P2P 的文件共享系统 实现方式&#xff1a; 使用 Python 的socket库构建 P2P 网络&#xff0c;节点之间通过 TCP 或 UDP 协议进行通信。每个节点维护一个文件列表&#xff0c;并向其他节点广播自己拥有的文件信息。当一个节点需要某个文件时&#xff0c;它会向网络中的其…

DotNet AutoMap映射扩展,免去每次每次定义转换规则

传统的AutoMap需要给每一个转换定义规则&#xff0c;如果有很多实体就会很麻烦&#xff0c;所以做了一个扩展类用来简化步骤 使用 //使用 实体1的结果.MapTo<实体2>();//例如 有两个实体 public class User {public int Id { get; set; } } public class UserDto {publ…

国产超强开源大语言模型 DeepSeek-R1-70B 一键部署教程

DeepSeek-R1-Distill-Llama-70B 是深度求索 (DeepSeek) 公司于 2025 年推出的开源大语言模型&#xff0c;参数规模高达 700 亿。它是基于 Llama3.3-70B-Instruct 进行训练的&#xff0c;采用强化学习和蒸馏技术提升推理表现&#xff0c;不仅继承了 Llama 系列模型的优势&#x…