7 #include "parse-options.h"
9 #include "prune-packed.h"
10 #include "object-store.h"
13 static const char * const prune_usage
[] = {
14 N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"),
19 static timestamp_t expire
;
20 static int show_progress
= -1;
22 static int prune_tmp_file(const char *fullpath
)
25 if (lstat(fullpath
, &st
))
26 return error("Could not stat '%s'", fullpath
);
27 if (st
.st_mtime
> expire
)
29 if (S_ISDIR(st
.st_mode
)) {
30 if (show_only
|| verbose
)
31 printf("Removing stale temporary directory %s\n", fullpath
);
33 struct strbuf remove_dir_buf
= STRBUF_INIT
;
35 strbuf_addstr(&remove_dir_buf
, fullpath
);
36 remove_dir_recursively(&remove_dir_buf
, 0);
37 strbuf_release(&remove_dir_buf
);
40 if (show_only
|| verbose
)
41 printf("Removing stale temporary file %s\n", fullpath
);
43 unlink_or_warn(fullpath
);
48 static void perform_reachability_traversal(struct rev_info
*revs
)
50 static int initialized
;
51 struct progress
*progress
= NULL
;
57 progress
= start_delayed_progress(_("Checking connectivity"), 0);
58 mark_reachable_objects(revs
, 1, expire
, progress
);
59 stop_progress(&progress
);
63 static int is_object_reachable(const struct object_id
*oid
,
64 struct rev_info
*revs
)
68 perform_reachability_traversal(revs
);
70 obj
= lookup_object(the_repository
, oid
);
71 return obj
&& (obj
->flags
& SEEN
);
74 static int prune_object(const struct object_id
*oid
, const char *fullpath
,
77 struct rev_info
*revs
= data
;
80 if (is_object_reachable(oid
, revs
))
83 if (lstat(fullpath
, &st
)) {
84 /* report errors, but do not stop pruning */
85 error("Could not stat '%s'", fullpath
);
88 if (st
.st_mtime
> expire
)
90 if (show_only
|| verbose
) {
91 enum object_type type
= oid_object_info(the_repository
, oid
,
93 printf("%s %s\n", oid_to_hex(oid
),
94 (type
> 0) ? type_name(type
) : "unknown");
97 unlink_or_warn(fullpath
);
101 static int prune_cruft(const char *basename
, const char *path
, void *data
)
103 if (starts_with(basename
, "tmp_obj_"))
104 prune_tmp_file(path
);
106 fprintf(stderr
, "bad sha1 file: %s\n", path
);
110 static int prune_subdir(unsigned int nr
, const char *path
, void *data
)
118 * Write errors (particularly out of space) can result in
119 * failed temporary packs (and more rarely indexes and other
120 * files beginning with "tmp_") accumulating in the object
121 * and the pack directories.
123 static void remove_temporary_files(const char *path
)
131 fprintf(stderr
, "Unable to open directory %s: %s\n",
132 path
, strerror(errno
));
135 while ((de
= readdir(dir
)) != NULL
)
136 if (starts_with(de
->d_name
, "tmp_"))
137 prune_tmp_file(mkpath("%s/%s", path
, de
->d_name
));
141 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
143 struct rev_info revs
;
144 int exclude_promisor_objects
= 0;
145 const struct option options
[] = {
146 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
147 OPT__VERBOSE(&verbose
, N_("report pruned objects")),
148 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
149 OPT_EXPIRY_DATE(0, "expire", &expire
,
150 N_("expire objects older than <time>")),
151 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects
,
152 N_("limit traversal to objects outside promisor packfiles")),
158 save_commit_buffer
= 0;
159 read_replace_refs
= 0;
160 repo_init_revisions(the_repository
, &revs
, prefix
);
162 argc
= parse_options(argc
, argv
, prefix
, options
, prune_usage
, 0);
164 if (repository_format_precious_objects
)
165 die(_("cannot prune in a precious-objects repo"));
168 struct object_id oid
;
169 const char *name
= *argv
++;
171 if (!get_oid(name
, &oid
)) {
172 struct object
*object
= parse_object_or_die(&oid
,
174 add_pending_object(&revs
, object
, "");
177 die("unrecognized argument: %s", name
);
180 if (show_progress
== -1)
181 show_progress
= isatty(2);
182 if (exclude_promisor_objects
) {
183 fetch_if_missing
= 0;
184 revs
.exclude_promisor_objects
= 1;
187 for_each_loose_file_in_objdir(get_object_directory(), prune_object
,
188 prune_cruft
, prune_subdir
, &revs
);
190 prune_packed_objects(show_only
? PRUNE_PACKED_DRY_RUN
: 0);
191 remove_temporary_files(get_object_directory());
192 s
= mkpathdup("%s/pack", get_object_directory());
193 remove_temporary_files(s
);
196 if (is_repository_shallow(the_repository
)) {
197 perform_reachability_traversal(&revs
);
198 prune_shallow(show_only
? PRUNE_SHOW_ONLY
: 0);
201 release_revisions(&revs
);