#include #include #include #include #include #include #include #include #include #include #include "folder.h" #include "ignore.h" #include "input.h" #include "platform.h" static int fld_fs(void) { return pic_platform_get() == pic_platform_host(); } int pic_folder_seal(const char *dir) { char path[PATH_MAX]; int fd, ok; if (!fld_fs() || snprintf(path, sizeof path, "%s/.gitignore", dir) >= (int)sizeof path) { return 0; } if ((fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0644)) < 0) { return 0; } ok = write(fd, "*\n", 2) == 2; close(fd); return ok; } /* ---- the checkout ------------------------------------------------------------------------- */ typedef struct { char root[PATH_MAX]; const char *tool; /* "zit" or "fossil": what to ask for the settings it keeps outside the tree */ } fld_checkout; static int fld_file(const char *dir, const char *name) { char path[PATH_MAX]; struct stat sb; return snprintf(path, sizeof path, "%s/%s", dir, name) < (int)sizeof path && stat(path, &sb) == 0; } /* The checkout DIR is in: its root, from the folder up. */ static int fld_find(const char *dir, fld_checkout *c) { char cur[PATH_MAX]; if (!realpath(dir, cur)) { return 0; } for (;;) { char *slash; if (fld_file(cur, ".zit-checkout") || fld_file(cur, ".fslckout") || fld_file(cur, "_FOSSIL_")) { c->tool = fld_file(cur, ".zit-checkout") ? "zit" : "fossil"; (void)snprintf(c->root, sizeof c->root, "%s", cur); return 1; } if (!(slash = strrchr(cur, '/')) || slash == cur) { return 0; } *slash = '\0'; } } /* What `TOOL settings NAME` says in the checkout, run there: the repository and global tiers, which * are not files in the tree. NULL when the tool is missing or fails. */ static char *fld_tool(const fld_checkout *c, const char *name) { int fds[2]; pid_t pid; char buf[4096], *s; size_t n = 0; int zit = strcmp(c->tool, "zit") == 0, status; if (pipe(fds) != 0) { return NULL; } if ((pid = fork()) < 0) { close(fds[0]); close(fds[1]); return NULL; } if (pid == 0) { int nul = open("/dev/null", O_RDWR); if (nul >= 0) { (void)dup2(nul, 0); (void)dup2(nul, 2); } (void)dup2(fds[1], 1); if (chdir(c->root) == 0) { if (zit) { execlp("zit", "zit", "settings", "--value", name, (char *)NULL); } else { execlp("fossil", "fossil", "settings", name, (char *)NULL); } } _exit(127); } close(fds[1]); for (ssize_t r; n < sizeof buf - 1 && (r = read(fds[0], buf + n, sizeof buf - 1 - n)) > 0;) { n += (size_t)r; } close(fds[0]); (void)waitpid(pid, &status, 0); if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { return NULL; } buf[n] = '\0'; s = buf; if (!zit) { /* "NAME (local) VALUE": the name and its tier word are not the value */ s += strlen(name) * (strncmp(s, name, strlen(name)) == 0); while (*s == ' ' || *s == '\t') { s++; } if (*s == '(' && (s = strchr(s, ')'))) { s++; } } while (s && isspace((unsigned char)*s)) { s++; } while (s && n && (buf[n - 1] == '\n' || buf[n - 1] == ' ')) { buf[--n] = '\0'; } return s ? strdup(s) : NULL; } static int fld_truthy(const char *s) { return s && (strncasecmp(s, "on", 2) == 0 || strncasecmp(s, "yes", 3) == 0 || strncasecmp(s, "true", 4) == 0 || *s == '1'); } /* The versioned file .fossil-settings/NAME, else .zit-settings/NAME, in the root (Fossil's wins). */ static char *fld_versioned(const fld_checkout *c, const char *name) { static const char *const dirs[2] = {".fossil-settings", ".zit-settings"}; for (int i = 0; i < 2; i++) { char path[PATH_MAX]; char *t; size_t len; if (snprintf(path, sizeof path, "%s/%s/%s", c->root, dirs[i], name) < (int)sizeof path && (t = pic_input_read(path, PIC_IGNORE_MAX, 0, &len, NULL))) { return t; } } return NULL; } /* The setting in force: a versioned file, else what the tool reports. */ static char *fld_setting(const fld_checkout *c, const char *name) { char *v = fld_versioned(c, name); return v ? v : fld_tool(c, name); } static int fld_covered(const fld_checkout *c, const char *rel) { pic_ignore ig = {0}; char path[PATH_MAX], *t; size_t len; int hit; static const char *const files[2] = {".zitignore", ".fslignore"}; for (int i = 0; i < 2; i++) { if (snprintf(path, sizeof path, "%s/%s", c->root, files[i]) < (int)sizeof path && (t = pic_input_read(path, PIC_IGNORE_MAX, 0, &len, NULL))) { (void)pic_ignore_add_named(&ig, files[i], t, len, ""); free(t); } } if ((t = fld_setting(c, "ignore-glob"))) { (void)pic_ignore_add_glob(&ig, t, strlen(t), ""); free(t); } hit = pic_ignore_ignored(&ig, rel, 1); if (!hit && snprintf(path, sizeof path, "%s/journal.sqlite", rel) < (int)sizeof path) { hit = pic_ignore_ignored(&ig, path, 0); } pic_ignore_free(&ig); return hit; } int pic_folder_advice(const char *dir, char *out, size_t cap) { fld_checkout c; char real[PATH_MAX], rel[PATH_MAX], *v; int on; if (!fld_fs() || !fld_find(dir, &c) || !realpath(dir, real) || strncmp(real, c.root, strlen(c.root)) != 0 || real[strlen(c.root)] != '/') { return 0; } (void)snprintf(rel, sizeof rel, "%s", real + strlen(c.root) + 1); v = fld_setting(&c, "dotfiles"); on = fld_truthy(v); free(v); if (!on || fld_covered(&c, rel)) { return 0; } (void)snprintf(out, cap, "this folder is a %s checkout with the dotfiles setting on, so %s will list %s/ as new files to " "add; %s reads only project-level ignore rules, not .gitignore. Add the line %s/* to " ".%s-settings/ignore-glob", c.tool, c.tool, rel, c.tool, rel, c.tool); return 1; }