Skip to content

Instantly share code, notes, and snippets.

@designfrontier
Last active January 31, 2017 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save designfrontier/9824d98c9f0940c00f20b11a9b8cd944 to your computer and use it in GitHub Desktop.
Save designfrontier/9824d98c9f0940c00f20b11a9b8cd944 to your computer and use it in GitHub Desktop.
foresight: A way to find major owners of files you have changed in your commit
#!/usr/bin/env bash
set -e
function ADD_AUTHOR {
local author_to_add=$1
if ! [ -z "${author_to_add}" ]; then
#add something to add people to review in your source repo
fi
}
function CHECK_OWNERSHIP {
local line=$1
read -a input <<< "${line}"
local size=${input[0]}
local owned_lines=${input[1]}
local author=${input[2]}
local file_path=${input[3]}
local ownership="$(bc -l <<< "scale=1; ($owned_lines/$size)*100")"
local is_owner="$(bc -l <<< "$ownership>=20")"
if [ $is_owner -eq 1 ]; then
echo $author " owns: " $ownership " percent of " $file_path
ADD_AUTHOR $author
fi
}
function GET_EMAIL {
while read data; do
echo "$data" | grep -E -o "<\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b>"\
| grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b";
done;
}
function PROCESS_FILE {
local FILE_PATH=$1
local FILE_SIZE="$(cat $(echo $FILE_PATH) | wc -l)"
# Find the people with current ownership
git blame --show-email HEAD~1 -- $1 2> /dev/null | GET_EMAIL | sort | uniq -c\
| while read line; do CHECK_OWNERSHIP "$FILE_SIZE $line $FILE_PATH"; done
}
function FORESIGHT {
echo "FORESIGHT!"
echo "Looking into the past to save the future..."
echo "-------------------------------------------"
git diff-tree --no-commit-id --name-only -r HEAD \
| while read line; do PROCESS_FILE "$line"; done | sort | uniq
echo "-------------------------------------------"
echo ""
}
@designfrontier
Copy link
Author

just added a bit to check for who created the file and add pull them as well

@designfrontier
Copy link
Author

To use Foresight you will need to source it in your env file

So if you are using zsh you would add . ~/path/to/foresight.sh to your .zshrc file.
Basically the same instructions for bash just in your .bashrc file.

Then you will have FORESIGHT as a command that you can run from your console!

@designfrontier
Copy link
Author

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