hash-object: pass 'write_object' as a flag

Instead of forcing callers of lower level functions write (write_object ? HASH_WRITE_OBJECT : 0), prepare the flag to be passed down in the callchain from the command line parser. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Sep 11, 2014 at 12:44 UTC 17b787f6031d72619e4e2982ea84f47fe161cb30
1 file changed +15 -17
builtin/hash-object.c
+15 -17
@@ -10,33 +10,31 @@
10 #include "parse-options.h"
11 #include "exec_cmd.h"
12
13 -static void hash_fd(int fd, const char *type, int write_object, const char *path)
13 +static void hash_fd(int fd, const char *type, const char *path, unsigned flags)
14 {
15 struct stat st;
16 unsigned char sha1[20];
17 - unsigned flags = (HASH_FORMAT_CHECK |
18 - (write_object ? HASH_WRITE_OBJECT : 0));
17
18 if (fstat(fd, &st) < 0 ||
19 index_fd(sha1, fd, &st, type_from_string(type), path, flags))
22 - die(write_object
20 + die((flags & HASH_WRITE_OBJECT)
21 ? "Unable to add %s to database"
22 : "Unable to hash %s", path);
23 printf("%s\n", sha1_to_hex(sha1));
24 maybe_flush_or_die(stdout, "hash to stdout");
25 }
26
29 -static void hash_object(const char *path, const char *type, int write_object,
30 - const char *vpath)
27 +static void hash_object(const char *path, const char *type, const char *vpath,
28 + unsigned flags)
29 {
30 int fd;
31 fd = open(path, O_RDONLY);
32 if (fd < 0)
33 die_errno("Cannot open '%s'", path);
36 - hash_fd(fd, type, write_object, vpath);
34 + hash_fd(fd, type, vpath, flags);
35 }
36
39 -static void hash_stdin_paths(const char *type, int write_objects, int no_filters)
37 +static void hash_stdin_paths(const char *type, int no_filters, unsigned flags)
38 {
39 struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
40
@@ -47,8 +45,7 @@ static void hash_stdin_paths(const char *type, int write_objects, int no_filters
45 die("line is badly quoted");
46 strbuf_swap(&buf, &nbuf);
47 }
50 - hash_object(buf.buf, type, write_objects,
51 - no_filters ? NULL : buf.buf);
48 + hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags);
49 }
50 strbuf_release(&buf);
51 strbuf_release(&nbuf);
@@ -64,12 +61,13 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
61 const char *type = blob_type;
62 int hashstdin = 0;
63 int stdin_paths = 0;
67 - int write_object = 0;
64 int no_filters = 0;
65 + unsigned flags = HASH_FORMAT_CHECK;
66 const char *vpath = NULL;
67 const struct option hash_object_options[] = {
68 OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
72 - OPT_BOOL('w', NULL, &write_object, N_("write the object into the object database")),
69 + OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
70 + HASH_WRITE_OBJECT),
71 OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
72 OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
73 OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")),
@@ -83,7 +81,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
81 argc = parse_options(argc, argv, NULL, hash_object_options,
82 hash_object_usage, 0);
83
86 - if (write_object) {
84 + if (flags & HASH_WRITE_OBJECT) {
85 prefix = setup_git_directory();
86 prefix_length = prefix ? strlen(prefix) : 0;
87 if (vpath && prefix)
@@ -113,19 +111,19 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
111 }
112
113 if (hashstdin)
116 - hash_fd(0, type, write_object, vpath);
114 + hash_fd(0, type, vpath, flags);
115
116 for (i = 0 ; i < argc; i++) {
117 const char *arg = argv[i];
118
119 if (0 <= prefix_length)
120 arg = prefix_filename(prefix, prefix_length, arg);
123 - hash_object(arg, type, write_object,
124 - no_filters ? NULL : vpath ? vpath : arg);
121 + hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
122 + flags);
123 }
124
125 if (stdin_paths)
128 - hash_stdin_paths(type, write_object, no_filters);
126 + hash_stdin_paths(type, no_filters, flags);
127
128 return 0;
129 }