fix(agents-init): fix gitignore duplicate entries, add block comment style

- Strip leading / from pattern before git check-ignore so root-anchored
  patterns like /AGENTS.md are checked as repo-relative paths (AGENTS.md)
  and correctly match existing rules — fixes duplicates on every run
- Rework helper to accept multiple patterns and write them as a single
  labeled block with project-style header/footer comment rather than
  appending bare patterns one at a time
- Move gitignore calls out of per-plug loop in --plugins mode so all
  three docs/ patterns are checked and written in one block
- Remove 5 duplicate bare entries left in .gitignore by the broken runs
This commit is contained in:
2026-06-12 23:30:12 -04:00
parent 3a054021cf
commit 9f9248deb1
2 changed files with 58 additions and 33 deletions
+56 -29
View File
@@ -2,54 +2,81 @@
# SPDX-License-Identifier: AGPL-3.0-or-later # SPDX-License-Identifier: AGPL-3.0-or-later
# SYNOPSIS # SYNOPSIS
# _agents_init_ensure_gitignore <root> <pattern> # _agents_init_ensure_gitignore <root> <label> <pattern>...
# #
# DESCRIPTION # DESCRIPTION
# Appends <pattern> to <root>/.gitignore if the path it describes is not # Appends any patterns not already covered by the project's .gitignore.
# already covered by any existing ignore rule. Uses `git check-ignore` for # Uses `git check-ignore` for accurate rule matching (catches wildcards
# accurate rule matching (catches wildcards and parent-dir globs). Falls # and parent-dir globs). Falls back to a plain string search when the
# back to a plain string search when the root is not a git repository. # root is not a git repository. Leading `/` is stripped from each pattern
# before the path-based check so root-anchored patterns (e.g. /AGENTS.md)
# are matched correctly.
#
# Missing patterns are written as a single labeled block:
#
# # ──────────────── Added by agents-init ──────────────────
# # <label>
# <pattern>
# # ────────────────────────────────────────────────────────
# #
# ARGUMENTS # ARGUMENTS
# root Absolute path to the project root containing .gitignore # root Absolute path to the project root containing .gitignore
# pattern Path pattern to ensure is ignored (e.g. "AGENTS/", "CLAUDE.md") # label Short description used in the block comment header
# pattern One or more gitignore patterns to ensure are present
# #
# RETURNS # RETURNS
# 0 Pattern already ignored or successfully appended # 0 All patterns already ignored or successfully appended
# 1 Could not write to .gitignore # 1 Could not write to .gitignore
# #
# EXAMPLE # EXAMPLE
# _agents_init_ensure_gitignore /home/user/myproject "AGENTS/" # _agents_init_ensure_gitignore /home/user/myproject "agents-init" "AGENTS/" "/AGENTS.md"
function _agents_init_ensure_gitignore function _agents_init_ensure_gitignore
set -l c_ok (set_color green) set -l c_ok (set_color green)
set -l c_reset (set_color normal) set -l c_reset (set_color normal)
if test (count $argv) -lt 2 if test (count $argv) -lt 3
echo (set_color red)"_agents_init_ensure_gitignore: requires <root> and <pattern>"(set_color normal) >&2 echo (set_color red)"_agents_init_ensure_gitignore: requires <root> <label> <pattern>..."(set_color normal) >&2
return 1 return 1
end end
set -l root $argv[1] set -l root $argv[1]
set -l pattern $argv[2] set -l label $argv[2]
set -l patterns $argv[3..]
set -l gitignore "$root/.gitignore" set -l gitignore "$root/.gitignore"
# Prefer git check-ignore (respects wildcards, parent globs, negations). set -l in_git 0
# --no-index lets it evaluate non-existent paths. git -C "$root" rev-parse --git-dir >/dev/null 2>&1
set -l already_ignored 0 and set in_git 1
if git -C "$root" rev-parse --git-dir >/dev/null 2>&1
git -C "$root" check-ignore -q --no-index "$pattern" 2>/dev/null # Collect patterns not already covered
and set already_ignored 1 set -l missing
else if test -f "$gitignore" for pattern in $patterns
grep -qF "$pattern" "$gitignore" # Strip leading / so git check-ignore receives a repo-relative path,
and set already_ignored 1 # not an absolute filesystem path (which it cannot match against rules).
set -l check_path (string replace -r '^/' '' "$pattern")
set -l already 0
if test $in_git -eq 1
git -C "$root" check-ignore -q --no-index "$check_path" 2>/dev/null
and set already 1
else if test -f "$gitignore"
grep -qF "$pattern" "$gitignore"
and set already 1
end
if test $already -eq 0
set -a missing "$pattern"
end
end end
if test $already_ignored -eq 0 test (count $missing) -eq 0; and return 0
if not echo "$pattern" >>"$gitignore"
echo (set_color red)"Error: could not write to $gitignore"(set_color normal) >&2 # Write missing patterns as a labeled block
return 1 set -l header "# ──────────────── Added by agents-init ──────────────────"
end set -l footer "# ────────────────────────────────────────────────────────"
echo "$c_ok→ Added $pattern to .gitignore$c_reset" printf '\n%s\n# %s\n' "$header" "$label" >>"$gitignore"
for p in $missing
printf '%s\n' "$p" >>"$gitignore"
end end
printf '%s\n' "$footer" >>"$gitignore"
echo "$c_ok→ Added "(count $missing)" pattern(s) to .gitignore$c_reset"
end end
+2 -4
View File
@@ -208,9 +208,7 @@ function agents-init --description 'scaffold AGENTS/ sub-repo with agent spec fi
end end
# ── .gitignore ──────────────────────────────────────────────────────── # ── .gitignore ────────────────────────────────────────────────────────
for pattern in "AGENTS/" "/AGENTS.md" "/CLAUDE.md" _agents_init_ensure_gitignore "$root" "agents-init --agents" "AGENTS/" "/AGENTS.md" "/CLAUDE.md"
_agents_init_ensure_gitignore "$root" "$pattern"
end
end end
# ─────────────────────────── --plugins mode ────────────────────────────── # ─────────────────────────── --plugins mode ──────────────────────────────
@@ -266,8 +264,8 @@ function agents-init --description 'scaffold AGENTS/ sub-repo with agent spec fi
echo "$c_ok→ Linked docs/$plug → AGENTS/plugins/$plug$c_reset" echo "$c_ok→ Linked docs/$plug → AGENTS/plugins/$plug$c_reset"
end end
_agents_init_ensure_gitignore "$root" "docs/$plug"
end end
_agents_init_ensure_gitignore "$root" "agents-init --plugins" "docs/superpowers" "docs/plans" "docs/specs"
end end
# ──────────────────────── Auto-commit AGENTS/ ──────────────────────────── # ──────────────────────── Auto-commit AGENTS/ ────────────────────────────