C#实现通过cmd命令获取git最后一次commit时间

C#通过程序来调用cmd命令的操作可以应用与许多地方,比如在打AB包的时候,通过获取最后一次commit时间来作为版本名称后缀,例如:1.1.1.123456789

使用前需加上:   using System.Diagnostics; 

1、确定必要成分

process.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
process.StartInfo.CreateNoWindow = true;//是否在新窗口启动该进程的值(不显示窗口)
process.StartInfo.RedirectStandardInput = true;//是否接收来自调用程序的输入信息
process.StartInfo.RedirectStandardOutput = true;//是否可以由调用程序获取输出信息
process.StartInfo.RedirectStandardError = true;//重定向标准错误输出

 

2、运行进程

process.StartInfo.FileName = "cmd.exe";

process.Start();//启动

StreamWriter stream = process.StandardInput;
stream.AutoFlush = true;

// streamWriter.WriteLine("rundll32.exe git-cmd.exe");  //启动git
for (int i = 0; i < inPut.Length; i++)
{
    stream.WriteLine(inPut[i]);
}
//不管前一个命令是否执行成功都要执行exit,要不后面执行的ReadEnd()方法会假死
stream.WriteLine("exit");

stream.Close();//关闭

 

3、获取所有的输出流和错误输出

outPut = process.StandardOutput.ReadToEnd();//获取所有的流输出
error = process.StandardOutput.ReadToEnd();//获取所有的错误输出

process.WaitForExit();//等待命令执行完之后退出
process.Close();

完整代码如下

public static void RunCMD(out string outPut, out string error, string[] inPut)
    {
        outPut = "";
        error = "";
        Process process = new Process();
        try
        {
            process.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
            process.StartInfo.CreateNoWindow = true;//是否在新窗口启动该进程的值(不显示窗口)
            process.StartInfo.RedirectStandardInput = true;//是否接收来自调用程序的输入信息
            process.StartInfo.RedirectStandardOutput = true;//是否可以由调用程序获取输出信息
            process.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            process.StartInfo.FileName = "cmd.exe";

            process.Start();//启动

            StreamWriter stream = process.StandardInput;
            stream.AutoFlush = true;

            // streamWriter.WriteLine("rundll32.exe git-cmd.exe");  //启动git
            for (int i = 0; i < inPut.Length; i++)
            {
                stream.WriteLine(inPut[i]);
            }
            //不管前一个命令是否执行成功都要执行exit,要不后面执行的ReadEnd()方法会假死
            stream.WriteLine("exit");

            stream.Close();//关闭

            outPut = process.StandardOutput.ReadToEnd();//获取所有的流输出
            error = process.StandardOutput.ReadToEnd();//获取所有的错误输出

            process.WaitForExit();//等待命令执行完之后退出
            process.Close();
        }
        catch
        {
            
        }
    }

调用方式:

public static long GetLastGitCommitData()
    {
        string outPut = "";
        string error = "";
        string[] inPutStr = new[] { "git config log.date iso-strict-local", "git log -1" };
#if UNITY_EDITOR_OSX
        outPut = ShellHelper.ShellGitLog();
#else
        CMDEditor.RunCMD(out outPut, out error, inPutStr);
#endif
        if (!string.IsNullOrEmpty(outPut) && !string.IsNullOrEmpty(error))
        {
            string[] subStr = outPut.Split('\n');
            for (int i = 0; i < subStr.Length; i++)
            {
                if (subStr[i].StartsWith("Data"))
                {
                    string dataStr = subStr[i].Substring(5).Replace("   ", "");//.Split(':')[1];
                    Debug.Log(dataStr);
                    DateTime commitDate = DateTime.Parse(dataStr);
                    DateTime zeroDate = TimeZoneInfo.ConvertTimeToUtc(new DateTime(2021, 5, 1, 0, 0, 0, 0));
                    long t = (commitDate.Ticks - zeroDate.Ticks) / 10000000;
                    // Debug.Log(t);
                    return t;
                }
            }
        }
        Debug.LogError(outPut);
        Debug.LogError(error);
        return 0;
    }

 

posted @ 2021-11-05 13:29  搬砖独行者  阅读(560)  评论(0)    收藏  举报