Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Last active July 30, 2023 05:34
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save RichardBronosky/9ab50abb8698e02341629db21e5fa6bf to your computer and use it in GitHub Desktop.
Save RichardBronosky/9ab50abb8698e02341629db21e5fa6bf to your computer and use it in GitHub Desktop.
Storing git Credentials
#!/bin/bash
dos_path_to_linux(){
sed -e 's?\\?/?g' -e' s?[cC]:?/mnt/c?' <<<"$1"
}
manager_path_from_git(){
sed -e 's?/cmd/git.exe?/mingw64/libexec/git-core/git-credential-manager.exe?' <<<"$1"
}
escape_spaces(){
sed -e 's? ?\\ ?g' <<<"$1"
}
find_where(){
out="$(/mnt/c/Windows/System32/where.exe "$1" 2>/dev/null)"
status=$?
if ! $(exit $status); then
return $status
fi
sed -e 's/[[:space:]]*$//' <<<"$out"
}
info(){
echo "$@" >/dev/stderr
}
get_manager_path(){
winpath="$(find_where git-credential-manager.exe)"
status=$?
if $(exit $status); then
dos_path_to_linux "$winpath"
else
info "Did not find git-credential-manager.exe in DOS path."
gitpath="$(find_where git.exe)"
status=$?
if ! $(exit $status); then
echo "Did not find git.exe in DOS path."
echo "Install https://github.com/Microsoft/Git-Credential-Manager-for-Windows/releases/latest and try again."
exit 256
fi
info "Found git at '$gitpath'"
manager_path_from_git "$(dos_path_to_linux "$gitpath")"
fi
}
path="$(get_manager_path)"
if ! [[ -x "$path" ]]; then
echo "Did not find executable at '$path'"
echo "Aborting"
exit 512
fi
info "Found Credential Manager at '$path'"
info "Configuring git"
git config --global credential.helper "$(escape_spaces "$path")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment