04b657394575168da1eb0bbe7974863145281ad7
   1#include "cache.h"
   2#include "commit.h"
   3#include "diff.h"
   4#include "revision.h"
   5#include "builtin.h"
   6#include "reachable.h"
   7#include "parse-options.h"
   8#include "progress.h"
   9#include "object-store.h"
  10
  11static const char * const prune_usage[] = {
  12        N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"),
  13        NULL
  14};
  15static int show_only;
  16static int verbose;
  17static timestamp_t expire;
  18static int show_progress = -1;
  19
  20static int prune_tmp_file(const char *fullpath)
  21{
  22        struct stat st;
  23        if (lstat(fullpath, &st))
  24                return error("Could not stat '%s'", fullpath);
  25        if (st.st_mtime > expire)
  26                return 0;
  27        if (show_only || verbose)
  28                printf("Removing stale temporary file %s\n", fullpath);
  29        if (!show_only)
  30                unlink_or_warn(fullpath);
  31        return 0;
  32}
  33
  34static void perform_reachability_traversal(struct rev_info *revs)
  35{
  36        static int initialized;
  37        struct progress *progress = NULL;
  38
  39        if (initialized)
  40                return;
  41
  42        if (show_progress)
  43                progress = start_delayed_progress(_("Checking connectivity"), 0);
  44        mark_reachable_objects(revs, 1, expire, progress);
  45        stop_progress(&progress);
  46        initialized = 1;
  47}
  48
  49static int is_object_reachable(const struct object_id *oid,
  50                               struct rev_info *revs)
  51{
  52        perform_reachability_traversal(revs);
  53
  54        /*
  55         * Do we know about this object?
  56         * It must have been reachable
  57         */
  58        return !!lookup_object(the_repository, oid->hash);
  59}
  60
  61static int prune_object(const struct object_id *oid, const char *fullpath,
  62                        void *data)
  63{
  64        struct rev_info *revs = data;
  65        struct stat st;
  66
  67        if (is_object_reachable(oid, revs))
  68                return 0;
  69
  70        if (lstat(fullpath, &st)) {
  71                /* report errors, but do not stop pruning */
  72                error("Could not stat '%s'", fullpath);
  73                return 0;
  74        }
  75        if (st.st_mtime > expire)
  76                return 0;
  77        if (show_only || verbose) {
  78                enum object_type type = oid_object_info(the_repository, oid,
  79                                                        NULL);
  80                printf("%s %s\n", oid_to_hex(oid),
  81                       (type > 0) ? type_name(type) : "unknown");
  82        }
  83        if (!show_only)
  84                unlink_or_warn(fullpath);
  85        return 0;
  86}
  87
  88static int prune_cruft(const char *basename, const char *path, void *data)
  89{
  90        if (starts_with(basename, "tmp_obj_"))
  91                prune_tmp_file(path);
  92        else
  93                fprintf(stderr, "bad sha1 file: %s\n", path);
  94        return 0;
  95}
  96
  97static int prune_subdir(unsigned int nr, const char *path, void *data)
  98{
  99        if (!show_only)
 100                rmdir(path);
 101        return 0;
 102}
 103
 104/*
 105 * Write errors (particularly out of space) can result in
 106 * failed temporary packs (and more rarely indexes and other
 107 * files beginning with "tmp_") accumulating in the object
 108 * and the pack directories.
 109 */
 110static void remove_temporary_files(const char *path)
 111{
 112        DIR *dir;
 113        struct dirent *de;
 114
 115        dir = opendir(path);
 116        if (!dir) {
 117                fprintf(stderr, "Unable to open directory %s\n", path);
 118                return;
 119        }
 120        while ((de = readdir(dir)) != NULL)
 121                if (starts_with(de->d_name, "tmp_"))
 122                        prune_tmp_file(mkpath("%s/%s", path, de->d_name));
 123        closedir(dir);
 124}
 125
 126int cmd_prune(int argc, const char **argv, const char *prefix)
 127{
 128        struct rev_info revs;
 129        int exclude_promisor_objects = 0;
 130        const struct option options[] = {
 131                OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
 132                OPT__VERBOSE(&verbose, N_("report pruned objects")),
 133                OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
 134                OPT_EXPIRY_DATE(0, "expire", &expire,
 135                                N_("expire objects older than <time>")),
 136                OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
 137                         N_("limit traversal to objects outside promisor packfiles")),
 138                OPT_END()
 139        };
 140        char *s;
 141
 142        expire = TIME_MAX;
 143        save_commit_buffer = 0;
 144        read_replace_refs = 0;
 145        ref_paranoia = 1;
 146        repo_init_revisions(the_repository, &revs, prefix);
 147
 148        argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
 149
 150        if (repository_format_precious_objects)
 151                die(_("cannot prune in a precious-objects repo"));
 152
 153        while (argc--) {
 154                struct object_id oid;
 155                const char *name = *argv++;
 156
 157                if (!get_oid(name, &oid)) {
 158                        struct object *object = parse_object_or_die(&oid,
 159                                                                    name);
 160                        add_pending_object(&revs, object, "");
 161                }
 162                else
 163                        die("unrecognized argument: %s", name);
 164        }
 165
 166        if (show_progress == -1)
 167                show_progress = isatty(2);
 168        if (exclude_promisor_objects) {
 169                fetch_if_missing = 0;
 170                revs.exclude_promisor_objects = 1;
 171        }
 172
 173        for_each_loose_file_in_objdir(get_object_directory(), prune_object,
 174                                      prune_cruft, prune_subdir, &revs);
 175
 176        prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
 177        remove_temporary_files(get_object_directory());
 178        s = mkpathdup("%s/pack", get_object_directory());
 179        remove_temporary_files(s);
 180        free(s);
 181
 182        if (is_repository_shallow(the_repository)) {
 183                perform_reachability_traversal(&revs);
 184                prune_shallow(show_only ? PRUNE_SHOW_ONLY : 0);
 185        }
 186
 187        return 0;
 188}