2006-04-28 06:37:18 +08:00
|
|
|
/*
|
|
|
|
* Builtin "git count-objects".
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Junio C Hamano
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cache.h"
|
|
|
|
#include "builtin.h"
|
2007-10-16 04:38:51 +08:00
|
|
|
#include "parse-options.h"
|
2006-04-28 06:37:18 +08:00
|
|
|
|
|
|
|
static void count_objects(DIR *d, char *path, int len, int verbose,
|
|
|
|
unsigned long *loose,
|
|
|
|
unsigned long *loose_size,
|
|
|
|
unsigned long *packed_loose,
|
|
|
|
unsigned long *garbage)
|
|
|
|
{
|
|
|
|
struct dirent *ent;
|
|
|
|
while ((ent = readdir(d)) != NULL) {
|
|
|
|
char hex[41];
|
|
|
|
unsigned char sha1[20];
|
|
|
|
const char *cp;
|
|
|
|
int bad = 0;
|
|
|
|
|
|
|
|
if ((ent->d_name[0] == '.') &&
|
|
|
|
(ent->d_name[1] == 0 ||
|
|
|
|
((ent->d_name[1] == '.') && (ent->d_name[2] == 0))))
|
|
|
|
continue;
|
|
|
|
for (cp = ent->d_name; *cp; cp++) {
|
|
|
|
int ch = *cp;
|
|
|
|
if (('0' <= ch && ch <= '9') ||
|
|
|
|
('a' <= ch && ch <= 'f'))
|
|
|
|
continue;
|
|
|
|
bad = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cp - ent->d_name != 38)
|
|
|
|
bad = 1;
|
|
|
|
else {
|
|
|
|
struct stat st;
|
|
|
|
memcpy(path + len + 3, ent->d_name, 38);
|
|
|
|
path[len + 2] = '/';
|
|
|
|
path[len + 41] = 0;
|
|
|
|
if (lstat(path, &st) || !S_ISREG(st.st_mode))
|
|
|
|
bad = 1;
|
|
|
|
else
|
2007-03-07 09:44:37 +08:00
|
|
|
(*loose_size) += xsize_t(st.st_blocks);
|
2006-04-28 06:37:18 +08:00
|
|
|
}
|
|
|
|
if (bad) {
|
|
|
|
if (verbose) {
|
|
|
|
error("garbage found: %.*s/%s",
|
|
|
|
len + 2, path, ent->d_name);
|
|
|
|
(*garbage)++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(*loose)++;
|
|
|
|
if (!verbose)
|
|
|
|
continue;
|
|
|
|
memcpy(hex, path+len, 2);
|
|
|
|
memcpy(hex+2, ent->d_name, 38);
|
|
|
|
hex[40] = 0;
|
|
|
|
if (get_sha1_hex(hex, sha1))
|
|
|
|
die("internal error");
|
2006-09-06 17:12:09 +08:00
|
|
|
if (has_sha1_pack(sha1, NULL))
|
2006-04-28 06:37:18 +08:00
|
|
|
(*packed_loose)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-16 04:38:51 +08:00
|
|
|
static char const * const count_objects_usage[] = {
|
|
|
|
"git-count-objects [-v]",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
int cmd_count_objects(int argc, const char **argv, const char *prefix)
|
2006-04-28 06:37:18 +08:00
|
|
|
{
|
2007-10-16 04:38:51 +08:00
|
|
|
int i, verbose = 0;
|
2006-04-28 06:37:18 +08:00
|
|
|
const char *objdir = get_object_directory();
|
|
|
|
int len = strlen(objdir);
|
|
|
|
char *path = xmalloc(len + 50);
|
|
|
|
unsigned long loose = 0, packed = 0, packed_loose = 0, garbage = 0;
|
|
|
|
unsigned long loose_size = 0;
|
2007-10-16 04:38:51 +08:00
|
|
|
struct option opts[] = {
|
|
|
|
OPT__VERBOSE(&verbose),
|
|
|
|
OPT_END(),
|
|
|
|
};
|
2006-04-28 06:37:18 +08:00
|
|
|
|
2007-10-16 04:38:51 +08:00
|
|
|
argc = parse_options(argc, argv, opts, count_objects_usage, 0);
|
2006-04-28 06:37:18 +08:00
|
|
|
/* we do not take arguments other than flags for now */
|
2007-10-16 04:38:51 +08:00
|
|
|
if (argc)
|
|
|
|
usage_with_options(count_objects_usage, opts);
|
2006-04-28 06:37:18 +08:00
|
|
|
memcpy(path, objdir, len);
|
|
|
|
if (len && objdir[len-1] != '/')
|
|
|
|
path[len++] = '/';
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
DIR *d;
|
|
|
|
sprintf(path + len, "%02x", i);
|
|
|
|
d = opendir(path);
|
|
|
|
if (!d)
|
|
|
|
continue;
|
|
|
|
count_objects(d, path, len, verbose,
|
|
|
|
&loose, &loose_size, &packed_loose, &garbage);
|
|
|
|
closedir(d);
|
|
|
|
}
|
|
|
|
if (verbose) {
|
|
|
|
struct packed_git *p;
|
2006-12-27 17:04:03 +08:00
|
|
|
unsigned long num_pack = 0;
|
2006-05-03 14:03:15 +08:00
|
|
|
if (!packed_git)
|
|
|
|
prepare_packed_git();
|
2006-04-28 06:37:18 +08:00
|
|
|
for (p = packed_git; p; p = p->next) {
|
|
|
|
if (!p->pack_local)
|
|
|
|
continue;
|
2007-05-30 14:12:28 +08:00
|
|
|
if (open_pack_index(p))
|
2007-05-26 13:24:19 +08:00
|
|
|
continue;
|
2007-04-09 13:06:28 +08:00
|
|
|
packed += p->num_objects;
|
2006-12-27 17:04:03 +08:00
|
|
|
num_pack++;
|
2006-04-28 06:37:18 +08:00
|
|
|
}
|
|
|
|
printf("count: %lu\n", loose);
|
|
|
|
printf("size: %lu\n", loose_size / 2);
|
|
|
|
printf("in-pack: %lu\n", packed);
|
2006-12-27 17:04:03 +08:00
|
|
|
printf("packs: %lu\n", num_pack);
|
2006-04-28 06:37:18 +08:00
|
|
|
printf("prune-packable: %lu\n", packed_loose);
|
|
|
|
printf("garbage: %lu\n", garbage);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("%lu objects, %lu kilobytes\n",
|
|
|
|
loose, loose_size / 2);
|
|
|
|
return 0;
|
|
|
|
}
|