Skip to content

Commit 17b787f

Browse files
committed
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 <[email protected]>
1 parent b64a984 commit 17b787f

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

builtin/hash-object.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,31 @@
1010
#include "parse-options.h"
1111
#include "exec_cmd.h"
1212

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)
1414
{
1515
struct stat st;
1616
unsigned char sha1[20];
17-
unsigned flags = (HASH_FORMAT_CHECK |
18-
(write_object ? HASH_WRITE_OBJECT : 0));
1917

2018
if (fstat(fd, &st) < 0 ||
2119
index_fd(sha1, fd, &st, type_from_string(type), path, flags))
22-
die(write_object
20+
die((flags & HASH_WRITE_OBJECT)
2321
? "Unable to add %s to database"
2422
: "Unable to hash %s", path);
2523
printf("%s\n", sha1_to_hex(sha1));
2624
maybe_flush_or_die(stdout, "hash to stdout");
2725
}
2826

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)
3129
{
3230
int fd;
3331
fd = open(path, O_RDONLY);
3432
if (fd < 0)
3533
die_errno("Cannot open '%s'", path);
36-
hash_fd(fd, type, write_object, vpath);
34+
hash_fd(fd, type, vpath, flags);
3735
}
3836

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)
4038
{
4139
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
4240

@@ -47,8 +45,7 @@ static void hash_stdin_paths(const char *type, int write_objects, int no_filters
4745
die("line is badly quoted");
4846
strbuf_swap(&buf, &nbuf);
4947
}
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);
5249
}
5350
strbuf_release(&buf);
5451
strbuf_release(&nbuf);
@@ -64,12 +61,13 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
6461
const char *type = blob_type;
6562
int hashstdin = 0;
6663
int stdin_paths = 0;
67-
int write_object = 0;
6864
int no_filters = 0;
65+
unsigned flags = HASH_FORMAT_CHECK;
6966
const char *vpath = NULL;
7067
const struct option hash_object_options[] = {
7168
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),
7371
OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
7472
OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
7573
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)
8381
argc = parse_options(argc, argv, NULL, hash_object_options,
8482
hash_object_usage, 0);
8583

86-
if (write_object) {
84+
if (flags & HASH_WRITE_OBJECT) {
8785
prefix = setup_git_directory();
8886
prefix_length = prefix ? strlen(prefix) : 0;
8987
if (vpath && prefix)
@@ -113,19 +111,19 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
113111
}
114112

115113
if (hashstdin)
116-
hash_fd(0, type, write_object, vpath);
114+
hash_fd(0, type, vpath, flags);
117115

118116
for (i = 0 ; i < argc; i++) {
119117
const char *arg = argv[i];
120118

121119
if (0 <= prefix_length)
122120
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);
125123
}
126124

127125
if (stdin_paths)
128-
hash_stdin_paths(type, write_object, no_filters);
126+
hash_stdin_paths(type, no_filters, flags);
129127

130128
return 0;
131129
}

0 commit comments

Comments
 (0)