blob: 25f9304b829c9074e8a034eba5e34c38794058a4 [file] [log] [blame]
Linus Torvaldsba84a792006-07-06 10:16:22 -07001#include "cache.h"
Linus Torvaldsba84a792006-07-06 10:16:22 -07002#include "commit.h"
Linus Torvaldsba84a792006-07-06 10:16:22 -07003#include "diff.h"
4#include "revision.h"
5#include "builtin.h"
Junio C Hamano94421472007-01-06 02:16:17 -08006#include "reachable.h"
Michele Ballabio629de472008-03-23 21:50:29 +01007#include "parse-options.h"
Linus Torvaldsba84a792006-07-06 10:16:22 -07008
Michele Ballabio629de472008-03-23 21:50:29 +01009static const char * const prune_usage[] = {
10 "git-prune [-n] [--expire <time>] [--] [<head>...]",
11 NULL
12};
David Rientjes96f1e582006-08-15 10:23:48 -070013static int show_only;
Johannes Schindelinf01913e2007-11-29 20:59:55 +000014static unsigned long expire;
Linus Torvaldsba84a792006-07-06 10:16:22 -070015
16static int prune_object(char *path, const char *filename, const unsigned char *sha1)
17{
Johannes Schindelinf01913e2007-11-29 20:59:55 +000018 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 }
Linus Torvaldsba84a792006-07-06 10:16:22 -070026 if (show_only) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050027 enum object_type type = sha1_object_info(sha1, NULL);
28 printf("%s %s\n", sha1_to_hex(sha1),
29 (type > 0) ? typename(type) : "unknown");
Nicolas Pitre3254d212007-03-20 23:32:13 -040030 } else
Johannes Schindelinf01913e2007-11-29 20:59:55 +000031 unlink(fullpath);
Linus Torvaldsba84a792006-07-06 10:16:22 -070032 return 0;
33}
34
35static int prune_dir(int i, char *path)
36{
37 DIR *dir = opendir(path);
38 struct dirent *de;
39
40 if (!dir)
41 return 0;
42
43 while ((de = readdir(dir)) != NULL) {
44 char name[100];
45 unsigned char sha1[20];
46 int len = strlen(de->d_name);
47
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;
61
62 /*
63 * Do we know about this object?
64 * It must have been reachable
65 */
66 if (lookup_object(sha1))
67 continue;
68
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 }
Nicolas Pitre3254d212007-03-20 23:32:13 -040074 if (!show_only)
75 rmdir(path);
Linus Torvaldsba84a792006-07-06 10:16:22 -070076 closedir(dir);
77 return 0;
78}
79
80static 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}
89
David Steven Tweed84640102008-02-07 02:55:14 +000090/*
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 */
96static void remove_temporary_files(void)
97{
98 DIR *dir;
99 struct dirent *de;
100 char* dirname=get_object_directory();
101
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);
126}
127
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700128int cmd_prune(int argc, const char **argv, const char *prefix)
Linus Torvaldsba84a792006-07-06 10:16:22 -0700129{
Junio C Hamano24304812007-01-06 02:16:10 -0800130 struct rev_info revs;
Michele Ballabio629de472008-03-23 21:50:29 +0100131 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 };
Linus Torvaldsba84a792006-07-06 10:16:22 -0700138
Junio C Hamano16157b82007-01-05 13:31:43 -0800139 save_commit_buffer = 0;
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700140 init_revisions(&revs, prefix);
Linus Torvaldsba84a792006-07-06 10:16:22 -0700141
Michele Ballabio629de472008-03-23 21:50:29 +0100142 argc = parse_options(argc, argv, options, prune_usage, 0);
Junio C Hamanofe308f52008-03-24 23:20:51 -0700143 while (argc--) {
144 unsigned char sha1[20];
145 const char *name = *argv++;
146
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 }
Michele Ballabio629de472008-03-23 21:50:29 +0100156 mark_reachable_objects(&revs, 1);
Linus Torvaldsba84a792006-07-06 10:16:22 -0700157 prune_object_dir(get_object_directory());
158
J. Bruce Fields2eb53e62006-10-22 19:01:23 -0400159 sync();
160 prune_packed_objects(show_only);
David Steven Tweed84640102008-02-07 02:55:14 +0000161 remove_temporary_files();
Linus Torvaldsba84a792006-07-06 10:16:22 -0700162 return 0;
163}