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