2006-04-28 00:37:18 +02:00
|
|
|
/*
|
|
|
|
* Builtin "git count-objects".
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Junio C Hamano
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cache.h"
|
2009-01-10 13:07:50 +01:00
|
|
|
#include "dir.h"
|
2006-04-28 00:37:18 +02:00
|
|
|
#include "builtin.h"
|
2007-10-15 22:38:51 +02:00
|
|
|
#include "parse-options.h"
|
2006-04-28 00:37:18 +02:00
|
|
|
|
2013-02-15 13:07:10 +01:00
|
|
|
static unsigned long garbage;
|
2013-02-13 10:13:19 +01:00
|
|
|
static off_t size_garbage;
|
2013-02-15 13:07:10 +01:00
|
|
|
|
|
|
|
static void real_report_garbage(const char *desc, const char *path)
|
|
|
|
{
|
2013-02-13 10:13:19 +01:00
|
|
|
struct stat st;
|
|
|
|
if (!stat(path, &st))
|
|
|
|
size_garbage += st.st_size;
|
2013-02-15 13:07:10 +01:00
|
|
|
warning("%s: %s", desc, path);
|
|
|
|
garbage++;
|
|
|
|
}
|
|
|
|
|
2006-04-28 00:37:18 +02:00
|
|
|
static void count_objects(DIR *d, char *path, int len, int verbose,
|
|
|
|
unsigned long *loose,
|
2009-12-29 20:09:15 +01:00
|
|
|
off_t *loose_size,
|
2013-02-13 10:13:19 +01:00
|
|
|
unsigned long *packed_loose)
|
2006-04-28 00:37:18 +02:00
|
|
|
{
|
|
|
|
struct dirent *ent;
|
|
|
|
while ((ent = readdir(d)) != NULL) {
|
|
|
|
char hex[41];
|
|
|
|
unsigned char sha1[20];
|
|
|
|
const char *cp;
|
|
|
|
int bad = 0;
|
|
|
|
|
2009-01-10 13:07:50 +01:00
|
|
|
if (is_dot_or_dotdot(ent->d_name))
|
2006-04-28 00:37:18 +02:00
|
|
|
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
|
2008-08-18 21:57:16 +02:00
|
|
|
(*loose_size) += xsize_t(on_disk_bytes(st));
|
2006-04-28 00:37:18 +02:00
|
|
|
}
|
|
|
|
if (bad) {
|
|
|
|
if (verbose) {
|
2013-02-13 10:13:19 +01:00
|
|
|
struct strbuf sb = STRBUF_INIT;
|
|
|
|
strbuf_addf(&sb, "%.*s/%s",
|
|
|
|
len + 2, path, ent->d_name);
|
|
|
|
report_garbage("garbage found", sb.buf);
|
|
|
|
strbuf_release(&sb);
|
2006-04-28 00:37:18 +02:00
|
|
|
}
|
|
|
|
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");
|
2009-02-28 08:15:53 +01:00
|
|
|
if (has_sha1_pack(sha1))
|
2006-04-28 00:37:18 +02:00
|
|
|
(*packed_loose)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-15 22:38:51 +02:00
|
|
|
static char const * const count_objects_usage[] = {
|
2013-04-10 21:03:24 +02:00
|
|
|
N_("git count-objects [-v] [-H | --human-readable]"),
|
2007-10-15 22:38:51 +02:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
int cmd_count_objects(int argc, const char **argv, const char *prefix)
|
2006-04-28 00:37:18 +02:00
|
|
|
{
|
2013-04-10 21:03:24 +02:00
|
|
|
int i, verbose = 0, human_readable = 0;
|
2006-04-28 00:37:18 +02:00
|
|
|
const char *objdir = get_object_directory();
|
|
|
|
int len = strlen(objdir);
|
|
|
|
char *path = xmalloc(len + 50);
|
2013-02-15 13:07:10 +01:00
|
|
|
unsigned long loose = 0, packed = 0, packed_loose = 0;
|
2009-12-29 20:09:15 +01:00
|
|
|
off_t loose_size = 0;
|
2007-10-15 22:38:51 +02:00
|
|
|
struct option opts[] = {
|
2012-08-20 14:32:06 +02:00
|
|
|
OPT__VERBOSE(&verbose, N_("be verbose")),
|
2013-04-10 21:03:24 +02:00
|
|
|
OPT_BOOL('H', "human-readable", &human_readable,
|
|
|
|
N_("print sizes in human readable format")),
|
2007-10-15 22:38:51 +02:00
|
|
|
OPT_END(),
|
|
|
|
};
|
2006-04-28 00:37:18 +02:00
|
|
|
|
2009-05-23 20:53:12 +02:00
|
|
|
argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
|
2006-04-28 00:37:18 +02:00
|
|
|
/* we do not take arguments other than flags for now */
|
2007-10-15 22:38:51 +02:00
|
|
|
if (argc)
|
|
|
|
usage_with_options(count_objects_usage, opts);
|
2013-02-15 13:07:10 +01:00
|
|
|
if (verbose)
|
|
|
|
report_garbage = real_report_garbage;
|
2006-04-28 00:37:18 +02: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,
|
2013-02-13 10:13:19 +01:00
|
|
|
&loose, &loose_size, &packed_loose);
|
2006-04-28 00:37:18 +02:00
|
|
|
closedir(d);
|
|
|
|
}
|
|
|
|
if (verbose) {
|
|
|
|
struct packed_git *p;
|
2006-12-27 10:04:03 +01:00
|
|
|
unsigned long num_pack = 0;
|
2009-12-29 20:09:15 +01:00
|
|
|
off_t size_pack = 0;
|
2013-04-10 21:03:24 +02:00
|
|
|
struct strbuf loose_buf = STRBUF_INIT;
|
|
|
|
struct strbuf pack_buf = STRBUF_INIT;
|
|
|
|
struct strbuf garbage_buf = STRBUF_INIT;
|
2006-05-03 08:03:15 +02:00
|
|
|
if (!packed_git)
|
|
|
|
prepare_packed_git();
|
2006-04-28 00:37:18 +02:00
|
|
|
for (p = packed_git; p; p = p->next) {
|
|
|
|
if (!p->pack_local)
|
|
|
|
continue;
|
2007-05-30 08:12:28 +02:00
|
|
|
if (open_pack_index(p))
|
2007-05-26 07:24:19 +02:00
|
|
|
continue;
|
2007-04-09 07:06:28 +02:00
|
|
|
packed += p->num_objects;
|
2008-08-15 06:20:20 +02:00
|
|
|
size_pack += p->pack_size + p->index_size;
|
2006-12-27 10:04:03 +01:00
|
|
|
num_pack++;
|
2006-04-28 00:37:18 +02:00
|
|
|
}
|
2013-04-10 21:03:24 +02:00
|
|
|
|
|
|
|
if (human_readable) {
|
|
|
|
strbuf_humanise_bytes(&loose_buf, loose_size);
|
|
|
|
strbuf_humanise_bytes(&pack_buf, size_pack);
|
|
|
|
strbuf_humanise_bytes(&garbage_buf, size_garbage);
|
|
|
|
} else {
|
|
|
|
strbuf_addf(&loose_buf, "%lu",
|
|
|
|
(unsigned long)(loose_size / 1024));
|
|
|
|
strbuf_addf(&pack_buf, "%lu",
|
|
|
|
(unsigned long)(size_pack / 1024));
|
|
|
|
strbuf_addf(&garbage_buf, "%lu",
|
|
|
|
(unsigned long)(size_garbage / 1024));
|
|
|
|
}
|
|
|
|
|
2006-04-28 00:37:18 +02:00
|
|
|
printf("count: %lu\n", loose);
|
2013-04-10 21:03:24 +02:00
|
|
|
printf("size: %s\n", loose_buf.buf);
|
2006-04-28 00:37:18 +02:00
|
|
|
printf("in-pack: %lu\n", packed);
|
2006-12-27 10:04:03 +01:00
|
|
|
printf("packs: %lu\n", num_pack);
|
2013-04-10 21:03:24 +02:00
|
|
|
printf("size-pack: %s\n", pack_buf.buf);
|
2006-04-28 00:37:18 +02:00
|
|
|
printf("prune-packable: %lu\n", packed_loose);
|
|
|
|
printf("garbage: %lu\n", garbage);
|
2013-04-10 21:03:24 +02:00
|
|
|
printf("size-garbage: %s\n", garbage_buf.buf);
|
|
|
|
strbuf_release(&loose_buf);
|
|
|
|
strbuf_release(&pack_buf);
|
|
|
|
strbuf_release(&garbage_buf);
|
|
|
|
} else {
|
|
|
|
struct strbuf buf = STRBUF_INIT;
|
|
|
|
if (human_readable)
|
|
|
|
strbuf_humanise_bytes(&buf, loose_size);
|
|
|
|
else
|
|
|
|
strbuf_addf(&buf, "%lu kilobytes",
|
|
|
|
(unsigned long)(loose_size / 1024));
|
|
|
|
printf("%lu objects, %s\n", loose, buf.buf);
|
|
|
|
strbuf_release(&buf);
|
2006-04-28 00:37:18 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|