Skip to content

Commit 0d3b729

Browse files
peffgitster
authored andcommitted
prune-packed: use for_each_loose_file_in_objdir
This saves us from manually traversing the directory structure ourselves. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3725427 commit 0d3b729

File tree

1 file changed

+23
-46
lines changed

1 file changed

+23
-46
lines changed

builtin/prune-packed.c

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,42 @@ static const char * const prune_packed_usage[] = {
1010

1111
static struct progress *progress;
1212

13-
static void prune_dir(int i, DIR *dir, struct strbuf *pathname, int opts)
13+
static int prune_subdir(int nr, const char *path, void *data)
1414
{
15-
struct dirent *de;
16-
char hex[40];
17-
int top_len = pathname->len;
15+
int *opts = data;
16+
display_progress(progress, nr + 1);
17+
if (!(*opts & PRUNE_PACKED_DRY_RUN))
18+
rmdir(path);
19+
return 0;
20+
}
21+
22+
static int prune_object(const unsigned char *sha1, const char *path,
23+
void *data)
24+
{
25+
int *opts = data;
1826

19-
sprintf(hex, "%02x", i);
20-
while ((de = readdir(dir)) != NULL) {
21-
unsigned char sha1[20];
22-
if (strlen(de->d_name) != 38)
23-
continue;
24-
memcpy(hex + 2, de->d_name, 38);
25-
if (get_sha1_hex(hex, sha1))
26-
continue;
27-
if (!has_sha1_pack(sha1))
28-
continue;
27+
if (!has_sha1_pack(sha1))
28+
return 0;
2929

30-
strbuf_add(pathname, de->d_name, 38);
31-
if (opts & PRUNE_PACKED_DRY_RUN)
32-
printf("rm -f %s\n", pathname->buf);
33-
else
34-
unlink_or_warn(pathname->buf);
35-
display_progress(progress, i + 1);
36-
strbuf_setlen(pathname, top_len);
37-
}
30+
if (*opts & PRUNE_PACKED_DRY_RUN)
31+
printf("rm -f %s\n", path);
32+
else
33+
unlink_or_warn(path);
34+
return 0;
3835
}
3936

4037
void prune_packed_objects(int opts)
4138
{
42-
int i;
43-
const char *dir = get_object_directory();
44-
struct strbuf pathname = STRBUF_INIT;
45-
int top_len;
46-
47-
strbuf_addstr(&pathname, dir);
4839
if (opts & PRUNE_PACKED_VERBOSE)
4940
progress = start_progress_delay(_("Removing duplicate objects"),
5041
256, 95, 2);
5142

52-
if (pathname.len && pathname.buf[pathname.len - 1] != '/')
53-
strbuf_addch(&pathname, '/');
54-
55-
top_len = pathname.len;
56-
for (i = 0; i < 256; i++) {
57-
DIR *d;
43+
for_each_loose_file_in_objdir(get_object_directory(),
44+
prune_object, NULL, prune_subdir, &opts);
5845

59-
display_progress(progress, i + 1);
60-
strbuf_setlen(&pathname, top_len);
61-
strbuf_addf(&pathname, "%02x/", i);
62-
d = opendir(pathname.buf);
63-
if (!d)
64-
continue;
65-
prune_dir(i, d, &pathname, opts);
66-
closedir(d);
67-
strbuf_setlen(&pathname, top_len + 2);
68-
rmdir(pathname.buf);
69-
}
46+
/* Ensure we show 100% before finishing progress */
47+
display_progress(progress, 256);
7048
stop_progress(&progress);
71-
strbuf_release(&pathname);
7249
}
7350

7451
int cmd_prune_packed(int argc, const char **argv, const char *prefix)

0 commit comments

Comments
 (0)