2

Our team commits to a big git repository.

Recently we've decided to export one of subdirectories (named framework) to a separate repo and remove all branches that contain commits only to that subdirectory.

How can I list such branches?

I've modified this advice to get:

for branch in `git for-each-ref --format="%(refname)" refs/remotes/origin `; do 
    git ls-tree -r --name-only $branch | grep -q "framework/" && echo $branch 
done

However this command returns branches with commits to other subdirectories too.

I've tried expanding this snippet:

for branch in `git for-each-ref --format="%(refname)" refs/remotes/origin `; do 
    if git ls-tree -r --name-only $branch | grep -q "framework/" ; then
        if ! git ls-tree -r --name-only $branch | grep -vq "framework/" ; then
            echo $branch 
        fi
    fi
done

However, this command prints nothing.

wl2776
  • 4,099
  • 4
  • 35
  • 77
  • 2
    If you just remove the directories entirely with [`git filter-repo`](https://github.com/newren/git-filter-repo/), empty commits and branches should be pruned automatically. Did you actually want the list you asked for, or were you only planning to delete the branches on it? – Useless Nov 27 '20 at 13:54
  • Yes, I actually want the list, with names of committers. I wanted to notify my team mates that I'm going to remove those branches. – wl2776 Nov 27 '20 at 13:56
  • 2
    Why not just filter a clone of the repo and then diff its branch list with the original? You're going to filter the repo eventually anyway, right? – Useless Nov 27 '20 at 14:01
  • Right. Will go and try that tool. – wl2776 Nov 27 '20 at 14:02

1 Answers1

2

Since framework/ is part of your repo, all branches contain a directory named framework/, so your git ls-tree command will always list something.


If you have a reference branch (say master or develop), you can check if a branch has modified this directory since it forked from that branch :

# will give view an understandable view of what commits are part of this branch :
git log --graph --oneline master..$branch -- framework/

# will give you an int as output (easier to use in a script) :
git rev-list --count master..$branch -- framework/

You can use an alternative syntax to a..b if you want to exclude several "reference branches" :

# count commits from $branch that modified 'framework/' since it forked
# either from master or from develop :
git rev-list --count ^master ^develop $branch -- framework/
LeGEC
  • 46,477
  • 5
  • 57
  • 104