get a list of all 1st level directories impacted b

2019-07-24 19:29发布

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条回答
做个烂人
2楼-- · 2019-07-24 20:09

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 level

    readme.md          NF==1
    project1/file      NF==2
    project2/src/file  NF==3
    
  • sort -u combined sort and unique

查看更多
登录 后发表回答