Skip to content

Instantly share code, notes, and snippets.

@k0pernikus
Created December 17, 2015 18:45
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 k0pernikus/275d1a08bbf9e93a58aa to your computer and use it in GitHub Desktop.
Save k0pernikus/275d1a08bbf9e93a58aa to your computer and use it in GitHub Desktop.
checkout branch based upon lievensthein distance from the argument
#!/bin/bash
BRANCH=$1
BRANCHES=$(git branch | sed 's/^..//')
array=($BRANCHES)
function levenshtein {
if [ "$#" -ne "2" ]; then
echo "Usage: $0 word1 word2" >&2
elif [ "${#1}" -lt "${#2}" ]; then
levenshtein "$2" "$1"
else
local str1len=$((${#1}))
local str2len=$((${#2}))
local d i j
for i in $(seq 0 $(((str1len+1)*(str2len+1)))); do
d[i]=0
done
for i in $(seq 0 $((str1len))); do
d[$((i+0*str1len))]=$i
done
for j in $(seq 0 $((str2len))); do
d[$((0+j*(str1len+1)))]=$j
done
for j in $(seq 1 $((str2len))); do
for i in $(seq 1 $((str1len))); do
[ "${1:i-1:1}" = "${2:j-1:1}" ] && local cost=0 || local cost=1
local del=$((d[(i-1)+str1len*j]+1))
local ins=$((d[i+str1len*(j-1)]+1))
local alt=$((d[(i-1)+str1len*(j-1)]+cost))
d[i+str1len*j]=$(echo -e "$del\n$ins\n$alt" | sort -n | head -1)
done
done
echo ${d[str1len+str1len*(str2len)]}
fi
}
for element in ${array[@]}
do
if [ $element = "develop" ]; then
continue
fi
if [ $element = "master" ]; then
continue
fi
if [[ -z "$score" ]]; then
TARGET_BRANCH=$element
currentScore=$(levenshtein $BRANCH $element)
score=$currentScore
fi
currentScore=$(levenshtein $BRANCH $element)
if [[ ${currentScore} -lt ${score} ]]; then
score=$currentScore
TARGET_BRANCH=$element
fi
done
git checkout ${TARGET_BRANCH}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment