问题
I am currently playing with monorepos and I am trying to retrieve a list all 1 level subfolders in the repo which are impacted since a given commit.
So far I can retrieve all the files impacted using git diff --name-only $COMMIT_ID..head
Using git diff --name-only $COMMIT_ID..head | xargs -L1 dirname
I manage to get only the folder names.
To remove all the duplicates I added sort | uniq
to the mix: git diff --name-only $COMMIT_ID..head | xargs -L1 dirname | sort | uniq
All I need now is to ensure I only retrieve the first level folders i.e. project1
not project1/src
and project1/lib
I have tried a few options but I have not managed to keep it as a one liner so far.
回答1:
Here is a solution with awk
git diff --name-only $COMMIT_ID | awk -F'/' 'NF!=1{print $1}' | sort -u
-F'/'
sets the delimiter field to slash/
NF!=1{print $1}
prints out the first field which is the first level directory name if the line contain slashes/
, this filters out files that exists in the first levelreadme.md NF==1 project1/file NF==2 project2/src/file NF==3
sort -u
combined sort and unique
来源:https://stackoverflow.com/questions/47564421/get-a-list-of-all-1st-level-directories-impacted-by-recent-commits-for-a-git-mon