From 12dcb09794c48a333bb850d353540c688c503b76 Mon Sep 17 00:00:00 2001 From: dev Date: Sat, 14 Nov 2026 17:42:44 +0000 Subject: Options: getopt_long instead of three hand-rolled parsers option() in hook.c and one_option() in cmd/main.c were the same function under two names, and validate's seven options were a chain of seven calls to one of them. getopt_long does all of it - both --opt value and --opt=value, the lot - so the parsers are gone and what is left is a table and what each entry sets. Two small helpers carry what getopt_long cannot: the name behind a long option's val, for the 'wants a value' message, and the optind reset that lets a run parse options twice on two different argv slices. A command now gets argv[0] as its own name, which is where getopt_long expects argv[0]; the commands that take names step over it. strip() in validate.c was authz_trim() again, and cmd_authorize freed the same three pointers on four exit paths. One behaviour change: 'authorize --' is now the end-of-options marker rather than an unknown argument. Anything after it is still refused. --- src/authz.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src/authz.h') diff --git a/src/authz.h b/src/authz.h index 671b45f..8e6c2bb 100644 --- a/src/authz.h +++ b/src/authz.h @@ -7,9 +7,10 @@ #ifndef AUTHZ_H #define AUTHZ_H +#include #include -#define MAX_FIELDS 64 // one rule line, split on whitespace +#define MAX_FIELDS 64 // one rule line, split on whitespace // one rule line, split on whitespace typedef enum { LV_NONE = 0, @@ -172,4 +173,15 @@ char *xsubstr(const char *s, size_t len); // 'pre-receive' when invoked through the hook symlink, 'git-authz' otherwise. extern const char *progname; +// getopt_long hands back an option's val, not the name that was typed, and for +// a long-only option the val is a number nobody wrote down. The table is the +// only place that still knows the pair. + +const char *authz_optname(int val, const struct option *lo); + +// A run parses options twice - the global ones, then the command's own - each on +// its own argv slice, so getopt_long has to be told to start again. + +void authz_opt_reset(void); + #endif /* AUTHZ_H */ -- cgit v1.2.3