count-objects.c 4.2 KB
Newer Older
J
Junio C Hamano 已提交
1 2 3 4 5 6 7
/*
 * Builtin "git count-objects".
 *
 * Copyright (c) 2006 Junio C Hamano
 */

#include "cache.h"
8
#include "config.h"
9
#include "dir.h"
10
#include "repository.h"
J
Junio C Hamano 已提交
11
#include "builtin.h"
12
#include "parse-options.h"
13
#include "quote.h"
14
#include "packfile.h"
15
#include "object-store.h"
J
Junio C Hamano 已提交
16

17
static unsigned long garbage;
18
static off_t size_garbage;
19 20 21
static int verbose;
static unsigned long loose, packed, packed_loose;
static off_t loose_size;
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
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)
41
{
42
	struct stat st;
43 44 45 46 47
	const char *desc = bits_to_msg(seen_bits);

	if (!desc)
		return;

48 49
	if (!stat(path, &st))
		size_garbage += st.st_size;
50 51 52 53
	warning("%s: %s", desc, path);
	garbage++;
}

54
static void loose_garbage(const char *path)
J
Junio C Hamano 已提交
55
{
56
	if (verbose)
57
		report_garbage(PACKDIR_FILE_GARBAGE, path);
58
}
J
Junio C Hamano 已提交
59

60
static int count_loose(const struct object_id *oid, const char *path, void *data)
61 62 63 64 65 66 67 68
{
	struct stat st;

	if (lstat(path, &st) || !S_ISREG(st.st_mode))
		loose_garbage(path);
	else {
		loose_size += on_disk_bytes(st);
		loose++;
69
		if (verbose && has_sha1_pack(oid->hash))
70
			packed_loose++;
J
Junio C Hamano 已提交
71
	}
72 73 74 75 76 77 78
	return 0;
}

static int count_cruft(const char *basename, const char *path, void *data)
{
	loose_garbage(path);
	return 0;
J
Junio C Hamano 已提交
79 80
}

81 82 83 84 85 86 87 88
static int print_alternate(struct alternate_object_database *alt, void *data)
{
	printf("alternate: ");
	quote_c_style(alt->path, NULL, stdout, 0);
	putchar('\n');
	return 0;
}

89
static char const * const count_objects_usage[] = {
90
	N_("git count-objects [-v] [-H | --human-readable]"),
91 92 93 94
	NULL
};

int cmd_count_objects(int argc, const char **argv, const char *prefix)
J
Junio C Hamano 已提交
95
{
96
	int human_readable = 0;
97
	struct option opts[] = {
98
		OPT__VERBOSE(&verbose, N_("be verbose")),
99 100
		OPT_BOOL('H', "human-readable", &human_readable,
			 N_("print sizes in human readable format")),
101 102
		OPT_END(),
	};
J
Junio C Hamano 已提交
103

104 105
	git_config(git_default_config, NULL);

106
	argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
J
Junio C Hamano 已提交
107
	/* we do not take arguments other than flags for now */
108 109
	if (argc)
		usage_with_options(count_objects_usage, opts);
110
	if (verbose) {
111
		report_garbage = real_report_garbage;
112 113
		report_linked_checkout_garbage();
	}
114 115 116 117

	for_each_loose_file_in_objdir(get_object_directory(),
				      count_loose, count_cruft, NULL, NULL);

J
Junio C Hamano 已提交
118 119
	if (verbose) {
		struct packed_git *p;
120
		unsigned long num_pack = 0;
121
		off_t size_pack = 0;
122 123 124
		struct strbuf loose_buf = STRBUF_INIT;
		struct strbuf pack_buf = STRBUF_INIT;
		struct strbuf garbage_buf = STRBUF_INIT;
125
		if (!get_packed_git(the_repository))
126
			prepare_packed_git();
127
		for (p = get_packed_git(the_repository); p; p = p->next) {
J
Junio C Hamano 已提交
128 129
			if (!p->pack_local)
				continue;
130
			if (open_pack_index(p))
131
				continue;
N
Nicolas Pitre 已提交
132
			packed += p->num_objects;
133
			size_pack += p->pack_size + p->index_size;
134
			num_pack++;
J
Junio C Hamano 已提交
135
		}
136 137 138 139 140 141 142 143 144 145 146 147 148 149

		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));
		}

J
Junio C Hamano 已提交
150
		printf("count: %lu\n", loose);
151
		printf("size: %s\n", loose_buf.buf);
J
Junio C Hamano 已提交
152
		printf("in-pack: %lu\n", packed);
153
		printf("packs: %lu\n", num_pack);
154
		printf("size-pack: %s\n", pack_buf.buf);
J
Junio C Hamano 已提交
155 156
		printf("prune-packable: %lu\n", packed_loose);
		printf("garbage: %lu\n", garbage);
157
		printf("size-garbage: %s\n", garbage_buf.buf);
158
		foreach_alt_odb(print_alternate, NULL);
159 160 161 162 163 164 165 166 167 168 169 170
		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);
J
Junio C Hamano 已提交
171 172 173
	}
	return 0;
}