Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Empty directories and GIT

Discussion in 'External Tools' started by Immanuel-Scholz, Nov 27, 2014.

  1. Immanuel-Scholz


    Jun 8, 2013

    to everyone using GIT and Unity: How did you solve the problem that GIT does not remove empty directories, Unity keeps creating empty directories and .meta files for directories when only one of both are missing?

    More specific the following workflow hickup occurs in our team. Assume Alan is an experienced coder and Bob is an intern beginner.

    • User Alan deletes a directory "DIR" in the Editor. Unity will delete the DIR.meta file.
    • Alan commits and pushes his changes.
    • Bob pulls, but GIT will not remove the empty directory. (Of course, it will remove DIR.meta)
    • Bob starts Unity Editor and Unity creates the DIR.meta again.
    • Bob tries to commits his stuff. An "uncommited file change: new DIR.meta" appears.
    Now, Bob has severaly options, most of them are just wrong but routinely taken by Beginners to GIT and Unity:
    • Deleting the file doesn't help as it gets constantly recreated when Unity launches. In my experience, beginner try that a couple of times until they give up.
    • Just not commiting the file doesn't help a lot, as these uncommited meta files get more and more and it become very difficult to find what need to be checked in and what not. What's worse is, that some Beginner will do this option and start to miss essential commits of other meta files, causing merge conflicts and broken scene links. Badbadbad.
    • Just commiting the file as new in is "working" for that developer, but when e.g. Alan pulls the next time, the DIR/ is recreated by unity again.

    For Bob, the only correct approach I can think of would be:
    • Fire up some File Explorer
    • Locate the DIR.meta
    • Recognize that the DIR.meta is actually a meta file corresponding to a directoy. (remember: Bob never had anything to do with "DIR" or "DIR.meta"). If its a file, the guy who checked it in just forgot to commit the meta. Bad guy, but not bad Bob.
    • If its a directory, open the directoy and recognize that its empty. (If its not empty, then someeone just forgot to checkin the DIR.meta when creating DIR).
    • Ok, its empty. Now delete the directory and the DIR.meta.
    Note that every single developer except Alan has to go through this steps. And there is nothing Alan can do to ease this pain.

    So how did you solve this? BTW.. we wrote an Editor - script that runs when Unity starts (actually on every assembly reload) and removes all empty directories and (if present) their corresponding .meta files. Takes some time to scan the about 1500 directories we have in our Assets/ folder. But at least it can be multi-threaded... Isn't there a simpler solution?
    Last edited: Nov 27, 2014
    AndyKorth likes this.
  2. Kirk Clawson

    Kirk Clawson

    Nov 4, 2014
    Have you tried incorporating a git clean step in your workflow? with the -d option, it will remove untracked directories. It's kind of a bull in a china shop solution, though so it may kill too much stuff unless you use the exclude option.
  3. Immanuel-Scholz


    Jun 8, 2013
    That would only remove the directories but not the meta file so when Unity starts, it will immediately re-create the directory.

    Beside, it would remove untracked files too, right? That would be a disaster for some of our co-workers who don't bother using .gitignore and just keep their working files (like preliminate versions) next to versioned files.

    Also, all git operations are extremely slow now and so we try to avoid them as much as possible.

    Is there an option for Unity to not generate meta files for directories? I don't see any need to reference directories anyway..

    Or maybe someone knows an awesome git-hack to have it ignore directory - meta files, but not file - meta files? (Like some script that decides whether a file is ignored or not instead of file pattern/regex based only on the file path)
  4. Kirk Clawson

    Kirk Clawson

    Nov 4, 2014
    If you're tracking meta files in your Git repo, then Bob's pull will have removed the meta file, so the clean only needs to remove the directory.
    You might be able to use the exclude option on clean so that it only removes directories (e.g. excluding "*.*" will work as long as your directory names don't have periods in them). And yes, that means when a directory gets deleted, it the deleter's responsibility to check in the corresponding .meta delete as well (which git will see anyway, so it's not like it's an extra step for them).
    Perhaps something like this in .gitignore?

    would first ignore all meta files, then un-ignore any meta files that have an extra period in them (again, assuming you don't use periods in your directory names). Never tried that though, so it may need some testing.
  5. Immanuel-Scholz


    Jun 8, 2013
    Yes, you are right.. as long as you run the "git clean" always after a pull and not giving Unity editor a chance to recreate anything (i.e. don't keep the editor open and focused while doing a git pull), that could work. Still a bit wiggly.. ;)

    Awesome.. not allowing dots in directory names is probably a feasable restriction we can punch through the team.. :). Thanks Kirk, I'll try that out.
  6. v2k


    Mar 16, 2012
    Or a script that removes empty directories: (OSX version)

    Code (CSharp):
    1. #! /usr/bin/env sh
    3. #rm DS_Store files first
    4. find Assets -type f -name .DS_Store -exec rm {} \;
    6. while [ -n "$(find ./Assets -type d -empty)" ]
    7. do
    8.         echo "Found empty directories... removing...";
    9.         find ./Assets -type d -empty -exec rm -rf {}.meta \;
    10.         find ./Assets -type d -empty -exec rm -rf {} \; &> /dev/null
    11. done
    13. echo "Clean.";
    ledlamp likes this.
  7. Wellmann


    Nov 27, 2015
  8. FeastSC2


    Sep 30, 2016
    jorgegalvaominiclip likes this.
  9. okaybenji


    Mar 10, 2017
    Looks like Unity's Feedback Forum is gone? Is there some other place I can lend weight to the idea of stopping Unity from generating .meta files for empty directories?