A common problem game developers have with the Unity3D engine and Git is that Unity and Git fight for whether an empty directory should exist or not - Git doesn't care, Unity3D will keep making git trackable *.meta files for empty directories. Basically means devs have to manually find and delete directories and meta files if a commit is made removing files and directories.
I'd like to create a Git post-checkout hook to trigger a directory removal when Git deletes files.
I have made a good start on it - I know what I need Git to do. But I'm not great with shell scripting and I'm struggling to make it work efficiently and correctly.
The main issue I'm having is that I just cannot get this line right:
dirs_to_check="($changed_files | xargs dirname | xargs sort -u)"
I was hoping it'd pipe each line into dirname, then take the whole list and remove dupes.
#!/bin/sh # This script will be run by Git after a checkout. # --- Command line oldRev="$1" newRev="$2" isBranchCheckout="$3" # Grab a list of deleted files: changed_files="$(git diff-tree -r --name-only --diff-filter=D --no-commit-id $oldRev $newRev)" # Just testing: ##changed_files="$(git diff-tree -r --name-only --no-commit-id f5865290 eb793b0c)" # Early exit if there are no removed files at all: if [ -z "$changed_files" ]; then echo "No empty dirs" exit 0 fi echo "$changed_files" # Get the list of dir paths and then sort and remove dupes: dirs_to_check="($changed_files | xargs dirname | xargs sort -u)" # For each dir check if its empty and if so, remove it: # TODO: What about the case where the parent dir is also empty of files? for dir in $dirs_to_check; do if [ "$(ls -A $dir)" ]; then echo "$dir Not Empty" else echo "$dir Empty" rm $dir fi done
Here is some sample changed_files text if you want to test more easily:
test/with dir spaces/debrief/css/style.css WFTO/uiresources/wftoUI/debrief/debrief.html WFTO/uiresources/wftoUI/debrief/debrief_specification.js WFTO/uiresources/wftoUI/debrief/js/debrief.js WFTO/uiresources/wftoUI/debrief/js/debrief_specification.js WFTO/uiresources/wftoUI/loading/Loading.css WFTO/uiresources/wftoUI/loading/Loading.html WFTO/uiresources/wftoUI/loading/LoadingDLC1.html WFTO/uiresources/wftoUI/loading/images/HoG-logo.png WFTO/uiresources/wftoUI/loading/images/background_unused.jpg WFTO/uiresources/wftoUI/loading/images/banner-patch-1_4.png WFTO/uiresources/wftoUI/loading/images/bg-back.jpg WFTO/uiresources/wftoUI/loading/images/bg-front_unused.jpg WFTO/uiresources/wftoUI/loading/images/bg-front_unused.png WFTO/uiresources/wftoUI/loading/images/bg-logo.png WFTO/uiresources/wftoUI/loading/images/dlc1/bg-back.jpg WFTO/uiresources/wftoUI/loading/images/dlc1/bg-logo.png WFTO/uiresources/wftoUI/loading/images/dlc1/load-bar-empty.png WFTO/uiresources/wftoUI/loading/images/dlc1/load-bar-full.png WFTO/uiresources/wftoUI/loading/images/load-bar-empty.png WFTO/uiresources/wftoUI/loading/images/load-bar-full.png WFTO/uiresources/wftoUI/loading/images/loading-background.png WFTO/uiresources/wftoUI/loading/images/loading-background_unused.jpg WFTO/uiresources/wftoUI/loading/images/random_loading_pics/Ld0.png WFTO/uiresources/wftoUI/loading/images/random_loading_pics/Ld1.png WFTO/uiresources/wftoUI/loading/images/random_loading_pics/Ld10.png