Bash – Check if folders exist in Git repository… testing if a sub-string exists in bash with NULL as a separator

bashgitregex

I have a common git "post-receive" script for several projects, and it needs to perform different actions if an /app/ or /public/ folder exists in the root.

Using:

FOLDERS=`git ls-tree -d --name-only -z master`;

I can see the directory listing, and I would like to use the RegExp support in bash to run something like:

if [[ "$FOLDERS" =~ app ]]; then
    ...
fi

But that won't work if there was something like an "app lication" folder… I specified the "-z" option in the git "ls-tree" command so I could use the \0 (null) character as a separator, but not sure how to test for that in the bash RegExp.

Likewise I know there is support for specifying a particular path in the ls-tree command, and could then pipe that to "wc -l", but I'd have thought it was quicker to get a full directory listing of the root (not recursive) then test for the 2 (or more) folders with the returned output.

Possibly related to:
https://stackoverflow.com/questions/7938094/git-how-to-check-which-files-exist-and-their-content-in-a-shared-bare-repos

Best Answer

It looks like the NULL character is lost (or ignored), as if the listing includes "app" and "httpd", it will match the string "apphttpd".

So I've gone with calling "git ls-tree" multiple times for now to see if each individual file exists.

if [ `git ls-tree -d --name-only master "$F" | wc -l` -eq 1 ]; then
fi