]>
Commit | Line | Data |
---|---|---|
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" |
8ca12c0d | 8 | #include "dir.h" |
ba84a797 | 9 | |
629de472 | 10 | static const char * const prune_usage[] = { |
b35ddf41 | 11 | "git prune [-n] [-v] [--expire <time>] [--] [<head>...]", |
629de472 MB |
12 | NULL |
13 | }; | |
96f1e58f | 14 | static int show_only; |
b35ddf41 | 15 | static int verbose; |
f01913e4 | 16 | static unsigned long expire; |
ba84a797 | 17 | |
db87e396 | 18 | static int prune_tmp_object(const char *path, const char *filename) |
0e8316cc BC |
19 | { |
20 | const char *fullpath = mkpath("%s/%s", path, filename); | |
cbf731ed AS |
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; | |
0e8316cc BC |
26 | printf("Removing stale temporary file %s\n", fullpath); |
27 | if (!show_only) | |
691f1a28 | 28 | unlink_or_warn(fullpath); |
0e8316cc BC |
29 | return 0; |
30 | } | |
31 | ||
ba84a797 LT |
32 | static int prune_object(char *path, const char *filename, const unsigned char *sha1) |
33 | { | |
f01913e4 | 34 | const char *fullpath = mkpath("%s/%s", path, filename); |
cbf731ed AS |
35 | struct stat st; |
36 | if (lstat(fullpath, &st)) | |
37 | return error("Could not stat '%s'", fullpath); | |
38 | if (st.st_mtime > expire) | |
39 | return 0; | |
b35ddf41 | 40 | if (show_only || verbose) { |
21666f1a NP |
41 | enum object_type type = sha1_object_info(sha1, NULL); |
42 | printf("%s %s\n", sha1_to_hex(sha1), | |
43 | (type > 0) ? typename(type) : "unknown"); | |
b35ddf41 MG |
44 | } |
45 | if (!show_only) | |
691f1a28 | 46 | unlink_or_warn(fullpath); |
ba84a797 LT |
47 | return 0; |
48 | } | |
49 | ||
50 | static int prune_dir(int i, char *path) | |
51 | { | |
52 | DIR *dir = opendir(path); | |
53 | struct dirent *de; | |
54 | ||
55 | if (!dir) | |
56 | return 0; | |
57 | ||
58 | while ((de = readdir(dir)) != NULL) { | |
59 | char name[100]; | |
60 | unsigned char sha1[20]; | |
ba84a797 | 61 | |
8ca12c0d | 62 | if (is_dot_or_dotdot(de->d_name)) |
ba84a797 | 63 | continue; |
8ca12c0d | 64 | if (strlen(de->d_name) == 38) { |
ba84a797 | 65 | sprintf(name, "%02x", i); |
8ca12c0d | 66 | memcpy(name+2, de->d_name, 39); |
ba84a797 LT |
67 | if (get_sha1_hex(name, sha1) < 0) |
68 | break; | |
69 | ||
70 | /* | |
71 | * Do we know about this object? | |
72 | * It must have been reachable | |
73 | */ | |
74 | if (lookup_object(sha1)) | |
75 | continue; | |
76 | ||
77 | prune_object(path, de->d_name, sha1); | |
78 | continue; | |
79 | } | |
3d32a46b BC |
80 | if (!prefixcmp(de->d_name, "tmp_obj_")) { |
81 | prune_tmp_object(path, de->d_name); | |
82 | continue; | |
83 | } | |
ba84a797 LT |
84 | fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name); |
85 | } | |
3254d218 NP |
86 | if (!show_only) |
87 | rmdir(path); | |
ba84a797 LT |
88 | closedir(dir); |
89 | return 0; | |
90 | } | |
91 | ||
92 | static void prune_object_dir(const char *path) | |
93 | { | |
94 | int i; | |
95 | for (i = 0; i < 256; i++) { | |
96 | static char dir[4096]; | |
97 | sprintf(dir, "%s/%02x", path, i); | |
98 | prune_dir(i, dir); | |
99 | } | |
100 | } | |
101 | ||
8464010f DST |
102 | /* |
103 | * Write errors (particularly out of space) can result in | |
104 | * failed temporary packs (and more rarely indexes and other | |
9517e6b8 | 105 | * files beginning with "tmp_") accumulating in the object |
db87e396 | 106 | * and the pack directories. |
8464010f | 107 | */ |
db87e396 | 108 | static void remove_temporary_files(const char *path) |
8464010f DST |
109 | { |
110 | DIR *dir; | |
111 | struct dirent *de; | |
8464010f | 112 | |
db87e396 | 113 | dir = opendir(path); |
8464010f | 114 | if (!dir) { |
db87e396 | 115 | fprintf(stderr, "Unable to open directory %s\n", path); |
8464010f DST |
116 | return; |
117 | } | |
0e8316cc BC |
118 | while ((de = readdir(dir)) != NULL) |
119 | if (!prefixcmp(de->d_name, "tmp_")) | |
db87e396 | 120 | prune_tmp_object(path, de->d_name); |
8464010f DST |
121 | closedir(dir); |
122 | } | |
123 | ||
a633fca0 | 124 | int cmd_prune(int argc, const char **argv, const char *prefix) |
ba84a797 | 125 | { |
24304816 | 126 | struct rev_info revs; |
629de472 | 127 | const struct option options[] = { |
e21adb8c | 128 | OPT__DRY_RUN(&show_only, "do not remove, show only"), |
fd03881a | 129 | OPT__VERBOSE(&verbose, "report pruned objects"), |
629de472 MB |
130 | OPT_DATE(0, "expire", &expire, |
131 | "expire objects older than <time>"), | |
132 | OPT_END() | |
133 | }; | |
db87e396 | 134 | char *s; |
ba84a797 | 135 | |
cbf731ed | 136 | expire = ULONG_MAX; |
16157b80 | 137 | save_commit_buffer = 0; |
dae556bd | 138 | read_replace_refs = 0; |
a633fca0 | 139 | init_revisions(&revs, prefix); |
ba84a797 | 140 | |
37782920 | 141 | argc = parse_options(argc, argv, prefix, options, prune_usage, 0); |
fe308f53 JH |
142 | while (argc--) { |
143 | unsigned char sha1[20]; | |
144 | const char *name = *argv++; | |
145 | ||
146 | if (!get_sha1(name, sha1)) { | |
147 | struct object *object = parse_object(sha1); | |
148 | if (!object) | |
149 | die("bad object: %s", name); | |
150 | add_pending_object(&revs, object, ""); | |
151 | } | |
152 | else | |
153 | die("unrecognized argument: %s", name); | |
154 | } | |
629de472 | 155 | mark_reachable_objects(&revs, 1); |
ba84a797 LT |
156 | prune_object_dir(get_object_directory()); |
157 | ||
2eb53e65 | 158 | prune_packed_objects(show_only); |
db87e396 BC |
159 | remove_temporary_files(get_object_directory()); |
160 | s = xstrdup(mkpath("%s/pack", get_object_directory())); | |
161 | remove_temporary_files(s); | |
162 | free(s); | |
ba84a797 LT |
163 | return 0; |
164 | } |