Skip to content

Instantly share code, notes, and snippets.

@arnaudjolly
Last active October 27, 2022 15:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arnaudjolly/49d8552d7026f8435a0959a9f1738f0d to your computer and use it in GitHub Desktop.
Save arnaudjolly/49d8552d7026f8435a0959a9f1738f0d to your computer and use it in GitHub Desktop.
Dogr bash function and some shortcuts
# init colors to be used in other commands if possible
function _initColors() {
if test -t 1; then
local nbColors=$(tput colors)
if test -n "$nbColors" && test $nbColors -ge 8; then
green=$(tput setaf 2)
red=$(tput setaf 1)
cyan=$(tput setaf 6)
normal=$(tput sgr0)
fi
fi
}
_initColors
# "do on git repositories":
# pass command to execute on each git repo in this directory (only scan direct children)
#
# Usage: dogr [cmd] [cmd_args...]
#
# Ex:
# dogr -- see git repositories (with actual HEAD position). This is a unintentionnal behaviour but seems great ;-)
# dogr gst -s -- see git status summarized on each git repo in this directory (only scan direct children)
# dogr gpom -- launch a "git pull origin master" on each git repo in this directory (only scan direct children)
# dogr glog -1 -- see HEAD commit on each git repo in this directory (only scan direct children)
# "do on git repositories":
# pass command to execute on each git repo in this directory (only scan direct children)
#
# Usage: dogr [cmd] [cmd_args...]
#
# Ex:
# dogr -- see git repositories (with actual HEAD position). This is a unintentionnal behaviour but seems great ;-)
# dogr gst -s -- see git status summarized on each git repo in this directory (only scan direct children)
# dogr gpom -- launch a "git pull origin master" on each git repo in this directory (only scan direct children)
# dogr glog -1 -- see HEAD commit on each git repo in this directory (only scan direct children)
function dogr() {
for d in $(\ls); do
[ -d "$d" ] || continue
# execute those commands in subshells
(
cd "$d"
br=$(git branch 2>/dev/null | grep \* | cut -d' ' -f2-)
if [ -n "$br" ]
then
echo -e "\n[${cyan}>>>${normal}] $d ($br)"
"${@:1}"
if [ $? -eq 0 ]
then
echo "[${cyan}<<<${normal}] $d : ${green}OK${normal}"
else
echo "[${cyan}<<<${normal}] $d : ${red}KO${normal}"
fi
fi
)
done
}
function g() {
git "$@"
}
function glog() {
git log "$@"
}
function gtop() {
gloga -5 "$@"
}
function gloga() {
git log --graph --decorate --pretty='%C(auto)%h -%d %s %Cgreen(%ad: %cr) %C(bold blue)[%an <%aE>]%Creset' --abbrev-commit --date=short --all "$@"
}
function gst() {
git status "$@"
}
function ga() {
git add "$@"
}
function gaa() {
git add --all "$@"
}
function gb() {
git branch "$@"
}
function gbd() {
git branch --delete "$@"
}
function gbr() {
git branch --remote "$@"
}
function gc() {
git commit --verbose "$@"
}
function gca() {
git commit --verbose --all "$@"
}
function gcb() {
git checkout -b "$@"
}
function gcm() {
git checkout master "$@"
}
function gco() {
git checkout "$@"
}
function gd() {
git diff "$@"
}
function gdca() {
git diff --cached "$@"
}
function gdcw() {
git diff --cached --word-diff "$@"
}
function gdct() {
git describe --tags `git rev-list --tags --max-count=1` "$@"
}
function gdt() {
git diff-tree --no-commit-id --name-only -r "$@"
}
function gdw() {
git diff --word-diff "$@"
}
function gpl() {
git pull "$@"
}
function gpom() {
git pull origin master "$@"
}
function gfm() {
if [[ -z $1 || -z $2 ]]
then
echo "Find the merge commit that has brought a commit into a branch"
echo "usage: ./gfm commit branch"
return
fi
local commit=$1
local branch=$2
echo "Searching the merge commit that has brought [$commit] into [$branch]"
git rev-list $commit..$branch --ancestry-path --merges --reverse \
| grep -f <(git rev-list $commit..$branch --first-parent) \
| head -1 \
| xargs git show --pretty
}
# Git Checkout Last Tag matching pattern
function gcolt(){
if [ -z "$1" ]
then
USAGE=$(cat <<-EndOfText
Usage: $0 pattern [branch_name]
Checkout the last tag matching pattern if any.
if branch_name is given, create/reset the branch to this commit
Ex: $0 2.2.1 hotfix/2.2.1
This command will create a branch hotfix/2.2.1 based on the last tag matching 2.2.1
EndOfText
)
echo $USAGE
return
fi
local pattern=$1
local branch_name=$2
local tag=$(git tag --list --sort=-creatordate "*$pattern*" | head -1)
[ -z "$tag" ] && echo "no tag found matching '$pattern'" && return
echo "Tag $tag will be checked out."
local options
[ -n "$branch_name" ] && options="-B $branch_name"
git checkout $options $tag
}
#
# Add completion for some of those bash functions
#
__git_complete g __git_main
__git_complete ga _git_add
__git_complete gaa _git_add
__git_complete gb _git_branch
__git_complete gbr _git_branch
__git_complete gbd _git_branch
__git_complete gc _git_commit
__git_complete gca _git_commit
__git_complete gcb _git_checkout
__git_complete gco _git_checkout
__git_complete gcm _git_checkout
__git_complete gd _git_diff
__git_complete gdca _git_diff
__git_complete gdcw _git_diff
__git_complete gdw _git_diff
__git_complete glog _git_log
__git_complete gtop _git_log
__git_complete gloga _git_log
__git_complete gpl _git_pull
@arnaudjolly
Copy link
Author

Added gcolt function to checkout directly to the last tag matching a pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment