finder – Is there a method to “auto-tag” all folders which have git in them?


For those who’re glad utilizing the Terminal, this may be executed with a shell command.  Here is the one-liner model (which you’ll be able to paste straight into the Terminal window):

discover "$HOME" -name '.git' -type d | whereas IFS= learn FILENAME; do xattr -wrx "com.apple.metadata:_kMDItemUserTags" "62 70 6C 69 73 74 30 30 A1 01 59 5F 47 49 54 48 55 42 0A 36 08 0A 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14" "$(dirname "$FILENAME")"; executed

And here is the identical factor break up into traces, which might be extra appropriate for saving to a script file:

discover "$HOME" -name '.git' -type d 
| whereas IFS= learn FILENAME; do
    xattr -wrx "com.apple.metadata:_kMDItemUserTags" 
        "62 70 6C 69 73 74 30 30 A1 01 59 5F 47 49 54 48 55 42 0A 36 08 0A 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14" 
        "$(dirname "$FILENAME")"
executed

Each variations begin looking from your own home listing; you’ll be able to change the $HOME with any Unix-style filename if you wish to begin elsewhere.

(This could deal with filenames with areas or another characters apart from newlines.  And it ought to work in all shells.)

Clarification follows, for anybody :

The discover command searches below the given listing, in search of a file named ‘.git’ that is additionally a listing (sort ‘d’).  (It searches recursively, so will discover git repositories in any subdirectory, sub-subdirectory, and so on.)

The record of matches, one per line, is piped right into a whereas loop (with the IFS= telling it not to separate on whitespace); for each, it calls dirname to get the title of the containing listing (which is the repository), after which calls xattr on the outcome to set the tag on that listing and the complete listing tree below it.

The xattr command is a bit opaque, I am afraid.  macOS makes use of many alternative attributes; ‘com.apple.metadata:_kMDItemUserTags’ is the one which the Finder makes use of for its tags.  And the lengthy hex string is the illustration of the tag ‘_GITHUB’ in pink — I discovered it by setting such a tag on a file within the regular means, after which calling xattr -px com.apple.metadata:_kMDItemUserTags to record the hex codes.

You may even see errors similar to ‘Permission denied’ from the discover command if there are any directories you do not have permissions for; you’ll be able to in all probability ignore these.  (Or insert 2>/dev/null after the -type d to cover errors.)

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles