get a list of all 1st level directories impacted by recent commits for a git monorepo

戏子无情 提交于 2020-01-13 19:04:07

问题


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 level

    readme.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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!