1919#include "strbuf.h"
2020#include "write-or-die.h"
2121
22- enum {
23- HASH_OBJECT_CHECK = (1 << 0 ),
24- HASH_OBJECT_WRITE = (1 << 1 ),
25- };
26-
27- /*
28- * This is to create corrupt objects for debugging and as such it
29- * needs to bypass the data conversion performed by, and the type
30- * limitation imposed by, index_fd() and its callees.
31- */
32- static int hash_literally (struct object_id * oid , int fd , const char * type , unsigned flags )
22+ static void hash_fd (int fd , const char * type , const char * path , unsigned flags )
3323{
34- struct strbuf buf = STRBUF_INIT ;
35- int ret ;
36-
37- if (strbuf_read (& buf , fd , 4096 ) < 0 )
38- ret = -1 ;
39- else
40- ret = write_object_file_literally (buf .buf , buf .len , type , oid ,
41- (flags & HASH_OBJECT_WRITE ) ? WRITE_OBJECT_FILE_PERSIST : 0 );
42- close (fd );
43- strbuf_release (& buf );
44- return ret ;
45- }
46-
47- static void hash_fd (int fd , const char * type , const char * path , unsigned flags ,
48- int literally )
49- {
50- unsigned int index_flags = 0 ;
5124 struct stat st ;
5225 struct object_id oid ;
5326
54- if (flags & HASH_OBJECT_WRITE )
55- index_flags |= INDEX_WRITE_OBJECT ;
56- if (flags & HASH_OBJECT_CHECK )
57- index_flags |= INDEX_FORMAT_CHECK ;
58-
5927 if (fstat (fd , & st ) < 0 ||
60- (literally
61- ? hash_literally (& oid , fd , type , flags )
62- : index_fd (the_repository -> index , & oid , fd , & st ,
63- type_from_string (type ), path , index_flags )))
64- die ((flags & HASH_OBJECT_WRITE )
28+ index_fd (the_repository -> index , & oid , fd , & st ,
29+ type_from_string (type ), path , flags ))
30+ die ((flags & INDEX_WRITE_OBJECT )
6531 ? "Unable to add %s to database"
6632 : "Unable to hash %s" , path );
6733 printf ("%s\n" , oid_to_hex (& oid ));
6834 maybe_flush_or_die (stdout , "hash to stdout" );
6935}
7036
7137static void hash_object (const char * path , const char * type , const char * vpath ,
72- unsigned flags , int literally )
38+ unsigned flags )
7339{
7440 int fd ;
7541 fd = xopen (path , O_RDONLY );
76- hash_fd (fd , type , vpath , flags , literally );
42+ hash_fd (fd , type , vpath , flags );
7743}
7844
79- static void hash_stdin_paths (const char * type , int no_filters , unsigned flags ,
80- int literally )
45+ static void hash_stdin_paths (const char * type , int no_filters , unsigned flags )
8146{
8247 struct strbuf buf = STRBUF_INIT ;
8348 struct strbuf unquoted = STRBUF_INIT ;
@@ -89,8 +54,7 @@ static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
8954 die ("line is badly quoted" );
9055 strbuf_swap (& buf , & unquoted );
9156 }
92- hash_object (buf .buf , type , no_filters ? NULL : buf .buf , flags ,
93- literally );
57+ hash_object (buf .buf , type , no_filters ? NULL : buf .buf , flags );
9458 }
9559 strbuf_release (& buf );
9660 strbuf_release (& unquoted );
@@ -111,19 +75,20 @@ int cmd_hash_object(int argc,
11175 int hashstdin = 0 ;
11276 int stdin_paths = 0 ;
11377 int no_filters = 0 ;
114- int literally = 0 ;
11578 int nongit = 0 ;
116- unsigned flags = HASH_OBJECT_CHECK ;
79+ unsigned flags = INDEX_FORMAT_CHECK ;
11780 const char * vpath = NULL ;
11881 char * vpath_free = NULL ;
11982 const struct option hash_object_options [] = {
12083 OPT_STRING ('t' , NULL , & type , N_ ("type" ), N_ ("object type" )),
12184 OPT_BIT ('w' , NULL , & flags , N_ ("write the object into the object database" ),
122- HASH_OBJECT_WRITE ),
85+ INDEX_WRITE_OBJECT ),
12386 OPT_COUNTUP ( 0 , "stdin" , & hashstdin , N_ ("read the object from stdin" )),
12487 OPT_BOOL ( 0 , "stdin-paths" , & stdin_paths , N_ ("read file names from stdin" )),
12588 OPT_BOOL ( 0 , "no-filters" , & no_filters , N_ ("store file as is without filters" )),
126- 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 ),
12792 OPT_STRING ( 0 , "path" , & vpath , N_ ("file" ), N_ ("process file as it were from this path" )),
12893 OPT_END ()
12994 };
@@ -133,7 +98,7 @@ int cmd_hash_object(int argc,
13398 argc = parse_options (argc , argv , prefix , hash_object_options ,
13499 hash_object_usage , 0 );
135100
136- if (flags & HASH_OBJECT_WRITE )
101+ if (flags & INDEX_WRITE_OBJECT )
137102 prefix = setup_git_directory ();
138103 else
139104 prefix = setup_git_directory_gently (& nongit );
@@ -169,7 +134,7 @@ int cmd_hash_object(int argc,
169134 }
170135
171136 if (hashstdin )
172- hash_fd (0 , type , vpath , flags , literally );
137+ hash_fd (0 , type , vpath , flags );
173138
174139 for (i = 0 ; i < argc ; i ++ ) {
175140 const char * arg = argv [i ];
@@ -178,12 +143,12 @@ int cmd_hash_object(int argc,
178143 if (prefix )
179144 arg = to_free = prefix_filename (prefix , arg );
180145 hash_object (arg , type , no_filters ? NULL : vpath ? vpath : arg ,
181- flags , literally );
146+ flags );
182147 free (to_free );
183148 }
184149
185150 if (stdin_paths )
186- hash_stdin_paths (type , no_filters , flags , literally );
151+ hash_stdin_paths (type , no_filters , flags );
187152
188153 free (vpath_free );
189154
0 commit comments