4 #include "pack-revindex.h"
5 #include "parse-options.h"
9 #define VERIFY_PACK_VERBOSE 01
10 #define VERIFY_PACK_STAT_ONLY 02
12 static void show_pack_info(struct packed_git
*p
, unsigned int flags
)
14 uint32_t nr_objects
, i
;
16 int stat_only
= flags
& VERIFY_PACK_STAT_ONLY
;
17 unsigned long chain_histogram
[MAX_CHAIN
+1], baseobjects
;
19 nr_objects
= p
->num_objects
;
20 memset(chain_histogram
, 0, sizeof(chain_histogram
));
23 for (i
= 0; i
< nr_objects
; i
++) {
24 const unsigned char *sha1
;
25 unsigned char base_sha1
[20];
28 unsigned long store_size
;
30 unsigned int delta_chain_length
;
32 sha1
= nth_packed_object_sha1(p
, i
);
34 die("internal error pack-check nth-packed-object");
35 offset
= nth_packed_object_offset(p
, i
);
36 type
= packed_object_info_detail(p
, offset
, &size
, &store_size
,
40 printf("%s ", sha1_to_hex(sha1
));
41 if (!delta_chain_length
) {
43 printf("%-6s %lu %lu %"PRIuMAX
"\n",
44 type
, size
, store_size
, (uintmax_t)offset
);
49 printf("%-6s %lu %lu %"PRIuMAX
" %u %s\n",
50 type
, size
, store_size
, (uintmax_t)offset
,
51 delta_chain_length
, sha1_to_hex(base_sha1
));
52 if (delta_chain_length
<= MAX_CHAIN
)
53 chain_histogram
[delta_chain_length
]++;
60 printf("non delta: %lu object%s\n",
61 baseobjects
, baseobjects
> 1 ? "s" : "");
63 for (cnt
= 1; cnt
<= MAX_CHAIN
; cnt
++) {
64 if (!chain_histogram
[cnt
])
66 printf("chain length = %d: %lu object%s\n", cnt
,
68 chain_histogram
[cnt
] > 1 ? "s" : "");
70 if (chain_histogram
[0])
71 printf("chain length > %d: %lu object%s\n", MAX_CHAIN
,
73 chain_histogram
[0] > 1 ? "s" : "");
76 static int verify_one_pack(const char *path
, unsigned int flags
)
80 int verbose
= flags
& VERIFY_PACK_VERBOSE
;
81 int stat_only
= flags
& VERIFY_PACK_STAT_ONLY
;
82 struct packed_git
*pack
;
85 len
= strlcpy(arg
, path
, PATH_MAX
);
87 return error("name too long: %s", path
);
90 * In addition to "foo.idx" we accept "foo.pack" and "foo";
91 * normalize these forms to "foo.idx" for add_packed_git().
93 if (has_extension(arg
, ".pack")) {
94 strcpy(arg
+ len
- 5, ".idx");
96 } else if (!has_extension(arg
, ".idx")) {
97 if (len
+ 4 >= PATH_MAX
)
98 return error("name too long: %s.idx", arg
);
99 strcpy(arg
+ len
, ".idx");
104 * add_packed_git() uses our buffer (containing "foo.idx") to
105 * build the pack filename ("foo.pack"). Make sure it fits.
107 if (len
+ 1 >= PATH_MAX
) {
109 return error("name too long: %s.pack", arg
);
112 pack
= add_packed_git(arg
, len
, 1);
114 return error("packfile %s not found.", arg
);
116 install_packed_git(pack
);
119 err
= verify_pack(pack
);
121 err
= open_pack_index(pack
);
123 if (verbose
|| stat_only
) {
125 printf("%s: bad\n", pack
->pack_name
);
127 show_pack_info(pack
, flags
);
129 printf("%s: ok\n", pack
->pack_name
);
136 static const char * const verify_pack_usage
[] = {
137 "git verify-pack [-v|--verbose] [-s|--stat-only] <pack>...",
141 int cmd_verify_pack(int argc
, const char **argv
, const char *prefix
)
144 unsigned int flags
= 0;
146 const struct option verify_pack_options
[] = {
147 OPT_BIT('v', "verbose", &flags
, "verbose",
148 VERIFY_PACK_VERBOSE
),
149 OPT_BIT('s', "stat-only", &flags
, "show statistics only",
150 VERIFY_PACK_STAT_ONLY
),
154 git_config(git_default_config
, NULL
);
155 argc
= parse_options(argc
, argv
, prefix
, verify_pack_options
,
156 verify_pack_usage
, 0);
158 usage_with_options(verify_pack_usage
, verify_pack_options
);
159 for (i
= 0; i
< argc
; i
++) {
160 if (verify_one_pack(argv
[i
], flags
))