Skip to content

Instantly share code, notes, and snippets.

@tao12345666333
Last active May 31, 2026 09:04
Show Gist options
  • Select an option

  • Save tao12345666333/973e7bcef4b8e279f17277231c7d9e88 to your computer and use it in GitHub Desktop.

Select an option

Save tao12345666333/973e7bcef4b8e279f17277231c7d9e88 to your computer and use it in GitHub Desktop.
gr
#!/usr/bin/env bash
set -euo pipefail
usage() {
echo "Usage: gr <repo-url>" >&2
exit 1
}
repo_url="${1:-}"
[[ -n "$repo_url" ]] || usage
if ! command -v git >/dev/null 2>&1; then
echo "git is required but not found in PATH." >&2
exit 1
fi
repo_url="${repo_url%/}"
host=""
path=""
case "$repo_url" in
*://*)
no_scheme="${repo_url#*://}"
no_scheme="${no_scheme#*@}"
authority="${no_scheme%%/*}"
host="${authority%%:*}"
path="${no_scheme#*/}"
;;
*@*:*)
left="${repo_url%%:*}"
host="${left#*@}"
path="${repo_url#*:}"
;;
*)
echo "Unsupported repository URL: $repo_url" >&2
exit 1
;;
esac
path="${path%%\?*}"
path="${path%%\#*}"
path="${path%/}"
path="${path%.git}"
if [[ -z "$host" || -z "$path" || "$path" != */* ]]; then
echo "Cannot parse repository URL: $repo_url" >&2
exit 1
fi
dest="$HOME/go/src/$host/$path"
if [[ -e "$dest" ]]; then
if [[ -d "$dest/.git" ]]; then
printf '%s\n' "$dest"
exit 0
fi
echo "Target path exists and is not a git repository: $dest" >&2
exit 1
fi
mkdir -p "$(dirname "$dest")"
git clone --quiet "$repo_url" "$dest"
printf '%s\n' "$dest"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment