8 static const char prune_usage[] = "git-prune [-n]";
10 static unsigned long expire;
12 static int prune_object(char *path, const char *filename, const unsigned char *sha1)
14 const char *fullpath = mkpath("%s/%s", path, filename);
17 if (lstat(fullpath, &st))
18 return error("Could not stat '%s'", fullpath);
19 if (st.st_mtime > expire)
23 enum object_type type = sha1_object_info(sha1, NULL);
24 printf("%s %s\n", sha1_to_hex(sha1),
25 (type > 0) ? typename(type) : "unknown");
31 static int prune_dir(int i, char *path)
33 DIR *dir = opendir(path);
39 while ((de = readdir(dir)) != NULL) {
41 unsigned char sha1[20];
42 int len = strlen(de->d_name);
46 if (de->d_name[1] != '.')
49 if (de->d_name[0] != '.')
53 sprintf(name, "%02x", i);
54 memcpy(name+2, de->d_name, len+1);
55 if (get_sha1_hex(name, sha1) < 0)
59 * Do we know about this object?
60 * It must have been reachable
62 if (lookup_object(sha1))
65 prune_object(path, de->d_name, sha1);
68 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
76 static void prune_object_dir(const char *path)
79 for (i = 0; i < 256; i++) {
80 static char dir[4096];
81 sprintf(dir, "%s/%02x", path, i);
87 * Write errors (particularly out of space) can result in
88 * failed temporary packs (and more rarely indexes and other
89 * files begining with "tmp_") accumulating in the
92 static void remove_temporary_files(void)
96 char* dirname=get_object_directory();
98 dir = opendir(dirname);
100 fprintf(stderr, "Unable to open object directory %s\n",
104 while ((de = readdir(dir)) != NULL) {
105 if (!prefixcmp(de->d_name, "tmp_")) {
107 int c = snprintf(name, PATH_MAX, "%s/%s",
108 dirname, de->d_name);
109 if (c < 0 || c >= PATH_MAX)
113 if (stat(name, &st) != 0 || st.st_mtime >= expire)
116 printf("Removing stale temporary file %s\n", name);
124 int cmd_prune(int argc, const char **argv, const char *prefix)
127 struct rev_info revs;
129 for (i = 1; i < argc; i++) {
130 const char *arg = argv[i];
131 if (!strcmp(arg, "-n")) {
135 if (!strcmp(arg, "--expire")) {
137 expire = approxidate(argv[i]);
141 else if (!prefixcmp(arg, "--expire=")) {
142 expire = approxidate(arg + 9);
148 save_commit_buffer = 0;
149 init_revisions(&revs, prefix);
150 mark_reachable_objects(&revs, 1);
152 prune_object_dir(get_object_directory());
155 prune_packed_objects(show_only);
156 remove_temporary_files();