Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env bash
AWK="`which awk`"
BASENAME="`which basename`"
DATE="`which date`"
FIND="`which find`"
GIT="`which git`"
GREP="`which grep`"
LESS="`which less`"
PRINTF="`which printf`"
RM="`which rm`"
SORT="`which sort`"
SLEEP="`which sleep`"
TAR="`which tar`"
XARGS="`which xargs`"
${PRINTF} "[bashrc] [ext-git]\n"
function git_invoke() {
local command="${GIT}"
if [ -n "${GIT_DEFAULTOPTS}" ]; then
command="${command} ${GIT_DEFAULTOPTS}"
fi
sleep=false
if [ "--sleep" == "${1}" ]; then
sleep=true
shift
fi
command="${command} ${@}"
${PRINTF} "[git] [%s]\n" "${command}"
if [ true == ${sleep} ]; then
${SLEEP} 3s
fi
eval "${command}"
}
function ga() {
unset GIT_FILESET
GIT_FILESET="`"${GIT}" status --short|${GREP} -E '^ M |^MM |^AM |^\?\? ' |${AWK} '{print $2}' |${XARGS}`"
# modified ---------------------------------------^
# modified/modified -----------------------------------^
# added/modified -------------------------------------------^
# untracked -----------------------------------------------------^
${PRINTF} "[git] [%s]\n" "${GIT_FILESET}"
git_invoke 'add' "${GIT_FILESET}"
}
function gad() {
unset GIT_FILESET
GIT_FILESET="`"${GIT}" status --short|${GREP} -E '^M |^A |^\?\? ' |${AWK} '{print $2}' |${XARGS}`"
# modified ---------------------------------------^
# new file --------------------------------------------^
# untracked ----------------------------------------------^
${PRINTF} "[git] [%s]\n" "${GIT_FILESET}"
project="`${BASENAME} ${PWD}`"
file="/tmp/${USER}-${project}-`${DATE} +%Y%m%d_%H%M%S`.tar"
command="${TAR} -cvf ${file} ${GIT_FILESET}"
$PRINTF "[git] [%s]\n" "${command}"
eval "${command}"
$PRINTF "[git] [%s]\n" "${file}"
}
function gb() {
local b="${1}"
if [ -n "${b}" ]; then
git_invoke 'checkout' '-b' "${b}"
fi
unset GIT_CURRENTBRANCH
if [ "1.7.1" == "${GIT_VERSION}" ]; then
# no quiet flag
GIT_CURRENTBRANCH="`"${GIT}" branch |${GREP} '* '|${AWK} '{print $2}'`"
else
GIT_CURRENTBRANCH="`"${GIT}" branch --quiet |${GREP} '* '|${AWK} '{print $2}'`"
fi
${PRINTF} "[git] [branch=%s]\n" "${GIT_CURRENTBRANCH}"
}
function gbb() {
gb
OUT="`"${GIT}" branch |${GREP} -v '* ' |${SORT}`"
unset GIT_ALLBRANCHES
for b in ${OUT}
do
if [ -z "${GIT_ALLBRANCHES}" ]; then
GIT_ALLBRANCHES="${b}"
else
GIT_ALLBRANCHES="${GIT_ALLBRANCHES} ${b}"
fi
done
${PRINTF} "[git] [branches=%s]\n" "${GIT_ALLBRANCHES}"
}
function gbc() {
local branch="${1}"
if [ -z "${branch}" ]; then
gh
return 1
fi
git_invoke 'checkout' "${branch}"
}
function gch() {
git_invoke 'checkout'
}
function gcl() {
local confirm=false
for a in ${@}
do
if [[ "${a}" == "--yes" || "${a}" == "-y" ]]; then
confirm=true
break
fi
done
if [ true != ${confirm} ]; then
read -p "[git] [confirm:y[es],n[o]]:" in
if [[ "${in}" == "y" || "${in}" == "yes" ]]; then
confirm=true
fi
fi
if [ true == ${confirm} ]; then
git_invoke 'clean' '-f' '-d'
fi
}
function gco() {
git_invoke 'commit'
}
function gd() {
local gd_file='/tmp/rkroeker-bashgit-diff'
git_invoke 'diff' '2>&1' ">${gd_file}"
if [ ! -s "${gd_file}" ]; then
git_invoke 'diff' '--cached' '2>&1' ">${gd_file}"
fi
if [ -s "${gd_file}" ]; then
${LESS} "${gd_file}"
else
${PRINTF} "[git] [no diff]\n"
fi
}
function gdf() {
local gdf_out='/tmp/rkroeker-bashgit-diff'
local gdf_pattern="${1}"
if [ -z "${gdf_pattern}" ]; then
gd
return
fi
if [ -f "${gdf_out}" ]; then
${RM} -f "${gdf_out}"
fi
local gdf_files="`${FIND} . -type f -path *${gdf_pattern}* |${XARGS}`"
for f in ${gdf_files}
do
git_invoke 'diff' "${f}" '2>&1' ">>${gdf_out}"
git_invoke 'diff' '--cached' "${f}" '2>&1' ">>${gdf_out}"
done
if [ -s "${gdf_out}" ]; then
${LESS} "${gdf_out}"
else
${PRINTF} "[git] [no diff]\n"
fi
}
function gdos() {
gf
gb
git_invoke 'diff' --name-status origin/${GIT_CURRENTBRANCH}
}
function gdo() {
gf
gb
git_invoke 'diff' origin/${GIT_CURRENTBRANCH}
}
function gmds() {
gb
git_invoke 'diff' --name-status origin/master..${GIT_CURRENTBRANCH}
}
function gmd() {
gb
git_invoke 'diff' origin/master..${GIT_CURRENTBRANCH}
}
function gf() {
git_invoke 'fetch'
}
function gh() {
${PRINTF} "[git] [%s]\t- %s\n" "ga" "add <M>"
${PRINTF} "[git] [%s]\t- %s\n" "gad" "archive diff"
${PRINTF} "[git] [%s]\t- %s\n" "gb" "print the current git branch"
${PRINTF} "[git] [%s]\t- %s\n" "gb" "[branch] create a branch"
${PRINTF} "[git] [%s]\t- %s\n" "gbb" "print all branches"
${PRINTF} "[git] [%s]\t- %s\n" "gbc" "[branch] checkout a branch"
${PRINTF} "[git] [%s]\t- %s\n" "gcl" "clean"
${PRINTF} "[git] [%s]\t- %s\n" "gco" "commit"
${PRINTF} "[git] [%s]\t- %s\n" "gd" "diff"
${PRINTF} "[git] [%s]\t- %s\n" "gdf" "[file] diff"
${PRINTF} "[git] [%s]\t- %s\n" "gdo" "diff origin"
${PRINTF} "[git] [%s]\t- %s\n" "gdos" "diff origin (stat)"
${PRINTF} "[git] [%s]\t- %s\n" "gf" "fetch"
${PRINTF} "[git] [%s]\t- %s\n" "gh" "print this help"
${PRINTF} "[git] [%s]\t- %s\n" "gl" "log [n]"
${PRINTF} "[git] [%s]\t- %s\n" "gm" "checkout master"
${PRINTF} "[git] [%s]\t- %s\n" "gmd" "diff master branch"
${PRINTF} "[git] [%s]\t- %s\n" "gmds" "diff (stat) master branch"
${PRINTF} "[git] [%s]\t- %s\n" "gmm" "merge master"
${PRINTF} "[git] [%s]\t- %s\n" "gp" "push"
${PRINTF} "[git] [%s]\t- %s\n" "gpr" "checkout pull request <n>"
${PRINTF} "[git] [%s]\t- %s\n" "gr" "hard reset"
${PRINTF} "[git] [%s]\t- %s\n" "gre" "print the current remotes"
${PRINTF} "[git] [%s]\t- %s\n" "gs" "status"
${PRINTF} "[git] [%s]\t- %s\n" "gss" "more status"
${PRINTF} "[git] [%s]\t- %s\n" "gfm" "fetch and merge"
}
function gl() {
local n="${1}"
if [ -z "${n}" ]; then
n=1
fi
git_invoke 'log' '-n' "${n}"
}
function gm() {
git_invoke 'checkout' 'master'
}
function gmm() {
git_invoke 'merge' 'master'
}
function gr() {
local confirm=false
for a in ${@}
do
if [[ "${a}" == "--yes" || "${a}" == "-y" ]]; then
confirm=true
break
fi
done
if [ true != ${confirm} ]; then
read -p "[git] [confirm:y[es],n[o]]:" in
if [[ "${in}" == "y" || "${in}" == "yes" ]]; then
confirm=true
fi
fi
if [ true == ${confirm} ]; then
git_invoke 'reset' '--hard' 'HEAD'
fi
}
function gre() {
unset GIT_REMOTES
GIT_REMOTES="`"${GIT}" remote`"
${PRINTF} "[git] [remotes=%s]\n" "${GIT_REMOTES}"
}
function gp() {
gb
local branch="${GIT_CURRENTBRANCH}"
if [ -z "${branch}" ]; then
${PRINTF} "[git] [cannot determine branch]\n"
return 1
fi
git_invoke 'push' 'origin' "${branch}"
}
function gpr() {
local n="${1}"
if [ -z "${n}" ]; then
GIT_PULLREQUESTS="`"${GIT}" branch --verbose|${GREP} -E '^ pr/'`"
"${PRINTF}" "[git] [need number]\n"
"${PRINTF}" "%s\n" "${GIT_PULLREQUESTS}"
return 1
fi
git_invoke 'checkout' "pr/${n}"
}
function gs() {
gb
git_invoke 'status' '--short'
}
function gss() {
gb # what branch are we on
local branch="${GIT_CURRENTBRANCH}"
if [ -z "${branch}" ]; then
${PRINTF} "[git] cannot determine branch]"
return 1
fi
git_invoke 'log' "origin/${branch}..${branch}"
}
function gmb() {
gb
local branch="${GIT_CURRENTBRANCH}"
if [ -z "${branch}" ]; then
${PRINTF} "[git] [cannot determine branch]\n"
return 1
fi
git_invoke 'merge' "origin/${branch}"
}
function gfm() {
gb
local branch="${GIT_CURRENTBRANCH}"
if [ -z "${branch}" ]; then
${PRINTF} "[git] [cannot determine branch]\n"
return 1
fi
git_invoke 'fetch' && \
git_invoke 'merge' "origin/${branch}"
}
function gv() {
local quiet=false
for a in ${@}
do
if [[ "${a}" == "--quiet" || "${a}" == "-q" ]]; then
quiet=true
break
fi
done
unset GIT_VERSION
GIT_VERSION="`"${GIT}" --version |${AWK} '{print $3}'`"
if [ true == ${quiet} ]; then
return
fi
"${PRINTF}" "[git] [%s]\n" "${GIT_VERSION}"
}
gv --quiet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.