Search Unity

Source control with git and empty directories in Unity

Discussion in 'Editor & General Support' started by FeastSC2, Apr 29, 2018.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Unity creates metafiles for any folder and Git doesn't accept folders with nothing in them. This means when using git with empty folders my teammates won't get any of my empty folders but they will get useless meta files that will be deleted by their own system since they don't have the folder.
    So I must succeed to send my empty folders. The solution for this is to simply create a file in those empty folders so that they're not empty anymore.

    So I created this script to make files in empty directories in order to avoid this problem: It works well except for when I create a gitkeep.gitkeep file WHILE I'm importing assets. Does someone know why that's the case?

    What I really simply want to do is automatically add a file to any folder I create in Unity.

    Here's the script I'm using:


    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.IO;
    5.  
    6.  
    7. class GitKeeper : AssetPostprocessor
    8. {
    9.     private static string AssetsString = "Assets";
    10.  
    11.     static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets,
    12.         string[] movedFromAssetPaths)
    13.     {
    14.         GitkeepCheck(movedFromAssetPaths, true);
    15.         //GitkeepCheck(importedAssets); // <----- TODO: This makes Unity crash.
    16.     }
    17.  
    18.     static void GitkeepCheck(string[] paths , bool _movedFrom = false)
    19.     {
    20.         foreach (string path in paths)
    21.             FindEmptyDirectoriesAndCreateGitkeep(path, _movedFrom);
    22.     }
    23.  
    24.     static void FindEmptyDirectoriesAndCreateGitkeep(string assetPath, bool _movedFrom = false)
    25.     {
    26.         try
    27.         {
    28.             string assetDir = Path.GetDirectoryName(assetPath);
    29.             string absoluteDir = AssetPathToAbsolutePath(assetDir);
    30.  
    31.             //Debug.Log("assetPath: " + assetPath);
    32.             //Debug.Log("assetDir: " + assetDir);
    33.             //Debug.Log("absoluteDir: " + absoluteDir);
    34.  
    35.             var combinedDir =
    36.                 absoluteDir +
    37.                 assetPath.Substring(assetPath.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase));
    38.  
    39.             //Debug.Log("combinedDir: " + combinedDir);
    40.  
    41.             string[] files = Directory.GetFiles(_movedFrom == false ? combinedDir : absoluteDir, "*.*", SearchOption.AllDirectories);
    42.  
    43.             //Debug.Log("files.Length: " + files.Length);
    44.  
    45.             if (files.Length <= 1)
    46.             {
    47.                 var path = assetPath + "/gitkeep.gitkeep";
    48.                 if (_movedFrom) path = assetPath.Substring(0, assetPath.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase))
    49.                                        + "/gitkeep.gitkeep";
    50.                 Debug.Log("file is empty: creating gitkeep @ " + path);
    51.                 AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<GitKeep>(), path);
    52.             }
    53.             else
    54.             {
    55.                 // Check if there's a gitKeep and delete if there is one.
    56.                 var path = assetPath + "/gitkeep.gitkeep";
    57.                 if (_movedFrom) path = assetPath.Substring(0, assetPath.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase))
    58.                                        + "/gitkeep.gitkeep";
    59.                 Debug.Log("removing gitkeep @ " + path);
    60.  
    61.                 AssetDatabase.DeleteAsset(path);
    62.             }
    63.  
    64.             //Debug.Log("-------");
    65.         }
    66.         catch
    67.         {
    68.         }
    69.     }
    70.  
    71.  
    72.     static string AssetPathToAbsolutePath(string assetPath)
    73.     {
    74.         if (assetPath == AssetsString)
    75.             return Application.dataPath;
    76.         else
    77.             return Path.Combine(Application.dataPath, assetPath.Substring(AssetsString.Length + 1));
    78.     }
    79. }
    80.  
    81.  
    82.  
    83.  
    84.  
    85. // Gitkeep class. Just a scriptableObject
    86. using UnityEngine;
    87.  
    88. public class GitKeep : ScriptableObject
    89. {
    90.  
    91. }
    92.  
    93.  
     
    Last edited: Apr 29, 2018
    OnatKorucu likes this.