Code

builtin-fetch.c (store_updated_refs): Honor update_local_ref() return value
[git.git] / builtin-prune.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "diff.h"
4 #include "revision.h"
5 #include "builtin.h"
6 #include "reachable.h"
7 #include "parse-options.h"
9 static const char * const prune_usage[] = {
10         "git-prune [-n] [--expire <time>] [--] [<head>...]",
11         NULL
12 };
13 static int show_only;
14 static unsigned long expire;
16 static int prune_object(char *path, const char *filename, const unsigned char *sha1)
17 {
18         const char *fullpath = mkpath("%s/%s", path, filename);
19         if (expire) {
20                 struct stat st;
21                 if (lstat(fullpath, &st))
22                         return error("Could not stat '%s'", fullpath);
23                 if (st.st_mtime > expire)
24                         return 0;
25         }
26         if (show_only) {
27                 enum object_type type = sha1_object_info(sha1, NULL);
28                 printf("%s %s\n", sha1_to_hex(sha1),
29                        (type > 0) ? typename(type) : "unknown");
30         } else
31                 unlink(fullpath);
32         return 0;
33 }
35 static int prune_dir(int i, char *path)
36 {
37         DIR *dir = opendir(path);
38         struct dirent *de;
40         if (!dir)
41                 return 0;
43         while ((de = readdir(dir)) != NULL) {
44                 char name[100];
45                 unsigned char sha1[20];
46                 int len = strlen(de->d_name);
48                 switch (len) {
49                 case 2:
50                         if (de->d_name[1] != '.')
51                                 break;
52                 case 1:
53                         if (de->d_name[0] != '.')
54                                 break;
55                         continue;
56                 case 38:
57                         sprintf(name, "%02x", i);
58                         memcpy(name+2, de->d_name, len+1);
59                         if (get_sha1_hex(name, sha1) < 0)
60                                 break;
62                         /*
63                          * Do we know about this object?
64                          * It must have been reachable
65                          */
66                         if (lookup_object(sha1))
67                                 continue;
69                         prune_object(path, de->d_name, sha1);
70                         continue;
71                 }
72                 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
73         }
74         if (!show_only)
75                 rmdir(path);
76         closedir(dir);
77         return 0;
78 }
80 static void prune_object_dir(const char *path)
81 {
82         int i;
83         for (i = 0; i < 256; i++) {
84                 static char dir[4096];
85                 sprintf(dir, "%s/%02x", path, i);
86                 prune_dir(i, dir);
87         }
88 }
90 /*
91  * Write errors (particularly out of space) can result in
92  * failed temporary packs (and more rarely indexes and other
93  * files begining with "tmp_") accumulating in the
94  * object directory.
95  */
96 static void remove_temporary_files(void)
97 {
98         DIR *dir;
99         struct dirent *de;
100         char* dirname=get_object_directory();
102         dir = opendir(dirname);
103         if (!dir) {
104                 fprintf(stderr, "Unable to open object directory %s\n",
105                         dirname);
106                 return;
107         }
108         while ((de = readdir(dir)) != NULL) {
109                 if (!prefixcmp(de->d_name, "tmp_")) {
110                         char name[PATH_MAX];
111                         int c = snprintf(name, PATH_MAX, "%s/%s",
112                                          dirname, de->d_name);
113                         if (c < 0 || c >= PATH_MAX)
114                                 continue;
115                         if (expire) {
116                                 struct stat st;
117                                 if (stat(name, &st) != 0 || st.st_mtime >= expire)
118                                         continue;
119                         }
120                         printf("Removing stale temporary file %s\n", name);
121                         if (!show_only)
122                                 unlink(name);
123                 }
124         }
125         closedir(dir);
128 int cmd_prune(int argc, const char **argv, const char *prefix)
130         struct rev_info revs;
131         const struct option options[] = {
132                 OPT_BOOLEAN('n', NULL, &show_only,
133                             "do not remove, show only"),
134                 OPT_DATE(0, "expire", &expire,
135                          "expire objects older than <time>"),
136                 OPT_END()
137         };
139         save_commit_buffer = 0;
140         init_revisions(&revs, prefix);
142         argc = parse_options(argc, argv, options, prune_usage, 0);
143         while (argc--) {
144                 unsigned char sha1[20];
145                 const char *name = *argv++;
147                 if (!get_sha1(name, sha1)) {
148                         struct object *object = parse_object(sha1);
149                         if (!object)
150                                 die("bad object: %s", name);
151                         add_pending_object(&revs, object, "");
152                 }
153                 else
154                         die("unrecognized argument: %s", name);
155         }
156         mark_reachable_objects(&revs, 1);
157         prune_object_dir(get_object_directory());
159         sync();
160         prune_packed_objects(show_only);
161         remove_temporary_files();
162         return 0;