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