using UnityEditor; using UnityEngine; namespace BFEditor { public static class GitUtils { /// /// 推送(标签也推送) /// /// /// /// public static bool GitPushWithTag(string branch = "", string path = "", bool showDialog = false) { return GitOperate("push origin" + branch + " --tags", path, showDialog, true); } /// /// 推送 /// /// /// /// public static bool GitPush(string branch = "", string path = "", bool showDialog = false) { return GitOperate("push origin " + branch, path, showDialog, true); } /// /// 拉取 /// /// /// /// public static bool GitPull(string branch = "", string path = "", bool showDialog = false) { return GitOperate("pull origin " + branch, path, showDialog, true); } /// /// 抓取 /// /// /// /// public static bool GitFetch(string path = "", bool showDialog = false) { return GitOperate("fetch", path, showDialog); } /// /// 无论暂存与否的文件都提交,相当于将git add 和 git commit两步操作合并为一步,注意,只对已跟踪的文件有效 /// /// /// /// /// public static bool GitCommit(string path = "", string log = "no message", bool showDialog = false) { return GitOperate("commit -a -m " + "'" + log + "'", path, showDialog); } /// /// 合并 /// /// 被合并的分支 /// 要合并src的分支 /// /// /// public static bool GitMerge(string src, string dst, string path = "", bool showDialog = false) { bool res = GitSwitchBranch(dst, path); if (res) { return GitOperate("merge " + src, path, showDialog); } return false; } /// /// 创建分支 /// /// /// /// /// public static bool GitCreateBranch(string branch, string path = "", bool showDialog = false) { return GitOperate("branch " + branch, path, showDialog); } /// /// 切换分支 /// /// /// /// /// public static bool GitSwitchBranch(string branch, string path = "", bool showDialog = false) { return GitOperate("checkout " + branch, path, showDialog, true); } /// /// 创建并切换分支 /// /// /// /// /// public static bool GitCreateAndSwitchBranch(string branch, string path = "", bool showDialog = false) { return GitOperate("checkout -b " + branch, path, showDialog, true); } /// /// 删除分支 /// /// /// /// /// public static bool GitDeleteBranch(string branch, string path = "", bool showDialog = false) { return GitOperate("branch -d " + branch, path, showDialog); } /// /// 还原文件,仅对未暂存的文件有效,对于暂存的文件,应该先GitUnstageFiles,再GitRevertFiles /// /// /// /// /// public static bool GitRevertFiles(string path = "", bool showDialog = false, params string[] filenames) { string args = "checkout --"; for (int i = 0; i < filenames.Length; i++) { args += " " + filenames[i]; } return GitOperate(args, path, showDialog); } public static bool GitTag(string tagName, string path = "", bool showDialog = false, string log = " ") { return GitOperate("tag -a " + tagName + " -m " + "'" + log + "'", path, showDialog); } /// ///重置当前分支 /// /// /// /// public static bool GitResetHard(string path = "", bool showDialog = false) { return GitOperate("reset --hard HEAD", path, showDialog, true); } /// /// 取消暂存,仅仅是取消暂存 /// /// /// /// 文件列表 /// public static bool GitUnstageFiles(string path = "", bool showDialog = false, params string[] filenames) { string args = "reset HEAD"; for (int i = 0; i < filenames.Length; i++) { args += " " + filenames[i]; } return GitOperate(args, path, showDialog); } /// /// 将暂存区的文件提交,即已经git add的文件 /// /// /// 日志信息 /// /// public static bool GitCommitStaged(string path = "", string log = "no message", bool showDialog = false) { return GitOperate("commit -m " + "'" + log + "'", path, showDialog); } /// /// 查询Git状态 /// /// /// public static bool GitStatus(string path = "") { return GitOperate("status", path); } /// /// 删除文件 /// /// 删除文件的名称,可以是通配符 /// /// /// public static bool GitRemoveFile(string filename, string path = "", bool showDialog = false) { return GitOperate("rm " + filename, path, showDialog); } /// /// 批量添加到暂存区 /// /// /// /// ".":将修改的文件以及未跟踪文件添加到暂存区,但不包含删除的文件 /// "-u":将被修改的已跟踪文件以及删除的文件添加到暂存区,但不包含未跟踪的文件 /// "-A":"." 和 "-u"操作的集合 /// 还可以是其它通配符 /// /// public static bool GitAdd(string path = "", string options = "-A", bool showDialog = false) { var args = "add " + options; return GitOperate(args, path, showDialog); } /// /// 添加filenames到暂存区 /// /// /// /// 文件列表 /// public static bool GitAddFiles(string path, bool showDialog = false, params string[] filenames) { string args = "add"; for (int i = 0; i < filenames.Length; i++) { args += " " + filenames[i]; } return GitOperate(args, path, showDialog); } /// /// git操作 /// /// /// /// /// 是否忽略错误,这个参数主要用于切分支,切换分支无论如何都会有errormsg /// private static bool GitOperate(string args, string path = "", bool showDialog = false, bool ignoreError = false) { bool result = true; bool error = ignoreError; string errorMsg = ""; if (!string.IsNullOrEmpty(path)) { args = "-C " + path + " " + args; } BFEditorUtils.RunCommond("git", args, null, (outputMsg) => { Debug.Log(outputMsg); }, (errormsg) => { if (!ignoreError) { error = true; result = false; } Debug.LogError(errormsg); errorMsg = errormsg; }); if (result) { string msg = string.Format("git {0} 执行成功", args); if (showDialog) { EditorUtility.DisplayDialog("提示", msg, "确定"); } else { Debug.Log(msg); } } else { if (showDialog) { if (error) { EditorUtility.DisplayDialog("Error", errorMsg, "确定"); } if (!result) { EditorUtility.DisplayDialog("Error", string.Format("git {0} 执行失败", args), "确定"); } } else { Debug.LogError(string.Format("git {0} 执行失败", args)); } } return result; } } }