From 013d95a0d7e05529f6c5e6b9144b3c97ad1a1c97 Mon Sep 17 00:00:00 2001 From: Joel Croteau Date: Wed, 17 Nov 2021 10:26:12 -0800 Subject: [PATCH 1/6] Have pre-commit hook ignore comments in .gitignore While trying to add my own customization to the .gitignore, I had an issue where the pre-commit hook was seeing a reference to an issue in another project inside a comment as a directory and producing an error. This fixes my issue by ignore lines that begin with # when parsing the .gitignore. Note that this will still pick up inline comments, so there should probably be a better fix than this. --- pre-commit | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pre-commit b/pre-commit index f452a71..6c41aca 100755 --- a/pre-commit +++ b/pre-commit @@ -116,8 +116,8 @@ if [ ! `git diff --name-only --cached | grep -- "\.gitignore"` ]; then exit 0 fi -# new lines have the format {+xxxxx+} -raw_diff_output=`git diff --cached --word-diff=plain .gitignore | egrep "\{\+.*\+\}"` +# new lines have the format {+xxxxx+}. Ignore comments +raw_diff_output=`git diff --cached --word-diff=plain .gitignore | egrep "\{\+[^#].*\+\}"` # prepare two strings: on for directories and one for meta files for raw_entry in $raw_diff_output; do From 26354eac7e180c0211566c3889c5b7c0a45a376f Mon Sep 17 00:00:00 2001 From: Joel Croteau Date: Thu, 18 Nov 2021 11:15:02 -0800 Subject: [PATCH 2/6] Ignore UserSettings directory --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 46a9763..f2f913d 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ Build/ Builds/ Logs/ Bin/ +UserSettings/ # erroneous meta files library.meta @@ -32,6 +33,7 @@ Build.meta Builds.meta Logs.meta Bin.meta +UserSettings.meta # Never ignore Asset meta data From 5fe0f2b5850310530fdd5b315d883e3ef0864a92 Mon Sep 17 00:00:00 2001 From: Joel Croteau Date: Wed, 23 Mar 2022 18:33:33 -0700 Subject: [PATCH 3/6] Tighten missing meta file searching (and improve some comments) This changes the regex that `pre-commit` uses to validate .gitignore in a couple ways: * Only match whole line instead of single-word changes * This may cause us to miss some changes, but the trimming below expects it, so we kinda have to. * Ignore files with leading .s * Unity will not create meta files for these, so we don't need to check. * Ignore files that end in *s. * This will also include the .meta files, so don't need a separate line for them. Also add a comment saying WTF this code is actually doing and fix a spelling error. --- pre-commit | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pre-commit b/pre-commit index 6c41aca..7e13e4c 100755 --- a/pre-commit +++ b/pre-commit @@ -110,6 +110,7 @@ if [ "$ret" != 0 ]; then exit "$ret" fi +## Check if changes to .gitignore include corresponding meta files. # check if staged files contain .gitignore if [ ! `git diff --name-only --cached | grep -- "\.gitignore"` ]; then # avoid expensive diff actions if there is no change in .gitignore @@ -117,9 +118,9 @@ if [ ! `git diff --name-only --cached | grep -- "\.gitignore"` ]; then fi # new lines have the format {+xxxxx+}. Ignore comments -raw_diff_output=`git diff --cached --word-diff=plain .gitignore | egrep "\{\+[^#].*\+\}"` +raw_diff_output=`git diff --cached --word-diff=plain .gitignore | egrep -x "\{\+[^#.].*[^*]\+\}"` -# prepare two strings: on for directories and one for meta files +# prepare two strings: one for directories and one for meta files for raw_entry in $raw_diff_output; do # strip leading and trailing separator "{+" and "+}" if ((${#raw_entry} <= 4)); then From b09b50712505a67ae2c069512b1f12f1a7b3811f Mon Sep 17 00:00:00 2001 From: Joel Croteau Date: Wed, 23 Mar 2022 18:44:45 -0700 Subject: [PATCH 4/6] Fix bugs in metafile search in .gitignore * Replace hacky string manipulation with `basename` * The existing approavh is very brittle and leads to a lot of false negatives, so just use the shell builtin `basename` instead. * Prevent file glob expansion when echoing dir name * Some shells (including POSIX-standard sh) expand file globs in `echo`. This was causing some very confusing output. Fix that. * Display full file name in listing * A lot of gitignore entries were showing only blank lines. This makes it clearer what is actually being processed. * Disable regex expansion when searching output metas * `grep` by default treats search patterns as regexes, which was causing a lot of false negatives in entries with asterisks. Disable regex expansion to catch more things. * Also search for full path in metas. * Searching only for the base path caused some false positives where the full .meta path was included. This fixes that. * Also adds other possibility to error message. --- pre-commit | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pre-commit b/pre-commit index 7e13e4c..c9107f5 100755 --- a/pre-commit +++ b/pre-commit @@ -139,11 +139,15 @@ done # iterate over directories and check if there is an entry for the appropriate meta file for i in $diff_output_dirs; do # meta file entries are often without directory, so strip the path - dir_name=`echo $i | egrep -o "/[^/]+$" | cut -d / -f 2` - echo $dir_name - has_meta_ignore=$((`echo $diff_output_metas | grep -- "$dir_name.meta" | wc -l`)) + base_name=`basename $i` + echo "$i" + has_meta_ignore=$((`echo $diff_output_metas | grep -F -- "$base_name.meta" | wc -l`)) if [ ${has_meta_ignore} -eq 0 ]; then - echo "$dir_name found in .gitignore but not the corresponding meta file! Please add ${dir_name}.meta to .gitignore" + # Check if the full path name meta is included. + has_meta_ignore=$((`echo $diff_output_metas | grep -F -- "${i%/}.meta" | wc -l`)) + fi + if [ ${has_meta_ignore} -eq 0 ]; then + echo "${i} found in .gitignore but not the corresponding meta file! Please add ${i%/}.meta or ${base_name}.meta to .gitignore" exit 1 fi done From 16b636a953b5b4ff6f1b131bb4fc7b03373cb6ce Mon Sep 17 00:00:00 2001 From: Joel Croteau Date: Wed, 23 Mar 2022 18:58:01 -0700 Subject: [PATCH 5/6] Make provided .gitignore compliant with pre-commit script Includes a lot of extra .meta files that probably should also be excluded if we're excluding their associated assets. --- .gitignore | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index f2f913d..1aba7f4 100644 --- a/.gitignore +++ b/.gitignore @@ -49,7 +49,7 @@ assets/Plugins/Editor/JetBrains/* # comment these out if you wish to commit the asset store tools plugins assets/AssetStoreTools* Assets/AssetStoreTools* -AssetStoreTools.meta +AssetStoreTools*.meta # Visual Studio cache directory .vs/ @@ -60,14 +60,18 @@ AssetStoreTools.meta #maya intermediate files **/incrementalSave/ +incrementalSave.meta # wwise intermediate files -**/.cache/ +.cache/ *.akd +*.akd.meta # fmod autogen files fmod.log +fmod.log.meta fmod_editor.log +fmod_editor.log.meta **/FMODStudioCache.asset **/FMODStudioCache.asset.meta **/FMODStudioSettings.asset @@ -81,24 +85,41 @@ fmod_editor.log # Autogenerated VS MD Consulo solution and project files ExportedObj/ +ExportedObj.meta .consulo/ *.csproj +*.csproj.meta *.unityproj +*.unityproj.meta *.sln +*.sln.meta *.suo +*.suo.meta *.tmp +*.tmp.meta *.user +*.user.meta *.userprefs +*.userprefs.meta *.pidb +*.pidb.meta *.booproj +*.booproj.meta *.svd +*.svd.meta *.pdb +*.pdb.meta *.mdb +*.mdb.meta *.opendb +*.opendb.meta *.VC.db +*.VC.db.meta *Resharper* *ReSharper* *.orig +# Corner case on the .gitignore validation +*.orig.meta *.orig.* # Unity3D generated meta files @@ -135,7 +156,11 @@ crashlytics-build.properties .Spotlight-V100 .Trashes Icon? +Icon?.meta Thumbs.db +Thumbs.db.meta Desktop.ini +Desktop.ini.meta ehthumbs.db +ehthumbs.db.meta Local/* From 72845937db695c7b74070007a804757827c85a62 Mon Sep 17 00:00:00 2001 From: Joel Croteau Date: Wed, 3 Aug 2022 18:42:38 -0700 Subject: [PATCH 6/6] Fix metafile check breaking with spaces in paths --- pre-commit | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pre-commit b/pre-commit index c9107f5..4fae50e 100755 --- a/pre-commit +++ b/pre-commit @@ -121,17 +121,20 @@ fi raw_diff_output=`git diff --cached --word-diff=plain .gitignore | egrep -x "\{\+[^#.].*[^*]\+\}"` # prepare two strings: one for directories and one for meta files +IFS=$'\n' +declare -a diff_output_dirs +declare -a diff_output_metas for raw_entry in $raw_diff_output; do # strip leading and trailing separator "{+" and "+}" if ((${#raw_entry} <= 4)); then continue else - e=${raw_entry:2:$((${#raw_entry}-4))} + e="${raw_entry:2:$((${#raw_entry}-4))}" is_meta=$((`echo $e | egrep ".*meta" | wc -l`)) if [ $is_meta -eq 0 ]; then - diff_output_dirs="${diff_output_dirs} ${e}" + diff_output_dirs+=("${e}") else - diff_output_metas="${diff_output_metas} ${e}" + diff_output_metas+=("${e}") fi fi done