7 #include "parse-options.h"
9 #include "object-store.h"
11 static const char * const prune_usage
[] = {
12 N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"),
17 static timestamp_t expire
;
18 static int show_progress
= -1;
20 static int prune_tmp_file(const char *fullpath
)
23 if (lstat(fullpath
, &st
))
24 return error("Could not stat '%s'", fullpath
);
25 if (st
.st_mtime
> expire
)
27 if (show_only
|| verbose
)
28 printf("Removing stale temporary file %s\n", fullpath
);
30 unlink_or_warn(fullpath
);
34 static void perform_reachability_traversal(struct rev_info
*revs
)
36 static int initialized
;
37 struct progress
*progress
= NULL
;
43 progress
= start_delayed_progress(_("Checking connectivity"), 0);
44 mark_reachable_objects(revs
, 1, expire
, progress
);
45 stop_progress(&progress
);
49 static int is_object_reachable(const struct object_id
*oid
,
50 struct rev_info
*revs
)
54 perform_reachability_traversal(revs
);
56 obj
= lookup_object(the_repository
, oid
);
57 return obj
&& (obj
->flags
& SEEN
);
60 static int prune_object(const struct object_id
*oid
, const char *fullpath
,
63 struct rev_info
*revs
= data
;
66 if (is_object_reachable(oid
, revs
))
69 if (lstat(fullpath
, &st
)) {
70 /* report errors, but do not stop pruning */
71 error("Could not stat '%s'", fullpath
);
74 if (st
.st_mtime
> expire
)
76 if (show_only
|| verbose
) {
77 enum object_type type
= oid_object_info(the_repository
, oid
,
79 printf("%s %s\n", oid_to_hex(oid
),
80 (type
> 0) ? type_name(type
) : "unknown");
83 unlink_or_warn(fullpath
);
87 static int prune_cruft(const char *basename
, const char *path
, void *data
)
89 if (starts_with(basename
, "tmp_obj_"))
92 fprintf(stderr
, "bad sha1 file: %s\n", path
);
96 static int prune_subdir(unsigned int nr
, const char *path
, void *data
)
104 * Write errors (particularly out of space) can result in
105 * failed temporary packs (and more rarely indexes and other
106 * files beginning with "tmp_") accumulating in the object
107 * and the pack directories.
109 static void remove_temporary_files(const char *path
)
116 fprintf(stderr
, "Unable to open directory %s\n", path
);
119 while ((de
= readdir(dir
)) != NULL
)
120 if (starts_with(de
->d_name
, "tmp_"))
121 prune_tmp_file(mkpath("%s/%s", path
, de
->d_name
));
125 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
127 struct rev_info revs
;
128 int exclude_promisor_objects
= 0;
129 const struct option options
[] = {
130 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
131 OPT__VERBOSE(&verbose
, N_("report pruned objects")),
132 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
133 OPT_EXPIRY_DATE(0, "expire", &expire
,
134 N_("expire objects older than <time>")),
135 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects
,
136 N_("limit traversal to objects outside promisor packfiles")),
142 save_commit_buffer
= 0;
143 read_replace_refs
= 0;
145 repo_init_revisions(the_repository
, &revs
, prefix
);
147 argc
= parse_options(argc
, argv
, prefix
, options
, prune_usage
, 0);
149 if (repository_format_precious_objects
)
150 die(_("cannot prune in a precious-objects repo"));
153 struct object_id oid
;
154 const char *name
= *argv
++;
156 if (!get_oid(name
, &oid
)) {
157 struct object
*object
= parse_object_or_die(&oid
,
159 add_pending_object(&revs
, object
, "");
162 die("unrecognized argument: %s", name
);
165 if (show_progress
== -1)
166 show_progress
= isatty(2);
167 if (exclude_promisor_objects
) {
168 fetch_if_missing
= 0;
169 revs
.exclude_promisor_objects
= 1;
172 for_each_loose_file_in_objdir(get_object_directory(), prune_object
,
173 prune_cruft
, prune_subdir
, &revs
);
175 prune_packed_objects(show_only
? PRUNE_PACKED_DRY_RUN
: 0);
176 remove_temporary_files(get_object_directory());
177 s
= mkpathdup("%s/pack", get_object_directory());
178 remove_temporary_files(s
);
181 if (is_repository_shallow(the_repository
)) {
182 perform_reachability_traversal(&revs
);
183 prune_shallow(show_only
? PRUNE_SHOW_ONLY
: 0);