builtin / prune.con commit Merge branch 'en/disable-dir-rename-in-recursive-merge' (0bdaec1)
   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        struct object *obj;
  53
  54        perform_reachability_traversal(revs);
  55
  56        obj = lookup_object(the_repository, oid);
  57        return obj && (obj->flags & SEEN);
  58}
  59
  60static int prune_object(const struct object_id *oid, const char *fullpath,
  61                        void *data)
  62{
  63        struct rev_info *revs = data;
  64        struct stat st;
  65
  66        if (is_object_reachable(oid, revs))
  67                return 0;
  68
  69        if (lstat(fullpath, &st)) {
  70                /* report errors, but do not stop pruning */
  71                error("Could not stat '%s'", fullpath);
  72                return 0;
  73        }
  74        if (st.st_mtime > expire)
  75                return 0;
  76        if (show_only || verbose) {
  77                enum object_type type = oid_object_info(the_repository, oid,
  78                                                        NULL);
  79                printf("%s %s\n", oid_to_hex(oid),
  80                       (type > 0) ? type_name(type) : "unknown");
  81        }
  82        if (!show_only)
  83                unlink_or_warn(fullpath);
  84        return 0;
  85}
  86
  87static int prune_cruft(const char *basename, const char *path, void *data)
  88{
  89        if (starts_with(basename, "tmp_obj_"))
  90                prune_tmp_file(path);
  91        else
  92                fprintf(stderr, "bad sha1 file: %s\n", path);
  93        return 0;
  94}
  95
  96static int prune_subdir(unsigned int nr, const char *path, void *data)
  97{
  98        if (!show_only)
  99                rmdir(path);
 100        return 0;
 101}
 102
 103/*
 104 * Write errors (particularly out of space) can result in
 105 * failed temporary packs (and more rarely indexes and other
 106 * files beginning with "tmp_") accumulating in the object
 107 * and the pack directories.
 108 */
 109static void remove_temporary_files(const char *path)
 110{
 111        DIR *dir;
 112        struct dirent *de;
 113
 114        dir = opendir(path);
 115        if (!dir) {
 116                fprintf(stderr, "Unable to open directory %s\n", path);
 117                return;
 118        }
 119        while ((de = readdir(dir)) != NULL)
 120                if (starts_with(de->d_name, "tmp_"))
 121                        prune_tmp_file(mkpath("%s/%s", path, de->d_name));
 122        closedir(dir);
 123}
 124
 125int cmd_prune(int argc, const char **argv, const char *prefix)
 126{
 127        struct rev_info revs;
 128        int exclude_promisor_objects = 0;
 129        const struct option options[] = {
 130                OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
 131                OPT__VERBOSE(&verbose, N_("report pruned objects")),
 132                OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
 133                OPT_EXPIRY_DATE(0, "expire", &expire,
 134                                N_("expire objects older than <time>")),
 135                OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
 136                         N_("limit traversal to objects outside promisor packfiles")),
 137                OPT_END()
 138        };
 139        char *s;
 140
 141        expire = TIME_MAX;
 142        save_commit_buffer = 0;
 143        read_replace_refs = 0;
 144        ref_paranoia = 1;
 145        repo_init_revisions(the_repository, &revs, prefix);
 146
 147        argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
 148
 149        if (repository_format_precious_objects)
 150                die(_("cannot prune in a precious-objects repo"));
 151
 152        while (argc--) {
 153                struct object_id oid;
 154                const char *name = *argv++;
 155
 156                if (!get_oid(name, &oid)) {
 157                        struct object *object = parse_object_or_die(&oid,
 158                                                                    name);
 159                        add_pending_object(&revs, object, "");
 160                }
 161                else
 162                        die("unrecognized argument: %s", name);
 163        }
 164
 165        if (show_progress == -1)
 166                show_progress = isatty(2);
 167        if (exclude_promisor_objects) {
 168                fetch_if_missing = 0;
 169                revs.exclude_promisor_objects = 1;
 170        }
 171
 172        for_each_loose_file_in_objdir(get_object_directory(), prune_object,
 173                                      prune_cruft, prune_subdir, &revs);
 174
 175        prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
 176        remove_temporary_files(get_object_directory());
 177        s = mkpathdup("%s/pack", get_object_directory());
 178        remove_temporary_files(s);
 179        free(s);
 180
 181        if (is_repository_shallow(the_repository)) {
 182                perform_reachability_traversal(&revs);
 183                prune_shallow(show_only ? PRUNE_SHOW_ONLY : 0);
 184        }
 185
 186        return 0;
 187}