Skip to content

Instantly share code, notes, and snippets.

@shadowhand
Last active August 29, 2015 14:25
Show Gist options
  • Save shadowhand/130dbcf6d9b01877797b to your computer and use it in GitHub Desktop.
Save shadowhand/130dbcf6d9b01877797b to your computer and use it in GitHub Desktop.
Git branches affecting a file
#!/bin/bash
# git-affecting
# @link https://gist.github.com/shadowhand/130dbcf6d9b01877797b
# @author shadowhand https://github.com/shadowhand
# @license MIT
#
# installation:
# save this file as git-affecting somewhere in your $PATH
# make it executable: chmod +x git-affecting
#
# optional:
# git config --global alias.af '! git affecting'
#
# usage:
# git affecting
# see all the branches that are modifying the file
# ???
# profit
#
# enjoy!
#
usage() {
echo "usage: git affecting <file> [<file> ...]"
}
# changelog
#
# 0.0.1
# - initial release
#
version() {
echo "switchbranch v0.0.1"
}
_affecting() {
# First get all branches that are known to the origin
local branches=$(git branch -a | grep "remotes/origin/" | grep -v "master")
local found=0
local GREEN="\033[0;32m"
local BLANK="\033[0m"
if [ -z "$1" ]; then
usage
exit 1
fi
for file in $@; do
if [ $found -gt 0 ]; then
echo " " # add newline from previous run
fi
found=0
for branch in $branches; do
git diff --name-only $branch $(git merge-base $branch master) | grep "$file" 2>&1 >/dev/null
if [ $? -eq 0 ]; then
if [ $found -eq 0 ]; then
found=1
echo -e "File $GREEN$file$BLANK changed in:"
fi
echo "- $branch" | sed -e 's/remotes\/origin\///'
fi
done
done
exit 0
}
main() {
local command="$1"
case $command in
"version") version;;
*) _affecting "$@";;
esac
}
main "$@"
@dolfelt
Copy link

dolfelt commented Jul 15, 2015

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment