1

Given a base branch master and subdirectory A in that branch, how can I list out all branches whose differences from master contain changes in the contents of A?

My use case: I'm doing a subtree split on an active repository with lots of branches and I need to migrate all the affected branches as well. I'd like to avoid having to manually inspect every branch's changes or contacting all the branch owners.

Community
  • 1
  • 1
Edward Samson
  • 2,395
  • 2
  • 26
  • 39

1 Answers1

1

I ended up scripting this out as:

#!/bin/bash

BASE_BRANCH=$1
DIR=$2

while read -r branch; do
    while read -r file; do
        if [ ${file:0:${#DIR}} == "$DIR" ] ; then
            echo "$branch"
            break
        fi
    done < <(git --no-pager diff --name-only $BASE_BRANCH...$branch)
done < <(git branch -a --no-merged $BASE_BRANCH | cut -c 3-)

This takes two arguments, the base branch (master in my example) and the subdirectory to check (in my example, I'd provide A/ with a slash to avoid prefix matches that aren't actually a directory).

The outer loop iterates through all the branches that have not been merged to the base branch. Then inner loop iterates through all the files in each that have been modified from the base branch. When a file's prefix matches, the branch name is sent to output.

Edward Samson
  • 2,395
  • 2
  • 26
  • 39