Skip to content

Instantly share code, notes, and snippets.

@kob-to-wni
Last active August 26, 2021 02:47
Show Gist options
  • Save kob-to-wni/81d8edf06b1abf0b70a165396ecdd648 to your computer and use it in GitHub Desktop.
Save kob-to-wni/81d8edf06b1abf0b70a165396ecdd648 to your computer and use it in GitHub Desktop.
Launch VS Code from Terminal with automated Git repository detection
#!/bin/bash
VSCODE=/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code
scanrepo () {
p=$@
if [ -d "$p" ]; then
pushd $p > /dev/null
else
exit -1
fi
git=$(git rev-parse --git-dir 2> /dev/null)
if [ -z "$git" ]; then
echo ""
else
echo $(dirname "$git")
fi
popd > /dev/null
}
repo=""
if [ -z "$1" ]; then
repo=$(scanrepo $(/bin/pwd))
else
if [ -d "$1" ]; then
repo=$(scanrepo "$1")
if [ ! -z $repo ]; then
shift
fi
elif [ -f "$1" ]; then
repo=$(scanrepo $(dirname "$1"))
fi
fi
if [ -z "$repo" ]; then
"$VSCODE" $*
else
# echo "$VSCODE" "$repo" "$*"
"$VSCODE" "$repo" "$*"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment