]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-prune.c
bash completion: Add completion for 'git clone'
[thirdparty/git.git] / builtin-prune.c
CommitLineData
ba84a797 1#include "cache.h"
ba84a797 2#include "commit.h"
ba84a797
LT
3#include "diff.h"
4#include "revision.h"
5#include "builtin.h"
94421474 6#include "reachable.h"
629de472 7#include "parse-options.h"
ba84a797 8
629de472 9static const char * const prune_usage[] = {
1b1dd23f 10 "git prune [-n] [--expire <time>] [--] [<head>...]",
629de472
MB
11 NULL
12};
96f1e58f 13static int show_only;
f01913e4 14static unsigned long expire;
ba84a797 15
0e8316cc
BC
16static 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
ba84a797
LT
32static int prune_object(char *path, const char *filename, const unsigned char *sha1)
33{
f01913e4
JS
34 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 }
ba84a797 42 if (show_only) {
21666f1a
NP
43 enum object_type type = sha1_object_info(sha1, NULL);
44 printf("%s %s\n", sha1_to_hex(sha1),
45 (type > 0) ? typename(type) : "unknown");
3254d218 46 } else
f01913e4 47 unlink(fullpath);
ba84a797
LT
48 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;
0e8316cc
BC
72 case 14:
73 if (prefixcmp(de->d_name, "tmp_obj_"))
74 break;
75 prune_tmp_object(path, de->d_name);
76 continue;
ba84a797
LT
77 case 38:
78 sprintf(name, "%02x", i);
79 memcpy(name+2, de->d_name, len+1);
80 if (get_sha1_hex(name, sha1) < 0)
81 break;
82
83 /*
84 * Do we know about this object?
85 * It must have been reachable
86 */
87 if (lookup_object(sha1))
88 continue;
89
90 prune_object(path, de->d_name, sha1);
91 continue;
92 }
93 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
94 }
3254d218
NP
95 if (!show_only)
96 rmdir(path);
ba84a797
LT
97 closedir(dir);
98 return 0;
99}
100
101static void prune_object_dir(const char *path)
102{
103 int i;
104 for (i = 0; i < 256; i++) {
105 static char dir[4096];
106 sprintf(dir, "%s/%02x", path, i);
107 prune_dir(i, dir);
108 }
109}
110
8464010f
DST
111/*
112 * Write errors (particularly out of space) can result in
113 * failed temporary packs (and more rarely indexes and other
114 * files begining with "tmp_") accumulating in the
115 * object directory.
116 */
117static void remove_temporary_files(void)
118{
119 DIR *dir;
120 struct dirent *de;
121 char* dirname=get_object_directory();
122
123 dir = opendir(dirname);
124 if (!dir) {
125 fprintf(stderr, "Unable to open object directory %s\n",
126 dirname);
127 return;
128 }
0e8316cc
BC
129 while ((de = readdir(dir)) != NULL)
130 if (!prefixcmp(de->d_name, "tmp_"))
131 prune_tmp_object(dirname, de->d_name);
8464010f
DST
132 closedir(dir);
133}
134
a633fca0 135int cmd_prune(int argc, const char **argv, const char *prefix)
ba84a797 136{
24304816 137 struct rev_info revs;
629de472
MB
138 const struct option options[] = {
139 OPT_BOOLEAN('n', NULL, &show_only,
140 "do not remove, show only"),
141 OPT_DATE(0, "expire", &expire,
142 "expire objects older than <time>"),
143 OPT_END()
144 };
ba84a797 145
16157b80 146 save_commit_buffer = 0;
a633fca0 147 init_revisions(&revs, prefix);
ba84a797 148
629de472 149 argc = parse_options(argc, argv, options, prune_usage, 0);
fe308f53
JH
150 while (argc--) {
151 unsigned char sha1[20];
152 const char *name = *argv++;
153
154 if (!get_sha1(name, sha1)) {
155 struct object *object = parse_object(sha1);
156 if (!object)
157 die("bad object: %s", name);
158 add_pending_object(&revs, object, "");
159 }
160 else
161 die("unrecognized argument: %s", name);
162 }
629de472 163 mark_reachable_objects(&revs, 1);
ba84a797
LT
164 prune_object_dir(get_object_directory());
165
2eb53e65 166 prune_packed_objects(show_only);
8464010f 167 remove_temporary_files();
ba84a797
LT
168 return 0;
169}