2

I use the 'rev-list [remote_branch] --count' value as the internal build version number in mobile submissions, but would like to retrieve the commit hash id from the value for reference at a later date.

Below is the C# code I use to retrieve the rev-list count:

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Debug = UnityEngine.Debug;

public static string GetCommitCountFromCorrespondingRemoteBranch()
{
    string strCommitCount = "";

    Process p = new Process();
    // Set path to git exe.
    p.StartInfo.FileName = GIT_EXEC_PATH;
    // Set git command.
    p.StartInfo.Arguments = "rev-list " + GetRemoteBranchName() + " --count";
    // Set working directory.
    p.StartInfo.WorkingDirectory = Application.dataPath + "/../";
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.UseShellExecute = false;
    p.Start();

    // Pass output to variable.
    strCommitCount = p.StandardOutput.ReadToEnd();

    p.WaitForExit();

    if (string.IsNullOrEmpty(strCommitCount) == true)
    {
        Debug.LogError("UNABLE TO GET BRANCH COMMIT COUNT");
    }

    return strCommitCount;
}

Example Return Value: 4427

Steven
  • 166,672
  • 24
  • 332
  • 435
Vizionz
  • 1,137
  • 1
  • 7
  • 11

1 Answers1

2

I know that this question is a few months old but I thought I'd post what worked for me just so that others have a possible solution.

Get the commit hash as so:

How to retrieve the hash for the current commit in Git?

Later, I plan to inject it into the AssemblyInfo file as described here: http://nowfromhome.com/posts/msbuild-add-git-commit-hash-to-assemblyinfo/

Community
  • 1
  • 1
Dee
  • 103
  • 7