Artifact 5387283d5e402ba033a6b0009d58095ca33279fb9e4e1396f1628af12f799cd3:
- File
src/attention.c
— part of check-in
[3cb21b0428]
at
2026-11-16 08:11:34
on branch trunk
— Phase 8c-1: an upstream is taken from a folder, reviewed, and merged
suit upstream NAME take FOLDER checks the upstream's files in on the branch upstream-NAME of the wardrobe that defines it, at INTO/ENTRY, with the comment upstream NAME at PIN. The check-in is built as a deck on a repository opened with no checkout, so the working copy is not touched. Its first parent is trunk's first check-in, so a later merge is three-way. Dot entries are skipped, links are refused, the lint runs, and a file that something else supplies is a clash. into is a new key, default skills; trust defaults to review.
With trust = review the update waits: the attention file says so, suit upstreams lists each upstream as never taken, current, waiting for review or conflict, and suit upstream NAME diff lists what accept would add, change, remove or conflict on. suit upstream NAME accept merges the branch into trunk after a dry run. A conflict changes no file and writes no marker; --mine PATH and --theirs PATH settle one. With trust = auto a clean take is accepted at once.
Every accept writes the tracked file upstreams/NAME, holding the hash and pin it accepted, into its merge check-in. So a merge that changes no other file can still be checked in, and keeping your version of the only conflict ends as current.
The proof: a local fix to a taken file survives an update that changes another line, and an update that rewrites the same line waits as a conflict with trunk byte-identical to before.
Limits, all in docs/upstreams.md. suit back after an accept refuses, as after any check-in, and no command undoes an accept. diff prints the list of files, not their text. After --mine, a later take that touches the same lines can conflict again, because the library leaves the merge parent off that check-in. Accept works only at trunk's tip. A wardrobe and an upstream of one name share an attention item. Fetching over the network is part two and is not built.
1473 unit checks (was 1431), 620 shell cases (was 612), 968 store cases (was 669), 846 placement cases and 378 project cases, under clang, under gcc 16, with no user name set, and under a strict umask, in one run of make gate (34 minutes). 100% coverage in all 26 source files. The speed budget still passes. No existing case was changed. (user: dmestas, size: 7352) [annotate] [blame] [check-ins using]
#include "attention.h" #include "platform.h" #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <unistd.h> static char cur[ATTENTION_FILE_MAX + 1]; static char out[ATTENTION_FILE_MAX + 1]; // Read the file into cur. A file that is not there is empty. static int load(const char *path, size_t *len) { *len = 0; int fd = sys_open(path, O_RDONLY, 0); if (fd < 0 && errno == ENOENT) return 0; if (fd < 0) { say(STDERR_FILENO, "suit: cannot open %s: %s\n", path, strerror(errno)); return 1; } ssize_t n = 1; while (*len <= ATTENTION_FILE_MAX && n > 0) { n = sys_read(fd, cur + *len, ATTENTION_FILE_MAX + 1 - *len); if (n < 0 && errno == EINTR) { n = 1; continue; } *len += n > 0 ? (size_t)n : 0; } int err = errno; (void)sys_close(fd); if (n < 0) { say(STDERR_FILENO, "suit: cannot read %s: %s\n", path, strerror(err)); return 1; } if (*len > ATTENTION_FILE_MAX) { say(STDERR_FILENO, "suit: %s is over %zu bytes\n", path, ATTENTION_FILE_MAX); return 1; } return 0; } // Whether the header line LINE ("KIND NAME" or "KIND NAME: ...") is the item // of that kind (any if KIND is null) for NAME. static bool item_is(const char *line, size_t n, const char *kind, const char *name) { const char *sp = memchr(line, ' ', n); if (sp == nullptr) return false; size_t kl = (size_t)(sp - line); if (kind != nullptr && (strlen(kind) != kl || memcmp(line, kind, kl) != 0)) return false; const char *w = sp + 1; size_t room = n - kl - 1; size_t wl = 0; while (wl < room && w[wl] != ':') wl++; return wl == strlen(name) && memcmp(w, name, wl) == 0; } // Copy cur to out without the items of KIND and NAME. Returns the length of // out, or SIZE_MAX... cannot overflow: out is as big as cur. static size_t filter(size_t len, const char *kind, const char *name) { size_t o = 0; size_t p = 0; bool keep = true; while (p < len) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; if (cur[p] != ' ') keep = !item_is(cur + p, end - p, kind, name); if (keep) { memcpy(out + o, cur + p, end - p); o += end - p; out[o++] = '\n'; } p = end + 1; } return o; } // Write OUT, or remove the file when OUT is empty. CHANGED says whether OUT // differs from what was read. static int store_out(const char *path, size_t o, size_t len) { if (o == len && memcmp(out, cur, o) == 0) return 0; if (o == 0) { if (sys_unlink(path) != 0 && errno != ENOENT) { say(STDERR_FILENO, "suit: cannot remove %s: %s\n", path, strerror(errno)); return 1; } return 0; } int err = sys_write_atomic(path, out, o); if (err != 0) say(STDERR_FILENO, "suit: cannot write %s: %s\n", path, strerror(err)); return err == 0 ? 0 : 1; } int attention_remove(const char *path, const char *kind, const char *name) { size_t len; if (load(path, &len) != 0) return 1; return store_out(path, filter(len, kind, name), len); } int attention_add(const char *path, const char *kind, const char *name, const char *header, const char *details) { size_t len; if (load(path, &len) != 0) return 1; size_t o = filter(len, kind, name); size_t more = strlen(header) + 1 + (details != nullptr ? strlen(details) * 3 : 0); if (o + more > ATTENTION_FILE_MAX) { say(STDERR_FILENO, "suit: %s would be over %zu bytes\n", path, ATTENTION_FILE_MAX); return 1; } o += (size_t)snprintf(out + o, sizeof out - o, "%s\n", header); for (const char *d = details; d != nullptr && *d != '\0';) { size_t n = strcspn(d, "\n"); o += (size_t)snprintf(out + o, sizeof out - o, " %.*s\n", (int)n, d); ensure(d[n] == '\n'); d += n + 1; } return store_out(path, o, len); } int attention_sync(const char *path, const char *kind, size_t n, const char *const *names, const char *const *headers) { ensure(n <= ATTENTION_SYNC_MAX); size_t len; if (load(path, &len) != 0) return 1; bool seen[ATTENTION_SYNC_MAX] = {}; size_t o = 0; bool keep = true; for (size_t p = 0; p < len;) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; if (cur[p] != ' ') { keep = true; size_t kl = strlen(kind); if (end - p > kl && memcmp(cur + p, kind, kl) == 0 && cur[p + kl] == ' ') { size_t want = 0; while (want < n && !(item_is(cur + p, end - p, kind, names[want]) && strlen(headers[want]) == end - p && memcmp(headers[want], cur + p, end - p) == 0)) want++; keep = want < n && !seen[want]; if (keep) seen[want] = true; } } if (keep) { memcpy(out + o, cur + p, end - p); o += end - p; out[o++] = '\n'; } p = end + 1; } for (size_t i = 0; i < n; i++) { if (seen[i]) continue; if (o + strlen(headers[i]) + 1 > ATTENTION_FILE_MAX) { say(STDERR_FILENO, "suit: %s would be over %zu bytes\n", path, ATTENTION_FILE_MAX); return 1; } o += (size_t)snprintf(out + o, sizeof out - o, "%s\n", headers[i]); } return store_out(path, o, len); } int attention_print(const char *path) { size_t len; if (load(path, &len) != 0) return 1; if (len == 0) return 0; say(STDOUT_FILENO, "needs you:\n"); for (size_t p = 0; p < len;) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; say(STDOUT_FILENO, "%.*s\n", (int)(end - p), cur + p); p = end + 1; } return 0; } int attention_count(const char *path, unsigned *count) { size_t len; *count = 0; if (load(path, &len) != 0) return 1; for (size_t p = 0; p < len;) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; if (end > p && cur[p] != ' ') (*count)++; p = end + 1; } return 0; } int attention_has(const char *path, const char *kind, const char *name, bool *has) { size_t len; *has = false; if (load(path, &len) != 0) return 1; for (size_t p = 0; p < len && !*has; p++) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; *has = cur[p] != ' ' && item_is(cur + p, end - p, kind, name); p = end; } return 0; } int attention_header(const char *path, const char *kind, const char *name, char *header, size_t cap) { size_t len; header[0] = '\0'; if (load(path, &len) != 0) return 1; for (size_t p = 0; p < len;) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; if (cur[p] != ' ' && item_is(cur + p, end - p, kind, name)) snprintf(header, cap, "%.*s", (int)(end - p), cur + p); p = end + 1; } return 0; }