From 312ddd4936f09ae27a180ca495cacf2c06faf33e Mon Sep 17 00:00:00 2001 From: dev Date: Sat, 14 Nov 2026 01:05:08 +0000 Subject: Comments: cut to what the code cannot say The comments had grown into essays. A file header explaining why the file exists, then a paragraph above each function restating what the next eight lines do, then a paragraph above that one explaining why that matters. The reader gets no further than the first paragraph, and the second is the one that goes stale: it describes code that has since changed, and nothing fails when it does. Cut by about half. What is left is the three things a comment can do that code cannot: say why the constraint exists (no regex because a hook must not depend on what might be missing at 3am), say what is not obvious from the shape (a bare ref name is a branch, never a tag; the seventh field of /etc/passwd is the shell), and say where it hurts (the NULL in an execvp array, the mode and owner that have to survive an atomic rewrite, the child that must not keep the hook's stdin). Block comments became // lines, which is also what the other C trees here use, and the file headers went to two or three lines. Section banners stayed: they are navigation, not prose. Behaviour is unchanged and the suites prove it: 171 direct checks, 162 behavioural, clean under -Werror and under ASan/UBSan. --- src/hook.c | 73 +++++++++++++++++++++++++++----------------------------------- 1 file changed, 32 insertions(+), 41 deletions(-) (limited to 'src/hook.c') diff --git a/src/hook.c b/src/hook.c index d581bc8..ddce87d 100644 --- a/src/hook.c +++ b/src/hook.c @@ -1,18 +1,11 @@ -/* hook.c - the mode git calls, and the only one a push depends on. - * - * This is what `core.hooksPath` points at. The shape is git's: an identity in - * the environment, one "old new ref" line per ref on stdin, and a non-zero exit - * to stop the whole push. Nothing is asked of the client and nothing is - * inferred from the transport, because ref names travel inside a compressed - * request body - the hook is the first place they exist as data. - * - * Two facts are not in the hook's input, and only the object store has them: - * whether "old -> new" is a fast-forward, and which branch HEAD points at. - * Those are asked of git, and an answer that cannot be had is taken the - * expensive way: a rewrite until proven a fast-forward, and any deletion of a - * branch treated as the default one while HEAD itself is unknown. Guessing the - * cheap way would be the hole. - */ +// What core.hooksPath points at. Nothing is inferred from the transport: ref +// names travel inside a compressed request body, and this is the first place +// they exist as data. +// +// Two facts are not in the input and only git has them - is old an ancestor of +// new, and which branch is HEAD. An answer that cannot be had is taken the +// expensive way, because guessing the cheap way is the hole. + #include "authz.h" #include @@ -24,13 +17,13 @@ #define DEV_NULL "/dev/null" -/* Run a program, optionally keeping the first line of its stdout. - * - * stdin is shut off from /dev/null: a child left holding the hook's stdin eats - * the ref lines that have not been read yet, and a push that loses refs from - * its own list is a push that skips checks. stderr goes to /dev/null too, so - * that a failed lookup does not shout "fatal:" at the person pushing about - * machinery they cannot touch. */ +// Run a program, optionally keeping the first line of its stdout. +// +// The child gets /dev/null on stdin: one left holding the hook's stdin eats the +// ref lines not yet read, and a push that loses refs from its own list is a push +// that skips checks. stderr is /dev/null too, so a failed lookup does not shout +// "fatal:" at the person pushing about machinery they cannot touch. + static int spawn(const char *const argv[], int capture, char **out) { int fds[2] = { -1, -1 }; @@ -114,9 +107,9 @@ static int is_zero(const char *s) return 1; } -/* Object ids are 40 or 64 hex digits, or all zeroes for "does not exist". - * Anything else is a protocol oddity, and an oddity gets denied rather than - * interpreted. */ +// 40 or 64 hex digits, or all zeroes. Anything else is denied rather than +// interpreted. + static int object_id_ok(const char *s) { size_t i; @@ -135,10 +128,10 @@ static int object_id_ok(const char *s) static char *head_ref(hook_opts o, int *unknown) { - /* Terminated by hand, and by name: execvp walks this array until it finds - * the NULL, and an array that runs off the end takes whatever is on the - * stack next as an argument - which is not a crash you can read, and made - * every deletion cost admin because the answer was never received. */ + // The NULL is load-bearing: execvp walks until it finds one, and an array + // that runs off the end passes stack garbage as an argument. That bug was + // silent, and every deletion cost admin. + const char *argv[5] = { "git", "symbolic-ref", "-q", "HEAD", NULL }; char *out = NULL; int rc; @@ -149,20 +142,18 @@ static char *head_ref(hook_opts o, int *unknown) if (strcmp(o.head, "auto") != 0) return xstrdup(o.head); if (!o.use_git) - return NULL; /* told not to ask, so do not guess either way */ + return NULL; // told not to ask; so do not guess either rc = spawn(argv, 1, &out); if (rc == 0 && out != NULL && out[0] != '\0') return out; free(out); - *unknown = 1; /* cannot tell: charge deletions the most */ + *unknown = 1; // cannot tell: charge deletions the most return NULL; } -/* Is old an ancestor of new? */ static int fast_forward(hook_opts o, const char *old, const char *new) { - /* Same as head_ref: the NULL is part of the contract, not padding. */ const char *argv[6] = { "git", "merge-base", "--is-ancestor", NULL, NULL, NULL }; int rc; @@ -202,7 +193,7 @@ static char *derive_repo(hook_opts o) root = "/var/lib/git"; rootlen = strlen(root); while (rootlen > 1 && root[rootlen - 1] == '/') - rootlen--; /* "/var/lib/git/" is "/var/lib/git" */ + rootlen--; // "/var/lib/git/" is "/var/lib/git" if (strncmp(cwd, root, rootlen) == 0 && cwd[rootlen] == '/') name = cwd + rootlen + 1; @@ -300,9 +291,9 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) level_t need; decision_t d; - /* Split on whitespace by hand: the object ids and refnames in this line - * never contain it, and it keeps the reader's index the same as the - * writer's. */ + // Split by hand: object ids and refnames never contain whitespace, and + // this keeps the reader's index the same as the writer's. + { char *q = line; @@ -347,14 +338,14 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) } if (is_zero(new)) { - /* A deletion. Branches cost force; the one HEAD points at costs - * an admin, because a repository without a default branch is a - * repository whose clones check out nothing. */ + // The branch HEAD points at costs admin: a repository whose + // clones check out nothing is a broken repository. + need = LV_FORCE; if (head_unknown || (head != NULL && strcmp(ref, head) == 0)) need = LV_ADMIN; } else if (is_zero(old)) { - need = LV_WRITE; /* a creation */ + need = LV_WRITE; // a creation } else { need = fast_forward(o, old, new) ? LV_WRITE : LV_FORCE; } -- cgit v1.2.3