Skip to content

Instantly share code, notes, and snippets.

@Oluwasetemi
Created October 29, 2025 14:55
Show Gist options
  • Select an option

  • Save Oluwasetemi/b9c8f8fea9d2b274b7b45964eda9ae7f to your computer and use it in GitHub Desktop.

Select an option

Save Oluwasetemi/b9c8f8fea9d2b274b7b45964eda9ae7f to your computer and use it in GitHub Desktop.
An iterated version of my git clone or gh repo clone
function clone() {
if [[ -z $1 ]]; then
echo "Error: Repository name or URL is required."
return 1
fi
input_url="$1"
branch=""
repo_url=""
github_user=""
repo_name=""
gh_clone_url=""
echo "DEBUG: Input URL: '$input_url'"
# Case 1: Handle full GitHub URL with branch (e.g., https://github.com/user/repo/tree/branch)
# Use proper zsh regex matching with =~ operator
if [[ "$input_url" =~ ^https?://github\.com/([^/]+)/([^/]+)/tree/(.+)$ ]]; then
github_user="${match[1]}"
repo_name="${match[2]}"
# Strip .git if present
repo_name="${repo_name%.git}"
branch="${match[3]}" # This captures the full path after tree/
repo_url="https://github.com/$github_user/$repo_name.git"
gh_clone_url="https://github.com/$github_user/$repo_name"
echo "DEBUG: Matched Case 1 (with branch URL)"
# Case 2: Handle full GitHub URL (e.g., https://github.com/user/repo.git or without .git)
elif [[ "$input_url" =~ ^https?://github\.com/([^/]+)/([^/]+)(\.git)?$ ]]; then
github_user="${match[1]}"
repo_name="${match[2]}"
# Strip .git if present - this is the key fix
repo_name="${repo_name%.git}"
repo_url="https://github.com/$github_user/$repo_name.git"
gh_clone_url="https://github.com/$github_user/$repo_name"
echo "DEBUG: Matched Case 2 (full HTTPS URL)"
# Case 3: Handle SSH format (e.g., [email protected]:user/repo.git)
elif [[ "$input_url" =~ ^git@github\.com:([^/]+)/([^/]+)(\.git)?$ ]]; then
github_user="${match[1]}"
repo_name="${match[2]}"
# Strip .git if present
repo_name="${repo_name%.git}"
repo_url="[email protected]:$github_user/$repo_name.git"
gh_clone_url="$repo_url"
echo "DEBUG: Matched Case 3 (SSH URL)"
# Case 4: Handle shorthand like user/repo
elif [[ "$input_url" =~ ^([^/]+)/([^/]+)$ ]]; then
github_user="${match[1]}"
repo_name="${match[2]}"
# Strip .git if present (though unlikely in shorthand)
repo_name="${repo_name%.git}"
repo_url="https://github.com/$github_user/$repo_name.git"
gh_clone_url="https://github.com/$github_user/$repo_name"
echo "DEBUG: Matched Case 4 (shorthand)"
else
echo "Error: Could not determine repository from input '$input_url'"
return 1
fi
echo "DEBUG: Extracted GitHub User: '$github_user'"
echo "DEBUG: Extracted Repo Name: '$repo_name'"
echo "DEBUG: Extracted Branch: '$branch' (if applicable)"
echo "DEBUG: Internal Git URL (repo_url): '$repo_url'"
echo "DEBUG: URL passed to gh repo clone (gh_clone_url): '$gh_clone_url'"
target_dir="${2:-$repo_name}"
echo "DEBUG: Target directory for clone: '$target_dir'"
echo "DEBUG: Executing: gh repo clone '$gh_clone_url' '$target_dir'"
if gh repo clone "$gh_clone_url" "$target_dir"; then
echo "DEBUG: Initial clone successful."
cd "$target_dir" || { echo "Failed to navigate to $target_dir"; return 1; }
if [[ -n "$branch" ]]; then
echo "DEBUG: Attempting to fetch and checkout branch '$branch'..."
git fetch origin "$branch" || echo "Warning: Failed to fetch branch '$branch'"
git checkout "$branch" || echo "Warning: Failed to checkout branch '$branch'"
fi
return 0
else
echo "DEBUG: Initial clone failed. Trying fallback directories."
echo "Initial clone failed, retrying with alternate folder names..."
lowercase_github_user=$(echo "$github_user" | tr '[:upper:]' '[:lower:]')
base_dir="${repo_name}_${lowercase_github_user}"
retry_count=1
max_retries=100
while [[ $retry_count -le $max_retries ]]; do
fallback_dir="${base_dir}_$retry_count"
echo "DEBUG: Attempting retry with folder: '$fallback_dir'"
echo "DEBUG: Executing: gh repo clone '$gh_clone_url' '$fallback_dir'"
if gh repo clone "$gh_clone_url" "$fallback_dir"; then
echo "DEBUG: Retry clone successful."
cd "$fallback_dir" || { echo "Failed to navigate to $fallback_dir"; return 1; }
if [[ -n "$branch" ]]; then
echo "DEBUG: Attempting to fetch and checkout branch '$branch' for retry clone..."
git fetch origin "$branch" || echo "Warning: Failed to fetch branch '$branch'"
git checkout "$branch" || echo "Warning: Failed to checkout branch '$branch'"
fi
return 0
fi
((retry_count++))
done
echo "Clone failed after $max_retries attempts. Please check the repository URL or your gh CLI setup."
return 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment