7 #include "parse-options.h"
11 static const char * const prune_usage
[] = {
12 N_("git prune [-n] [-v] [--expire <time>] [--] [<head>...]"),
17 static unsigned long 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 int prune_object(const unsigned char *sha1
, const char *fullpath
,
40 * Do we know about this object?
41 * It must have been reachable
43 if (lookup_object(sha1
))
46 if (lstat(fullpath
, &st
)) {
47 /* report errors, but do not stop pruning */
48 error("Could not stat '%s'", fullpath
);
51 if (st
.st_mtime
> expire
)
53 if (show_only
|| verbose
) {
54 enum object_type type
= sha1_object_info(sha1
, NULL
);
55 printf("%s %s\n", sha1_to_hex(sha1
),
56 (type
> 0) ? typename(type
) : "unknown");
59 unlink_or_warn(fullpath
);
63 static int prune_cruft(const char *basename
, const char *path
, void *data
)
65 if (starts_with(basename
, "tmp_obj_"))
68 fprintf(stderr
, "bad sha1 file: %s\n", path
);
72 static int prune_subdir(int nr
, const char *path
, void *data
)
80 * Write errors (particularly out of space) can result in
81 * failed temporary packs (and more rarely indexes and other
82 * files beginning with "tmp_") accumulating in the object
83 * and the pack directories.
85 static void remove_temporary_files(const char *path
)
92 fprintf(stderr
, "Unable to open directory %s\n", path
);
95 while ((de
= readdir(dir
)) != NULL
)
96 if (starts_with(de
->d_name
, "tmp_"))
97 prune_tmp_file(mkpath("%s/%s", path
, de
->d_name
));
101 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
103 struct rev_info revs
;
104 struct progress
*progress
= NULL
;
105 const struct option options
[] = {
106 OPT__DRY_RUN(&show_only
, N_("do not remove, show only")),
107 OPT__VERBOSE(&verbose
, N_("report pruned objects")),
108 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
109 OPT_EXPIRY_DATE(0, "expire", &expire
,
110 N_("expire objects older than <time>")),
116 save_commit_buffer
= 0;
117 check_replace_refs
= 0;
118 init_revisions(&revs
, prefix
);
120 argc
= parse_options(argc
, argv
, prefix
, options
, prune_usage
, 0);
122 unsigned char sha1
[20];
123 const char *name
= *argv
++;
125 if (!get_sha1(name
, sha1
)) {
126 struct object
*object
= parse_object_or_die(sha1
, name
);
127 add_pending_object(&revs
, object
, "");
130 die("unrecognized argument: %s", name
);
133 if (show_progress
== -1)
134 show_progress
= isatty(2);
136 progress
= start_progress_delay(_("Checking connectivity"), 0, 0, 2);
138 mark_reachable_objects(&revs
, 1, expire
, progress
);
139 stop_progress(&progress
);
140 for_each_loose_file_in_objdir(get_object_directory(), prune_object
,
141 prune_cruft
, prune_subdir
, NULL
);
143 prune_packed_objects(show_only
? PRUNE_PACKED_DRY_RUN
: 0);
144 remove_temporary_files(get_object_directory());
145 s
= mkpathdup("%s/pack", get_object_directory());
146 remove_temporary_files(s
);
149 if (is_repository_shallow())
150 prune_shallow(show_only
);