Skip to main content

git-repository-detection

Detect GitHub repository name and owner from git remotes. Use when needing repo identifier for GitHub CLI, API calls, or when working with multiple repositories. Automatically extracts owner/repo format.

설치로 이동

소스 정보

저장소
laurigates/CleanScope
최근 소스 활동
2025년 12월 21일 14:28
감지된 SKILL.md 언어
영어
스타
1
포크
0

설치 방법

기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.

소스 파일 검토

설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.

SKILL.md 표시 중

SKILL.md
소스 지침 · 읽기 전용 미리보기
created
2025-12-16T00:00:00.000Z
modified
2025-12-16T00:00:00.000Z
reviewed
2025-12-16T00:00:00.000Z
name
Git Repository Detection
description
Detect GitHub repository name and owner from git remotes. Use when needing repo identifier for GitHub CLI, API calls, or when working with multiple repositories. Automatically extracts owner/repo format.
allowed-tools
Bash, Read, Grep
# Git Repository Detection Expert knowledge for detecting and extracting GitHub repository information from git remotes, including repository name, owner, and full identifiers. ## Core Expertise **Repository Identification** - Extract owner and repository name from git remotes - Parse GitHub URLs (HTTPS and SSH) - Handle GitHub Enterprise URLs - Format as `owner/repo` for CLI/API usage **URL Parsing** - Parse HTTPS URLs: `https://github.com/owner/repo.git` - Parse SSH URLs: `git@github.com:owner/repo.git` - Handle custom domains: `https://github.enterprise.com/owner/repo.git` - Clean `.git` suffix and extra paths ## Essential Commands ### Get Remote URLs ```bash # List all remotes git remote -v # Get origin URL git remote get-url origin # Get specific remote git remote get-url upstream # Show remote details git remote show origin ``` ### Extract Repository Name ```bash # From HTTPS URL git remote get-url origin | sed 's/.*\/\([^/]*\)\.git/\1/' # From SSH URL git remote get-url origin | sed 's/.*:\([^/]*\/[^/]*\)\.git/\1/' | cut -d'/' -f2 # From any URL (owner/repo format) git remote get-url origin | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/' # Just repository name (no owner) basename $(git remote get-url origin) .git ``` ### Extract Owner ```bash # From any URL git remote get-url origin | sed 's/.*[:/]\([^/]*\)\/[^/]*\.git/\1/' # Alternative with awk git remote get-url origin | awk -F '[:/]' '{print $(NF-1)}' ``` ### Get Full Identifier (owner/repo) ```bash # Standard format for GitHub CLI/API git remote get-url origin | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/' # With validation REPO=$(git remote get-url origin 2>/dev/null | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/') echo "${REPO:-Unknown}" # Store in variable REPO_FULL=$(git remote get-url origin | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/') echo "Repository: $REPO_FULL" ``` ## URL Format Examples ### HTTPS URLs ```bash # Standard GitHub https://github.com/owner/repo.git # Extract: owner/repo # Without .git suffix https://github.com/owner/repo # Extract: owner/repo # GitHub Enterprise https://github.company.com/owner/repo.git # Extract: owner/repo ``` ### SSH URLs ```bash # Standard SSH git@github.com:owner/repo.git # Extract: owner/repo # Custom SSH port ssh://git@github.com:443/owner/repo.git # Extract: owner/repo # Enterprise SSH git@github.company.com:owner/repo.git # Extract: owner/repo ``` ## Parsing Patterns ### Universal Parser (HTTPS or SSH) ```bash # Works for both HTTPS and SSH parse_repo() { local url="$1" # Remove .git suffix url="${url%.git}" # Extract owner/repo if [[ "$url" =~ github\.com[:/]([^/]+/[^/]+) ]]; then echo "${BASH_REMATCH[1]}" elif [[ "$url" =~ :([^/]+/[^/]+)$ ]]; then echo "${BASH_REMATCH[1]}" fi } REPO=$(parse_repo "$(git remote get-url origin)") echo "$REPO" ``` ### Robust Extraction with sed ```bash # Handle both HTTPS and SSH, with or without .git git remote get-url origin \ | sed -E 's#.*github\.com[:/]##; s#\.git$##' ``` ### Using awk ```bash # Split by : or / and get last two components git remote get-url origin \ | awk -F'[/:]' '{print $(NF-1)"/"$NF}' \ | sed 's/\.git$//' ``` ## Real-World Usage ### With GitHub CLI ```bash # Get repo identifier for gh commands REPO=$(git remote get-url origin | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/') # Use with gh gh repo view "$REPO" gh issue list --repo "$REPO" gh pr list --repo "$REPO" # Or use current directory (gh auto-detects) gh repo view ``` ### With GitHub API ```bash # Extract for API calls REPO=$(git remote get-url origin | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/') OWNER=$(echo "$REPO" | cut -d'/' -f1) REPO_NAME=$(echo "$REPO" | cut -d'/' -f2) # API request curl -H "Authorization: token $GITHUB_TOKEN" \ "https://api.github.com/repos/$OWNER/$REPO_NAME" # Or use full identifier curl -H "Authorization: token $GITHUB_TOKEN" \ "https://api.github.com/repos/$REPO" ``` ### Check Multiple Remotes ```bash # List all remotes with parsed names git remote | while read remote; do url=$(git remote get-url "$remote") repo=$(echo "$url" | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/') echo "$remote: $repo" done # Output: # origin: owner/repo # upstream: original-owner/repo ``` ### Validate Repository Exists ```bash # Extract and validate REPO=$(git remote get-url origin | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/') # Check if repo exists on GitHub if gh repo view "$REPO" &>/dev/null; then echo "Repository exists: $REPO" else echo "Repository not found or not accessible: $REPO" fi ``` ### Clone URL from Identifier ```bash # Given owner/repo, construct URLs REPO="owner/repo" # HTTPS echo "https://github.com/${REPO}.git" # SSH echo "git@github.com:${REPO}.git" ``` ## Common Patterns ### Script Integration ```bash #!/bin/bash # Get repository info for current directory get_repo_info() { local origin_url=$(git remote get-url origin 2>/dev/null) if [[ -z "$origin_url" ]]; then echo "Error: Not a git repository or no origin remote" >&2 return 1 fi # Extract owner/repo local repo=$(echo "$origin_url" | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/') # Split into owner and name local owner=$(echo "$repo" | cut -d'/' -f1) local name=$(echo "$repo" | cut -d'/' -f2) echo "Full: $repo" echo "Owner: $owner" echo "Name: $name" } get_repo_info ``` ### Environment Variables ```bash # Set repo variables for scripts export REPO_FULL=$(git remote get-url origin | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/') export REPO_OWNER=$(echo "$REPO_FULL" | cut -d'/' -f1) export REPO_NAME=$(echo "$REPO_FULL" | cut -d'/' -f2) # Use in scripts gh issue create --repo "$REPO_FULL" --title "Bug Report" ``` ### Aliases ```bash # Add to ~/.gitconfig or ~/.bashrc # Git alias git config --global alias.repo-name "!git remote get-url origin | sed 's/.*[\\/:]\\([^\\/]*\\/[^\\/]*\\)\\.git/\\1/'" # Usage git repo-name # Shell alias alias repo='git remote get-url origin | sed "s/.*[:/]\([^/]*\/[^/]*\)\.git/\1/"' # Usage repo ``` ## Edge Cases and Error Handling ### No Remote ```bash # Check if remote exists if git remote get-url origin &>/dev/null; then REPO=$(git remote get-url origin | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/') echo "Repository: $REPO" else echo "Error: No origin remote configured" fi ``` ### Multiple Remotes ```bash # Use specific remote (origin, upstream, etc.) REMOTE="${1:-origin}" if git remote get-url "$REMOTE" &>/dev/null; then REPO=$(git remote get-url "$REMOTE" | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/') echo "Repository ($REMOTE): $REPO" else echo "Error: Remote '$REMOTE' not found" fi ``` ### Non-GitHub Remotes ```bash # Check if it's a GitHub URL URL=$(git remote get-url origin) if [[ "$URL" =~ github\.com ]]; then REPO=$(echo "$URL" | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/') echo "GitHub Repository: $REPO" else echo "Not a GitHub repository: $URL" fi ``` ### Submodules ```bash # Get parent repo PARENT_REPO=$(git -C "$(git rev-parse --show-toplevel)" remote get-url origin \ | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/') # Get submodule repo SUBMODULE_REPO=$(git remote get-url origin | sed 's/.*[:/]\([^/]*\/[^/]*\)\.git/\1/')
GitHub에서 보기
이 SKILL.md는 매우 커서 SkillsMP가 여기에는 첫 섹션만 미리 보여줍니다. GitHub에서 보기