git: Fix working directory of shell aliases

In git aliases, shell commands are executed from the top-level directory
of the repo. Because of this, something like `git glog -- file` didn't
work in subdirectories.

Fix this by creating a wrapper script that first changes into
$GIT_PREFIX before executing the script. Since (currently) the only
use-case is to launch the functions in `autoload/git/`, most of the path
could move into the script, making the git config a lot cleaner.
This commit is contained in:
2025-05-23 16:19:12 +02:00
parent e232c8f37b
commit bbc6d5c9e0
3 changed files with 41 additions and 10 deletions

View File

@@ -4,14 +4,19 @@
addIgnoredFile = off addIgnoredFile = off
detachedHead = off detachedHead = off
[alias] [alias]
# NOTE: git-zsh-autoload (./zsh-autoload.sh) is a small wrapper that
# launches autoloadable zsh functions (.config/zsh/autoload/git/*) in
# the right directory, as shell commands in git aliases are executed
# from the top-level directory of the repository.
autosquash = -c sequence.editor=/bin/true rebase -i --autosquash autosquash = -c sequence.editor=/bin/true rebase -i --autosquash
autofixup= autosquash autofixup= autosquash
c = commit c = commit
changes = flog HEAD...FETCH_HEAD changes = flog HEAD...FETCH_HEAD
checkout-worktree = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-checkout-worktree\"" checkout-worktree = "!git-zsh-autoload checkout-worktree"
cow = checkout-worktree cow = checkout-worktree
co = checkout co = checkout
commit-last-msg = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-commit-last-msg\"" commit-last-msg = "!git-zsh-autoload commit-last-msg"
clm = commit-last-msg clm = commit-last-msg
last-msg = commit-last-msg last-msg = commit-last-msg
recommit = commit-last-msg --no-edit recommit = commit-last-msg --no-edit
@@ -20,20 +25,20 @@
ft = fetch-tags-only ft = fetch-tags-only
filter-repo = !git-filter-repo filter-repo = !git-filter-repo
fixes = log -1 --pretty=fixes fixes = log -1 --pretty=fixes
glog = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/glog\"" glog = "!git-zsh-autoload glog"
https-and-ssh = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-https-and-ssh\"" https-and-ssh = "!git-zsh-autoload https-and-ssh"
ssh-and-https = https-and-ssh ssh-and-https = https-and-ssh
l = log l = log
last-changed = "!cd \"$GIT_PREFIX\"; \"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-last-changed\"" last-changed = "!git-zsh-autoload last-changed"
make-fork = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-make-fork\"" make-fork = "!git-zsh-autoload make-fork"
p = push p = push
perm-stash = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-perm-stash\"" perm-stash = "!git-zsh-autoload perm-stash"
root = rev-parse --show-toplevel root = rev-parse --show-toplevel
signoff = rebase --signoff signoff = rebase --signoff
ss = stash ss = stash
ssync = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-ssync\"" ssync = "!git-zsh-autoload ssync"
submodule-rm = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-submodule-rm\"" submodule-rm = "!git-zsh-autoload submodule-rm"
track = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-track\"" track = "!git-zsh-autoload track"
branches = track branches = track
[blame] [blame]
date = short date = short

25
.config/git/zsh-autoload.sh Executable file
View File

@@ -0,0 +1,25 @@
#!/bin/sh
# SPDX-License-Identifier: MIT
# Copyright (c) 2025 Julian Prein
#
# Meant to be used in git aliases to launch an autoloadable zsh function in the
# correct directory.
if [ $# -eq 0 ]; then
printf >&2 "Usage: $(basename "$0") <function>\n"
exit 1
fi
BASE="${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git"
# In git aliases, shell commands are executed from the top-level directory of
# the repo. GIT_PREFIX contains the original directory relative to the
# top-level.
[ -z "$GIT_PREFIX" ] || cd "$GIT_PREFIX"
# no need for error handling, the message from sh is descriptive enough
if [ "${1#git-}" != "$1" ] || [ -e "$BASE/$1" ]; then
exec "$BASE/$@"
else
exec "$BASE/git-$@"
fi

1
.local/bin/git-zsh-autoload Symbolic link
View File

@@ -0,0 +1 @@
../../.config/git/zsh-autoload.sh