blob: 7b4ec80e62997fe70f53c92380cd4d66e80d62a0 [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[] = {
Michael J Gruberb35ddf42008-09-29 18:49:52 +020010 "git prune [-n] [-v] [--expire <time>] [--] [<head>...]",
Michele Ballabio629de472008-03-23 21:50:29 +010011 NULL
12};
David Rientjes96f1e582006-08-15 10:23:48 -070013static int show_only;
Michael J Gruberb35ddf42008-09-29 18:49:52 +020014static int verbose;
Johannes Schindelinf01913e2007-11-29 20:59:55 +000015static unsigned long expire;
Linus Torvaldsba84a792006-07-06 10:16:22 -070016
Brandon Caseydb87e392008-09-22 18:34:26 -050017static int prune_tmp_object(const char *path, const char *filename)
Brandon Casey0e8316c2008-07-24 17:41:12 -050018{
19 const char *fullpath = mkpath("%s/%s", path, filename);
20 if (expire) {
21 struct stat st;
22 if (lstat(fullpath, &st))
23 return error("Could not stat '%s'", fullpath);
24 if (st.st_mtime > expire)
25 return 0;
26 }
27 printf("Removing stale temporary file %s\n", fullpath);
28 if (!show_only)
29 unlink(fullpath);
30 return 0;
31}
32
Linus Torvaldsba84a792006-07-06 10:16:22 -070033static int prune_object(char *path, const char *filename, const unsigned char *sha1)
34{
Johannes Schindelinf01913e2007-11-29 20:59:55 +000035 const char *fullpath = mkpath("%s/%s", path, filename);
36 if (expire) {
37 struct stat st;
38 if (lstat(fullpath, &st))
39 return error("Could not stat '%s'", fullpath);
40 if (st.st_mtime > expire)
41 return 0;
42 }
Michael J Gruberb35ddf42008-09-29 18:49:52 +020043 if (show_only || verbose) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050044 enum object_type type = sha1_object_info(sha1, NULL);
45 printf("%s %s\n", sha1_to_hex(sha1),
46 (type > 0) ? typename(type) : "unknown");
Michael J Gruberb35ddf42008-09-29 18:49:52 +020047 }
48 if (!show_only)
Johannes Schindelinf01913e2007-11-29 20:59:55 +000049 unlink(fullpath);
Linus Torvaldsba84a792006-07-06 10:16:22 -070050 return 0;
51}
52
53static int prune_dir(int i, char *path)
54{
55 DIR *dir = opendir(path);
56 struct dirent *de;
57
58 if (!dir)
59 return 0;
60
61 while ((de = readdir(dir)) != NULL) {
62 char name[100];
63 unsigned char sha1[20];
64 int len = strlen(de->d_name);
65
66 switch (len) {
67 case 2:
68 if (de->d_name[1] != '.')
69 break;
70 case 1:
71 if (de->d_name[0] != '.')
72 break;
73 continue;
74 case 38:
75 sprintf(name, "%02x", i);
76 memcpy(name+2, de->d_name, len+1);
77 if (get_sha1_hex(name, sha1) < 0)
78 break;
79
80 /*
81 * Do we know about this object?
82 * It must have been reachable
83 */
84 if (lookup_object(sha1))
85 continue;
86
87 prune_object(path, de->d_name, sha1);
88 continue;
89 }
Brandon Casey3d32a462008-08-05 13:01:50 -050090 if (!prefixcmp(de->d_name, "tmp_obj_")) {
91 prune_tmp_object(path, de->d_name);
92 continue;
93 }
Linus Torvaldsba84a792006-07-06 10:16:22 -070094 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
95 }
Nicolas Pitre3254d212007-03-20 23:32:13 -040096 if (!show_only)
97 rmdir(path);
Linus Torvaldsba84a792006-07-06 10:16:22 -070098 closedir(dir);
99 return 0;
100}
101
102static void prune_object_dir(const char *path)
103{
104 int i;
105 for (i = 0; i < 256; i++) {
106 static char dir[4096];
107 sprintf(dir, "%s/%02x", path, i);
108 prune_dir(i, dir);
109 }
110}
111
David Steven Tweed84640102008-02-07 02:55:14 +0000112/*
113 * Write errors (particularly out of space) can result in
114 * failed temporary packs (and more rarely indexes and other
Brandon Caseydb87e392008-09-22 18:34:26 -0500115 * files begining with "tmp_") accumulating in the object
116 * and the pack directories.
David Steven Tweed84640102008-02-07 02:55:14 +0000117 */
Brandon Caseydb87e392008-09-22 18:34:26 -0500118static void remove_temporary_files(const char *path)
David Steven Tweed84640102008-02-07 02:55:14 +0000119{
120 DIR *dir;
121 struct dirent *de;
David Steven Tweed84640102008-02-07 02:55:14 +0000122
Brandon Caseydb87e392008-09-22 18:34:26 -0500123 dir = opendir(path);
David Steven Tweed84640102008-02-07 02:55:14 +0000124 if (!dir) {
Brandon Caseydb87e392008-09-22 18:34:26 -0500125 fprintf(stderr, "Unable to open directory %s\n", path);
David Steven Tweed84640102008-02-07 02:55:14 +0000126 return;
127 }
Brandon Casey0e8316c2008-07-24 17:41:12 -0500128 while ((de = readdir(dir)) != NULL)
129 if (!prefixcmp(de->d_name, "tmp_"))
Brandon Caseydb87e392008-09-22 18:34:26 -0500130 prune_tmp_object(path, de->d_name);
David Steven Tweed84640102008-02-07 02:55:14 +0000131 closedir(dir);
132}
133
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700134int cmd_prune(int argc, const char **argv, const char *prefix)
Linus Torvaldsba84a792006-07-06 10:16:22 -0700135{
Junio C Hamano24304812007-01-06 02:16:10 -0800136 struct rev_info revs;
Michele Ballabio629de472008-03-23 21:50:29 +0100137 const struct option options[] = {
138 OPT_BOOLEAN('n', NULL, &show_only,
139 "do not remove, show only"),
Michael J Gruberb35ddf42008-09-29 18:49:52 +0200140 OPT_BOOLEAN('v', NULL, &verbose,
141 "report pruned objects"),
Michele Ballabio629de472008-03-23 21:50:29 +0100142 OPT_DATE(0, "expire", &expire,
143 "expire objects older than <time>"),
144 OPT_END()
145 };
Brandon Caseydb87e392008-09-22 18:34:26 -0500146 char *s;
Linus Torvaldsba84a792006-07-06 10:16:22 -0700147
Junio C Hamano16157b82007-01-05 13:31:43 -0800148 save_commit_buffer = 0;
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700149 init_revisions(&revs, prefix);
Linus Torvaldsba84a792006-07-06 10:16:22 -0700150
Michele Ballabio629de472008-03-23 21:50:29 +0100151 argc = parse_options(argc, argv, options, prune_usage, 0);
Junio C Hamanofe308f52008-03-24 23:20:51 -0700152 while (argc--) {
153 unsigned char sha1[20];
154 const char *name = *argv++;
155
156 if (!get_sha1(name, sha1)) {
157 struct object *object = parse_object(sha1);
158 if (!object)
159 die("bad object: %s", name);
160 add_pending_object(&revs, object, "");
161 }
162 else
163 die("unrecognized argument: %s", name);
164 }
Michele Ballabio629de472008-03-23 21:50:29 +0100165 mark_reachable_objects(&revs, 1);
Linus Torvaldsba84a792006-07-06 10:16:22 -0700166 prune_object_dir(get_object_directory());
167
J. Bruce Fields2eb53e62006-10-22 19:01:23 -0400168 prune_packed_objects(show_only);
Brandon Caseydb87e392008-09-22 18:34:26 -0500169 remove_temporary_files(get_object_directory());
170 s = xstrdup(mkpath("%s/pack", get_object_directory()));
171 remove_temporary_files(s);
172 free(s);
Linus Torvaldsba84a792006-07-06 10:16:22 -0700173 return 0;
174}