Git Details for Gnome-Terminal
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
# Script to display the git details of a git tracked folder | |
# | |
# MIT License | |
# | |
# Copyright (c) 2021-2022 Matt Carlotta | |
# | |
# Introduction: | |
# - This script aims to display git details within a gnome terminal | |
# by traversing each directory from the current working directory | |
# and checking if a '.git' folder is present | |
# - In addition, it displays the current status of the branch and | |
# what commit is currently at the HEAD | |
# | |
# Installation: | |
# - Add this script to your '~/.bashrc' or '~/bash_profile' and | |
# source the file | |
# | |
# Terminal: | |
# ┌─---------------------------------------------------------------------------------------------------------┐ | |
# | Terminal | | |
# ├----------------------------------------------------------------------------------------------------------┤ | |
# | ┌─ 🌀 [user@user-user] 📂 [~/Documents/myproject] 🌿 [git:main] 🌱 [branch:current(b055d2d)] | | |
# | └➤ | | |
# | | | |
# | | | |
# | | | |
# | | | |
# | | | |
# | | | |
# └---------------------------------------------------------------------------------------------------------─┘ | |
# | |
# Parses the parent directories of the current working directory to determine if any are git tracked | |
function dir_is_tracked() { | |
IFS='\/' | |
read -ra CWD<<< "$PWD" | |
IFS='' | |
local parentdir="" | |
local gittracked=false | |
for dir in "${CWD[@]}"; do | |
parentdir+=$dir/ | |
if [ -d "$parentdir/.git" ]; then | |
gittracked=true | |
break; | |
fi | |
done | |
echo $gittracked | |
} | |
# Displays the file status of a git branch | |
function check_branch_status() { | |
local gitstatus=$(git status) | |
local unstaged=$(echo $gitstatus | grep -e "Changes not staged" -e "Untracked files") | |
local staged=$(echo $gitstatus | grep -e "Changes to be committed") | |
local branch=$(echo $gitstatus | grep -e "On branch" | awk '{ print $3 }') | |
local detached_head=$(echo $gitstatus | grep -e "HEAD detached" | awk '{ print $4 }') | |
local remote_branch=$(git remote -v) | |
local unpushed_commits=$(git log origin/$branch..$branch 2>&1) | |
local commit=$(git rev-parse --short HEAD 2>/dev/null) | |
if [ ! -z "$detached_head" ]; then | |
echo " ✂️ \[\033[96m\][branch:detached]" | |
elif [ ! -z "$unstaged" ]; then | |
echo " 🔴 \[\033[91m\][branch:unstaged]" | |
elif [ ! -z "$staged" ]; then | |
echo " 🟣 \[\033[95m\][branch:staged]" | |
elif [ ! -z "$remote_branch" ] && [ ! -z "$unpushed_commits" ]; then | |
echo " 📤 \[\033[96m\][branch:desynced(${commit:="unknown"})]" | |
else | |
echo " 🌱 \[\033[32m\][branch:current(${commit:="unknown"})]" | |
fi | |
} | |
# Displays the status of a git branch if a parent folder is git tracked | |
function check_git_status() { | |
local gitbranch="" | |
if $(dir_is_tracked); then | |
local gitstatus=$(git status) | |
local gitbranchstatus=$(check_branch_status) | |
local checkedoutbranch=$(echo $gitstatus | grep -e "On branch" | awk '{ print $3 }') | |
local detached_head=$(echo $gitstatus | grep -e "HEAD detached at" | awk '{ print $4 }') | |
gitbranch="🌿 \[\033[32m\][git:${checkedoutbranch:=$detached_head}]$gitbranchstatus" | |
fi | |
PS1="\[\033[34m\]┌─\[\033[m\] 🌀 \[\033[34m\][\u@\h] 📂 \[\033[33;1m\][\w\]]\[\033[m\] $gitbranch\[\033[m\]\n\[\033[34m\]└➤\[\033[m\] " | |
} | |
PROMPT_COMMAND=check_git_status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment