4 #include "environment.h"
10 #include "parse-options.h"
12 #include "prune-packed.h"
13 #include "replace-object.h"
14 #include "object-name.h"
15 #include "object-store.h"
18 static const char * const prune_usage
[] = {
19 N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"),
24 static timestamp_t expire
;
25 static int show_progress
= -1;
27 static int prune_tmp_file(const char *fullpath
)
30 if (lstat(fullpath
, &st
))
31 return error("Could not stat '%s'", fullpath
);
32 if (st
.st_mtime
> expire
)
34 if (S_ISDIR(st
.st_mode
)) {
35 if (show_only
|| verbose
)
36 printf("Removing stale temporary directory %s\n", fullpath
);
38 struct strbuf remove_dir_buf
= STRBUF_INIT
;
40 strbuf_addstr(&remove_dir_buf
, fullpath
);
41 remove_dir_recursively(&remove_dir_buf
, 0);
42 strbuf_release(&remove_dir_buf
);
45 if (show_only
|| verbose
)
46 printf("Removing stale temporary file %s\n", fullpath
);
48 unlink_or_warn(fullpath
);
53 static void perform_reachability_traversal(struct rev_info
*revs
)
55 static int initialized
;
56 struct progress
*progress
= NULL
;
62 progress
= start_delayed_progress(_("Checking connectivity"), 0);
63 mark_reachable_objects(revs
, 1, expire
, progress
);
64 stop_progress(&progress
);
68 static int is_object_reachable(const struct object_id
*oid
,
69 struct rev_info
*revs
)
73 perform_reachability_traversal(revs
);
75 obj
= lookup_object(the_repository
, oid
);
76 return obj
&& (obj
->flags
& SEEN
);
79 static int prune_object(const struct object_id
*oid
, const char *fullpath
,
82 struct rev_info
*revs
= data
;
85 if (is_object_reachable(oid
, revs
))
88 if (lstat(fullpath
, &st
)) {
89 /* report errors, but do not stop pruning */
90 error("Could not stat '%s'", fullpath
);
93 if (st
.st_mtime
> expire
)
95 if (show_only
|| verbose
) {
96 enum object_type type
= oid_object_info(the_repository
, oid
,
98 printf("%s %s\n", oid_to_hex(oid
),
99 (type
> 0) ? type_name(type
) : "unknown");
102 unlink_or_warn(fullpath
);
106 static int prune_cruft(const char *basename
, const char *path
,
109 if (starts_with(basename
, "tmp_obj_"))
110 prune_tmp_file(path
);
112 fprintf(stderr
, "bad sha1 file: %s\n", path
);
116 static int prune_subdir(unsigned int nr UNUSED
, const char *path
,
125 * Write errors (particularly out of space) can result in
126 * failed temporary packs (and more rarely indexes and other
127 * files beginning with "tmp_") accumulating in the object
128 * and the pack directories.
130 static void remove_temporary_files(const char *path
)
138 fprintf(stderr
, "Unable to open directory %s: %s\n",
139 path
, strerror(errno
));
142 while ((de
= readdir(dir
)) != NULL
)
143 if (starts_with(de
->d_name
, "tmp_"))
144 prune_tmp_file(mkpath("%s/%s", path
, de
->d_name
));
148 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
150 struct rev_info revs
;
151 int exclude_promisor_objects
= 0;
152 const struct option options
[] = {
153 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
154 OPT__VERBOSE(&verbose
, N_("report pruned objects")),
155 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
156 OPT_EXPIRY_DATE(0, "expire", &expire
,
157 N_("expire objects older than <time>")),
158 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects
,
159 N_("limit traversal to objects outside promisor packfiles")),
165 save_commit_buffer
= 0;
166 read_replace_refs
= 0;
167 repo_init_revisions(the_repository
, &revs
, prefix
);
169 argc
= parse_options(argc
, argv
, prefix
, options
, prune_usage
, 0);
171 if (repository_format_precious_objects
)
172 die(_("cannot prune in a precious-objects repo"));
175 struct object_id oid
;
176 const char *name
= *argv
++;
178 if (!repo_get_oid(the_repository
, name
, &oid
)) {
179 struct object
*object
= parse_object_or_die(&oid
,
181 add_pending_object(&revs
, object
, "");
184 die("unrecognized argument: %s", name
);
187 if (show_progress
== -1)
188 show_progress
= isatty(2);
189 if (exclude_promisor_objects
) {
190 fetch_if_missing
= 0;
191 revs
.exclude_promisor_objects
= 1;
194 for_each_loose_file_in_objdir(get_object_directory(), prune_object
,
195 prune_cruft
, prune_subdir
, &revs
);
197 prune_packed_objects(show_only
? PRUNE_PACKED_DRY_RUN
: 0);
198 remove_temporary_files(get_object_directory());
199 s
= mkpathdup("%s/pack", get_object_directory());
200 remove_temporary_files(s
);
203 if (is_repository_shallow(the_repository
)) {
204 perform_reachability_traversal(&revs
);
205 prune_shallow(show_only
? PRUNE_SHOW_ONLY
: 0);
208 release_revisions(&revs
);