본문 바로가기
카테고리 없음

Unity)현재 사용 중인 Git 정보 알아보기 (Git 현재 저장소 확인)

by 테샤르 2023. 4. 18.

Git Branch (Git 현재 저장소 확인)

 

Unity 에서 현재 사용하는 정보를 알고 싶어서 테스트 코드를 만들어봤다.

원리는 간단하게 Unity 에서 Git Process를 통해서 Git 명령어를 전송 이후에 해당 결과를 노출하는 형태이다.

 

반응형

 

<Git 명령어>

명령어 설명
rev-parse --short HEAD 현재 커밋에서 짧은 SHA-1 해시 키를 표시
log -n 1 현재 저장소의 가장 최근 커밋의 세부 정보 1개만 표시
branch --show-current 현재 branch 이름 표시 (Git 2.22이상)

 

 

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Diagnostics;
using System;

public class GitInformation : MonoBehaviour 
{
	[MenuItem("Infomation/Show - Git Infomation")]
	public static void ShowGitBracnName()
    {
		string header = GetGitProcess("rev-parse --short HEAD");
		string log = GetGitProcess("log -n 1");
		string branchName = GetGitProcess("branch --show-current");

		if (EditorUtility.DisplayDialog($"Git - Short Head : [{header}]", $" Git Current Revision : {log}", $" Branch :{branchName}  "))
		{
			Debug.Log($"[Git] Git Information head: {header} / branch : {branchName} / log : {log}");
        }
	}
	

	/// <summary>
	/// Git Process
	/// </summary>
	/// <param name="_gitCommand"></param>
	/// <returns></returns>
	public static string GetGitProcess(string _gitCommand)
    {
		try
		{
			Process process = new Process();
			ProcessStartInfo procStartInfo = new ProcessStartInfo();
			procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
			procStartInfo.UseShellExecute = false;
			procStartInfo.RedirectStandardOutput = true;
			procStartInfo.WorkingDirectory = Path.GetFullPath(Application.dataPath + "/..");
			procStartInfo.CreateNoWindow = true;
			procStartInfo.FileName = "git.exe";
			procStartInfo.Arguments = _gitCommand;

			Process p = Process.Start(procStartInfo);
			StreamReader sreader = p.StandardOutput;
			string output = sreader.ReadToEnd();
			p.WaitForExit();

			return output;
		}
		catch (Exception ex)
		{
			return string.Empty;
		}
	}
}

 

 

 

.Net Prcosse 클래스 : [링크]

 

Process 클래스 (System.Diagnostics)

로컬 및 원격 프로세스에 대한 액세스를 제공하고 로컬 시스템 프로세스를 시작하고 중지할 수 있습니다.

learn.microsoft.com

 

★☆☆☆☆

 

반응형

댓글