커맨드 창을 열어, git 커맨드를 입력해보자.
git이 설치되어 있고, 윈도우 시스템 PATH에 추가되어 있다면 아래와 같이 표시될 것이다.

현재 브랜치를 얻어오는 명령어는 git branch —show-current 이고, 입력이 틀리지 않는다면 현재 브랜치 정보만 심플하게 프린트해준다.

그럼 이제 저 명령어를 유니티 스크립트를 통해 실행하고, 브랜치 이름을 얻어보자.
우선 System.Diagnostics.Process의 instance를 하나 만들고, 그 instance를 실행할 때 필요한 정보를 구성한다. 그 정보는 ProcessStartInfo 클래스에서 정의한다.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo procStartInfo= new System.Diagnostics.ProcessStartInfo();
procStartInfo에는 프로세스를 실행할 때 필요한 요소들을 정의해 주어야 하는데, 대략 아래와 같이 정의를 하면 된다.
procStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //윈도우 감추기
procStartInfo.CreateNoWindow = true; // 새로운 윈도우를 만들지 않는다.
procStartInfo.UseShellExecute = false; // shell로 실행하지 않는다.
procStartInfo.RedirectStandardOutput = true; // 콘솔로 출력하지 않기 때문에, 이 옵션은 true로 설정해야 함.
procStartInfo.WorkingDirectory = Path.GetFullPath(Application.dataPath + "/..");
procStartInfo.FileName = "git.exe";
procStartInfo.Arguments = "branch --show-current";
- WindowStyle/CreateNoWindow/UseShellExecute/RedirectStandardOutput 옵션은 프로세스를 노출 시키지 않고 실행하기 위해서 설정한다.
- WorkingDirectory는 현재 프로젝트 경로를 설정하여, git을 실행하기 위해 세팅한다.
- FileName은 실행할 프로세스 이름을 지정한다.
- Arguments는 실행 프로세스와 함께 사용할 Parameter를 지정한다.
실행할 프로세스에 대한 정보는 모두 세팅되었다. 이제 이 프로세스를 실행하여 원하는 정보를 얻어보자.
Process p = Process.Start(procStartInfo); //git 프로세스 실행
StreamReader retStream = p.StandardOutput; //StandardOuput 스트림으로 부터
string output = retStream.ReadToEnd(); //output 스트링을 가져온다.
p.WaitForExit(); //현재 프로세스가 종료될 때 까지 기다린다.
위에 코드를 통해, git 프로세스를 실행하여, StandardOutput 스트림을 가져오고, 그 스트림을 읽어 프로세스가 내뱉은 정보를 스트링으로 가져온다. 이 스트링에는 ‘master’라는 정보가 포함되어 있다.
이제 이 코드를 종합해 보면, 아래와 같다.
public static string GetCurrentGitBranchName()
{
try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo();
procStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.WorkingDirectory = Path.GetFullPath(Application.dataPath + "/..");
procStartInfo.CreateNoWindow = true;
procStartInfo.FileName = "git.exe";
procStartInfo.Arguments = "branch --show-current";
System.Diagnostics.Process p = Process.Start(procStartInfo);
System.IO.StreamReader sreader = p.StandardOutput;
string output = sreader.ReadToEnd();
p.WaitForExit();
return output;
}
catch (Exception ex)
{
return "";
}
}
만약, git.exe가 로컬 컴퓨터에 설치가 되어 있지 않거나, PATH에 추가되어 있지 않아 실행할 수 없을 때 Exception이 발생하기 때문에 try/catch로 예외 처리를 해주어야 한다.
git의 HEAD 커밋 Revision 정보도 같은 원리로 쉽게 얻어올 수 있다. procStartInfo.Arguments를 아래와 같이 변경하면 된다.
procStartInfo.Arguments = "rev-parse --short HEAD";
이를 응용하면, git 뿐만 아니라, 다른 프로세스도 실행 가능하여 여러모로 활용도가 높다.
(추가)
Mac에서 사용할 경우, git이 어디에 설치되어 있는지 찾아보고 그 경로를 지정해야 한다.
>whereis git
/usr/bin/git
whereis 명령어를 통해 git이 /usr/bin/git 에 있는 것을 알았다. 아래와 같이 수정해보자.
procStartInfo.FileName = "/usr/bin/git";
변수로 지정하여 , 윈도우와 맥 모두 사용 가능 하도록 설정하면 좋을 것이다.