Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git Details for Gnome-Terminal
# Script to display the git details of a git tracked folder
#
# MIT License
#
# Copyright (c) 2021 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")
if [ ! -z "$unstaged" ]; then
echo " 🔴 \[\033[91m\][branch:unstaged]"
elif [ ! -z "$staged" ]; then
echo " 🟣 \[\033[95m\][branch:staged]"
else
echo " 🌱 \[\033[32m\][branch:current($(git rev-parse --short HEAD))]"
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 gitbranchstatus=$(check_branch_status)
local checkedoutbranch=$(git branch | grep -e "*" | cut -c3-)
gitbranch="🌿 \[\033[32m\][git:$checkedoutbranch]$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