3

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.

Coyote
  • 2,454
  • 26
  • 47

1 Answers1

2

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

etopylight
  • 1,239
  • 1
  • 10
  • 15
  • Perfect! I was missing the `NF!=1` for awk. Thank you! – Coyote Nov 30 '17 at 07:51
  • @Coyote Glad to help! The option `--dirstat` of `git diff` might also come in handy if you want to view the percentage of changes in each directory, however this will include subdirectories as well – etopylight Nov 30 '17 at 08:24