Something went wrong. Try again.
My dotfiles for void linux with niri
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224#!/bin/bash## Script to extract PR/commit URL and information from git blame for a specific file lineset -e# Initialize variablesDEBUG=0URL_ONLY=0FILE=""LINE=""VERSION="1.1.0"# Function to display help messageshow_help() { cat << EOFUsage: $(basename "$0") [OPTIONS] <file_path> <line_number> OR: $(basename "$0") [OPTIONS] <line_number> <file_path>Extract PR/commit information and URL from git blame for a specific file line.By default, outputs author name, time since commit, commit message, and URL.Options: -h, --help Show this help message and exit -d, --debug Enable debug output -v, --version Show version information and exit -u, --url-only Output only the URL, suitable for opening in browserEOF exit 0}# Function to display version informationshow_version() { echo "$(basename "$0") version $VERSION" exit 0}# Function to print debug messages when debug mode is enableddebug() { if [ $DEBUG -eq 1 ]; then echo "[DEBUG] $1" >&2 fi}# Function to display error messages and exiterror() { echo "ERROR: $1" >&2 exit 1}# Check if git is availablecommand -v git >/dev/null 2>&1 || error "git command not found. Please install git."# Process command line argumentswhile [[ $# -gt 0 ]]; do case "$1" in -h|--help) show_help ;; -d|--debug) DEBUG=1 shift ;; -v|--version) show_version ;; -u|--url-only) URL_ONLY=1 shift ;; *) # Try to determine if the argument is a line number or file path if [[ "$1" =~ ^[0-9]+$ ]]; then # It's a number, assume it's a line number LINE="$1" else # It's not a number, assume it's a file path FILE="$1" fi shift ;; esacdone# Validate required argumentsif [ -z "$FILE" ] || [ -z "$LINE" ]; then error "Missing required arguments. See --help for usage."fidebug "File: $FILE"debug "Line: $LINE"# Check if we're in a git repositorygit rev-parse --is-inside-work-tree >/dev/null 2>&1 || error "Not in a git repository"# Get git root directoryGIT_ROOT=$(git rev-parse --show-toplevel)debug "Git root: $GIT_ROOT"# Resolve file path relative to git root if neededif [ ! -f "$FILE" ]; then # Try with git root if [ -f "$GIT_ROOT/$FILE" ]; then FILE="$GIT_ROOT/$FILE" debug "Resolved file path: $FILE" else error "File does not exist: $FILE (tried $GIT_ROOT/$FILE)" fifi# Get the git blame info for the specified lineblame_info=$(git blame -L "${LINE},${LINE}" "${FILE}" 2>&1) || error "Failed to get git blame info: $blame_info"debug "Blame info: $blame_info"# Extract the commit ID (possibly shortened)short_commit_id=$(echo "$blame_info" | awk '{print $1}')[ -n "$short_commit_id" ] || error "Could not extract commit ID from blame output"debug "Short Commit ID: $short_commit_id"# Get the full commit hashfull_commit_id=$(git rev-parse "$short_commit_id" 2>&1) || error "Failed to get full commit hash: $full_commit_id"debug "Full Commit ID: $full_commit_id"# Get the remote URLremote_url=$(git remote get-url origin 2>&1) || error "Failed to get remote URL: $remote_url"debug "Original remote URL: $remote_url"# Process remote URLprocess_remote_url() { local url="$1" # Convert SSH URL to HTTPS if needed if [[ $url == git@* ]]; then local domain=$(echo "$url" | cut -d'@' -f2 | cut -d':' -f1) local repo_path=$(echo "$url" | cut -d':' -f2) url="https://${domain}/${repo_path}" debug "Converted SSH URL to HTTPS" fi # Remove .git suffix if present url=${url%.git} # Remove any username@ part from the URL url=$(echo "$url" | sed -E 's|https://[^@]+@|https://|') echo "$url"}remote_url=$(process_remote_url "$remote_url")debug "Processed remote URL: $remote_url"# Get author name, time since commit, and commit messageauthor_name=$(git show -s --format="%an" "$full_commit_id")commit_time=$(git show -s --format="%ar" "$full_commit_id") # already in "X time ago" formatcommit_msg=$(git show -s --format="%s" "$full_commit_id")debug "Author: $author_name"debug "Time: $commit_time"debug "Subject: $commit_msg"# Get the full commit messagefull_commit_msg=$(git show -s --format=%B "$full_commit_id" 2>&1) || error "Failed to get commit message"if [ $DEBUG -eq 1 ]; then debug "Full commit message:" echo "$full_commit_msg" | sed 's/^/ /' >&2fi# Try to find PR number in commit message (multiple formats)find_pr_number() { local msg="$1" local pr_number="" # Format: "Merge pull request #123" pr_number=$(echo "$msg" | grep -oE "Merge pull request #[0-9]+" | grep -oE "[0-9]+" | head -1) # Format: "PR #123:" if [ -z "$pr_number" ]; then pr_number=$(echo "$msg" | grep -oE "PR #[0-9]+" | grep -oE "[0-9]+" | head -1) fi # Format: "(#123)" if [ -z "$pr_number" ]; then pr_number=$(echo "$msg" | grep -oE "\\(#[0-9]+\\)" | grep -oE "[0-9]+" | head -1) fi # Format: "Merged PR 123:" if [ -z "$pr_number" ]; then pr_number=$(echo "$msg" | grep -oE "Merged PR [0-9]+" | grep -oE "[0-9]+" | head -1) fi echo "$pr_number"}pr_number=$(find_pr_number "$full_commit_msg")debug "PR number found: $pr_number"# Determine URL type based on repository hostget_pull_path() { if [[ "$remote_url" == *"github.com"* ]]; then echo "pull" elif [[ "$remote_url" == *"dev.azure.com"* ]]; then echo "pullrequest" else echo "pull" # Default for most Git hosts fi}# Generate URLif [ -n "$pr_number" ]; then pull_path=$(get_pull_path) url="${remote_url}/${pull_path}/${pr_number}" debug "Generated PR URL: $url"else url="${remote_url}/commit/${full_commit_id}" debug "Generated commit URL: $url"fi# Output based on modeif [ $URL_ONLY -eq 1 ]; then # URL-only mode echo "$url"else # Default rich output echo "$author_name, $commit_time • $commit_msg" echo "$url"fi