LoginSignup
1
1

More than 1 year has passed since last update.

Unityエディタ起動時にgit config --local core.autocrlf xxxを実行

Last updated at Posted at 2022-12-17

2021/12/17 : 初稿
Unity : 2021.3.15f1

ソースファイルインポート時に文字コードをUTF8に書き換える、
なんてエディタ拡張は皆さんよくされていると思うのですが、
改行コードはどうされてますか?

チーム内にWindows派とMac派が混在していると、
「差分のない.csファイルが大量にSourceTreeのステージングリストに出るんだけど!」
みたいな問題を起こすことがあります。

これを解決するための第一歩ということで、
ちゃんとgitの改行コード設定をしておきましょうという話。

やりたいこと

Unityエディタ起動時に自動でシェルスクリプトまたはバッチファイルを呼び出し、
Windowsなら
git config --local core.autocrlf true
Macなら
git config --local core.autocrlf input
を実行する。

いや、SourceTreeならSourceTree上でやればいいんだけど、
プログラマーではない人たちにとってはハードル高かったり、
クローンしなおした時に設定し忘れたりするので。

実現手順

  1. gitコマンドを呼び出す.sh.batファイルを作成
  2. Unityエディタからそれらを実行する処理を記述。
    この時、処理が終わったら自動でコンソールウインドウ閉じるように。
  3. Unityエディタ起動時にそれを呼び出す。

gitコマンドを呼び出す.sh.batファイルを作成

とりあえず下記のような感じで用意して、
[プロジェクトフォルダ]/Utilsというフォルダを掘ってそこに配置。

Utils/setgitautocrlf.sh
#!/bin/bash
cd `dirname $0`
cd ..
git config --local core.autocrlf input
git config --local core.ignorecase false
Utils/setgitautocrlf.bat
cd %~dp0
cd ..
git config --local core.autocrlf true
git config --local core.ignorecase false

Unityエディタ起動時にそれらを呼び出す。

下記のようなエディタ拡張をします。
Assets/Editorとかに配置。

Assets/Editor/TerminalExecuter.cs
///
/// @file   TerminalExecuter.cs
/// @author KYukimoto
/// @date   
///
/// @brief エディタから.shや.batを実行する。
///         ついでにエディタ起動時に
///         git config --local core.autocrlf inputに設定するシェルスクリプトを呼び出す
///
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Diagnostics;

#if UNITY_EDITOR
public class TerminalExecuter
{
    // ターミナル実行
    public static void ExecTerminalApp(string relPath)
    {
        string dir = Path.GetDirectoryName(Application.dataPath);
        string sh = Path.Combine(dir, relPath);
#if UNITY_EDITOR_OSX
        ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash");
        startInfo.WorkingDirectory = dir;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardInput = true;
        startInfo.RedirectStandardOutput = true;

        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();

        process.StandardInput.WriteLine(sh);
        //process.StandardInput.WriteLine("osascript -e 'tell application \"Terminal\" to close first window' & exit");  // if no exit then WaitForExit will lockup your program
        process.StandardInput.WriteLine("ps - ef | grep Terminal | grep - v grep | awk '{print $2}' | xargs kill");
        process.StandardInput.Flush();
#endif
#if UNITY_EDITOR_WIN
        Process.Start(sh);
#endif
    }

    [InitializeOnLoadMethod]
    static void SetGitAutoCRLF()
    {
#if UNITY_EDITOR_OSX
        TerminalExecuter.ExecTerminalApp("Utils/setgitautocrlf.sh");
#endif
#if UNITY_EDITOR_WIN
        TerminalExecuter.ExecTerminalApp("Utils/setgitautocrlf.bat");
#endif
    }
}
#endif

忘れちゃいけない前提

gitコマンド自体がインストールされてないといけないので、
Windowsマシンの場合はgit for Windowsをインストールしておきます。

Macの場合は、最初から入ってることが多い?
実行時に「デベロッパーツールをインストールしますか?」みたいなダイアログが出て
「はい」を選べばその場でインストールしてくれる?よく分からず。

SourceTree内蔵のGitとか呼び出す方法ないのかなあ。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1