Last active
August 29, 2015 14:25
-
-
Save shadowhand/130dbcf6d9b01877797b to your computer and use it in GitHub Desktop.
Git branches affecting a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍