patch-id: make get_one_patchid() more extensible

We pass two independent Boolean flags (i.e. do we want the stable variant of patch-id? do we want to hash the stuff verbatim?) into the function as two separate parameters. Before adding the third one and make the interface even wider, let's consolidate them into a single flag word. No changes in behaviour. Just a trivial interface change. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 29, 2024 at 18:17 UTC 2438294a13dc3f04684bcfb1af75008b3ca07f86
1 file changed +20 -5
builtin/patch-id.c
+20 -5
@@ -58,9 +58,19 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
58 return 1;
59 }
60
61 +/*
62 + * flag bits to control get_one_patchid()'s behaviour.
63 + */
64 +enum {
65 + GOPID_STABLE = (1<<0), /* --stable */
66 + GOPID_VERBATIM = (1<<1), /* --verbatim */
67 +};
68 +
69 static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
62 - struct strbuf *line_buf, int stable, int verbatim)
70 + struct strbuf *line_buf, unsigned flags)
71 {
72 + int stable = flags & GOPID_STABLE;
73 + int verbatim = flags & GOPID_VERBATIM;
74 int patchlen = 0, found_next = 0;
75 int before = -1, after = -1;
76 int diff_is_binary = 0;
@@ -171,7 +181,7 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
181 return patchlen;
182 }
183
174 -static void generate_id_list(int stable, int verbatim)
184 +static void generate_id_list(unsigned flags)
185 {
186 struct object_id oid, n, result;
187 int patchlen;
@@ -179,7 +189,7 @@ static void generate_id_list(int stable, int verbatim)
189
190 oidclr(&oid);
191 while (!feof(stdin)) {
182 - patchlen = get_one_patchid(&n, &result, &line_buf, stable, verbatim);
192 + patchlen = get_one_patchid(&n, &result, &line_buf, flags);
193 if (patchlen)
194 flush_current_id(&oid, &result);
195 oidcpy(&oid, &n);
@@ -218,6 +228,7 @@ int cmd_patch_id(int argc, const char **argv, const char *prefix)
228 /* if nothing is set, default to unstable */
229 struct patch_id_opts config = {0, 0};
230 int opts = 0;
231 + unsigned flags = 0;
232 struct option builtin_patch_id_options[] = {
233 OPT_CMDMODE(0, "unstable", &opts,
234 N_("use the unstable patch-id algorithm"), 1),
@@ -237,7 +248,11 @@ int cmd_patch_id(int argc, const char **argv, const char *prefix)
248 argc = parse_options(argc, argv, prefix, builtin_patch_id_options,
249 patch_id_usage, 0);
250
240 - generate_id_list(opts ? opts > 1 : config.stable,
241 - opts ? opts == 3 : config.verbatim);
251 + if (opts ? opts > 1 : config.stable)
252 + flags |= GOPID_STABLE;
253 + if (opts ? opts == 3 : config.verbatim)
254 + flags |= GOPID_VERBATIM;
255 + generate_id_list(flags);
256 +
257 return 0;
258 }