Last active
October 3, 2016 09:05
-
-
Save vonKrafft/45d673da94be9620d578fa76d1a22277 to your computer and use it in GitHub Desktop.
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
#!/bin/sh | |
# Title: git-overview.sh | |
# Description: This script will provide a shot overview for a Git repository | |
# Author: Wandrille K. | |
# Date: oct. 2016 | |
# Version:1.0 | |
#============================================================================== | |
DIVIDER="-" | |
WIDTH=$(tput cols) | |
# Exit if there no Git repository | |
if [ -z $(git rev-parse --git-dir) ]; then | |
printf '%s%s\n' "fatal: Not a git repository " \ | |
"(or any of the parent directories): .git" 1>&2 | |
return | |
fi | |
# Get and print the project name | |
repo_name=$(git remote -v|sed -n 1p|awk '{print $2}'|rev|cut -d'/' -f1|rev) | |
printf '%*s\n' $WIDTH | tr ' ' "$DIVIDER" | |
printf '%*s\n' $(((${#repo_name}+$WIDTH)/2)) "$repo_name" | |
printf '%*s\n' $WIDTH | tr ' ' "$DIVIDER" | |
# Get the current branch and some other information | |
cur_branch=$(git branch | grep "^* " | sed 's/^* //') | |
cnt_commit=$(git rev-list --count --all) | |
cnt_branch=$(git branch | wc -l) | |
cnt_tags=$(git tag | wc -l) | |
cnt_author=$(git log --format="%aN <%aE>" | sort | uniq | wc -l) | |
# Build and print the status line | |
status_line="\e[31m$cur_branch\e[0m \ | |
$cnt_commit commits \ | |
$cnt_branch branches \ | |
$cnt_tags releases \ | |
$cnt_author contributors" | |
printf '\n%*b\n\n' $(((${#status_line}+$WIDTH+11)/2)) "$status_line" | |
# Define two column's sizes | |
COL1=$((($WIDTH*60)/100)) | |
COL2=$(($WIDTH-$COL1)) | |
# Get and print information from the last commit | |
commit_left=$(git log -n 1 --format="\e[33m%cn\e[0m %s") | |
commit_right=$(git log -n 1 --format="Latest commit \e[33m%h\e[0m %cr") | |
printf '%-.*b\e[0m%.*b\e[0m\n\n' \ | |
$(($COL1+11)) "$commit_left$(printf '%*s' $(($COL1+11-${#commit_left})))" \ | |
$(($COL2+10)) "$(printf '%*s' $(($COL2+10-${#commit_right})))$commit_right" | |
# Define three column's sizes | |
COL1=$((($WIDTH*20)/100)) | |
COL2=$((($WIDTH*60)/100)) | |
COL3=$(($WIDTH-$COL1-$COL2)) | |
# For each file in the current directory | |
for elt in ./*; do | |
# Choose a color depending of the type | |
if [ -d $elt ]; then fcolor="\e[34m"; else fcolor="\e[32m"; fi | |
# Get filename, last commit's comment and date | |
cname=${elt:2} | |
ccomm=$(git log -n 1 --format="%s" -- $elt) | |
cdate=$(git log -n 1 --format="%cr" -- $elt) | |
# Adjust the length | |
if [ "$COL1" -lt "${#cname}" ]; then cname="${cname:0:$(($COL1-4))}... "; fi | |
if [ "$COL2" -lt "${#ccomm}" ]; then ccomm="${ccomm:0:$(($COL2-4))}... "; fi | |
if [ "$COL3" -lt "${#cdate}" ]; then cdate="${cdate:0:$(($COL3-4))}... "; fi | |
# Print the output | |
printf '%b%.*s\e[0m%.*s%.*s\n' "$fcolor" \ | |
$COL1 "$cname$(printf '%*s' $(($COL1-${#cname})))" \ | |
$COL2 "$ccomm$(printf '%*s' $(($COL2-${#ccomm})))" \ | |
$COL3 "$(printf '%*s' $(($COL3-${#cdate})))$cdate" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment