1#!/bin/bash
2set -e
3
4if [ ! -e .git ]; then
5  echo "This script must be run from the root of the git repository"
6  exit 1
7fi
8
9function usage() {
10  echo "Usage: split_change_into_owners.sh <commit message>"
11  echo
12  echo "Splits changes in the current repository based on OWNERS files"
13  exit 1
14}
15
16commitMessage="$1"
17if [ "$commitMessage" == "" ]; then
18  usage
19fi
20
21ownersFiles="$(find -name OWNERS)"
22ownedDirs="$(echo "$ownersFiles" | sed 's|/OWNERS||' | sort -r)"
23
24for d in $ownedDirs; do
25  echo "Checking $d"
26  git add "$d"
27  if git status | grep -i "changes to be committed" >/dev/null; then
28    echo making commit for "$d"
29    git commit -m "$commitMessage
30
31This change includes files under $d"
32  fi
33done
34