git: Use switch instead of checkout if available

For git versions that support it, the aliases will use the newer
`switch` instruction instead of `checkout` for its use cases.
Creating a new branch through `gcb` is for example done by
`git switch -c` instead of the older `git checkout -b`.

Additionally streamlined git version checking to be a little faster and
to unify its approach on posix sh, bash and zsh instead of utilizing
individual checking functions.
This commit is contained in:
Marty Oehme 2021-11-23 10:33:37 +01:00
parent cf85357f5c
commit df0cf3d540
Signed by: Marty
GPG Key ID: B7538B8F50A1C800
3 changed files with 21 additions and 30 deletions

View File

@ -1,9 +0,0 @@
#!/usr/bin/env bash
if ! exist git; then
return 1
fi
# while zsh can detect versions, bash
# in shells other than zsh, simply fall back to save
alias gsta='git stash save'

View File

@ -4,7 +4,9 @@ if ! exist git; then
return 1
fi
# git alias
# print git version output and get raw version number by stripping prefix
git_version=$(git --version 2>/dev/null)
git_version="${git_version##git version }"
alias g='git'
@ -18,10 +20,17 @@ alias gc='git commit -v'
alias gc!='git commit -v --amend'
alias gcn!='git commit -v --no-edit --amend'
alias gcm='git checkout master 2>/dev/null || git checkout main'
alias gcd='git checkout develop'
alias gcb='git checkout -b'
alias gco='git checkout'
if version_at_least 2.23 "$git_version"; then
alias gcm='git switch master 2>/dev/null || git switch main'
alias gcd='git switch develop'
alias gcb='git switch -c'
alias gco='git switch'
else
alias gcm='git checkout master 2>/dev/null || git checkout main'
alias gcd='git checkout develop'
alias gcb='git checkout -b'
alias gco='git checkout'
fi
alias gd='git diff'
alias gds='git diff --staged'
@ -59,7 +68,13 @@ alias grbm='git rebase master || git rebase main'
alias gst='git status'
# stash push/save is handled differently by zsh/bash
alias gstp='git stash pop'
alias gstl='git stash list'
alias gstL='git stash list --stat'
if version_at_least 2.13 "$git_version"; then
alias gsta='git stash push'
else
alias gsta='git stash save'
fi
unset -v git_version

View File

@ -1,15 +0,0 @@
#!/usr/bin/env zsh
# shellcheck shell=sh
if ! exist git; then
return 1
fi
# git alias
# if git is at least version 2.13, we can use git stash push
autoload -Uz is-at-least
if is-at-least 2.13 "$(git --version 2>/dev/null | awk '{print $3}')"; then
alias gsta='git stash push'
else
alias gsta='git stash save'
fi