blob: b6079ae6cb03c7f3112c6eebc8c9a012d690a125 [file] [log] [blame]
Rene Scharfe2e3ed672006-08-10 17:02:38 +02001#include "builtin.h"
Junio C Hamanof9253392005-06-29 02:51:27 -07002#include "cache.h"
3#include "pack.h"
Nicolas Pitre4b480c62008-08-22 15:45:53 -04004#include "pack-revindex.h"
Stephen Boydc9c3c672009-07-07 22:15:40 -07005#include "parse-options.h"
Nicolas Pitre77d3ece2008-06-24 23:18:17 -04006
7#define MAX_CHAIN 50
8
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -07009#define VERIFY_PACK_VERBOSE 01
10#define VERIFY_PACK_STAT_ONLY 02
11
12static void show_pack_info(struct packed_git *p, unsigned int flags)
Nicolas Pitre77d3ece2008-06-24 23:18:17 -040013{
Junio C Hamano262b04f2009-08-07 15:36:31 -070014 uint32_t nr_objects, i;
15 int cnt;
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070016 int stat_only = flags & VERIFY_PACK_STAT_ONLY;
Junio C Hamano262b04f2009-08-07 15:36:31 -070017 unsigned long chain_histogram[MAX_CHAIN+1], baseobjects;
Nicolas Pitre77d3ece2008-06-24 23:18:17 -040018
19 nr_objects = p->num_objects;
20 memset(chain_histogram, 0, sizeof(chain_histogram));
Junio C Hamano262b04f2009-08-07 15:36:31 -070021 baseobjects = 0;
Nicolas Pitre77d3ece2008-06-24 23:18:17 -040022
23 for (i = 0; i < nr_objects; i++) {
24 const unsigned char *sha1;
25 unsigned char base_sha1[20];
26 const char *type;
27 unsigned long size;
28 unsigned long store_size;
29 off_t offset;
30 unsigned int delta_chain_length;
31
32 sha1 = nth_packed_object_sha1(p, i);
33 if (!sha1)
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,
37 &delta_chain_length,
38 base_sha1);
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070039 if (!stat_only)
40 printf("%s ", sha1_to_hex(sha1));
Junio C Hamano262b04f2009-08-07 15:36:31 -070041 if (!delta_chain_length) {
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070042 if (!stat_only)
43 printf("%-6s %lu %lu %"PRIuMAX"\n",
44 type, size, store_size, (uintmax_t)offset);
Junio C Hamano262b04f2009-08-07 15:36:31 -070045 baseobjects++;
46 }
Nicolas Pitre77d3ece2008-06-24 23:18:17 -040047 else {
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070048 if (!stat_only)
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));
Nicolas Pitre77d3ece2008-06-24 23:18:17 -040052 if (delta_chain_length <= MAX_CHAIN)
53 chain_histogram[delta_chain_length]++;
54 else
55 chain_histogram[0]++;
56 }
57 }
58
Junio C Hamano262b04f2009-08-07 15:36:31 -070059 if (baseobjects)
60 printf("non delta: %lu object%s\n",
61 baseobjects, baseobjects > 1 ? "s" : "");
62
63 for (cnt = 1; cnt <= MAX_CHAIN; cnt++) {
64 if (!chain_histogram[cnt])
Nicolas Pitre77d3ece2008-06-24 23:18:17 -040065 continue;
Junio C Hamano262b04f2009-08-07 15:36:31 -070066 printf("chain length = %d: %lu object%s\n", cnt,
67 chain_histogram[cnt],
68 chain_histogram[cnt] > 1 ? "s" : "");
Nicolas Pitre77d3ece2008-06-24 23:18:17 -040069 }
70 if (chain_histogram[0])
Junio C Hamano262b04f2009-08-07 15:36:31 -070071 printf("chain length > %d: %lu object%s\n", MAX_CHAIN,
72 chain_histogram[0],
73 chain_histogram[0] > 1 ? "s" : "");
Nicolas Pitre77d3ece2008-06-24 23:18:17 -040074}
75
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070076static int verify_one_pack(const char *path, unsigned int flags)
Junio C Hamanof9253392005-06-29 02:51:27 -070077{
Rene Scharfeae9c86f2006-08-10 17:02:32 +020078 char arg[PATH_MAX];
79 int len;
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -070080 int verbose = flags & VERIFY_PACK_VERBOSE;
81 int stat_only = flags & VERIFY_PACK_STAT_ONLY;
Rene Scharfed0d619c2006-08-10 17:02:35 +020082 struct packed_git *pack;
83 int err;
Rene Scharfeae9c86f2006-08-10 17:02:32 +020084
85 len = strlcpy(arg, path, PATH_MAX);
86 if (len >= PATH_MAX)
87 return error("name too long: %s", path);
88
Rene Scharfefc5fc502006-08-10 17:02:34 +020089 /*
90 * In addition to "foo.idx" we accept "foo.pack" and "foo";
91 * normalize these forms to "foo.idx" for add_packed_git().
92 */
Rene Scharfe5bb1cda2006-08-11 14:01:45 +020093 if (has_extension(arg, ".pack")) {
Rene Scharfefc5fc502006-08-10 17:02:34 +020094 strcpy(arg + len - 5, ".idx");
95 len--;
Rene Scharfe5bb1cda2006-08-11 14:01:45 +020096 } else if (!has_extension(arg, ".idx")) {
Rene Scharfefc5fc502006-08-10 17:02:34 +020097 if (len + 4 >= PATH_MAX)
98 return error("name too long: %s.idx", arg);
99 strcpy(arg + len, ".idx");
100 len += 4;
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700101 }
Rene Scharfefc5fc502006-08-10 17:02:34 +0200102
Rene Scharfef711ab52006-08-10 17:02:36 +0200103 /*
104 * add_packed_git() uses our buffer (containing "foo.idx") to
105 * build the pack filename ("foo.pack"). Make sure it fits.
106 */
107 if (len + 1 >= PATH_MAX) {
108 arg[len - 4] = '\0';
109 return error("name too long: %s.pack", arg);
110 }
111
Rene Scharfed0d619c2006-08-10 17:02:35 +0200112 pack = add_packed_git(arg, len, 1);
113 if (!pack)
Rene Scharfefc5fc502006-08-10 17:02:34 +0200114 return error("packfile %s not found.", arg);
115
Nicolas Pitre34081462008-02-28 00:25:18 -0500116 install_packed_git(pack);
Nicolas Pitre77d3ece2008-06-24 23:18:17 -0400117
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -0700118 if (!stat_only)
119 err = verify_pack(pack);
120 else
121 err = open_pack_index(pack);
122
123 if (verbose || stat_only) {
Nicolas Pitre77d3ece2008-06-24 23:18:17 -0400124 if (err)
125 printf("%s: bad\n", pack->pack_name);
126 else {
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -0700127 show_pack_info(pack, flags);
128 if (!stat_only)
129 printf("%s: ok\n", pack->pack_name);
Nicolas Pitre77d3ece2008-06-24 23:18:17 -0400130 }
131 }
Rene Scharfed0d619c2006-08-10 17:02:35 +0200132
133 return err;
Junio C Hamanof9253392005-06-29 02:51:27 -0700134}
135
Stephen Boydc9c3c672009-07-07 22:15:40 -0700136static const char * const verify_pack_usage[] = {
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -0700137 "git verify-pack [-v|--verbose] [-s|--stat-only] <pack>...",
Stephen Boydc9c3c672009-07-07 22:15:40 -0700138 NULL
139};
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700140
Rene Scharfe2e3ed672006-08-10 17:02:38 +0200141int cmd_verify_pack(int argc, const char **argv, const char *prefix)
Junio C Hamanof9253392005-06-29 02:51:27 -0700142{
Rene Scharfe0eaf22f2006-08-10 17:02:37 +0200143 int err = 0;
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -0700144 unsigned int flags = 0;
Stephen Boydc9c3c672009-07-07 22:15:40 -0700145 int i;
146 const struct option verify_pack_options[] = {
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -0700147 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),
Stephen Boydc9c3c672009-07-07 22:15:40 -0700151 OPT_END()
152 };
Junio C Hamanof9253392005-06-29 02:51:27 -0700153
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100154 git_config(git_default_config, NULL);
Stephen Boydc9c3c672009-07-07 22:15:40 -0700155 argc = parse_options(argc, argv, prefix, verify_pack_options,
156 verify_pack_usage, 0);
157 if (argc < 1)
158 usage_with_options(verify_pack_usage, verify_pack_options);
159 for (i = 0; i < argc; i++) {
Junio C Hamano6c4f3ec2009-08-07 15:45:30 -0700160 if (verify_one_pack(argv[i], flags))
Stephen Boydc9c3c672009-07-07 22:15:40 -0700161 err = 1;
162 discard_revindex();
Junio C Hamanof9253392005-06-29 02:51:27 -0700163 }
Rene Scharfe6f05b572006-08-10 17:02:31 +0200164
Rene Scharfe0eaf22f2006-08-10 17:02:37 +0200165 return err;
Junio C Hamanof9253392005-06-29 02:51:27 -0700166}