@@ -20,6 +20,35 @@ static int check_strict = 0;
2020static int keep_cache_objects = 0 ;
2121static unsigned char head_sha1 [20 ];
2222
23+
24+ static void objreport (struct object * obj , const char * severity ,
25+ const char * err , va_list params )
26+ {
27+ fprintf (stderr , "%s in %s %s: " ,
28+ severity , obj -> type , sha1_to_hex (obj -> sha1 ));
29+ vfprintf (stderr , err , params );
30+ fputs ("\n" , stderr );
31+ }
32+
33+ int objerror (struct object * obj , const char * err , ...)
34+ {
35+ va_list params ;
36+ va_start (params , err );
37+ objreport (obj , "error" , err , params );
38+ va_end (params );
39+ return -1 ;
40+ }
41+
42+ int objwarning (struct object * obj , const char * err , ...)
43+ {
44+ va_list params ;
45+ va_start (params , err );
46+ objreport (obj , "warning" , err , params );
47+ va_end (params );
48+ return -1 ;
49+ }
50+
51+
2352static void check_connectivity (void )
2453{
2554 int i ;
@@ -162,56 +191,44 @@ static int fsck_tree(struct tree *item)
162191
163192 retval = 0 ;
164193 if (has_full_path ) {
165- fprintf (stderr , "warning: git-fsck-objects: tree %s "
166- "has full pathnames in it\n" ,
167- sha1_to_hex (item -> object .sha1 ));
194+ objwarning (& item -> object , "contains full pathnames" );
168195 }
169196 if (has_zero_pad ) {
170- fprintf (stderr , "warning: git-fsck-objects: tree %s "
171- "has zero-padded file modes in it\n" ,
172- sha1_to_hex (item -> object .sha1 ));
197+ objwarning (& item -> object , "contains zero-padded file modes" );
173198 }
174199 if (has_bad_modes ) {
175- fprintf (stderr , "warning: git-fsck-objects: tree %s "
176- "has bad file modes in it\n" ,
177- sha1_to_hex (item -> object .sha1 ));
200+ objwarning (& item -> object , "contains bad file modes" );
178201 }
179202 if (has_dup_entries ) {
180- fprintf (stderr , "error: git-fsck-objects: tree %s "
181- "has duplicate file entries\n" ,
182- sha1_to_hex (item -> object .sha1 ));
183- retval = -1 ;
203+ retval = objerror (& item -> object , "contains duplicate file entries" );
184204 }
185205 if (not_properly_sorted ) {
186- fprintf (stderr , "error: git-fsck-objects: tree %s "
187- "is not properly sorted\n" ,
188- sha1_to_hex (item -> object .sha1 ));
189- retval = -1 ;
206+ retval = objerror (& item -> object , "not properly sorted" );
190207 }
191208 return retval ;
192209}
193210
194211static int fsck_commit (struct commit * commit )
195212{
196213 char * buffer = commit -> buffer ;
197- unsigned char sha1 [20 ];
214+ unsigned char tree_sha1 [ 20 ], sha1 [20 ];
198215
199216 if (memcmp (buffer , "tree " , 5 ))
200- return -1 ;
201- if (get_sha1_hex (buffer + 5 , sha1 ) || buffer [45 ] != '\n' )
202- return -1 ;
217+ return objerror ( & commit -> object , "invalid format - expected 'tree' line" ) ;
218+ if (get_sha1_hex (buffer + 5 , tree_sha1 ) || buffer [45 ] != '\n' )
219+ return objerror ( & commit -> object , "invalid 'tree' line format - bad sha1" ) ;
203220 buffer += 46 ;
204221 while (!memcmp (buffer , "parent " , 7 )) {
205222 if (get_sha1_hex (buffer + 7 , sha1 ) || buffer [47 ] != '\n' )
206- return -1 ;
223+ return objerror ( & commit -> object , "invalid 'parent' line format - bad sha1" ) ;
207224 buffer += 48 ;
208225 }
209226 if (memcmp (buffer , "author " , 7 ))
210- return -1 ;
227+ return objerror ( & commit -> object , "invalid format - expected 'author' line" ) ;
211228 free (commit -> buffer );
212229 commit -> buffer = NULL ;
213230 if (!commit -> tree )
214- return -1 ;
231+ return objerror ( & commit -> object , "could not load commit's tree %s" , tree_sha1 ) ;
215232 if (!commit -> parents && show_root )
216233 printf ("root %s\n" , sha1_to_hex (commit -> object .sha1 ));
217234 if (!commit -> date )
@@ -225,8 +242,7 @@ static int fsck_tag(struct tag *tag)
225242 struct object * tagged = tag -> tagged ;
226243
227244 if (!tagged ) {
228- printf ("bad object in tag %s\n" , sha1_to_hex (tag -> object .sha1 ));
229- return -1 ;
245+ return objerror (& tag -> object , "could not load tagged object" );
230246 }
231247 if (!show_tags )
232248 return 0 ;
@@ -240,7 +256,7 @@ static int fsck_sha1(unsigned char *sha1)
240256{
241257 struct object * obj = parse_object (sha1 );
242258 if (!obj )
243- return -1 ;
259+ return error ( "%s: object not found" , sha1_to_hex ( sha1 )) ;
244260 if (obj -> type == blob_type )
245261 return 0 ;
246262 if (obj -> type == tree_type )
@@ -249,7 +265,8 @@ static int fsck_sha1(unsigned char *sha1)
249265 return fsck_commit ((struct commit * ) obj );
250266 if (obj -> type == tag_type )
251267 return fsck_tag ((struct tag * ) obj );
252- return -1 ;
268+ /* By now, parse_object() would've returned NULL instead. */
269+ return objerror (obj , "unknown type '%s' (internal fsck error)" , obj -> type );
253270}
254271
255272/*
@@ -285,8 +302,7 @@ static void fsck_sha1_list(void)
285302 unsigned char * sha1 = entry -> sha1 ;
286303
287304 sha1_list .entry [i ] = NULL ;
288- if (fsck_sha1 (sha1 ) < 0 )
289- fprintf (stderr , "bad sha1 entry '%s'\n" , sha1_to_hex (sha1 ));
305+ fsck_sha1 (sha1 );
290306 free (entry );
291307 }
292308 sha1_list .nr = 0 ;
@@ -479,9 +495,7 @@ int main(int argc, char **argv)
479495 for (i = 0 ; i < num ; i ++ ) {
480496 unsigned char sha1 [20 ];
481497 nth_packed_object_sha1 (p , i , sha1 );
482- if (fsck_sha1 (sha1 ) < 0 )
483- fprintf (stderr , "bad sha1 entry '%s'\n" , sha1_to_hex (sha1 ));
484-
498+ fsck_sha1 (sha1 );
485499 }
486500 }
487501 }
@@ -505,7 +519,7 @@ int main(int argc, char **argv)
505519 heads ++ ;
506520 continue ;
507521 }
508- error ("expected sha1, got %s " , arg );
522+ error ("invalid parameter: expected sha1, got '%s' " , arg );
509523 }
510524
511525 /*
0 commit comments