Revert bd2f73a6(Support the tools scripted in perl.)
[git/platforms.git] / builtin-prune.c
blob02e4c09b0e2987bf397aa1d81663af524edab2d8
1 #include "cache.h"
2 #include "commit.h"
3 #include "diff.h"
4 #include "revision.h"
5 #include "builtin.h"
6 #include "reachable.h"
8 static const char prune_usage[] = "git-prune [-n]";
9 static int show_only;
11 void sync(void);
13 static int prune_object(char *path, const char *filename, const unsigned char *sha1)
15 if (show_only) {
16 enum object_type type = sha1_object_info(sha1, NULL);
17 printf("%s %s\n", sha1_to_hex(sha1),
18 (type > 0) ? typename(type) : "unknown");
19 } else
20 unlink(mkpath("%s/%s", path, filename));
21 return 0;
24 static int prune_dir(int i, char *path)
26 DIR *dir = opendir(path);
27 struct dirent *de;
29 if (!dir)
30 return 0;
32 while ((de = readdir(dir)) != NULL) {
33 char name[100];
34 unsigned char sha1[20];
35 int len = strlen(de->d_name);
37 switch (len) {
38 case 2:
39 if (de->d_name[1] != '.')
40 break;
41 case 1:
42 if (de->d_name[0] != '.')
43 break;
44 continue;
45 case 38:
46 sprintf(name, "%02x", i);
47 memcpy(name+2, de->d_name, len+1);
48 if (get_sha1_hex(name, sha1) < 0)
49 break;
52 * Do we know about this object?
53 * It must have been reachable
55 if (lookup_object(sha1))
56 continue;
58 prune_object(path, de->d_name, sha1);
59 continue;
61 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
63 if (!show_only)
64 rmdir(path);
65 closedir(dir);
66 return 0;
69 static void prune_object_dir(const char *path)
71 int i;
72 for (i = 0; i < 256; i++) {
73 static char dir[4096];
74 sprintf(dir, "%s/%02x", path, i);
75 prune_dir(i, dir);
79 int cmd_prune(int argc, const char **argv, const char *prefix)
81 int i;
82 struct rev_info revs;
84 for (i = 1; i < argc; i++) {
85 const char *arg = argv[i];
86 if (!strcmp(arg, "-n")) {
87 show_only = 1;
88 continue;
90 usage(prune_usage);
93 save_commit_buffer = 0;
94 init_revisions(&revs, prefix);
95 mark_reachable_objects(&revs, 1);
97 prune_object_dir(get_object_directory());
99 sync();
100 prune_packed_objects(show_only);
101 return 0;