Skip to content
pi-c Artifact [7fab19c353]
Login

Artifact [7fab19c353]

Artifact 7fab19c35323ef9bc23c9a8b815ce243ff10dcea5b6c7961f7c352b3a1000b1d:


/* Ignore rules for the skill and @file walks (also Fossil globs, below).
 * Gitignore-style patterns for skill discovery, as Pi's skills.ts feeds them
 * to the `ignore` library: .gitignore, .ignore and .fdignore found while
 * walking a skills folder, each pattern prefixed by its folder's path from
 * the walk's root. Globs (* ? [a-z]), **, a trailing / (folders only), !
 * negation, # comments and \ escapes; the last matching rule wins, and
 * nothing under an ignored folder is reachable. */
#ifndef PIC_IGNORE_H
#define PIC_IGNORE_H

#include <stddef.h>

typedef struct {
    char *pat;
    int negated, dir_only, anchored;
    int fossil;   /* Fossil glob: SQLite GLOB, any match ignores, no negation */
    size_t plen;  /* fossil only: length of the literal folder prefix in pat */
} pic_ignore_rule;

typedef struct {
    pic_ignore_rule *v;
    size_t n, cap;
} pic_ignore;

/* Adds the lines of TEXT, each prefixed by PREFIX ("" or "sub/dir/").
 * 0, or -1 when out of memory. */
int pic_ignore_add(pic_ignore *ig, const char *text, size_t len, const char *prefix);

/* Fossil's dialect (.fslignore, ignore-glob settings): patterns separated by
 * commas and/or white space and/or newlines, "double quotes" holding spaces
 * or commas. Each is a SQLite GLOB (sqlite3_strglob: '*' crosses '/') matched
 * against the path relative to the folder PREFIX names; a trailing '/' makes
 * it folders-only; a pattern naming a folder ignores what is under it. */
int pic_ignore_add_glob(pic_ignore *ig, const char *text, size_t len, const char *prefix);

/* The per-folder ignore files, in the order they are read; .fslignore is the
 * Fossil dialect, the rest gitignore. pic_ignore_add_named picks by NAME. */
#define PIC_IGNORE_NAMES 5
extern const char *const pic_ignore_names[PIC_IGNORE_NAMES];
int pic_ignore_add_named(pic_ignore *ig, const char *name, const char *text, size_t len, const char *prefix);

/* Fossil's versioned settings, read at the walk root only (Fossil dialect). */
#define PIC_IGNORE_SETTINGS 2
extern const char *const pic_ignore_settings[PIC_IGNORE_SETTINGS];

/* Reads every ignore file in DIR (REL is DIR's path from the walk's root;
 * ROOT also reads the settings files) through pic_input_read. 0, or -1 when out of memory. */
#define PIC_IGNORE_MAX (1024 * 1024)
int pic_ignore_load_dir(pic_ignore *ig, const char *dir, const char *rel, int root);

/* Forgets the rules added after the first N (leaving a folder). */
void pic_ignore_truncate(pic_ignore *ig, size_t n);
void pic_ignore_free(pic_ignore *ig);

/* 1 when REL (slash separated, relative to the walk's root) is ignored. */
int pic_ignore_ignored(const pic_ignore *ig, const char *rel, int isdir);

/* pic's own ignore file, the only one search honours: .picignore, gitignore
 * syntax, in any folder, scoped to it. Nothing is excluded unless one says so;
 * the VCS ignore files above are not read for search (they keep files out of
 * commits, not out of what an agent can look at). pic_agent_ignore_add adds
 * the lines of a .picignore found in folder REL ("" for the walk's root);
 * pic_agent_ignore_load_dir reads DIR/.picignore itself; pic_agent_excluded
 * answers whether REL is excluded for agents. Grep, find and list_files use
 * them, and so will full-text search. */
#define PIC_AGENT_IGNORE ".picignore"
int pic_agent_ignore_add(pic_ignore *ig, const char *text, size_t len, const char *rel_dir);
int pic_agent_ignore_load_dir(pic_ignore *ig, const char *dir, const char *rel_dir);
int pic_agent_excluded(const pic_ignore *ig, const char *rel, int isdir);

/* One entry of a walk: its path from the walk's base, whether it is a folder,
 * and its depth (1 for the base's own entries). */
typedef struct {
    char *path;
    int is_dir;
    int depth;
} pic_ignore_entry;

/* Lists BASE breadth-first (so a cap keeps the shallow entries): at most
 * MAX_ENTRIES entries, MAX_DEPTH folders deep, following symlinks, skipping
 * version-control metadata (.git, node_modules, Fossil and zit files) and what
 * a .picignore excludes (pic_agent_excluded). *OUT is malloc'd (pic_ignore_entries_free); the count. */
size_t pic_ignore_walk(const char *base, size_t max_entries, int max_depth, pic_ignore_entry **out);
void pic_ignore_entries_free(pic_ignore_entry *v, size_t n);

#endif