Skip to content

Instantly share code, notes, and snippets.

@monking
Last active August 16, 2018 16:50
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 monking/96ef78fdecc7eba33f26392b31ed97f7 to your computer and use it in GitHub Desktop.
Save monking/96ef78fdecc7eba33f26392b31ed97f7 to your computer and use it in GitHub Desktop.
Some bash functions leveraging git to speed up workflows.
function gcg() { # git checkout grep
local branches flag git_options pattern OPTIND OPTARG
git_options=()
while getopts 'a' flag; do
case $flag in
a) git_options+=('-a');;
esac
done
shift $((OPTIND - 1))
branches=($(git branch ${git_options[@]} | perl -pe 's#^[\*\s]*(remotes/[^/]+/)?##' | grep "$*" | sort | uniq))
if [[ -z $branches ]]; then
pattern="$(echo -n "$*" | perl -pe 's/\s+//g' | perl -pe 's/(.)/.*\1/g')"
echo "No literal match found. Checking on exploded pattern: $pattern."
branches=($(git branch ${git_options[@]} | perl -pe 's#^[\*\s]*(remotes/[^/]+/)?##' | grep "$pattern" | sort | uniq))
fi
if [[ ${#branches[@]} -eq 1 ]]; then
git checkout ${branches[0]}
elif [[ ${#branches[@]} -gt 1 ]]; then
echo "Pattern \"$*\" matched multiple branches:"
for b in ${branches[@]}; do
echo $b
done
else
echo "No branch found by pattern \"$*\"."
fi
}
# list all TODO-like notes in diff to parent branch
function git-todo() {
local diff_reference remote remote_head_branch
diff_reference=$1
if [[ -z $diff_reference ]]; then
remote=origin
if [[ -z $(git remote | grep "\<$remote\>") ]]; then
remote=$(git branch -vv | grep '^\*' | perl -pe 's;^.* \[([^/]+).*;\1;')
if [[ $remote =~ ^\* ]]; then # no upstream branch found
remote=""
fi
fi
if [[ -n $remote ]]; then
remote_head_branch=develop
if [[ -z $(git branch | grep "\<$remote_head_branch\>") ]]; then
remote_head_branch=$(git remote show $remote | grep 'HEAD branch' | perl -pe 's/^\s*HEAD branch:\s*//')
fi
if [[ -n $remote_head_branch ]]; then
diff_reference=$remote/$remote_head_branch
fi
fi
fi
if [[ -n $diff_reference ]]; then
git diff -U0 $diff_reference | grep '^+++\|^+.*\(FIXME\|XXX\|TODO\)' | perl -pe 's/^\++( b\/)?//'| grep -B 1 --color=always 'FIXME\|XXX\|TODO' | less -RS
else
git grep --color=always -n '\(FIXME\|XXX\|TODO\)' | less -RS
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment