reset --hard: make use of the pretty machinery
[git.git] / builtin / prune.c
blob42633e0c6e672c46852f0590f1941249691f962c
1 #include "cache.h"
2 #include "commit.h"
3 #include "diff.h"
4 #include "revision.h"
5 #include "builtin.h"
6 #include "reachable.h"
7 #include "parse-options.h"
8 #include "progress.h"
10 static const char * const prune_usage[] = {
11 N_("git prune [-n] [-v] [--expire <time>] [--] [<head>...]"),
12 NULL
14 static int show_only;
15 static int verbose;
16 static unsigned long expire;
17 static int show_progress = -1;
19 static int prune_tmp_file(const char *fullpath)
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 if (show_only || verbose)
27 printf("Removing stale temporary file %s\n", fullpath);
28 if (!show_only)
29 unlink_or_warn(fullpath);
30 return 0;
33 static int prune_object(const struct object_id *oid, const char *fullpath,
34 void *data)
36 struct stat st;
39 * Do we know about this object?
40 * It must have been reachable
42 if (lookup_object(oid->hash))
43 return 0;
45 if (lstat(fullpath, &st)) {
46 /* report errors, but do not stop pruning */
47 error("Could not stat '%s'", fullpath);
48 return 0;
50 if (st.st_mtime > expire)
51 return 0;
52 if (show_only || verbose) {
53 enum object_type type = sha1_object_info(oid->hash, NULL);
54 printf("%s %s\n", oid_to_hex(oid),
55 (type > 0) ? typename(type) : "unknown");
57 if (!show_only)
58 unlink_or_warn(fullpath);
59 return 0;
62 static int prune_cruft(const char *basename, const char *path, void *data)
64 if (starts_with(basename, "tmp_obj_"))
65 prune_tmp_file(path);
66 else
67 fprintf(stderr, "bad sha1 file: %s\n", path);
68 return 0;
71 static int prune_subdir(int nr, const char *path, void *data)
73 if (!show_only)
74 rmdir(path);
75 return 0;
79 * Write errors (particularly out of space) can result in
80 * failed temporary packs (and more rarely indexes and other
81 * files beginning with "tmp_") accumulating in the object
82 * and the pack directories.
84 static void remove_temporary_files(const char *path)
86 DIR *dir;
87 struct dirent *de;
89 dir = opendir(path);
90 if (!dir) {
91 fprintf(stderr, "Unable to open directory %s\n", path);
92 return;
94 while ((de = readdir(dir)) != NULL)
95 if (starts_with(de->d_name, "tmp_"))
96 prune_tmp_file(mkpath("%s/%s", path, de->d_name));
97 closedir(dir);
100 int cmd_prune(int argc, const char **argv, const char *prefix)
102 struct rev_info revs;
103 struct progress *progress = NULL;
104 const struct option options[] = {
105 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
106 OPT__VERBOSE(&verbose, N_("report pruned objects")),
107 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
108 OPT_EXPIRY_DATE(0, "expire", &expire,
109 N_("expire objects older than <time>")),
110 OPT_END()
112 char *s;
114 expire = ULONG_MAX;
115 save_commit_buffer = 0;
116 check_replace_refs = 0;
117 ref_paranoia = 1;
118 init_revisions(&revs, prefix);
120 argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
122 if (repository_format_precious_objects)
123 die(_("cannot prune in a precious-objects repo"));
125 while (argc--) {
126 unsigned char sha1[20];
127 const char *name = *argv++;
129 if (!get_sha1(name, sha1)) {
130 struct object *object = parse_object_or_die(sha1, name);
131 add_pending_object(&revs, object, "");
133 else
134 die("unrecognized argument: %s", name);
137 if (show_progress == -1)
138 show_progress = isatty(2);
139 if (show_progress)
140 progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
142 mark_reachable_objects(&revs, 1, expire, progress);
143 stop_progress(&progress);
144 for_each_loose_file_in_objdir(get_object_directory(), prune_object,
145 prune_cruft, prune_subdir, NULL);
147 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
148 remove_temporary_files(get_object_directory());
149 s = mkpathdup("%s/pack", get_object_directory());
150 remove_temporary_files(s);
151 free(s);
153 if (is_repository_shallow())
154 prune_shallow(show_only);
156 return 0;