Skip to content

Instantly share code, notes, and snippets.

@evansj
Last active June 13, 2019 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evansj/4077888bb0d0bb16e899d1cb03c56d47 to your computer and use it in GitHub Desktop.
Save evansj/4077888bb0d0bb16e899d1cb03c56d47 to your computer and use it in GitHub Desktop.
Util for adding current git repo name as an iTerm2 user variable so you can use it in a badge
#
# Git repo name badge for zsh and iTerm2
#
## Hook function definitions
function chpwd_git_utils() {
get_repo_basename
}
# Work out the current repo name (the name of the directory that
# contains the git repo). If the repo is a submodule then also get
# the name of the parent repo.
#
# The iterm2 variable `git_repo_name` will be set to either
# GIT_REPO_NAME or GIT_SUPERPROJECT_NAME/GIT_REPO_NAME
#
# Add it to a badge in your iTerm2 window by going to
# iTerm2 Preferences -> Profiles -> [your profile] -> General,
# and adding \(user.git_repo_name) to the string.
#
# e.g. \(session.username)@\(user.hostname)\n\(user.git_repo_name)
#
function get_repo_basename() {
local root
root=$(git rev-parse --show-toplevel 2> /dev/null) || return
superproject=$(git rev-parse --show-superproject-working-tree)
GIT_REPO_NAME=$(basename "$root")
if [[ -n $superproject ]]; then
GIT_SUPERPROJECT_NAME=$(basename "$superproject")
GIT_REPO_NAME="$GIT_SUPERPROJECT_NAME/$GIT_REPO_NAME"
fi
# Set an iterm2 user variable for use in badges
if type iterm2_set_user_var >/dev/null; then
iterm2_set_user_var git_repo_name "$GIT_REPO_NAME"
fi
}
chpwd_functions+=(chpwd_git_utils)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment