blob: c767a0ac8930166315c26d8ece2e72b4f1942d55 [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[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +020010 "git prune [-n] [--expire <time>] [--] [<head>...]",
Michele Ballabio629de472008-03-23 21:50:29 +010011 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
Brandon Casey0e8316c2008-07-24 17:41:12 -050016static int prune_tmp_object(char *path, const char *filename)
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 printf("Removing stale temporary file %s\n", fullpath);
27 if (!show_only)
28 unlink(fullpath);
29 return 0;
30}
31
Linus Torvaldsba84a792006-07-06 10:16:22 -070032static int prune_object(char *path, const char *filename, const unsigned char *sha1)
33{
Johannes Schindelinf01913e2007-11-29 20:59:55 +000034 const char *fullpath = mkpath("%s/%s", path, filename);
35 if (expire) {
36 struct stat st;
37 if (lstat(fullpath, &st))
38 return error("Could not stat '%s'", fullpath);
39 if (st.st_mtime > expire)
40 return 0;
41 }
Linus Torvaldsba84a792006-07-06 10:16:22 -070042 if (show_only) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050043 enum object_type type = sha1_object_info(sha1, NULL);
44 printf("%s %s\n", sha1_to_hex(sha1),
45 (type > 0) ? typename(type) : "unknown");
Nicolas Pitre3254d212007-03-20 23:32:13 -040046 } else
Johannes Schindelinf01913e2007-11-29 20:59:55 +000047 unlink(fullpath);
Linus Torvaldsba84a792006-07-06 10:16:22 -070048 return 0;
49}
50
51static int prune_dir(int i, char *path)
52{
53 DIR *dir = opendir(path);
54 struct dirent *de;
55
56 if (!dir)
57 return 0;
58
59 while ((de = readdir(dir)) != NULL) {
60 char name[100];
61 unsigned char sha1[20];
62 int len = strlen(de->d_name);
63
64 switch (len) {
65 case 2:
66 if (de->d_name[1] != '.')
67 break;
68 case 1:
69 if (de->d_name[0] != '.')
70 break;
71 continue;
72 case 38:
73 sprintf(name, "%02x", i);
74 memcpy(name+2, de->d_name, len+1);
75 if (get_sha1_hex(name, sha1) < 0)
76 break;
77
78 /*
79 * Do we know about this object?
80 * It must have been reachable
81 */
82 if (lookup_object(sha1))
83 continue;
84
85 prune_object(path, de->d_name, sha1);
86 continue;
87 }
Brandon Casey3d32a462008-08-05 13:01:50 -050088 if (!prefixcmp(de->d_name, "tmp_obj_")) {
89 prune_tmp_object(path, de->d_name);
90 continue;
91 }
Linus Torvaldsba84a792006-07-06 10:16:22 -070092 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
93 }
Nicolas Pitre3254d212007-03-20 23:32:13 -040094 if (!show_only)
95 rmdir(path);
Linus Torvaldsba84a792006-07-06 10:16:22 -070096 closedir(dir);
97 return 0;
98}
99
100static void prune_object_dir(const char *path)
101{
102 int i;
103 for (i = 0; i < 256; i++) {
104 static char dir[4096];
105 sprintf(dir, "%s/%02x", path, i);
106 prune_dir(i, dir);
107 }
108}
109
David Steven Tweed84640102008-02-07 02:55:14 +0000110/*
111 * Write errors (particularly out of space) can result in
112 * failed temporary packs (and more rarely indexes and other
113 * files begining with "tmp_") accumulating in the
114 * object directory.
115 */
116static void remove_temporary_files(void)
117{
118 DIR *dir;
119 struct dirent *de;
120 char* dirname=get_object_directory();
121
122 dir = opendir(dirname);
123 if (!dir) {
124 fprintf(stderr, "Unable to open object directory %s\n",
125 dirname);
126 return;
127 }
Brandon Casey0e8316c2008-07-24 17:41:12 -0500128 while ((de = readdir(dir)) != NULL)
129 if (!prefixcmp(de->d_name, "tmp_"))
130 prune_tmp_object(dirname, 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"),
140 OPT_DATE(0, "expire", &expire,
141 "expire objects older than <time>"),
142 OPT_END()
143 };
Linus Torvaldsba84a792006-07-06 10:16:22 -0700144
Junio C Hamano16157b82007-01-05 13:31:43 -0800145 save_commit_buffer = 0;
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700146 init_revisions(&revs, prefix);
Linus Torvaldsba84a792006-07-06 10:16:22 -0700147
Michele Ballabio629de472008-03-23 21:50:29 +0100148 argc = parse_options(argc, argv, options, prune_usage, 0);
Junio C Hamanofe308f52008-03-24 23:20:51 -0700149 while (argc--) {
150 unsigned char sha1[20];
151 const char *name = *argv++;
152
153 if (!get_sha1(name, sha1)) {
154 struct object *object = parse_object(sha1);
155 if (!object)
156 die("bad object: %s", name);
157 add_pending_object(&revs, object, "");
158 }
159 else
160 die("unrecognized argument: %s", name);
161 }
Michele Ballabio629de472008-03-23 21:50:29 +0100162 mark_reachable_objects(&revs, 1);
Linus Torvaldsba84a792006-07-06 10:16:22 -0700163 prune_object_dir(get_object_directory());
164
J. Bruce Fields2eb53e62006-10-22 19:01:23 -0400165 prune_packed_objects(show_only);
David Steven Tweed84640102008-02-07 02:55:14 +0000166 remove_temporary_files();
Linus Torvaldsba84a792006-07-06 10:16:22 -0700167 return 0;
168}