scripts/git-resolve.sh

Source file repositories/reference/linux-study-clean/scripts/git-resolve.sh

File Facts

System
Linux kernel
Corpus path
scripts/git-resolve.sh
Extension
.sh
Size
5940 bytes
Lines
202
Domain
Support Tooling And Documentation
Bucket
scripts
Inferred role
Support Tooling And Documentation: scripts
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# (c) 2025, Sasha Levin <sashal@kernel.org>

usage() {
	echo "Usage: $(basename "$0") [--selftest] [--force] <commit-id> [commit-subject]"
	echo "Resolves a short git commit ID to its full SHA-1 hash, particularly useful for fixing references in commit messages."
	echo ""
	echo "Arguments:"
	echo "  --selftest      Run self-tests"
	echo "  --force         Try to find commit by subject if ID lookup fails"
	echo "  commit-id       Short git commit ID to resolve"
	echo "  commit-subject  Optional commit subject to help resolve between multiple matches"
	exit 1
}

# Convert subject with ellipsis to grep pattern
convert_to_grep_pattern() {
	local subject="$1"
	# First escape ALL regex special characters
	local escaped_subject
	escaped_subject=$(printf '%s\n' "$subject" | sed 's/[[\.*^$()+?{}|]/\\&/g')
	# Also escape colons, parentheses, and hyphens as they are special in our context
	escaped_subject=$(echo "$escaped_subject" | sed 's/[:-]/\\&/g')
	# Then convert escaped ... sequence to .*?
	escaped_subject=$(echo "$escaped_subject" | sed 's/\\\.\\\.\\\./.*?/g')
	echo "^${escaped_subject}$"
}

git_resolve_commit() {
	local force=0
	if [ "$1" = "--force" ]; then
		force=1
		shift
	fi

	# Split input into commit ID and subject
	local input="$*"
	local commit_id="${input%% *}"
	local subject=""

	# Extract subject if present (everything after the first space)
	if [[ "$input" == *" "* ]]; then
		subject="${input#* }"
		# Strip the ("...") quotes if present
		subject="${subject#*(\"}"
		subject="${subject%\")*}"
	fi

	# Get all possible matching commit IDs
	local matches
	readarray -t matches < <(git rev-parse --disambiguate="$commit_id" 2>/dev/null)

	# Return immediately if we have exactly one match
	if [ ${#matches[@]} -eq 1 ]; then
		echo "${matches[0]}"
		return 0
	fi

	# If no matches and not in force mode, return failure
	if [ ${#matches[@]} -eq 0 ] && [ $force -eq 0 ]; then
		return 1
	fi

	# If we have a subject, try to find a match with that subject
	if [ -n "$subject" ]; then
		# Convert subject with possible ellipsis to grep pattern
		local grep_pattern
		grep_pattern=$(convert_to_grep_pattern "$subject")

Annotation

Implementation Notes