2006-04-28 02:37:18 +04:00
|
|
|
/*
|
|
|
|
* Builtin "git count-objects".
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Junio C Hamano
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cache.h"
|
2017-06-14 21:07:36 +03:00
|
|
|
#include "config.h"
|
2009-01-10 15:07:50 +03:00
|
|
|
#include "dir.h"
|
2018-03-23 20:20:59 +03:00
|
|
|
#include "repository.h"
|
2006-04-28 02:37:18 +04:00
|
|
|
#include "builtin.h"
|
2007-10-16 00:38:51 +04:00
|
|
|
#include "parse-options.h"
|
2016-10-03 23:36:18 +03:00
|
|
|
#include "quote.h"
|
2017-08-19 01:20:19 +03:00
|
|
|
#include "packfile.h"
|
2018-03-23 20:20:56 +03:00
|
|
|
#include "object-store.h"
|
2006-04-28 02:37:18 +04:00
|
|
|
|
2013-02-15 16:07:10 +04:00
|
|
|
static unsigned long garbage;
|
2013-02-13 13:13:19 +04:00
|
|
|
static off_t size_garbage;
|
2014-10-16 02:41:11 +04:00
|
|
|
static int verbose;
|
|
|
|
static unsigned long loose, packed, packed_loose;
|
|
|
|
static off_t loose_size;
|
2013-02-15 16:07:10 +04:00
|
|
|
|
2015-08-13 21:02:52 +03:00
|
|
|
static const char *bits_to_msg(unsigned seen_bits)
|
|
|
|
{
|
|
|
|
switch (seen_bits) {
|
|
|
|
case 0:
|
|
|
|
return "no corresponding .idx or .pack";
|
|
|
|
case PACKDIR_FILE_GARBAGE:
|
|
|
|
return "garbage found";
|
|
|
|
case PACKDIR_FILE_PACK:
|
|
|
|
return "no corresponding .idx";
|
|
|
|
case PACKDIR_FILE_IDX:
|
|
|
|
return "no corresponding .pack";
|
|
|
|
case PACKDIR_FILE_PACK|PACKDIR_FILE_IDX:
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void real_report_garbage(unsigned seen_bits, const char *path)
|
2013-02-15 16:07:10 +04:00
|
|
|
{
|
2013-02-13 13:13:19 +04:00
|
|
|
struct stat st;
|
2015-08-13 21:02:52 +03:00
|
|
|
const char *desc = bits_to_msg(seen_bits);
|
|
|
|
|
|
|
|
if (!desc)
|
|
|
|
return;
|
|
|
|
|
2013-02-13 13:13:19 +04:00
|
|
|
if (!stat(path, &st))
|
|
|
|
size_garbage += st.st_size;
|
2013-02-15 16:07:10 +04:00
|
|
|
warning("%s: %s", desc, path);
|
|
|
|
garbage++;
|
|
|
|
}
|
|
|
|
|
2014-10-16 02:41:11 +04:00
|
|
|
static void loose_garbage(const char *path)
|
2006-04-28 02:37:18 +04:00
|
|
|
{
|
2014-10-16 02:41:11 +04:00
|
|
|
if (verbose)
|
2015-08-13 21:02:52 +03:00
|
|
|
report_garbage(PACKDIR_FILE_GARBAGE, path);
|
2014-10-16 02:41:11 +04:00
|
|
|
}
|
2006-04-28 02:37:18 +04:00
|
|
|
|
2017-02-22 02:47:35 +03:00
|
|
|
static int count_loose(const struct object_id *oid, const char *path, void *data)
|
2014-10-16 02:41:11 +04:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (lstat(path, &st) || !S_ISREG(st.st_mode))
|
|
|
|
loose_garbage(path);
|
|
|
|
else {
|
|
|
|
loose_size += on_disk_bytes(st);
|
|
|
|
loose++;
|
2018-05-02 03:25:33 +03:00
|
|
|
if (verbose && has_object_pack(oid))
|
2014-10-16 02:41:11 +04:00
|
|
|
packed_loose++;
|
2006-04-28 02:37:18 +04:00
|
|
|
}
|
2014-10-16 02:41:11 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int count_cruft(const char *basename, const char *path, void *data)
|
|
|
|
{
|
|
|
|
loose_garbage(path);
|
|
|
|
return 0;
|
2006-04-28 02:37:18 +04:00
|
|
|
}
|
|
|
|
|
2018-11-12 17:48:47 +03:00
|
|
|
static int print_alternate(struct object_directory *odb, void *data)
|
2016-10-03 23:36:18 +03:00
|
|
|
{
|
|
|
|
printf("alternate: ");
|
2018-11-12 17:48:47 +03:00
|
|
|
quote_c_style(odb->path, NULL, stdout, 0);
|
2016-10-03 23:36:18 +03:00
|
|
|
putchar('\n');
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-16 00:38:51 +04:00
|
|
|
static char const * const count_objects_usage[] = {
|
2013-04-10 23:03:24 +04:00
|
|
|
N_("git count-objects [-v] [-H | --human-readable]"),
|
2007-10-16 00:38:51 +04:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
int cmd_count_objects(int argc, const char **argv, const char *prefix)
|
2006-04-28 02:37:18 +04:00
|
|
|
{
|
2014-10-16 02:41:11 +04:00
|
|
|
int human_readable = 0;
|
2007-10-16 00:38:51 +04:00
|
|
|
struct option opts[] = {
|
2012-08-20 16:32:06 +04:00
|
|
|
OPT__VERBOSE(&verbose, N_("be verbose")),
|
2013-04-10 23:03:24 +04:00
|
|
|
OPT_BOOL('H', "human-readable", &human_readable,
|
|
|
|
N_("print sizes in human readable format")),
|
2007-10-16 00:38:51 +04:00
|
|
|
OPT_END(),
|
|
|
|
};
|
2006-04-28 02:37:18 +04:00
|
|
|
|
2016-10-03 23:36:26 +03:00
|
|
|
git_config(git_default_config, NULL);
|
|
|
|
|
2009-05-23 22:53:12 +04:00
|
|
|
argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
|
2006-04-28 02:37:18 +04:00
|
|
|
/* we do not take arguments other than flags for now */
|
2007-10-16 00:38:51 +04:00
|
|
|
if (argc)
|
|
|
|
usage_with_options(count_objects_usage, opts);
|
2014-11-30 11:24:54 +03:00
|
|
|
if (verbose) {
|
2013-02-15 16:07:10 +04:00
|
|
|
report_garbage = real_report_garbage;
|
2014-11-30 11:24:54 +03:00
|
|
|
report_linked_checkout_garbage();
|
|
|
|
}
|
2014-10-16 02:41:11 +04:00
|
|
|
|
|
|
|
for_each_loose_file_in_objdir(get_object_directory(),
|
|
|
|
count_loose, count_cruft, NULL, NULL);
|
|
|
|
|
2006-04-28 02:37:18 +04:00
|
|
|
if (verbose) {
|
|
|
|
struct packed_git *p;
|
2006-12-27 12:04:03 +03:00
|
|
|
unsigned long num_pack = 0;
|
2009-12-29 22:09:15 +03:00
|
|
|
off_t size_pack = 0;
|
2013-04-10 23:03:24 +04:00
|
|
|
struct strbuf loose_buf = STRBUF_INIT;
|
|
|
|
struct strbuf pack_buf = STRBUF_INIT;
|
|
|
|
struct strbuf garbage_buf = STRBUF_INIT;
|
2018-03-23 20:45:27 +03:00
|
|
|
|
2018-08-20 19:52:04 +03:00
|
|
|
for (p = get_all_packs(the_repository); p; p = p->next) {
|
2006-04-28 02:37:18 +04:00
|
|
|
if (!p->pack_local)
|
|
|
|
continue;
|
2007-05-30 10:12:28 +04:00
|
|
|
if (open_pack_index(p))
|
2007-05-26 09:24:19 +04:00
|
|
|
continue;
|
2007-04-09 09:06:28 +04:00
|
|
|
packed += p->num_objects;
|
2008-08-15 08:20:20 +04:00
|
|
|
size_pack += p->pack_size + p->index_size;
|
2006-12-27 12:04:03 +03:00
|
|
|
num_pack++;
|
2006-04-28 02:37:18 +04:00
|
|
|
}
|
2013-04-10 23:03:24 +04: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 02:37:18 +04:00
|
|
|
printf("count: %lu\n", loose);
|
2013-04-10 23:03:24 +04:00
|
|
|
printf("size: %s\n", loose_buf.buf);
|
2006-04-28 02:37:18 +04:00
|
|
|
printf("in-pack: %lu\n", packed);
|
2006-12-27 12:04:03 +03:00
|
|
|
printf("packs: %lu\n", num_pack);
|
2013-04-10 23:03:24 +04:00
|
|
|
printf("size-pack: %s\n", pack_buf.buf);
|
2006-04-28 02:37:18 +04:00
|
|
|
printf("prune-packable: %lu\n", packed_loose);
|
|
|
|
printf("garbage: %lu\n", garbage);
|
2013-04-10 23:03:24 +04:00
|
|
|
printf("size-garbage: %s\n", garbage_buf.buf);
|
2016-10-03 23:36:18 +03:00
|
|
|
foreach_alt_odb(print_alternate, NULL);
|
2013-04-10 23:03:24 +04:00
|
|
|
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 02:37:18 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|