DEV Community

Shoichi Okaniwa
Shoichi Okaniwa

Posted on • Originally published at qiita.com

UnityのC#からGitを叩く

Executing Git Commands from Unity's C

I recently had the opportunity to execute Git commands from a C# editor extension script in Unity. Here, I'll summarize the process so I won't forget.

Sample Command

As an example, let's execute the following command from Unity's C#:

git config core.autocrlf
Enter fullscreen mode Exit fullscreen mode

This command checks if the feature to automatically convert line endings is enabled.

Sample Code

Below is the sample code. By calling GetAutocrlf externally, the command will be executed.

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

public class GitCommandPractice
{
    /// <summary>
    /// Check Git's autocrlf setting.
    /// </summary>
    public void GetAutocrlf()
    {
        // Retrieve the git path.
        string gitPath = GetGitPath();

        // Set the git command.
        string gitCommand = "config core.autocrlf";

        // Execute the command and get standard output.
        string autocrlf = GetStandardOutputFromProcess(gitPath, gitCommand).Trim();

        Debug.Log(autocrlf);
    }

    /// <summary>
    /// Retrieve the git executable path.
    /// </summary>
    /// <returns>Path of Git</returns>
    private string GetGitPath()
    {
        // For Mac
        if (Application.platform == RuntimePlatform.OSXEditor)
        {
            // Possible paths
            string[] exePaths =
            {
                "/usr/local/bin/git",
                "/usr/bin/git"
            };

            // First found existing path
            return exePaths.FirstOrDefault(exePath => File.Exists(exePath));
        }

        // For Windows, this is sufficient
        return "git";
    }

    /// <summary>
    /// Execute a command and get the standard output.
    /// </summary>
    /// <param name="exePath">Path to executable</param>
    /// <param name="arguments">Command line arguments</param>
    /// <returns>Standard output</returns>
    private string GetStandardOutputFromProcess(string exePath, string arguments)
    {
        // Set process start conditions.
        ProcessStartInfo startInfo = new ProcessStartInfo()
        {
            FileName = exePath,
            Arguments = arguments,
            WindowStyle = ProcessWindowStyle.Hidden,
            UseShellExecute = false,
            RedirectStandardOutput = true,
        };

        // Start the process.
        using (Process process = Process.Start(startInfo))
        {
            // Get the standard output.
            string output = process.StandardOutput.ReadToEnd();

            // Wait for the process to exit or time out.
            process.WaitForExit(TimeoutPeriod);

            return output;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

This process uses C#'s Process to execute Git commands and read their standard output.

In GetGitPath(), the path to the Git executable is retrieved. Generally, it should be sufficient to simply specify git since the path to the Git executable is typically registered in the environment variable 'Path'. However, in my Mac environment, this was not enough, so I explicitly hard-coded two options: /usr/local/bin/git and /usr/bin/git, using whichever is found.

Conclusion

In creating this article, I referred to the following resource. Thank you.

Top comments (0)