Zeshan Amjad
12/05/2025, 2:49 PMPylon
12/05/2025, 2:49 PMPylon
12/05/2025, 7:18 PMZeshan Amjad
12/05/2025, 10:08 PMManuel Werlberger
12/08/2025, 10:01 AM# Fish function to display git log with clickable GitHub PR links.
#
# Usage: git-pr-log [git-log-options]
# Example: git-pr-log -n 10 <sha>
function git-pr-log
# 1. Get the repository path (e.g., "owner/repo") from the 'origin' remote URL.
# The 'set -l' command creates a local variable.
# Command substitutions use parentheses: (command).
# Stderr is redirected with ^/dev/null.
set -l repo_url (git remote get-url origin)
# Check if the variable is empty.
if test -z "$repo_url"
echo "Error: Could not find 'origin' remote. Are you in a git repository?" >&2
return 1
end
# Use sed to extract "owner/repo" from the URL.
set -l repo_path (echo "$repo_url" | sed -e 's/.*<http://github.com[:\/]//|github.com[:\/]//>' -e 's/\.git$//')
# 2. Process the git log output commit by commit.
# In Fish, '$argv' contains all arguments passed to the function.
# The 'while read' loop is similar to bash.
git log --format='%H' $argv | while read -l sha
# Get details for the specific commit.
set -l short_sha (git show -s --format='%h' $sha)
set -l message (git show -s --format='%s %b' $sha) # Get subject and body
# 3. Try to find the PR number.
# The pipeline of external commands (echo, grep, head) works the same.
# First, check for the squash-merge format, e.g., "(#123)"
set -l pr_number (echo "$message" | grep -o '(#\([0-9]\+\))' | head -n 1 | grep -o '[0-9]\+')
# If not found, check for the merge-commit format, e.g., "Merge pull request #123"
if test -z "$pr_number"
set -l pr_number (echo "$message" | grep -o 'Merge pull request #[0-9]\+' | head -n 1 | grep -o '[0-9]\+')
end
# 4. Print the formatted output.
if test -n "$pr_number"
# If a PR number was found, print the link.
set -l pr_link "<https://github.com/$repo_path/pull/$pr_number>"
set -l commit_title (git show -s --format='%s' $sha | sed 's/ *(#[0-9]\+)$//')
# Fish has built-in helpers for color, which is more readable than ANSI codes.
printf "%s [#%s](%s) -- %s\n" $short_sha $pr_number $pr_link $commit_title
else
# Fallback: If no PR number, just print the standard oneline format.
git show -s --format='%h %s' $sha
end
end
end