Compare commits

..
Author SHA1 Message Date
Carlo Sala 69706a49df more fixes 2026-09-18 14:30:19 +02:00
Carlo Sala 59d7ef3171 fix comments 2026-09-18 14:17:05 +02:00
Robby RussellandClaude Opus 5 97dfe1ae54 fix(init): only accept an object ID as the resolved revision
A malformed HEAD or loose ref was copied into the zcompdump metadata as
though it had resolved, so a corrupt file could make #omz revision:
differ from git rev-parse HEAD instead of taking the fallback.

Fold the three resolution paths into one exit and check the result is a
40 or 64 character hex object ID, so symbolic refs, malformed files and
missed packed entries all fall through to git.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-07 08:25:57 -07:00
Robby RussellandClaude Opus 5 c6d78edc8f fix(init): keep the revision helper's REPLY out of the global scope
_omz_git_head reports through $REPLY, which stayed set for the rest of
startup and could overwrite caller state or be mistaken for the result
of an unrelated read. Call it inside an anonymous function that declares
REPLY local.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-07 07:56:48 -07:00
Robby RussellandCopilot Autofix powered by AI a8da200b18 Apply batched suggestions from code review
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-09-06 08:38:58 -07:00
Robby RussellandClaude Fable 5.1 40323d0e37 perf(init): read the OMZ revision from .git instead of forking git
`git rev-parse HEAD` was run on every startup only to stamp the
zcompdump metadata. Resolve HEAD by reading the git directory in zsh:
handles `.git` files (worktrees, submodules), worktree common dirs,
detached HEADs and packed refs, and falls back to `git rev-parse` when
anything looks unexpected.

Measured on macOS arm64, zsh 5.9: 7.4 ms -> 0.2 ms per interactive
start.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-06 08:01:19 -07:00
5 changed files with 181 additions and 102 deletions
-2
View File
@@ -36,8 +36,6 @@ jobs:
run: |
for file in ./oh-my-zsh.sh \
./lib/*.zsh \
./functions/* \
./completions/_* \
./plugins/*/*.plugin.zsh \
./plugins/*/_* \
./themes/*.zsh-theme; do
-90
View File
@@ -1,90 +0,0 @@
#compdef omz
local -a cmds subcmds
cmds=(
'changelog:Print the changelog'
'help:Usage information'
'plugin:Manage plugins'
'pr:Manage Oh My Zsh Pull Requests'
'reload:Reload the current zsh session'
'shop:Open the Oh My Zsh shop'
'theme:Manage themes'
'update:Update Oh My Zsh'
'version:Show the version'
)
if (( CURRENT == 2 )); then
_describe 'command' cmds
elif (( CURRENT == 3 )); then
case "$words[2]" in
changelog) local -a refs
refs=("${(@f)$(builtin cd -q "$ZSH"; command git for-each-ref --format="%(refname:short):%(subject)" refs/heads refs/tags)}")
_describe 'command' refs ;;
plugin) subcmds=(
'disable:Disable plugin(s)'
'enable:Enable plugin(s)'
'info:Get plugin information'
'list:List plugins'
'load:Load plugin(s)'
)
_describe 'command' subcmds ;;
pr) subcmds=('clean:Delete all Pull Request branches' 'test:Test a Pull Request')
_describe 'command' subcmds ;;
theme) subcmds=('list:List themes' 'set:Set a theme in your .zshrc file' 'use:Load a theme')
_describe 'command' subcmds ;;
esac
elif (( CURRENT == 4 )); then
case "${words[2]}::${words[3]}" in
plugin::(disable|enable|load))
local -aU valid_plugins
if [[ "${words[3]}" = disable ]]; then
# if command is "disable", only offer already enabled plugins
valid_plugins=($plugins)
else
valid_plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t))
# if command is "enable", remove already enabled plugins
[[ "${words[3]}" = enable ]] && valid_plugins=(${valid_plugins:|plugins})
fi
_describe 'plugin' valid_plugins ;;
plugin::info)
local -aU plugins
plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t))
_describe 'plugin' plugins ;;
plugin::list)
local -a opts
opts=('--enabled:List enabled plugins only')
_describe -o 'options' opts ;;
theme::(set|use))
local -aU themes
themes=("$ZSH"/themes/*.zsh-theme(-.N:t:r) "$ZSH_CUSTOM"/**/*.zsh-theme(-.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
_describe 'theme' themes ;;
esac
elif (( CURRENT > 4 )); then
case "${words[2]}::${words[3]}" in
plugin::(enable|disable|load))
local -aU valid_plugins
if [[ "${words[3]}" = disable ]]; then
# if command is "disable", only offer already enabled plugins
valid_plugins=($plugins)
else
valid_plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t))
# if command is "enable", remove already enabled plugins
[[ "${words[3]}" = enable ]] && valid_plugins=(${valid_plugins:|plugins})
fi
# Remove plugins already passed as arguments
# NOTE: $(( CURRENT - 1 )) is the last plugin argument completely passed, i.e. that which
# has a space after them. This is to avoid removing plugins partially passed, which makes
# the completion not add a space after the completed plugin.
local -a args
args=(${words[4,$(( CURRENT - 1))]})
valid_plugins=(${valid_plugins:|args})
_describe 'plugin' valid_plugins ;;
esac
fi
return 0
+98 -2
View File
@@ -1,3 +1,5 @@
#!/usr/bin/env zsh
function omz {
setopt localoptions noksharrays
[[ $# -gt 0 ]] || {
@@ -18,6 +20,102 @@ function omz {
_omz::$command "$@"
}
function _omz {
local -a cmds subcmds
cmds=(
'changelog:Print the changelog'
'help:Usage information'
'plugin:Manage plugins'
'pr:Manage Oh My Zsh Pull Requests'
'reload:Reload the current zsh session'
'shop:Open the Oh My Zsh shop'
'theme:Manage themes'
'update:Update Oh My Zsh'
'version:Show the version'
)
if (( CURRENT == 2 )); then
_describe 'command' cmds
elif (( CURRENT == 3 )); then
case "$words[2]" in
changelog) local -a refs
refs=("${(@f)$(builtin cd -q "$ZSH"; command git for-each-ref --format="%(refname:short):%(subject)" refs/heads refs/tags)}")
_describe 'command' refs ;;
plugin) subcmds=(
'disable:Disable plugin(s)'
'enable:Enable plugin(s)'
'info:Get plugin information'
'list:List plugins'
'load:Load plugin(s)'
)
_describe 'command' subcmds ;;
pr) subcmds=('clean:Delete all Pull Request branches' 'test:Test a Pull Request')
_describe 'command' subcmds ;;
theme) subcmds=('list:List themes' 'set:Set a theme in your .zshrc file' 'use:Load a theme')
_describe 'command' subcmds ;;
esac
elif (( CURRENT == 4 )); then
case "${words[2]}::${words[3]}" in
plugin::(disable|enable|load))
local -aU valid_plugins
if [[ "${words[3]}" = disable ]]; then
# if command is "disable", only offer already enabled plugins
valid_plugins=($plugins)
else
valid_plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t))
# if command is "enable", remove already enabled plugins
[[ "${words[3]}" = enable ]] && valid_plugins=(${valid_plugins:|plugins})
fi
_describe 'plugin' valid_plugins ;;
plugin::info)
local -aU plugins
plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t))
_describe 'plugin' plugins ;;
plugin::list)
local -a opts
opts=('--enabled:List enabled plugins only')
_describe -o 'options' opts ;;
theme::(set|use))
local -aU themes
themes=("$ZSH"/themes/*.zsh-theme(-.N:t:r) "$ZSH_CUSTOM"/**/*.zsh-theme(-.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
_describe 'theme' themes ;;
esac
elif (( CURRENT > 4 )); then
case "${words[2]}::${words[3]}" in
plugin::(enable|disable|load))
local -aU valid_plugins
if [[ "${words[3]}" = disable ]]; then
# if command is "disable", only offer already enabled plugins
valid_plugins=($plugins)
else
valid_plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t))
# if command is "enable", remove already enabled plugins
[[ "${words[3]}" = enable ]] && valid_plugins=(${valid_plugins:|plugins})
fi
# Remove plugins already passed as arguments
# NOTE: $(( CURRENT - 1 )) is the last plugin argument completely passed, i.e. that which
# has a space after them. This is to avoid removing plugins partially passed, which makes
# the completion not add a space after the completed plugin.
local -a args
args=(${words[4,$(( CURRENT - 1))]})
valid_plugins=(${valid_plugins:|args})
_describe 'plugin' valid_plugins ;;
esac
fi
return 0
}
# If run from a script, do not set the completion function
if (( ${+functions[compdef]} )); then
compdef _omz omz
fi
## Utility functions
function _omz::confirm {
@@ -844,5 +942,3 @@ function _omz::version {
printf "%s (%s)\n" "$version" "$commit"
)
}
omz "$@"
@@ -1,4 +1,4 @@
# omz_diagnostic_dump
# diagnostics.zsh
#
# Diagnostic and debugging support for oh-my-zsh
@@ -351,5 +351,3 @@ function _omz_diag_dump_os_specific_version() {
done
}
omz_diagnostic_dump "$@"
+82 -5
View File
@@ -78,9 +78,6 @@ fpath=($ZSH/{functions,completions} $ZSH_CUSTOM/{functions,completions} $fpath)
# Load all stock functions (from $fpath files) called below.
autoload -U compaudit compinit zrecompile
# The omz CLI and omz_diagnostic_dump are loaded on first use
autoload -Uz omz omz_diagnostic_dump
is_plugin() {
local base_dir=$1
local name=$2
@@ -113,9 +110,89 @@ if [[ -z "$ZSH_COMPDUMP" ]]; then
ZSH_COMPDUMP="${ZDOTDIR:-$HOME}/.zcompdump-${SHORT_HOST}-${ZSH_VERSION}"
fi
# Construct zcompdump OMZ metadata
zcompdump_revision="#omz revision: $(builtin cd -q "$ZSH"; git rev-parse HEAD 2>/dev/null)"
# Resolve the commit $ZSH is checked out at into $REPLY by reading the git
# directory, so that no git process is forked on every startup.
# Handles .git files (worktrees, submodules), worktree common dirs, detached
# HEADs and packed refs. Returns 1 if anything is unexpected.
_omz_git_head() {
local gitdir="$ZSH/.git" common head ref
local -a lines
REPLY=
# .git may be a file pointing at the real git dir
if [[ -f "$gitdir" ]]; then
read -r head 2>/dev/null < "$gitdir" || return 1
[[ "$head" = "gitdir: "* ]] || return 1
gitdir="${head#gitdir: }"
[[ -n "$gitdir" ]] || return 1
[[ "$gitdir" = /* ]] || gitdir="$ZSH/$gitdir"
fi
# worktrees keep their refs in the common git dir
common="$gitdir"
if [[ -f "$gitdir/commondir" ]]; then
read -r common 2>/dev/null < "$gitdir/commondir" || return 1
[[ "$common" = /* ]] || common="$gitdir/$common"
fi
[[ -r "$gitdir/HEAD" ]] || return 1
read -r head 2>/dev/null < "$gitdir/HEAD" || return 1
if [[ "$head" = ref:\ * ]]; then
ref="${head#ref: }"
# Only use well-formed full ref names as paths. Besides matching Git's ref
# rules, this prevents a malformed HEAD from escaping the git directory.
[[ "$ref" = refs/?* \
&& "$ref" != *..* \
&& "$ref" != *//* \
&& "$ref" != */ \
&& "$ref" != */.* \
&& "$ref" != *.lock \
&& "$ref" != *.lock/* \
&& "$ref" != *. \
&& "$ref" != *'@{'* \
&& "$ref" != *[[:cntrl:]\ \~\^\:\?\*\[\\]* \
]] || return 1
case "$ref" in
# These namespaces are private to each worktree and are never resolved
# from the common directory or its packed-refs file.
refs/bisect/*|refs/worktree/*|refs/rewritten/*)
[[ -r "$gitdir/$ref" ]] || return 1
read -r REPLY 2>/dev/null < "$gitdir/$ref" || return 1
;;
*)
if [[ -r "$common/$ref" ]]; then
read -r REPLY 2>/dev/null < "$common/$ref" || return 1
elif [[ -r "$common/packed-refs" ]]; then
lines=("${(@f)$(<"$common/packed-refs")}")
REPLY="${lines[(r)* ${(b)ref}]%% *}"
else
return 1
fi
;;
esac
else
# detached HEAD: the file holds the commit itself
REPLY="$head"
fi
# only an object ID is a usable answer: a symbolic ref, a malformed file or
# a missed packed entry all fall through to the git fallback instead
[[ -n "$REPLY" && -z "${REPLY//[0-9a-f]/}" ]] \
&& (( ${#REPLY} == 40 || ${#REPLY} == 64 ))
}
# Construct zcompdump OMZ metadata. The helper reports through $REPLY, so
# call it in a scope that keeps that out of the global namespace.
() {
local REPLY
_omz_git_head || REPLY="$(builtin cd -q "$ZSH"; git rev-parse HEAD 2>/dev/null)"
typeset -g zcompdump_revision="#omz revision: $REPLY"
}
zcompdump_fpath="#omz fpath: $fpath"
unset -f _omz_git_head
# Delete the zcompdump file if OMZ zcompdump metadata changed
if ! command grep -q -Fx "$zcompdump_revision" "$ZSH_COMPDUMP" 2>/dev/null \