5 #include "environment.h"
10 #include "parse-options.h"
13 #include "prune-packed.h"
14 #include "replace-object.h"
15 #include "object-file.h"
16 #include "object-name.h"
17 #include "object-store-ll.h"
20 static const char * const prune_usage
[] = {
21 N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"),
26 static timestamp_t expire
;
27 static int show_progress
= -1;
29 static int prune_tmp_file(const char *fullpath
)
32 if (lstat(fullpath
, &st
))
33 return error("Could not stat '%s'", fullpath
);
34 if (st
.st_mtime
> expire
)
36 if (S_ISDIR(st
.st_mode
)) {
37 if (show_only
|| verbose
)
38 printf("Removing stale temporary directory %s\n", fullpath
);
40 struct strbuf remove_dir_buf
= STRBUF_INIT
;
42 strbuf_addstr(&remove_dir_buf
, fullpath
);
43 remove_dir_recursively(&remove_dir_buf
, 0);
44 strbuf_release(&remove_dir_buf
);
47 if (show_only
|| verbose
)
48 printf("Removing stale temporary file %s\n", fullpath
);
50 unlink_or_warn(fullpath
);
55 static void perform_reachability_traversal(struct rev_info
*revs
)
57 static int initialized
;
58 struct progress
*progress
= NULL
;
64 progress
= start_delayed_progress(_("Checking connectivity"), 0);
65 mark_reachable_objects(revs
, 1, expire
, progress
);
66 stop_progress(&progress
);
70 static int is_object_reachable(const struct object_id
*oid
,
71 struct rev_info
*revs
)
75 perform_reachability_traversal(revs
);
77 obj
= lookup_object(the_repository
, oid
);
78 return obj
&& (obj
->flags
& SEEN
);
81 static int prune_object(const struct object_id
*oid
, const char *fullpath
,
84 struct rev_info
*revs
= data
;
87 if (is_object_reachable(oid
, revs
))
90 if (lstat(fullpath
, &st
)) {
91 /* report errors, but do not stop pruning */
92 error("Could not stat '%s'", fullpath
);
95 if (st
.st_mtime
> expire
)
97 if (show_only
|| verbose
) {
98 enum object_type type
= oid_object_info(the_repository
, oid
,
100 printf("%s %s\n", oid_to_hex(oid
),
101 (type
> 0) ? type_name(type
) : "unknown");
104 unlink_or_warn(fullpath
);
108 static int prune_cruft(const char *basename
, const char *path
,
111 if (starts_with(basename
, "tmp_obj_"))
112 prune_tmp_file(path
);
114 fprintf(stderr
, "bad sha1 file: %s\n", path
);
118 static int prune_subdir(unsigned int nr UNUSED
, const char *path
,
127 * Write errors (particularly out of space) can result in
128 * failed temporary packs (and more rarely indexes and other
129 * files beginning with "tmp_") accumulating in the object
130 * and the pack directories.
132 static void remove_temporary_files(const char *path
)
140 fprintf(stderr
, "Unable to open directory %s: %s\n",
141 path
, strerror(errno
));
144 while ((de
= readdir(dir
)) != NULL
)
145 if (starts_with(de
->d_name
, "tmp_"))
146 prune_tmp_file(mkpath("%s/%s", path
, de
->d_name
));
150 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
152 struct rev_info revs
;
153 int exclude_promisor_objects
= 0;
154 const struct option options
[] = {
155 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
156 OPT__VERBOSE(&verbose
, N_("report pruned objects")),
157 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
158 OPT_EXPIRY_DATE(0, "expire", &expire
,
159 N_("expire objects older than <time>")),
160 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects
,
161 N_("limit traversal to objects outside promisor packfiles")),
167 save_commit_buffer
= 0;
168 disable_replace_refs();
169 repo_init_revisions(the_repository
, &revs
, prefix
);
171 argc
= parse_options(argc
, argv
, prefix
, options
, prune_usage
, 0);
173 if (repository_format_precious_objects
)
174 die(_("cannot prune in a precious-objects repo"));
177 struct object_id oid
;
178 const char *name
= *argv
++;
180 if (!repo_get_oid(the_repository
, name
, &oid
)) {
181 struct object
*object
= parse_object_or_die(&oid
,
183 add_pending_object(&revs
, object
, "");
186 die("unrecognized argument: %s", name
);
189 if (show_progress
== -1)
190 show_progress
= isatty(2);
191 if (exclude_promisor_objects
) {
192 fetch_if_missing
= 0;
193 revs
.exclude_promisor_objects
= 1;
196 for_each_loose_file_in_objdir(get_object_directory(), prune_object
,
197 prune_cruft
, prune_subdir
, &revs
);
199 prune_packed_objects(show_only
? PRUNE_PACKED_DRY_RUN
: 0);
200 remove_temporary_files(get_object_directory());
201 s
= mkpathdup("%s/pack", get_object_directory());
202 remove_temporary_files(s
);
205 if (is_repository_shallow(the_repository
)) {
206 perform_reachability_traversal(&revs
);
207 prune_shallow(show_only
? PRUNE_SHOW_ONLY
: 0);
210 release_revisions(&revs
);