Reputation: 31
I am still looking through other similar questions, but I haven't quite found what I'm looking for and was hoping for some help.
I need to have a GIT repo that includes several directories each with files that have different extensions. So for example:
A\ <- A directory will have *.a files only
B\ <- B directory will have *.b files only
C\ <- C directory will have *.c, *.d, and *.f files only
I know I need to use the pre-commit hook to check for validation, but I'm not sure the best way to do it. The straightforward answer appears to be jumping to each directory and making sure only expected extensions exist, but that could get to be a fairly long/complicated script since some directories have multiple (sometimes 10 or more) file extensions allowed.
If knows of a simpler way I would love to hear any advice.
Upvotes: 0
Views: 273
Reputation: 496962
You probably want to parse the output of a Git command. The most obvious choice is:
git diff --cached --name-only --diff-filter=ACR
That will print a list of all added (A), copied (C), and renamed (R) files. If desired you could also check modified files (M), but as long as you don't let them get in in the first place, that won't be necessary. --name-only
keeps you from getting an actual diff, and --cached
tells it to look at the index (staging area) rather than the work tree, which might contain unstaged changes. You can then do whatever verification you like on each printed filename.
Alternatively, you could do the diff directory-by-directory, to make the verification easier, something like:
git diff --cached --name-only --diff-filter=ACR directory-A | grep -v '\.a$'
which will yield a list of all files whose names don't end in ".a". Replace directory-A
and '\.a$'
with variables, and then you could just iterate over a list of directory/regex pairs.
Upvotes: 1