Skip to content

Instantly share code, notes, and snippets.

@juanecl
Last active February 13, 2025 13:22
Show Gist options
  • Save juanecl/820020e00e215acf74f66fab823031a8 to your computer and use it in GitHub Desktop.
Save juanecl/820020e00e215acf74f66fab823031a8 to your computer and use it in GitHub Desktop.
Check access to git repo

Check Git Access Script

Overview

The check_git_access.sh script is a CLI tool designed to verify if the current user has permission to access a specified Git repository (clone or pull).

Prerequisites

  • Git: Ensure Git is installed on your system.
  • Network Access: The script must be able to reach the repository URL.

Installation and Setup

  1. Download or create the script.
  2. Grant execution permission:
    chmod +x check_git_access.sh
  3. Run the script with the Git repository URL:
    ./check_git_access.sh <repository_url>

Usage

check_git_access.sh <repository_url>

Arguments

  • <repository_url>: The URL of the Git repository to check.

Examples

Checking access to a public repository:

./check_git_access.sh https://github.com/user/repo.git

Checking access to a private repository:

./check_git_access.sh git@github.com:user/repo.git

Features

  • Checks for Git installation: Ensures Git is installed before proceeding.
  • Validates repository URL: Ensures a repository URL is provided.
  • Attempts remote access: Uses git ls-remote to check access permissions.

Return Codes

  • 0: The user has access to the repository.
  • 1: The user does not have permission or an error occurred.

Logs and Troubleshooting

  • If access is denied, verify:
    • The repository URL is correct.
    • Authentication credentials (SSH key or Git credentials) are properly configured.
    • Run manually:
      git ls-remote <repository_url>
    • Check network connectivity.

License

This script is provided as-is without warranties. Use at your own risk.

Author

#!/bin/bash
# -----------------------------------------------------------------------------
# Script: check_git_access.sh
# Description: This script is a CLI tool to check if the current user has
# permissions to access a Git repository (clone or pull).
#
# Usage:
# check_git_access.sh <repository_url>
#
# Arguments:
# <repository_url> The URL of the Git repository to check.
#
# Returns:
# 0 if the user has access, 1 otherwise.
#
# Example:
# check_git_access.sh https://github.com/user/repo.git
# -----------------------------------------------------------------------------
# Function to check if a command is installed
check_command_installed() {
if ! command -v "$1" &> /dev/null; then
echo "ERROR: $1 is not installed. Please install it to proceed." >&2
return 1
fi
return 0
}
# Function to check Git permissions
check_git_permissions() {
local repo_url="$1"
# Check if git is installed
check_command_installed "git" || return 1
# Check if a repository URL is provided
if [ -z "$repo_url" ]; then
echo "ERROR: Repository URL is required." >&2
return 1
fi
# Attempt to fetch the remote repository
git ls-remote "$repo_url" &> /dev/null
if [ $? -ne 0 ]; then
echo "ERROR: You do not have permissions to clone or pull from the repository $repo_url." >&2
return 1
fi
echo "INFO: Successfully validated permissions for $repo_url"
return 0
}
# Check if a repository URL is provided
if [ -z "$1" ]; then
echo "ERROR: Repository URL is required." >&2
echo "Usage: $0 <repository_url>" >&2
exit 1
fi
# Run the permission check
check_git_permissions "$1"
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment