Skip to content

Instantly share code, notes, and snippets.

@wotakuro
Created March 17, 2020 16:42
Show Gist options
  • Save wotakuro/64ad0a72209b439a91cc97c9bc914664 to your computer and use it in GitHub Desktop.
Save wotakuro/64ad0a72209b439a91cc97c9bc914664 to your computer and use it in GitHub Desktop.
VersionControl tips
using UnityEngine;
using UnityEditor;
public class VersionControlCLITips
{
public class GitInfo
{
public string branch;
public long lastupdate;
public string hash;
}
[MenuItem("Tools/LogGitRevision")]
public static void LogGitRevision()
{
var info = GetProjectGitInfo();
if( info!= null)
{
Debug.Log("branch:" + info.branch + "\nhash:" + info.hash + "\nlastupdate:"+info.lastupdate);
}
}
public static GitInfo GetProjectGitInfo() {
string output = null;
GitInfo gitInfo = new GitInfo();
if (GetGitBranch(out output))
{
gitInfo.branch = output;
}
else
{
Debug.LogError(output);
return null;
}
if (GetGitRevisionInfo(out output))
{
var results = output.Split(',');
gitInfo.hash = results[0];
long.TryParse( results[1] ,out gitInfo.lastupdate);
}
else
{
Debug.LogError(output);
return null;
}
return gitInfo;
}
public static bool GetGitBranch(out string res)
{
System.Diagnostics.Process pro = new System.Diagnostics.Process();
pro.StartInfo.FileName = "git";
pro.StartInfo.Arguments = "rev-parse --abbrev-ref @";
pro.StartInfo.CreateNoWindow = true;
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.WorkingDirectory = System.IO.Directory.GetCurrentDirectory();
pro.Start();
string err = pro.StandardError.ReadToEnd();
if (!string.IsNullOrEmpty(err))
{
res = err;
return false;
}
res = pro.StandardOutput.ReadToEnd().Trim();
return true;
}
public static bool GetGitRevisionInfo(out string res)
{
System.Diagnostics.Process pro = new System.Diagnostics.Process();
pro.StartInfo.FileName = "git";
pro.StartInfo.Arguments = "log -n 1 --format=%H,%cd --date=format:%Y%m%d%H%M%S";
pro.StartInfo.CreateNoWindow = true;
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.WorkingDirectory = System.IO.Directory.GetCurrentDirectory();
pro.Start();
string err = pro.StandardError.ReadToEnd();
if (!string.IsNullOrEmpty(err))
{
res = err;
return false;
}
res = pro.StandardOutput.ReadToEnd().Trim();
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment