prepare_packed_git(): refactor garbage reporting in pack directory

The hook to report "garbage" files in $GIT_OBJECT_DIRECTORY/pack/ could be generic but is too specific to count-object's needs. Move the part to produce human-readable messages to count-objects, and refine the interface to callback with the "bits" with values defined in the cache.h header file, so that other callers (e.g. prune) can later use the same mechanism to enumerate different kinds of garbage files and do something intelligent about them, other than reporting in textual messages. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 13, 2015 at 13:02 UTC 0a489b0680b841d3e7714be53b263ff190c39193
4 files changed +36 -22
builtin/count-objects.c
+24 -2
@@ -15,9 +15,31 @@ static int verbose;
15 static unsigned long loose, packed, packed_loose;
16 static off_t loose_size;
17
18 -static void real_report_garbage(const char *desc, const char *path)
18 +static const char *bits_to_msg(unsigned seen_bits)
19 +{
20 + switch (seen_bits) {
21 + case 0:
22 + return "no corresponding .idx or .pack";
23 + case PACKDIR_FILE_GARBAGE:
24 + return "garbage found";
25 + case PACKDIR_FILE_PACK:
26 + return "no corresponding .idx";
27 + case PACKDIR_FILE_IDX:
28 + return "no corresponding .pack";
29 + case PACKDIR_FILE_PACK|PACKDIR_FILE_IDX:
30 + default:
31 + return NULL;
32 + }
33 +}
34 +
35 +static void real_report_garbage(unsigned seen_bits, const char *path)
36 {
37 struct stat st;
38 + const char *desc = bits_to_msg(seen_bits);
39 +
40 + if (!desc)
41 + return;
42 +
43 if (!stat(path, &st))
44 size_garbage += st.st_size;
45 warning("%s: %s", desc, path);
@@ -27,7 +49,7 @@ static void real_report_garbage(const char *desc, const char *path)
49 static void loose_garbage(const char *path)
50 {
51 if (verbose)
30 - report_garbage("garbage found", path);
52 + report_garbage(PACKDIR_FILE_GARBAGE, path);
53 }
54
55 static int count_loose(const unsigned char *sha1, const char *path, void *data)
cache.h
+5 -2
@@ -1255,8 +1255,11 @@ struct pack_entry {
1255
1256 extern struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path);
1257
1258 -/* A hook for count-objects to report invalid files in pack directory */
1259 -extern void (*report_garbage)(const char *desc, const char *path);
1258 +/* A hook to report invalid files in pack directory */
1259 +#define PACKDIR_FILE_PACK 1
1260 +#define PACKDIR_FILE_IDX 2
1261 +#define PACKDIR_FILE_GARBAGE 4
1262 +extern void (*report_garbage)(unsigned seen_bits, const char *path);
1263
1264 extern void prepare_packed_git(void);
1265 extern void reprepare_packed_git(void);
path.c
+1 -1
@@ -143,7 +143,7 @@ void report_linked_checkout_garbage(void)
143 strbuf_setlen(&sb, len);
144 strbuf_addstr(&sb, path);
145 if (file_exists(sb.buf))
146 - report_garbage("unused in linked checkout", sb.buf);
146 + report_garbage(PACKDIR_FILE_GARBAGE, sb.buf);
147 }
148 strbuf_release(&sb);
149 }
sha1_file.c
+6 -17
@@ -1183,27 +1183,16 @@ void install_packed_git(struct packed_git *pack)
1183 packed_git = pack;
1184 }
1185
1186 -void (*report_garbage)(const char *desc, const char *path);
1186 +void (*report_garbage)(unsigned seen_bits, const char *path);
1187
1188 static void report_helper(const struct string_list *list,
1189 int seen_bits, int first, int last)
1190 {
1191 - const char *msg;
1192 - switch (seen_bits) {
1193 - case 0:
1194 - msg = "no corresponding .idx or .pack";
1195 - break;
1196 - case 1:
1197 - msg = "no corresponding .idx";
1198 - break;
1199 - case 2:
1200 - msg = "no corresponding .pack";
1201 - break;
1202 - default:
1191 + if (seen_bits == (PACKDIR_FILE_PACK|PACKDIR_FILE_IDX))
1192 return;
1204 - }
1193 +
1194 for (; first < last; first++)
1206 - report_garbage(msg, list->items[first].string);
1195 + report_garbage(seen_bits, list->items[first].string);
1196 }
1197
1198 static void report_pack_garbage(struct string_list *list)
@@ -1226,7 +1215,7 @@ static void report_pack_garbage(struct string_list *list)
1215 if (baselen == -1) {
1216 const char *dot = strrchr(path, '.');
1217 if (!dot) {
1229 - report_garbage("garbage found", path);
1218 + report_garbage(PACKDIR_FILE_GARBAGE, path);
1219 continue;
1220 }
1221 baselen = dot - path + 1;
@@ -1298,7 +1287,7 @@ static void prepare_packed_git_one(char *objdir, int local)
1287 ends_with(de->d_name, ".keep"))
1288 string_list_append(&garbage, path.buf);
1289 else
1301 - report_garbage("garbage found", path.buf);
1290 + report_garbage(PACKDIR_FILE_GARBAGE, path.buf);
1291 }
1292 closedir(dir);
1293 report_pack_garbage(&garbage);