8 static const char prune_usage
[] = "git-prune [-n]";
11 static int prune_object(char *path
, const char *filename
, const unsigned char *sha1
)
14 enum object_type type
= sha1_object_info(sha1
, NULL
);
15 printf("%s %s\n", sha1_to_hex(sha1
),
16 (type
> 0) ? typename(type
) : "unknown");
18 unlink(mkpath("%s/%s", path
, filename
));
22 static int prune_dir(int i
, char *path
)
24 DIR *dir
= opendir(path
);
30 while ((de
= readdir(dir
)) != NULL
) {
32 unsigned char sha1
[20];
33 int len
= strlen(de
->d_name
);
37 if (de
->d_name
[1] != '.')
40 if (de
->d_name
[0] != '.')
44 sprintf(name
, "%02x", i
);
45 memcpy(name
+2, de
->d_name
, len
+1);
46 if (get_sha1_hex(name
, sha1
) < 0)
50 * Do we know about this object?
51 * It must have been reachable
53 if (lookup_object(sha1
))
56 prune_object(path
, de
->d_name
, sha1
);
59 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
67 static void prune_object_dir(const char *path
)
70 for (i
= 0; i
< 256; i
++) {
71 static char dir
[4096];
72 sprintf(dir
, "%s/%02x", path
, i
);
77 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
82 for (i
= 1; i
< argc
; i
++) {
83 const char *arg
= argv
[i
];
84 if (!strcmp(arg
, "-n")) {
91 save_commit_buffer
= 0;
92 init_revisions(&revs
, prefix
);
93 mark_reachable_objects(&revs
, 1);
95 prune_object_dir(get_object_directory());
98 prune_packed_objects(show_only
);