Created
April 11, 2016 14:12
-
-
Save comm1x/5faa6cb4e827d676430c42f85ab4f98b to your computer and use it in GitHub Desktop.
git-brst - utility for display details about commits for branch
This file contains hidden or 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
#!/usr/bin/env bash | |
# Check is in git repo | |
if ! [ -d .git ] || ! git rev-parse --git-dir > /dev/null 2>&1; then | |
echo "Git repository not found"; | |
exit 1; | |
fi | |
branch=${1}; | |
commits_limit=5 | |
red='\e[1;31m' | |
green='\e[1;32m' | |
white='\e[1;37m' | |
color_off='\e[0m' | |
if [ -z $branch ]; then | |
# If no argument, use current branch | |
branch=`git rev-parse --abbrev-ref HEAD`; | |
fi | |
# Check out branch existing | |
git show-ref refs/heads/ | |
if [[ $(git show-ref refs/heads/${branch}) == "" ]]; then | |
echo " ${branch} -- not exist locally" | |
if [[ $(git show-ref refs/remotes/origin/${branch}) == "" ]]; then | |
echo " origin/${branch} -- not exist" | |
else | |
echo " origin/${branch} -- exist" | |
fi | |
exit | |
fi | |
function get_log { | |
target=${1} | |
dest=${2} | |
git log ${1}..${2} --pretty=format:"%C(auto) %h %ad %s [%an]" --date=short | |
} | |
function lines { | |
if [[ "$1" == "" ]]; then | |
echo 0 | |
else | |
echo "$1" | wc -l | |
fi | |
} | |
function out_log { | |
count=$(lines "${1}") | |
if [[ ${count} -gt ${commits_limit} ]]; then | |
echo "${1}" | head -n ${commits_limit} | |
echo " ..." | |
else | |
echo "${1}" | |
fi | |
echo "" | |
} | |
function warn { | |
echo -e "${red}${1}${color_off}" | |
} | |
function info { | |
echo -e "${green}${1}${color_off}" | |
} | |
function header { | |
echo -e "${white}${1}${color_off}" | |
} | |
unique=$(get_log master ${branch}) | |
falls=$(get_log ${branch} master) | |
unpushed=$(get_log ${branch} origin/${branch}) | |
unpulled=$(get_log origin/${branch} ${branch}) | |
unique_count=$(lines "${unique}") | |
falls_count=$(lines "${falls}") | |
unpushed_count=$(lines "${unpushed}") | |
unpulled_count=$(lines "${unpulled}") | |
if [[ ${unique_count} > 0 ]]; then | |
header "Has ${unique_count} unique commits (master..${branch})" | |
out_log "${unique}" | |
else | |
header "No unique (master..${branch})"; | |
fi | |
if [[ ${falls_count} > 0 ]]; then | |
header "Fall on ${falls_count} commits (${branch}..master)" | |
out_log "${falls}" | |
else | |
header "No falls (${branch}..master)" | |
fi | |
if [[ ${unpushed_count} > 0 ]]; then | |
header "Can push ${unpushed_count} commits (${branch}..origin/${branch})" | |
out_log "${unpushed}" | |
else | |
header "No unpushed commits (${branch}..origin/${branch})" | |
fi | |
if [[ ${unpulled_count} > 0 ]]; then | |
header "Can pull ${unpulled_count} commits (origin/${branch}..${branch})" | |
out_log "${unpulled}" | |
else | |
header "No unpulled commits (origin/${branch}..${branch})" | |
fi | |
echo "=====================" | |
if [[ ${unique_count} -gt 0 ]]; then | |
info "Contain ${unique_count} commits" | |
else | |
warn "Contain 0 commits (can be deleted)" | |
fi | |
if [[ ${falls_count} -gt 0 ]]; then | |
warn "Unmerge commits: ${falls_count}" | |
else | |
info "Unmerge commits: ${falls_count} (with 'master')" | |
fi | |
if [[ ${unpushed_count} -gt 0 ]]; then | |
warn "Unpushed commits: ${unpushed_count}" | |
else | |
info "Unpushed commits: ${unpushed_count}" | |
fi | |
if [[ ${unpulled_count} -gt 0 ]]; then | |
warn "Unpulled commits: ${unpulled_count}" | |
else | |
info "Unpulled commits: ${unpulled_count}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment