10
10
#include "parse-options.h"
11
11
#include "exec_cmd.h"
12
12
13
- static void hash_fd (int fd , const char * type , const char * path , unsigned flags )
13
+ /*
14
+ * This is to create corrupt objects for debugging and as such it
15
+ * needs to bypass the data conversion performed by, and the type
16
+ * limitation imposed by, index_fd() and its callees.
17
+ */
18
+ static int hash_literally (unsigned char * sha1 , int fd , const char * type , unsigned flags )
19
+ {
20
+ struct strbuf buf = STRBUF_INIT ;
21
+ int ret ;
22
+
23
+ if (strbuf_read (& buf , fd , 4096 ) < 0 )
24
+ ret = -1 ;
25
+ else if (flags & HASH_WRITE_OBJECT )
26
+ ret = write_sha1_file (buf .buf , buf .len , type , sha1 );
27
+ else
28
+ ret = hash_sha1_file (buf .buf , buf .len , type , sha1 );
29
+ strbuf_release (& buf );
30
+ return ret ;
31
+ }
32
+
33
+ static void hash_fd (int fd , const char * type , const char * path , unsigned flags ,
34
+ int literally )
14
35
{
15
36
struct stat st ;
16
37
unsigned char sha1 [20 ];
17
38
18
39
if (fstat (fd , & st ) < 0 ||
19
- index_fd (sha1 , fd , & st , type_from_string (type ), path , flags ))
40
+ (literally
41
+ ? hash_literally (sha1 , fd , type , flags )
42
+ : index_fd (sha1 , fd , & st , type_from_string (type ), path , flags )))
20
43
die ((flags & HASH_WRITE_OBJECT )
21
44
? "Unable to add %s to database"
22
45
: "Unable to hash %s" , path );
@@ -25,16 +48,17 @@ static void hash_fd(int fd, const char *type, const char *path, unsigned flags)
25
48
}
26
49
27
50
static void hash_object (const char * path , const char * type , const char * vpath ,
28
- unsigned flags )
51
+ unsigned flags , int literally )
29
52
{
30
53
int fd ;
31
54
fd = open (path , O_RDONLY );
32
55
if (fd < 0 )
33
56
die_errno ("Cannot open '%s'" , path );
34
- hash_fd (fd , type , vpath , flags );
57
+ hash_fd (fd , type , vpath , flags , literally );
35
58
}
36
59
37
- static void hash_stdin_paths (const char * type , int no_filters , unsigned flags )
60
+ static void hash_stdin_paths (const char * type , int no_filters , unsigned flags ,
61
+ int literally )
38
62
{
39
63
struct strbuf buf = STRBUF_INIT , nbuf = STRBUF_INIT ;
40
64
@@ -45,7 +69,8 @@ static void hash_stdin_paths(const char *type, int no_filters, unsigned flags)
45
69
die ("line is badly quoted" );
46
70
strbuf_swap (& buf , & nbuf );
47
71
}
48
- hash_object (buf .buf , type , no_filters ? NULL : buf .buf , flags );
72
+ hash_object (buf .buf , type , no_filters ? NULL : buf .buf , flags ,
73
+ literally );
49
74
}
50
75
strbuf_release (& buf );
51
76
strbuf_release (& nbuf );
@@ -62,6 +87,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
62
87
int hashstdin = 0 ;
63
88
int stdin_paths = 0 ;
64
89
int no_filters = 0 ;
90
+ int literally = 0 ;
65
91
unsigned flags = HASH_FORMAT_CHECK ;
66
92
const char * vpath = NULL ;
67
93
const struct option hash_object_options [] = {
@@ -71,6 +97,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
71
97
OPT_COUNTUP ( 0 , "stdin" , & hashstdin , N_ ("read the object from stdin" )),
72
98
OPT_BOOL ( 0 , "stdin-paths" , & stdin_paths , N_ ("read file names from stdin" )),
73
99
OPT_BOOL ( 0 , "no-filters" , & no_filters , N_ ("store file as is without filters" )),
100
+ OPT_BOOL ( 0 , "literally" , & literally , N_ ("just hash any random garbage to create corrupt objects for debugging Git" )),
74
101
OPT_STRING ( 0 , "path" , & vpath , N_ ("file" ), N_ ("process file as it were from this path" )),
75
102
OPT_END ()
76
103
};
@@ -111,19 +138,19 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
111
138
}
112
139
113
140
if (hashstdin )
114
- hash_fd (0 , type , vpath , flags );
141
+ hash_fd (0 , type , vpath , flags , literally );
115
142
116
143
for (i = 0 ; i < argc ; i ++ ) {
117
144
const char * arg = argv [i ];
118
145
119
146
if (0 <= prefix_length )
120
147
arg = prefix_filename (prefix , prefix_length , arg );
121
148
hash_object (arg , type , no_filters ? NULL : vpath ? vpath : arg ,
122
- flags );
149
+ flags , literally );
123
150
}
124
151
125
152
if (stdin_paths )
126
- hash_stdin_paths (type , no_filters , flags );
153
+ hash_stdin_paths (type , no_filters , flags , literally );
127
154
128
155
return 0 ;
129
156
}
0 commit comments