13
13
#include "dir.h"
14
14
#include "progress.h"
15
15
#include "streaming.h"
16
+ #include "decorate.h"
16
17
17
18
#define REACHABLE 0x0001
18
19
#define SEEN 0x0002
@@ -35,11 +36,26 @@ static int write_lost_and_found;
35
36
static int verbose ;
36
37
static int show_progress = -1 ;
37
38
static int show_dangling = 1 ;
39
+ static int name_objects ;
38
40
#define ERROR_OBJECT 01
39
41
#define ERROR_REACHABLE 02
40
42
#define ERROR_PACK 04
41
43
#define ERROR_REFS 010
42
44
45
+ static const char * describe_object (struct object * obj )
46
+ {
47
+ static struct strbuf buf = STRBUF_INIT ;
48
+ char * name = name_objects ?
49
+ lookup_decoration (fsck_walk_options .object_names , obj ) : NULL ;
50
+
51
+ strbuf_reset (& buf );
52
+ strbuf_addstr (& buf , oid_to_hex (& obj -> oid ));
53
+ if (name )
54
+ strbuf_addf (& buf , " (%s)" , name );
55
+
56
+ return buf .buf ;
57
+ }
58
+
43
59
static int fsck_config (const char * var , const char * value , void * cb )
44
60
{
45
61
if (strcmp (var , "fsck.skiplist" ) == 0 ) {
@@ -67,7 +83,7 @@ static void objreport(struct object *obj, const char *msg_type,
67
83
const char * err )
68
84
{
69
85
fprintf (stderr , "%s in %s %s: %s\n" ,
70
- msg_type , typename (obj -> type ), oid_to_hex ( & obj -> oid ), err );
86
+ msg_type , typename (obj -> type ), describe_object ( obj ), err );
71
87
}
72
88
73
89
static int objerror (struct object * obj , const char * err )
@@ -77,7 +93,8 @@ static int objerror(struct object *obj, const char *err)
77
93
return -1 ;
78
94
}
79
95
80
- static int fsck_error_func (struct object * obj , int type , const char * message )
96
+ static int fsck_error_func (struct fsck_options * o ,
97
+ struct object * obj , int type , const char * message )
81
98
{
82
99
objreport (obj , (type == FSCK_WARN ) ? "warning" : "error" , message );
83
100
return (type == FSCK_WARN ) ? 0 : 1 ;
@@ -97,7 +114,7 @@ static int mark_object(struct object *obj, int type, void *data, struct fsck_opt
97
114
if (!obj ) {
98
115
/* ... these references to parent->fld are safe here */
99
116
printf ("broken link from %7s %s\n" ,
100
- typename (parent -> type ), oid_to_hex ( & parent -> oid ));
117
+ typename (parent -> type ), describe_object ( parent ));
101
118
printf ("broken link from %7s %s\n" ,
102
119
(type == OBJ_ANY ? "unknown" : typename (type )), "unknown" );
103
120
errors_found |= ERROR_REACHABLE ;
@@ -114,9 +131,9 @@ static int mark_object(struct object *obj, int type, void *data, struct fsck_opt
114
131
if (!(obj -> flags & HAS_OBJ )) {
115
132
if (parent && !has_object_file (& obj -> oid )) {
116
133
printf ("broken link from %7s %s\n" ,
117
- typename (parent -> type ), oid_to_hex ( & parent -> oid ));
134
+ typename (parent -> type ), describe_object ( parent ));
118
135
printf (" to %7s %s\n" ,
119
- typename (obj -> type ), oid_to_hex ( & obj -> oid ));
136
+ typename (obj -> type ), describe_object ( obj ));
120
137
errors_found |= ERROR_REACHABLE ;
121
138
}
122
139
return 1 ;
@@ -190,7 +207,8 @@ static void check_reachable_object(struct object *obj)
190
207
return ; /* it is in pack - forget about it */
191
208
if (connectivity_only && has_object_file (& obj -> oid ))
192
209
return ;
193
- printf ("missing %s %s\n" , typename (obj -> type ), oid_to_hex (& obj -> oid ));
210
+ printf ("missing %s %s\n" , typename (obj -> type ),
211
+ describe_object (obj ));
194
212
errors_found |= ERROR_REACHABLE ;
195
213
return ;
196
214
}
@@ -215,7 +233,8 @@ static void check_unreachable_object(struct object *obj)
215
233
* since this is something that is prunable.
216
234
*/
217
235
if (show_unreachable ) {
218
- printf ("unreachable %s %s\n" , typename (obj -> type ), oid_to_hex (& obj -> oid ));
236
+ printf ("unreachable %s %s\n" , typename (obj -> type ),
237
+ describe_object (obj ));
219
238
return ;
220
239
}
221
240
@@ -234,11 +253,11 @@ static void check_unreachable_object(struct object *obj)
234
253
if (!obj -> used ) {
235
254
if (show_dangling )
236
255
printf ("dangling %s %s\n" , typename (obj -> type ),
237
- oid_to_hex ( & obj -> oid ));
256
+ describe_object ( obj ));
238
257
if (write_lost_and_found ) {
239
258
char * filename = git_pathdup ("lost-found/%s/%s" ,
240
259
obj -> type == OBJ_COMMIT ? "commit" : "other" ,
241
- oid_to_hex ( & obj -> oid ));
260
+ describe_object ( obj ));
242
261
FILE * f ;
243
262
244
263
if (safe_create_leading_directories_const (filename )) {
@@ -252,7 +271,7 @@ static void check_unreachable_object(struct object *obj)
252
271
if (stream_blob_to_fd (fileno (f ), obj -> oid .hash , NULL , 1 ))
253
272
die_errno ("Could not write '%s'" , filename );
254
273
} else
255
- fprintf (f , "%s\n" , oid_to_hex ( & obj -> oid ));
274
+ fprintf (f , "%s\n" , describe_object ( obj ));
256
275
if (fclose (f ))
257
276
die_errno ("Could not finish '%s'" ,
258
277
filename );
@@ -271,7 +290,7 @@ static void check_unreachable_object(struct object *obj)
271
290
static void check_object (struct object * obj )
272
291
{
273
292
if (verbose )
274
- fprintf (stderr , "Checking %s\n" , oid_to_hex ( & obj -> oid ));
293
+ fprintf (stderr , "Checking %s\n" , describe_object ( obj ));
275
294
276
295
if (obj -> flags & REACHABLE )
277
296
check_reachable_object (obj );
@@ -307,7 +326,7 @@ static int fsck_obj(struct object *obj)
307
326
308
327
if (verbose )
309
328
fprintf (stderr , "Checking %s %s\n" ,
310
- typename (obj -> type ), oid_to_hex ( & obj -> oid ));
329
+ typename (obj -> type ), describe_object ( obj ));
311
330
312
331
if (fsck_walk (obj , NULL , & fsck_obj_options ))
313
332
objerror (obj , "broken links" );
@@ -326,15 +345,17 @@ static int fsck_obj(struct object *obj)
326
345
free_commit_buffer (commit );
327
346
328
347
if (!commit -> parents && show_root )
329
- printf ("root %s\n" , oid_to_hex (& commit -> object . oid ));
348
+ printf ("root %s\n" , describe_object (& commit -> object ));
330
349
}
331
350
332
351
if (obj -> type == OBJ_TAG ) {
333
352
struct tag * tag = (struct tag * ) obj ;
334
353
335
354
if (show_tags && tag -> tagged ) {
336
- printf ("tagged %s %s" , typename (tag -> tagged -> type ), oid_to_hex (& tag -> tagged -> oid ));
337
- printf (" (%s) in %s\n" , tag -> tag , oid_to_hex (& tag -> object .oid ));
355
+ printf ("tagged %s %s" , typename (tag -> tagged -> type ),
356
+ describe_object (tag -> tagged ));
357
+ printf (" (%s) in %s\n" , tag -> tag ,
358
+ describe_object (& tag -> object ));
338
359
}
339
360
}
340
361
@@ -368,13 +389,18 @@ static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
368
389
369
390
static int default_refs ;
370
391
371
- static void fsck_handle_reflog_sha1 (const char * refname , unsigned char * sha1 )
392
+ static void fsck_handle_reflog_sha1 (const char * refname , unsigned char * sha1 ,
393
+ unsigned long timestamp )
372
394
{
373
395
struct object * obj ;
374
396
375
397
if (!is_null_sha1 (sha1 )) {
376
398
obj = lookup_object (sha1 );
377
399
if (obj ) {
400
+ if (timestamp && name_objects )
401
+ add_decoration (fsck_walk_options .object_names ,
402
+ obj ,
403
+ xstrfmt ("%s@{%ld}" , refname , timestamp ));
378
404
obj -> used = 1 ;
379
405
mark_object_reachable (obj );
380
406
} else {
@@ -394,8 +420,8 @@ static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
394
420
fprintf (stderr , "Checking reflog %s->%s\n" ,
395
421
sha1_to_hex (osha1 ), sha1_to_hex (nsha1 ));
396
422
397
- fsck_handle_reflog_sha1 (refname , osha1 );
398
- fsck_handle_reflog_sha1 (refname , nsha1 );
423
+ fsck_handle_reflog_sha1 (refname , osha1 , 0 );
424
+ fsck_handle_reflog_sha1 (refname , nsha1 , timestamp );
399
425
return 0 ;
400
426
}
401
427
@@ -424,6 +450,9 @@ static int fsck_handle_ref(const char *refname, const struct object_id *oid,
424
450
}
425
451
default_refs ++ ;
426
452
obj -> used = 1 ;
453
+ if (name_objects )
454
+ add_decoration (fsck_walk_options .object_names ,
455
+ obj , xstrdup (refname ));
427
456
mark_object_reachable (obj );
428
457
429
458
return 0 ;
@@ -539,6 +568,9 @@ static int fsck_cache_tree(struct cache_tree *it)
539
568
return 1 ;
540
569
}
541
570
obj -> used = 1 ;
571
+ if (name_objects )
572
+ add_decoration (fsck_walk_options .object_names ,
573
+ obj , xstrdup (":" ));
542
574
mark_object_reachable (obj );
543
575
if (obj -> type != OBJ_TREE )
544
576
err |= objerror (obj , "non-tree in cache-tree" );
@@ -567,6 +599,7 @@ static struct option fsck_opts[] = {
567
599
OPT_BOOL (0 , "lost-found" , & write_lost_and_found ,
568
600
N_ ("write dangling objects in .git/lost-found" )),
569
601
OPT_BOOL (0 , "progress" , & show_progress , N_ ("show progress" )),
602
+ OPT_BOOL (0 , "name-objects" , & name_objects , N_ ("show verbose names for reachable objects" )),
570
603
OPT_END (),
571
604
};
572
605
@@ -596,6 +629,10 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
596
629
include_reflogs = 0 ;
597
630
}
598
631
632
+ if (name_objects )
633
+ fsck_walk_options .object_names =
634
+ xcalloc (1 , sizeof (struct decoration ));
635
+
599
636
git_config (fsck_config , NULL );
600
637
601
638
fsck_head_link ();
@@ -651,6 +688,9 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
651
688
continue ;
652
689
653
690
obj -> used = 1 ;
691
+ if (name_objects )
692
+ add_decoration (fsck_walk_options .object_names ,
693
+ obj , xstrdup (arg ));
654
694
mark_object_reachable (obj );
655
695
heads ++ ;
656
696
continue ;
@@ -683,6 +723,10 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
683
723
continue ;
684
724
obj = & blob -> object ;
685
725
obj -> used = 1 ;
726
+ if (name_objects )
727
+ add_decoration (fsck_walk_options .object_names ,
728
+ obj ,
729
+ xstrfmt (":%s" , active_cache [i ]-> name ));
686
730
mark_object_reachable (obj );
687
731
}
688
732
if (active_cache_tree )
0 commit comments