]>
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" |
dc347195 | 8 | #include "progress.h" |
8ca12c0d | 9 | #include "dir.h" |
ba84a797 | 10 | |
629de472 | 11 | static const char * const prune_usage[] = { |
8f5b7281 | 12 | N_("git prune [-n] [-v] [--expire <time>] [--] [<head>...]"), |
629de472 MB |
13 | NULL |
14 | }; | |
96f1e58f | 15 | static int show_only; |
b35ddf41 | 16 | static int verbose; |
f01913e4 | 17 | static unsigned long expire; |
bf0a59b3 | 18 | static int show_progress = -1; |
ba84a797 | 19 | |
4454e9cb | 20 | static int prune_tmp_file(const char *fullpath) |
0e8316cc | 21 | { |
cbf731ed AS |
22 | struct stat st; |
23 | if (lstat(fullpath, &st)) | |
24 | return error("Could not stat '%s'", fullpath); | |
25 | if (st.st_mtime > expire) | |
26 | return 0; | |
90b29cb7 BC |
27 | if (show_only || verbose) |
28 | printf("Removing stale temporary file %s\n", fullpath); | |
0e8316cc | 29 | if (!show_only) |
691f1a28 | 30 | unlink_or_warn(fullpath); |
0e8316cc BC |
31 | return 0; |
32 | } | |
33 | ||
27e1e22d JK |
34 | static int prune_object(const unsigned char *sha1, const char *fullpath, |
35 | void *data) | |
ba84a797 | 36 | { |
cbf731ed | 37 | struct stat st; |
27e1e22d JK |
38 | |
39 | /* | |
40 | * Do we know about this object? | |
41 | * It must have been reachable | |
42 | */ | |
43 | if (lookup_object(sha1)) | |
44 | return 0; | |
45 | ||
46 | if (lstat(fullpath, &st)) { | |
47 | /* report errors, but do not stop pruning */ | |
48 | error("Could not stat '%s'", fullpath); | |
49 | return 0; | |
50 | } | |
cbf731ed AS |
51 | if (st.st_mtime > expire) |
52 | return 0; | |
b35ddf41 | 53 | if (show_only || verbose) { |
21666f1a NP |
54 | enum object_type type = sha1_object_info(sha1, NULL); |
55 | printf("%s %s\n", sha1_to_hex(sha1), | |
56 | (type > 0) ? typename(type) : "unknown"); | |
b35ddf41 MG |
57 | } |
58 | if (!show_only) | |
691f1a28 | 59 | unlink_or_warn(fullpath); |
ba84a797 LT |
60 | return 0; |
61 | } | |
62 | ||
27e1e22d | 63 | static int prune_cruft(const char *basename, const char *path, void *data) |
ba84a797 | 64 | { |
27e1e22d JK |
65 | if (starts_with(basename, "tmp_obj_")) |
66 | prune_tmp_file(path); | |
67 | else | |
68 | fprintf(stderr, "bad sha1 file: %s\n", path); | |
ba84a797 LT |
69 | return 0; |
70 | } | |
71 | ||
27e1e22d | 72 | static int prune_subdir(int nr, const char *path, void *data) |
ba84a797 | 73 | { |
27e1e22d JK |
74 | if (!show_only) |
75 | rmdir(path); | |
76 | return 0; | |
ba84a797 LT |
77 | } |
78 | ||
8464010f DST |
79 | /* |
80 | * Write errors (particularly out of space) can result in | |
81 | * failed temporary packs (and more rarely indexes and other | |
9517e6b8 | 82 | * files beginning with "tmp_") accumulating in the object |
db87e396 | 83 | * and the pack directories. |
8464010f | 84 | */ |
db87e396 | 85 | static void remove_temporary_files(const char *path) |
8464010f DST |
86 | { |
87 | DIR *dir; | |
88 | struct dirent *de; | |
8464010f | 89 | |
db87e396 | 90 | dir = opendir(path); |
8464010f | 91 | if (!dir) { |
db87e396 | 92 | fprintf(stderr, "Unable to open directory %s\n", path); |
8464010f DST |
93 | return; |
94 | } | |
0e8316cc | 95 | while ((de = readdir(dir)) != NULL) |
59556548 | 96 | if (starts_with(de->d_name, "tmp_")) |
4454e9cb | 97 | prune_tmp_file(mkpath("%s/%s", path, de->d_name)); |
8464010f DST |
98 | closedir(dir); |
99 | } | |
100 | ||
a633fca0 | 101 | int cmd_prune(int argc, const char **argv, const char *prefix) |
ba84a797 | 102 | { |
24304816 | 103 | struct rev_info revs; |
bf0a59b3 | 104 | struct progress *progress = NULL; |
629de472 | 105 | const struct option options[] = { |
8f5b7281 NTND |
106 | OPT__DRY_RUN(&show_only, N_("do not remove, show only")), |
107 | OPT__VERBOSE(&verbose, N_("report pruned objects")), | |
108 | OPT_BOOL(0, "progress", &show_progress, N_("show progress")), | |
27ec394a JH |
109 | OPT_EXPIRY_DATE(0, "expire", &expire, |
110 | N_("expire objects older than <time>")), | |
629de472 MB |
111 | OPT_END() |
112 | }; | |
db87e396 | 113 | char *s; |
ba84a797 | 114 | |
cbf731ed | 115 | expire = ULONG_MAX; |
16157b80 | 116 | save_commit_buffer = 0; |
afc711b8 | 117 | check_replace_refs = 0; |
a633fca0 | 118 | init_revisions(&revs, prefix); |
ba84a797 | 119 | |
37782920 | 120 | argc = parse_options(argc, argv, prefix, options, prune_usage, 0); |
fe308f53 JH |
121 | while (argc--) { |
122 | unsigned char sha1[20]; | |
123 | const char *name = *argv++; | |
124 | ||
125 | if (!get_sha1(name, sha1)) { | |
f7892d18 | 126 | struct object *object = parse_object_or_die(sha1, name); |
fe308f53 JH |
127 | add_pending_object(&revs, object, ""); |
128 | } | |
129 | else | |
130 | die("unrecognized argument: %s", name); | |
131 | } | |
bf0a59b3 JK |
132 | |
133 | if (show_progress == -1) | |
134 | show_progress = isatty(2); | |
135 | if (show_progress) | |
754dbc43 | 136 | progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2); |
bf0a59b3 | 137 | |
d3038d22 | 138 | mark_reachable_objects(&revs, 1, expire, progress); |
dc347195 | 139 | stop_progress(&progress); |
27e1e22d JK |
140 | for_each_loose_file_in_objdir(get_object_directory(), prune_object, |
141 | prune_cruft, prune_subdir, NULL); | |
ba84a797 | 142 | |
af0b4a3b | 143 | prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0); |
db87e396 | 144 | remove_temporary_files(get_object_directory()); |
4e2d094d | 145 | s = mkpathdup("%s/pack", get_object_directory()); |
db87e396 BC |
146 | remove_temporary_files(s); |
147 | free(s); | |
eab3296c NTND |
148 | |
149 | if (is_repository_shallow()) | |
150 | prune_shallow(show_only); | |
151 | ||
ba84a797 LT |
152 | return 0; |
153 | } |