7 #include "parse-options.h"
9 #include "prune-packed.h"
10 #include "object-store.h"
12 static const char * const prune_usage
[] = {
13 N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"),
18 static timestamp_t expire
;
19 static int show_progress
= -1;
21 static int prune_tmp_file(const char *fullpath
)
24 if (lstat(fullpath
, &st
))
25 return error("Could not stat '%s'", fullpath
);
26 if (st
.st_mtime
> expire
)
28 if (show_only
|| verbose
)
29 printf("Removing stale temporary file %s\n", fullpath
);
31 unlink_or_warn(fullpath
);
35 static void perform_reachability_traversal(struct rev_info
*revs
)
37 static int initialized
;
38 struct progress
*progress
= NULL
;
44 progress
= start_delayed_progress(_("Checking connectivity"), 0);
45 mark_reachable_objects(revs
, 1, expire
, progress
);
46 stop_progress(&progress
);
50 static int is_object_reachable(const struct object_id
*oid
,
51 struct rev_info
*revs
)
55 perform_reachability_traversal(revs
);
57 obj
= lookup_object(the_repository
, oid
);
58 return obj
&& (obj
->flags
& SEEN
);
61 static int prune_object(const struct object_id
*oid
, const char *fullpath
,
64 struct rev_info
*revs
= data
;
67 if (is_object_reachable(oid
, revs
))
70 if (lstat(fullpath
, &st
)) {
71 /* report errors, but do not stop pruning */
72 error("Could not stat '%s'", fullpath
);
75 if (st
.st_mtime
> expire
)
77 if (show_only
|| verbose
) {
78 enum object_type type
= oid_object_info(the_repository
, oid
,
80 printf("%s %s\n", oid_to_hex(oid
),
81 (type
> 0) ? type_name(type
) : "unknown");
84 unlink_or_warn(fullpath
);
88 static int prune_cruft(const char *basename
, const char *path
, void *data
)
90 if (starts_with(basename
, "tmp_obj_"))
93 fprintf(stderr
, "bad sha1 file: %s\n", path
);
97 static int prune_subdir(unsigned int nr
, const char *path
, void *data
)
105 * Write errors (particularly out of space) can result in
106 * failed temporary packs (and more rarely indexes and other
107 * files beginning with "tmp_") accumulating in the object
108 * and the pack directories.
110 static void remove_temporary_files(const char *path
)
117 fprintf(stderr
, "Unable to open directory %s\n", path
);
120 while ((de
= readdir(dir
)) != NULL
)
121 if (starts_with(de
->d_name
, "tmp_"))
122 prune_tmp_file(mkpath("%s/%s", path
, de
->d_name
));
126 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
128 struct rev_info revs
;
129 int exclude_promisor_objects
= 0;
130 const struct option options
[] = {
131 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
132 OPT__VERBOSE(&verbose
, N_("report pruned objects")),
133 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
134 OPT_EXPIRY_DATE(0, "expire", &expire
,
135 N_("expire objects older than <time>")),
136 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects
,
137 N_("limit traversal to objects outside promisor packfiles")),
143 save_commit_buffer
= 0;
144 read_replace_refs
= 0;
146 repo_init_revisions(the_repository
, &revs
, prefix
);
148 argc
= parse_options(argc
, argv
, prefix
, options
, prune_usage
, 0);
150 if (repository_format_precious_objects
)
151 die(_("cannot prune in a precious-objects repo"));
154 struct object_id oid
;
155 const char *name
= *argv
++;
157 if (!get_oid(name
, &oid
)) {
158 struct object
*object
= parse_object_or_die(&oid
,
160 add_pending_object(&revs
, object
, "");
163 die("unrecognized argument: %s", name
);
166 if (show_progress
== -1)
167 show_progress
= isatty(2);
168 if (exclude_promisor_objects
) {
169 fetch_if_missing
= 0;
170 revs
.exclude_promisor_objects
= 1;
173 for_each_loose_file_in_objdir(get_object_directory(), prune_object
,
174 prune_cruft
, prune_subdir
, &revs
);
176 prune_packed_objects(show_only
? PRUNE_PACKED_DRY_RUN
: 0);
177 remove_temporary_files(get_object_directory());
178 s
= mkpathdup("%s/pack", get_object_directory());
179 remove_temporary_files(s
);
182 if (is_repository_shallow(the_repository
)) {
183 perform_reachability_traversal(&revs
);
184 prune_shallow(show_only
? PRUNE_SHOW_ONLY
: 0);