hash-object: merge HASH_* and INDEX_* flags

The hash-object command has its own custom flag bits that it sets based on command-line options. But since we dropped hash_literally() in the previous commit, the only thing we do with those flag bits is convert them directly into "index_flags" to pass to index_fd(). This extra layer of indirection makes the code harder to read and reason about. Let's just use the INDEX_* flags directly. 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 931e5ca5079e1526045a55b60fc3134494387111
1 file changed +6 -17
builtin/hash-object.c
+6 -17
@@ -19,30 +19,19 @@
19 #include "strbuf.h"
20 #include "write-or-die.h"
21
22 -enum {
23 - HASH_OBJECT_CHECK = (1 << 0),
24 - HASH_OBJECT_WRITE = (1 << 1),
25 -};
26 -
22 static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
23 int literally)
24 {
30 - unsigned int index_flags = 0;
25 struct stat st;
26 struct object_id oid;
27
34 - if (flags & HASH_OBJECT_WRITE)
35 - index_flags |= INDEX_WRITE_OBJECT;
36 - if (flags & HASH_OBJECT_CHECK)
37 - index_flags |= INDEX_FORMAT_CHECK;
38 -
28 if (literally)
40 - index_flags &= ~INDEX_FORMAT_CHECK;
29 + flags &= ~INDEX_FORMAT_CHECK;
30
31 if (fstat(fd, &st) < 0 ||
32 index_fd(the_repository->index, &oid, fd, &st,
44 - type_from_string(type), path, index_flags))
45 - die((flags & HASH_OBJECT_WRITE)
33 + type_from_string(type), path, flags))
34 + die((flags & INDEX_WRITE_OBJECT)
35 ? "Unable to add %s to database"
36 : "Unable to hash %s", path);
37 printf("%s\n", oid_to_hex(&oid));
@@ -94,13 +83,13 @@ int cmd_hash_object(int argc,
83 int no_filters = 0;
84 int literally = 0;
85 int nongit = 0;
97 - unsigned flags = HASH_OBJECT_CHECK;
86 + unsigned flags = INDEX_FORMAT_CHECK;
87 const char *vpath = NULL;
88 char *vpath_free = NULL;
89 const struct option hash_object_options[] = {
90 OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
91 OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
103 - HASH_OBJECT_WRITE),
92 + INDEX_WRITE_OBJECT),
93 OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
94 OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
95 OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")),
@@ -114,7 +103,7 @@ int cmd_hash_object(int argc,
103 argc = parse_options(argc, argv, prefix, hash_object_options,
104 hash_object_usage, 0);
105
117 - if (flags & HASH_OBJECT_WRITE)
106 + if (flags & INDEX_WRITE_OBJECT)
107 prefix = setup_git_directory();
108 else
109 prefix = setup_git_directory_gently(&nongit);