Last active
November 28, 2023 08:40
-
-
Save vendethiel/d6256f783de75a3ad8100c92a19d0f9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def s [match?: string] { | |
if ($match == null) { | |
git rev-parse --abbrev-ref HEAD | str trim | |
} else { | |
let $branches = git branch | grep -i $match | tr '*' ' ' | lines | str trim | |
let $length = $branches | length | |
if ($length == 0) { | |
"No branch found with hint" | |
} else if ($length == 1) { | |
git checkout $branches.0 | |
} else if ($branches | any {|b| $b == $match}) { | |
git checkout $match | |
} else { | |
git checkout ($branches | to text | gum filter --placeholder "Several branches matched. Pick the one you want.") | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s() {( | |
COLOR_RED="\u001b[31m" | |
COLOR_RESET="\u001b[0m" | |
set -e | |
if [[ $# -gt 1 ]] | |
then | |
echo "${COLOR_RED}s accepts 0 or 1 argument${COLOR_RESET}" | |
exit | |
fi | |
if [[ $# -eq 1 ]] | |
then | |
hint=$1 | |
else | |
hint="DEV-" | |
fi | |
integer occs=$(git branch | grep -i $hint | wc -l) | |
if [[ $occs -eq 0 ]] | |
then | |
echo "${COLOR_RED}s: no match found${COLOR_RESET}" | |
exit | |
elif [[ $occs -eq 1 ]] | |
then | |
branch=$(git branch | grep -i $hint | sed 's/*//' | xargs) # XXX that `xargs` is a hack to trim | |
else | |
branch=$(git branch | grep -i $hint | sed 's/*//' | awk '{print $1}' | gum filter --placeholder "Multiple branch matching, pick one") | |
fi | |
if [[ -z "$branch" ]] then | |
echo "${COLOR_RED}s: no branch selected${COLOR_RESET}" | |
exit | |
fi | |
git checkout $branch | |
)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment