2023-04-03 11:04:31 +08:00

324 lines
11 KiB
C#

using UnityEditor;
using UnityEngine;
namespace BFEditor
{
public static class GitUtils
{
/// <summary>
/// 推送(标签也推送)
/// </summary>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <returns></returns>
public static bool GitPushWithTag(string branch = "", string path = "", bool showDialog = false)
{
return GitOperate("push origin" + branch + " --tags", path, showDialog, true);
}
/// <summary>
/// 推送
/// </summary>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <returns></returns>
public static bool GitPush(string branch = "", string path = "", bool showDialog = false)
{
return GitOperate("push origin " + branch, path, showDialog, true);
}
/// <summary>
/// 拉取
/// </summary>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <returns></returns>
public static bool GitPull(string branch = "", string path = "", bool showDialog = false)
{
return GitOperate("pull origin " + branch, path, showDialog, true);
}
/// <summary>
/// 抓取
/// </summary>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <returns></returns>
public static bool GitFetch(string path = "", bool showDialog = false)
{
return GitOperate("fetch", path, showDialog);
}
/// <summary>
/// 无论暂存与否的文件都提交,相当于将git add 和 git commit两步操作合并为一步,注意,只对已跟踪的文件有效
/// </summary>
/// <param name="path"></param>
/// <param name="log"></param>
/// <param name="showDialog"></param>
/// <returns></returns>
public static bool GitCommit(string path = "", string log = "no message", bool showDialog = false)
{
return GitOperate("commit -a -m " + "'" + log + "'", path, showDialog);
}
/// <summary>
/// 合并
/// </summary>
/// <param name="src">被合并的分支</param>
/// <param name="dst">要合并src的分支</param>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 创建分支
/// </summary>
/// <param name="branch"></param>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <returns></returns>
public static bool GitCreateBranch(string branch, string path = "", bool showDialog = false)
{
return GitOperate("branch " + branch, path, showDialog);
}
/// <summary>
/// 切换分支
/// </summary>
/// <param name="branch"></param>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <returns></returns>
public static bool GitSwitchBranch(string branch, string path = "", bool showDialog = false)
{
return GitOperate("checkout " + branch, path, showDialog, true);
}
/// <summary>
/// 创建并切换分支
/// </summary>
/// <param name="branch"></param>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <returns></returns>
public static bool GitCreateAndSwitchBranch(string branch, string path = "", bool showDialog = false)
{
return GitOperate("checkout -b " + branch, path, showDialog, true);
}
/// <summary>
/// 删除分支
/// </summary>
/// <param name="branch"></param>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <returns></returns>
public static bool GitDeleteBranch(string branch, string path = "", bool showDialog = false)
{
return GitOperate("branch -d " + branch, path, showDialog);
}
/// <summary>
/// 还原文件,仅对未暂存的文件有效,对于暂存的文件,应该先GitUnstageFiles,再GitRevertFiles
/// </summary>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <param name="filenames"></param>
/// <returns></returns>
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);
}
/// <summary>
///重置当前分支
/// </summary>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <returns></returns>
public static bool GitResetHard(string path = "", bool showDialog = false)
{
return GitOperate("reset --hard HEAD", path, showDialog, true);
}
/// <summary>
/// 取消暂存,仅仅是取消暂存
/// </summary>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <param name="filenames">文件列表</param>
/// <returns></returns>
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);
}
/// <summary>
/// 将暂存区的文件提交,即已经git add的文件
/// </summary>
/// <param name="path"></param>
/// <param name="log">日志信息</param>
/// <param name="showDialog"></param>
/// <returns></returns>
public static bool GitCommitStaged(string path = "", string log = "no message", bool showDialog = false)
{
return GitOperate("commit -m " + "'" + log + "'", path, showDialog);
}
/// <summary>
/// 查询Git状态
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static bool GitStatus(string path = "")
{
return GitOperate("status", path);
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="filename">删除文件的名称,可以是通配符</param>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <returns></returns>
public static bool GitRemoveFile(string filename, string path = "", bool showDialog = false)
{
return GitOperate("rm " + filename, path, showDialog);
}
/// <summary>
/// 批量添加到暂存区
/// </summary>
/// <param name="path"></param>
/// <param name="options">
/// ".":将修改的文件以及未跟踪文件添加到暂存区,但不包含删除的文件
/// "-u":将被修改的已跟踪文件以及删除的文件添加到暂存区,但不包含未跟踪的文件
/// "-A":"." 和 "-u"操作的集合
/// 还可以是其它通配符
/// </param>
/// <returns></returns>
public static bool GitAdd(string path = "", string options = "-A", bool showDialog = false)
{
var args = "add " + options;
return GitOperate(args, path, showDialog);
}
/// <summary>
/// 添加filenames到暂存区
/// </summary>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <param name="filenames">文件列表</param>
/// <returns></returns>
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);
}
/// <summary>
/// git操作
/// </summary>
/// <param name="args"></param>
/// <param name="path"></param>
/// <param name="showDialog"></param>
/// <param name="ignoreError">是否忽略错误,这个参数主要用于切分支,切换分支无论如何都会有errormsg</param>
/// <returns></returns>
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;
}
}
}