当前位置: 首页 > news >正文

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

直接上代码

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.lryc.cn/news/542942.html

相关文章:

  • 结构型模式 - 桥接模式 (Bridge)
  • 如何让传统制造企业从0到1实现数字化突破?
  • 【Elasticsearch】script_fields 和 runtime_fields的区别
  • 城电科技|会追日的智能花,光伏太阳花开启绿色能源新篇章
  • 【笔记ing】C语言补充、组成原理数据表示与汇编实战、操作系统文件实战(高级阶段)
  • 快节奏生活
  • 【音视频】音视频录制、播放原理
  • 前端Sass面试题及参考答案
  • Web自动化之Selenium控制已经打开的浏览器(Chrome,Edge)
  • AF3 unify_template_features 函数解读
  • FFmpeg.NET:.NET 平台上的音视频处理利器
  • 解决 Git 合并冲突:当本地修改与远程提交冲突时
  • SOME/IP-SD -- 协议英文原文讲解5
  • spark的一些指令
  • Redis常用数据类型及其应用案例
  • kafka数据拉取和发送
  • LLM全栈框架完整分类清单(预训练+微调+工具链)
  • 蓝桥杯备考:贪心算法之矩阵消除游戏
  • 【Matlab仿真】Matlab Function中如何使用静态变量?
  • DeepSeek 提示词:高效的提示词设计
  • 深入学习Java中的Lambda表达式
  • 1.2 AI 量化炒股的起源与发展
  • 计算机单位之详解——存储单位Byte 网络传输单位bps 视频码率单位bps
  • IDEA关闭SpringBoot程序后仍然占用端口的排查与解决
  • deepseek清华大学第二版 如何获取 DeepSeek如何赋能职场应用 PDF文档 电子档(附下载)
  • 【python随手记】——读取文本文件内容转换为json格式
  • k8s集群3主5从高可用架构(kubeadm方式安装k8s)
  • 基于 sklearn 的均值偏移聚类算法的应用
  • 三、大模型微调的多种方法与应用场景
  • 第2课 树莓派镜像的烧录