From 522fd03c124828b977b575d623aa385795a039d1 Mon Sep 17 00:00:00 2001 From: rootiest Date: Fri, 12 Jun 2026 21:38:26 -0400 Subject: [PATCH] feat(agents-init): add gitignore helper function --- functions/_agents_init_ensure_gitignore.fish | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 functions/_agents_init_ensure_gitignore.fish diff --git a/functions/_agents_init_ensure_gitignore.fish b/functions/_agents_init_ensure_gitignore.fish new file mode 100644 index 0000000..77235b5 --- /dev/null +++ b/functions/_agents_init_ensure_gitignore.fish @@ -0,0 +1,46 @@ +# Copyright (C) 2026 Rootiest +# SPDX-License-Identifier: AGPL-3.0-or-later + +# SYNOPSIS +# _agents_init_ensure_gitignore +# +# DESCRIPTION +# Appends to /.gitignore if the path it describes is not +# already covered by any existing ignore rule. Uses `git check-ignore` for +# accurate rule matching (catches wildcards and parent-dir globs). Falls +# back to a plain string search when the root is not a git repository. +# +# ARGUMENTS +# root Absolute path to the project root containing .gitignore +# pattern Path pattern to ensure is ignored (e.g. "AGENTS/", "CLAUDE.md") +# +# RETURNS +# 0 Pattern already ignored or successfully appended +# 1 Could not write to .gitignore +# +# EXAMPLE +# _agents_init_ensure_gitignore /home/user/myproject "AGENTS/" +function _agents_init_ensure_gitignore + set -l root $argv[1] + set -l pattern $argv[2] + set -l gitignore "$root/.gitignore" + + set -l c_ok (set_color green) + set -l c_reset (set_color normal) + + # Prefer git check-ignore (respects wildcards, parent globs, negations). + # --no-index lets it evaluate non-existent paths. + set -l already_ignored 0 + if test -d "$root/.git" + git -C "$root" check-ignore -q --no-index "$pattern" 2>/dev/null + and set already_ignored 1 + else if test -f "$gitignore" + grep -qF "$pattern" "$gitignore" + and set already_ignored 1 + end + + if test $already_ignored -eq 0 + echo "$pattern" >>"$gitignore" + echo "$c_ok→ Added $pattern to .gitignore$c_reset" + end +end