Skip to content

Instantly share code, notes, and snippets.

@cGuille
Created October 6, 2023 16:13
Show Gist options
  • Select an option

  • Save cGuille/74344e2426e46330b25fa0ded536fdc1 to your computer and use it in GitHub Desktop.

Select an option

Save cGuille/74344e2426e46330b25fa0ded536fdc1 to your computer and use it in GitHub Desktop.
Show status of the current branch vs. its remote tracking branch with `git rt`.
#!/usr/bin/env bash
# Make this file executable: `chmod +x git-rt`.
# Then move it to a PATH directory to be able to run it as a Git subcommand (`git rt`).
set -eu
function status_val() {
local property="${@:$#}" # last parameter
local status_opts="${@:1:$#-1}" # all parameters except the last
git status --porcelain=v2 "$status_opts" | grep -F "# ${property} " | sed -e "s/^# ${property} //"
}
# Text styles:
Rst='\e[0m'
Red='\e[0;31m'
Gre='\e[0;32m'
Pur='\e[0;35m'
Cya='\033[0;36m'
function out() {
echo -e "$@"
}
function outn() {
echo -e -n "$@"
}
function err() {
echo -e "${Red}${@}${Rst}" >&2
}
branch="$(status_val --branch branch.head)"
upstream="$(status_val --branch branch.upstream)"
if [[ $branch = '(detached)' ]]
then
err 'You are detached from your branch; aborting.'
exit 1
fi
if [[ $upstream = '' ]]
then
err 'No remote tracking branch; aborting.'
exit 1
fi
outn "On ${Pur}${branch}${Rst}, but not ${Cya}${upstream}${Rst}: "
logs="$(git log --oneline --color=always "${upstream}..${branch}")"
if [[ $logs = '' ]]
then
out "${Gre}clean${Rst}."
else
out
echo "$logs"
out
fi
outn "On ${Cya}${upstream}${Rst}, but not ${Pur}${branch}${Rst}: "
logs="$(git log --oneline --color=always "${branch}..${upstream}")"
if [[ $logs = '' ]]
then
out "${Gre}clean${Rst}."
else
out
echo "$logs"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment