hash-object: handle --literally with OPT_NEGBIT

Since we recently removed the hash_literally() function, the hash-object --literally option has been simplified to just removing the INDEX_FORMAT_CHECK flag. Rather than pass it around as a separate bool, we can just have the option parser remove the bit from the set of flags directly. This simplifies the helper functions. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 16, 2025 at 00:50 UTC f710fd7b49218ce3407a88b2c548704299c7c664
1 file changed +11 -16
builtin/hash-object.c
+11 -16
@@ -19,15 +19,11 @@
19 #include "strbuf.h"
20 #include "write-or-die.h"
21
22 -static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
23 - int literally)
22 +static void hash_fd(int fd, const char *type, const char *path, unsigned flags)
23 {
24 struct stat st;
25 struct object_id oid;
26
28 - if (literally)
29 - flags &= ~INDEX_FORMAT_CHECK;
30 -
27 if (fstat(fd, &st) < 0 ||
28 index_fd(the_repository->index, &oid, fd, &st,
29 type_from_string(type), path, flags))
@@ -39,15 +35,14 @@ static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
35 }
36
37 static void hash_object(const char *path, const char *type, const char *vpath,
42 - unsigned flags, int literally)
38 + unsigned flags)
39 {
40 int fd;
41 fd = xopen(path, O_RDONLY);
46 - hash_fd(fd, type, vpath, flags, literally);
42 + hash_fd(fd, type, vpath, flags);
43 }
44
49 -static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
50 - int literally)
45 +static void hash_stdin_paths(const char *type, int no_filters, unsigned flags)
46 {
47 struct strbuf buf = STRBUF_INIT;
48 struct strbuf unquoted = STRBUF_INIT;
@@ -59,8 +54,7 @@ static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
54 die("line is badly quoted");
55 strbuf_swap(&buf, &unquoted);
56 }
62 - hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
63 - literally);
57 + hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags);
58 }
59 strbuf_release(&buf);
60 strbuf_release(&unquoted);
@@ -81,7 +75,6 @@ int cmd_hash_object(int argc,
75 int hashstdin = 0;
76 int stdin_paths = 0;
77 int no_filters = 0;
84 - int literally = 0;
78 int nongit = 0;
79 unsigned flags = INDEX_FORMAT_CHECK;
80 const char *vpath = NULL;
@@ -93,7 +86,9 @@ int cmd_hash_object(int argc,
86 OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
87 OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
88 OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")),
96 - OPT_BOOL( 0, "literally", &literally, N_("just hash any random garbage to create corrupt objects for debugging Git")),
89 + OPT_NEGBIT( 0, "literally", &flags,
90 + N_("just hash any random garbage to create corrupt objects for debugging Git"),
91 + INDEX_FORMAT_CHECK),
92 OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")),
93 OPT_END()
94 };
@@ -139,7 +134,7 @@ int cmd_hash_object(int argc,
134 }
135
136 if (hashstdin)
142 - hash_fd(0, type, vpath, flags, literally);
137 + hash_fd(0, type, vpath, flags);
138
139 for (i = 0 ; i < argc; i++) {
140 const char *arg = argv[i];
@@ -148,12 +143,12 @@ int cmd_hash_object(int argc,
143 if (prefix)
144 arg = to_free = prefix_filename(prefix, arg);
145 hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
151 - flags, literally);
146 + flags);
147 free(to_free);
148 }
149
150 if (stdin_paths)
156 - hash_stdin_paths(type, no_filters, flags, literally);
151 + hash_stdin_paths(type, no_filters, flags);
152
153 free(vpath_free);
154