Skip to content

Commit

Permalink
feat: Add git wrapper function with root cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
EdenEast committed Dec 10, 2020
1 parent c95c64c commit ef355ed
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
3 changes: 0 additions & 3 deletions home/files/.config/git/cfg/alias/bootstrap.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
# Get the commit count from the current head revision
commit-count = rev-list --count HEAD

# Returns the root directory of the git repo
root = "!git rev-parse --show-toplevel 2> /dev/null"

# Returns the abbreviation of the sha1 hash for the current commit
abbreviation = "!git rev-parse --short ${1-`echo HEAD`}"

Expand Down
52 changes: 52 additions & 0 deletions home/files/.config/shell/sh/011-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,58 @@ function extract()
fi
}

# GIT Wrapper function
#
# Wrapper function around git to add extra functionality and the `root` command.
# The root command requires to be a shell function as you cannot change the shell's
# directory without it.
#
# USAGE:
# - `git` = `git status` ; if help message is required use `git help`
# - `git root` = cd to repository root
# - `git root ARGS...` = eval ARGS from root ; eg: `git root ls`
# - `git ARGS...` = default behaviour of git ; acts like default `git` command
function git()
{
if [ $# -eq 0 ]; then
command git status -s
elif [ "$1" = "root" ]; then
# we are in the git-root command
shift
local root

# check if inside a .git directory or barre repository
if [ "$(command git rev-parse --is-inside-git-dir 2> /dev/null)" = true ]; then
if [ "$(command git rev-parse --is-bare-repository)" = true ]; then
# we are in a bare repository get the abs git-dir
root="$(command git rev-parse --absolute-git-dir)"
else
# this should be good enough. GIT_DIR might be outside of the working tree
# see https://stackoverflow.com/a/38852055/2103996
root="$(command git --rev-parse --git-dir)/.."
fi
else
# git +2.13.0
root="$(command git rev-parse --show-superproject-working-tree 2> /dev/null)"
[ -z "$root" ] && root="$(command git rev-parse --show-toplevel 2> /dev/null)"
fi

# if still not root then set to current dir
[ -z "$root" ] && root=.


# cd and exec arguments if there are any
if [ $# -eq 0 ]; then
cd "$root"
else
(cd "$root" && eval "$@")
fi
else
# reqular git
command git "$@"
fi
}

function history()
{
local DEFAULT=100
Expand Down

0 comments on commit ef355ed

Please sign in to comment.