Get SVN Version from editor script? (Windows)

This question and answer are here for reference, for anyone they might help.

Question: Say I have a build script, and I want to get the current SVN version to include with the build somehow (maybe in an ‘about me’ file). How do I get that information?

This answer presumes that you have svn through cygwin, and that you have ‘svnversion’ installed along with svn (I believe you get both when grabbing svn for cygwin).

From the forum, you can use the following script to execute a command in the windows command shell:

/// <summary>
/// Executes a shell command synchronously.
/// </summary>
/// <param name="command">string command</param>
/// <returns>string, as output of the command.</returns>
private static string ExecuteCommand(string command)
{
    try
    {
        Debug.Log("Running command: " + command);
        // create the ProcessStartInfo using "cmd" as the program to be run,
        // and "/c " as the parameters.
        // Incidentally, /c tells cmd that we want it to execute the command that follows,
        // and then exit.
        System.Diagnostics.ProcessStartInfo procStartInfo =
            new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

        // The following commands are needed to redirect the standard output.
        // This means that it will be redirected to the Process.StandardOutput StreamReader.
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.RedirectStandardError = true;
        procStartInfo.UseShellExecute = false;
        // Do not create the black window.
        procStartInfo.CreateNoWindow = true;
        // Now we create a process, assign its ProcessStartInfo and start it
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo = procStartInfo;
        proc.Start();
        // Get the output into a string
        proc.WaitForExit();
        string result = proc.StandardOutput.ReadToEnd();
        string error = proc.StandardError.ReadToEnd();
        // Display the command output.
        Debug.Log(result + "

" + error);
return result;
}
catch (System.Exception e)
{
// Log the exception
Debug.Log("Got exception: " + e.Message);
}
return “”;
}

Just call that method with the following:

string svnVersion = ExecuteCommand("cd " + Application.dataPath + " && c:/cygwin/bin/svnversion");

Assuming that you have cygwin installed on the c: dir, and have svnversion in its standard install directory (bin), then you should be set. That command will return the svn version of your project.

Presumably this would work similarly on Mac if you have svn and svnversion installed, though you’d have to look for how to run commands from Unity in Unix.