4 #include "environment.h"
10 #include "parse-options.h"
12 #include "prune-packed.h"
13 #include "replace-object.h"
14 #include "object-store.h"
17 static const char * const prune_usage
[] = {
18 N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"),
23 static timestamp_t expire
;
24 static int show_progress
= -1;
26 static int prune_tmp_file(const char *fullpath
)
29 if (lstat(fullpath
, &st
))
30 return error("Could not stat '%s'", fullpath
);
31 if (st
.st_mtime
> expire
)
33 if (S_ISDIR(st
.st_mode
)) {
34 if (show_only
|| verbose
)
35 printf("Removing stale temporary directory %s\n", fullpath
);
37 struct strbuf remove_dir_buf
= STRBUF_INIT
;
39 strbuf_addstr(&remove_dir_buf
, fullpath
);
40 remove_dir_recursively(&remove_dir_buf
, 0);
41 strbuf_release(&remove_dir_buf
);
44 if (show_only
|| verbose
)
45 printf("Removing stale temporary file %s\n", fullpath
);
47 unlink_or_warn(fullpath
);
52 static void perform_reachability_traversal(struct rev_info
*revs
)
54 static int initialized
;
55 struct progress
*progress
= NULL
;
61 progress
= start_delayed_progress(_("Checking connectivity"), 0);
62 mark_reachable_objects(revs
, 1, expire
, progress
);
63 stop_progress(&progress
);
67 static int is_object_reachable(const struct object_id
*oid
,
68 struct rev_info
*revs
)
72 perform_reachability_traversal(revs
);
74 obj
= lookup_object(the_repository
, oid
);
75 return obj
&& (obj
->flags
& SEEN
);
78 static int prune_object(const struct object_id
*oid
, const char *fullpath
,
81 struct rev_info
*revs
= data
;
84 if (is_object_reachable(oid
, revs
))
87 if (lstat(fullpath
, &st
)) {
88 /* report errors, but do not stop pruning */
89 error("Could not stat '%s'", fullpath
);
92 if (st
.st_mtime
> expire
)
94 if (show_only
|| verbose
) {
95 enum object_type type
= oid_object_info(the_repository
, oid
,
97 printf("%s %s\n", oid_to_hex(oid
),
98 (type
> 0) ? type_name(type
) : "unknown");
101 unlink_or_warn(fullpath
);
105 static int prune_cruft(const char *basename
, const char *path
,
108 if (starts_with(basename
, "tmp_obj_"))
109 prune_tmp_file(path
);
111 fprintf(stderr
, "bad sha1 file: %s\n", path
);
115 static int prune_subdir(unsigned int nr UNUSED
, const char *path
,
124 * Write errors (particularly out of space) can result in
125 * failed temporary packs (and more rarely indexes and other
126 * files beginning with "tmp_") accumulating in the object
127 * and the pack directories.
129 static void remove_temporary_files(const char *path
)
137 fprintf(stderr
, "Unable to open directory %s: %s\n",
138 path
, strerror(errno
));
141 while ((de
= readdir(dir
)) != NULL
)
142 if (starts_with(de
->d_name
, "tmp_"))
143 prune_tmp_file(mkpath("%s/%s", path
, de
->d_name
));
147 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
149 struct rev_info revs
;
150 int exclude_promisor_objects
= 0;
151 const struct option options
[] = {
152 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
153 OPT__VERBOSE(&verbose
, N_("report pruned objects")),
154 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
155 OPT_EXPIRY_DATE(0, "expire", &expire
,
156 N_("expire objects older than <time>")),
157 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects
,
158 N_("limit traversal to objects outside promisor packfiles")),
164 save_commit_buffer
= 0;
165 read_replace_refs
= 0;
166 repo_init_revisions(the_repository
, &revs
, prefix
);
168 argc
= parse_options(argc
, argv
, prefix
, options
, prune_usage
, 0);
170 if (repository_format_precious_objects
)
171 die(_("cannot prune in a precious-objects repo"));
174 struct object_id oid
;
175 const char *name
= *argv
++;
177 if (!repo_get_oid(the_repository
, name
, &oid
)) {
178 struct object
*object
= parse_object_or_die(&oid
,
180 add_pending_object(&revs
, object
, "");
183 die("unrecognized argument: %s", name
);
186 if (show_progress
== -1)
187 show_progress
= isatty(2);
188 if (exclude_promisor_objects
) {
189 fetch_if_missing
= 0;
190 revs
.exclude_promisor_objects
= 1;
193 for_each_loose_file_in_objdir(get_object_directory(), prune_object
,
194 prune_cruft
, prune_subdir
, &revs
);
196 prune_packed_objects(show_only
? PRUNE_PACKED_DRY_RUN
: 0);
197 remove_temporary_files(get_object_directory());
198 s
= mkpathdup("%s/pack", get_object_directory());
199 remove_temporary_files(s
);
202 if (is_repository_shallow(the_repository
)) {
203 perform_reachability_traversal(&revs
);
204 prune_shallow(show_only
? PRUNE_SHOW_ONLY
: 0);
207 release_revisions(&revs
);