Skip to content

Instantly share code, notes, and snippets.

Created March 23, 2022 20:45
Show Gist options
  • Save deanhouseholder/cb7a1eb90f906b9914317eda50a15ca9 to your computer and use it in GitHub Desktop.
Save deanhouseholder/cb7a1eb90f906b9914317eda50a15ca9 to your computer and use it in GitHub Desktop.
Bash function to check for git updates in the current repo with nice output
# Check to see if there are new commits on the current branch
function check() {
# Check if in a git repo
git status &>/dev/null
test $? -ne 0 && printf "Not a git repo\n" && return 1
# Fetch latest commits from git remote
printf "\n${details}Checking for latest git changes...$n"
git fetch &>/dev/null
test $? -ne 0 && printf "\rFailed to check for updates from the git repo.\n\n" && return 1
# Define variables
if [[ $(tput colors) -eq 256 ]]; then
# 256 color support
local c_header="\e[4m\e[38;5;15m"
local c_message="\e[1;80m"
local c_hash="\e[38;5;178m"
local c_committed="\e[38;5;110m"
local c_author="\e[38;5;85m"
local c_current="\e[38;5;15m"
local c_newer="\e[38;5;185m"
local c_older="\e[38;5;245m"
# 16 colors only
local c_header="\e[1;32m"
local c_message="\e[1;36m"
local c_hash="\e[0;33m"
local c_committed="\e[1;34m"
local c_author="\e[1;36m"
local c_current="\e[1;33m"
local c_newer="\e[1;15m"
local c_older="\e[37m"
local n="\e[m" # Reset to normal
local s=$'\x01' # Obscure ASCII character as a separator
local counter=-1
local show_previous=3
local git_log_format="%h$s%cr$s%cd$s%s$s%an"
local git_branch=$(git branch | grep '*' | cut -d' ' -f2)
local not_yet_pulled="$(git log HEAD..origin/$git_branch --date=default --pretty=format:"$git_log_format" --decorate=full)"
local local_commits="$(git log --date=default --pretty=format:"$git_log_format")"
local count_available=$(echo -n "$not_yet_pulled" | grep -c '^')
local output=$(printf "Timeline \b${s}Hash${s}Committed${s}Author${s}Commit Message$n")
test $count_available -eq 1 && local is_or_are="is" || local is_or_are="are"
# Display status line
printf "\rThere $is_or_are $c_message$count_available$n new updates available on the $c_message$git_branch$n branch which can be pulled.\n\n$c_header"
# Function to display a single line with the proper formatting
print_git_log() {
test -z "$1" && return
if [[ $counter -eq 0 ]]; then
local marker="${c_current}Current \b$n" # \b is a hack to get columns to line up due to multi-byte characters for Newer and Older arrows
elif [[ $counter -lt 0 ]]; then
local marker="${c_newer}Newer ▲$n"
local marker="${c_older}Older ▼$n"
IFS="$s" read -r -a column <<< "$1"
local hash="${column[0]}"
local committed="${column[1]}"
local commit_msg="${column[3]}"
local author="${column[4]}"
output="$( \
printf "$output\n" && \
printf "%s$s$c_hash%s$n$s$c_committed%s$n$s$c_author%s$n$s%s\n\n" "$marker" "$hash" "$committed" "$author" "$commit_msg" \
# Display Newer commits
while read line; do
print_git_log "$line"
done <<< "$not_yet_pulled"
# Display Current and Older commits
while read line; do
test $counter -eq $show_previous && break
print_git_log "$line"
let counter++
done <<< "$local_commits"
# Display the output in columns
printf "$output\n" | column -s "$s" -t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment