Skip to content

Instantly share code, notes, and snippets.

@vendethiel
Last active November 28, 2023 08:40

Revisions

  1. vendethiel revised this gist Nov 28, 2023. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions branch-Switch.nu
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    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.")
    }
    }
    }
  2. vendethiel created this gist Aug 5, 2022.
    38 changes: 38 additions & 0 deletions branch-Switch.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    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
    )}