Skip to content

Commit 0a489b0

Browse files
committed
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 <[email protected]>
1 parent e88b858 commit 0a489b0

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

builtin/count-objects.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,31 @@ static int verbose;
1515
static unsigned long loose, packed, packed_loose;
1616
static off_t loose_size;
1717

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)
1936
{
2037
struct stat st;
38+
const char *desc = bits_to_msg(seen_bits);
39+
40+
if (!desc)
41+
return;
42+
2143
if (!stat(path, &st))
2244
size_garbage += st.st_size;
2345
warning("%s: %s", desc, path);
@@ -27,7 +49,7 @@ static void real_report_garbage(const char *desc, const char *path)
2749
static void loose_garbage(const char *path)
2850
{
2951
if (verbose)
30-
report_garbage("garbage found", path);
52+
report_garbage(PACKDIR_FILE_GARBAGE, path);
3153
}
3254

3355
static int count_loose(const unsigned char *sha1, const char *path, void *data)

cache.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,8 +1255,11 @@ struct pack_entry {
12551255

12561256
extern struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path);
12571257

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

12611264
extern void prepare_packed_git(void);
12621265
extern void reprepare_packed_git(void);

path.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void report_linked_checkout_garbage(void)
143143
strbuf_setlen(&sb, len);
144144
strbuf_addstr(&sb, path);
145145
if (file_exists(sb.buf))
146-
report_garbage("unused in linked checkout", sb.buf);
146+
report_garbage(PACKDIR_FILE_GARBAGE, sb.buf);
147147
}
148148
strbuf_release(&sb);
149149
}

sha1_file.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,27 +1183,16 @@ void install_packed_git(struct packed_git *pack)
11831183
packed_git = pack;
11841184
}
11851185

1186-
void (*report_garbage)(const char *desc, const char *path);
1186+
void (*report_garbage)(unsigned seen_bits, const char *path);
11871187

11881188
static void report_helper(const struct string_list *list,
11891189
int seen_bits, int first, int last)
11901190
{
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))
12031192
return;
1204-
}
1193+
12051194
for (; first < last; first++)
1206-
report_garbage(msg, list->items[first].string);
1195+
report_garbage(seen_bits, list->items[first].string);
12071196
}
12081197

12091198
static void report_pack_garbage(struct string_list *list)
@@ -1226,7 +1215,7 @@ static void report_pack_garbage(struct string_list *list)
12261215
if (baselen == -1) {
12271216
const char *dot = strrchr(path, '.');
12281217
if (!dot) {
1229-
report_garbage("garbage found", path);
1218+
report_garbage(PACKDIR_FILE_GARBAGE, path);
12301219
continue;
12311220
}
12321221
baselen = dot - path + 1;
@@ -1298,7 +1287,7 @@ static void prepare_packed_git_one(char *objdir, int local)
12981287
ends_with(de->d_name, ".keep"))
12991288
string_list_append(&garbage, path.buf);
13001289
else
1301-
report_garbage("garbage found", path.buf);
1290+
report_garbage(PACKDIR_FILE_GARBAGE, path.buf);
13021291
}
13031292
closedir(dir);
13041293
report_pack_garbage(&garbage);

0 commit comments

Comments
 (0)