Merge branch 'jc/diff-b-m'
[git] / builtin / prune.c
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"
9 #include "dir.h"
10
11 static const char * const prune_usage[] = {
12         N_("git prune [-n] [-v] [--expire <time>] [--] [<head>...]"),
13         NULL
14 };
15 static int show_only;
16 static int verbose;
17 static unsigned long expire;
18 static int show_progress = -1;
19
20 static int prune_tmp_file(const char *fullpath)
21 {
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;
27         if (show_only || verbose)
28                 printf("Removing stale temporary file %s\n", fullpath);
29         if (!show_only)
30                 unlink_or_warn(fullpath);
31         return 0;
32 }
33
34 static int prune_object(const unsigned char *sha1, const char *fullpath,
35                         void *data)
36 {
37         struct stat st;
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         }
51         if (st.st_mtime > expire)
52                 return 0;
53         if (show_only || verbose) {
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");
57         }
58         if (!show_only)
59                 unlink_or_warn(fullpath);
60         return 0;
61 }
62
63 static int prune_cruft(const char *basename, const char *path, void *data)
64 {
65         if (starts_with(basename, "tmp_obj_"))
66                 prune_tmp_file(path);
67         else
68                 fprintf(stderr, "bad sha1 file: %s\n", path);
69         return 0;
70 }
71
72 static int prune_subdir(int nr, const char *path, void *data)
73 {
74         if (!show_only)
75                 rmdir(path);
76         return 0;
77 }
78
79 /*
80  * Write errors (particularly out of space) can result in
81  * failed temporary packs (and more rarely indexes and other
82  * files beginning with "tmp_") accumulating in the object
83  * and the pack directories.
84  */
85 static void remove_temporary_files(const char *path)
86 {
87         DIR *dir;
88         struct dirent *de;
89
90         dir = opendir(path);
91         if (!dir) {
92                 fprintf(stderr, "Unable to open directory %s\n", path);
93                 return;
94         }
95         while ((de = readdir(dir)) != NULL)
96                 if (starts_with(de->d_name, "tmp_"))
97                         prune_tmp_file(mkpath("%s/%s", path, de->d_name));
98         closedir(dir);
99 }
100
101 int cmd_prune(int argc, const char **argv, const char *prefix)
102 {
103         struct rev_info revs;
104         struct progress *progress = NULL;
105         const struct option options[] = {
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")),
109                 OPT_EXPIRY_DATE(0, "expire", &expire,
110                                 N_("expire objects older than <time>")),
111                 OPT_END()
112         };
113         char *s;
114
115         expire = ULONG_MAX;
116         save_commit_buffer = 0;
117         check_replace_refs = 0;
118         init_revisions(&revs, prefix);
119
120         argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
121         while (argc--) {
122                 unsigned char sha1[20];
123                 const char *name = *argv++;
124
125                 if (!get_sha1(name, sha1)) {
126                         struct object *object = parse_object_or_die(sha1, name);
127                         add_pending_object(&revs, object, "");
128                 }
129                 else
130                         die("unrecognized argument: %s", name);
131         }
132
133         if (show_progress == -1)
134                 show_progress = isatty(2);
135         if (show_progress)
136                 progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
137
138         mark_reachable_objects(&revs, 1, expire, progress);
139         stop_progress(&progress);
140         for_each_loose_file_in_objdir(get_object_directory(), prune_object,
141                                       prune_cruft, prune_subdir, NULL);
142
143         prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
144         remove_temporary_files(get_object_directory());
145         s = mkpathdup("%s/pack", get_object_directory());
146         remove_temporary_files(s);
147         free(s);
148
149         if (is_repository_shallow())
150                 prune_shallow(show_only);
151
152         return 0;
153 }