builtin-reflog.con commit plug a few leaks in revision walking used in describe. (94d2367)
   1#include "cache.h"
   2#include "builtin.h"
   3#include "commit.h"
   4#include "refs.h"
   5#include "dir.h"
   6#include "tree-walk.h"
   7#include "diff.h"
   8#include "revision.h"
   9#include "reachable.h"
  10
  11/*
  12 * reflog expire
  13 */
  14
  15static const char reflog_expire_usage[] =
  16"git-reflog expire [--verbose] [--dry-run] [--fix-stale] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
  17
  18static unsigned long default_reflog_expire;
  19static unsigned long default_reflog_expire_unreachable;
  20
  21struct cmd_reflog_expire_cb {
  22        struct rev_info revs;
  23        int dry_run;
  24        int stalefix;
  25        int verbose;
  26        unsigned long expire_total;
  27        unsigned long expire_unreachable;
  28};
  29
  30struct expire_reflog_cb {
  31        FILE *newlog;
  32        const char *ref;
  33        struct commit *ref_commit;
  34        struct cmd_reflog_expire_cb *cmd;
  35};
  36
  37#define INCOMPLETE      (1u<<10)
  38#define STUDYING        (1u<<11)
  39
  40static int tree_is_complete(const unsigned char *sha1)
  41{
  42        struct tree_desc desc;
  43        struct name_entry entry;
  44        int complete;
  45        struct tree *tree;
  46
  47        tree = lookup_tree(sha1);
  48        if (!tree)
  49                return 0;
  50        if (tree->object.flags & SEEN)
  51                return 1;
  52        if (tree->object.flags & INCOMPLETE)
  53                return 0;
  54
  55        desc.buf = tree->buffer;
  56        desc.size = tree->size;
  57        if (!desc.buf) {
  58                char type[20];
  59                void *data = read_sha1_file(sha1, type, &desc.size);
  60                if (!data) {
  61                        tree->object.flags |= INCOMPLETE;
  62                        return 0;
  63                }
  64                desc.buf = data;
  65                tree->buffer = data;
  66        }
  67        complete = 1;
  68        while (tree_entry(&desc, &entry)) {
  69                if (!has_sha1_file(entry.sha1) ||
  70                    (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
  71                        tree->object.flags |= INCOMPLETE;
  72                        complete = 0;
  73                }
  74        }
  75        free(tree->buffer);
  76        tree->buffer = NULL;
  77
  78        if (complete)
  79                tree->object.flags |= SEEN;
  80        return complete;
  81}
  82
  83static int commit_is_complete(struct commit *commit)
  84{
  85        struct object_array study;
  86        struct object_array found;
  87        int is_incomplete = 0;
  88        int i;
  89
  90        /* early return */
  91        if (commit->object.flags & SEEN)
  92                return 1;
  93        if (commit->object.flags & INCOMPLETE)
  94                return 0;
  95        /*
  96         * Find all commits that are reachable and are not marked as
  97         * SEEN.  Then make sure the trees and blobs contained are
  98         * complete.  After that, mark these commits also as SEEN.
  99         * If some of the objects that are needed to complete this
 100         * commit are missing, mark this commit as INCOMPLETE.
 101         */
 102        memset(&study, 0, sizeof(study));
 103        memset(&found, 0, sizeof(found));
 104        add_object_array(&commit->object, NULL, &study);
 105        add_object_array(&commit->object, NULL, &found);
 106        commit->object.flags |= STUDYING;
 107        while (study.nr) {
 108                struct commit *c;
 109                struct commit_list *parent;
 110
 111                c = (struct commit *)study.objects[--study.nr].item;
 112                if (!c->object.parsed && !parse_object(c->object.sha1))
 113                        c->object.flags |= INCOMPLETE;
 114
 115                if (c->object.flags & INCOMPLETE) {
 116                        is_incomplete = 1;
 117                        break;
 118                }
 119                else if (c->object.flags & SEEN)
 120                        continue;
 121                for (parent = c->parents; parent; parent = parent->next) {
 122                        struct commit *p = parent->item;
 123                        if (p->object.flags & STUDYING)
 124                                continue;
 125                        p->object.flags |= STUDYING;
 126                        add_object_array(&p->object, NULL, &study);
 127                        add_object_array(&p->object, NULL, &found);
 128                }
 129        }
 130        if (!is_incomplete) {
 131                /*
 132                 * make sure all commits in "found" array have all the
 133                 * necessary objects.
 134                 */
 135                for (i = 0; i < found.nr; i++) {
 136                        struct commit *c =
 137                                (struct commit *)found.objects[i].item;
 138                        if (!tree_is_complete(c->tree->object.sha1)) {
 139                                is_incomplete = 1;
 140                                c->object.flags |= INCOMPLETE;
 141                        }
 142                }
 143                if (!is_incomplete) {
 144                        /* mark all found commits as complete, iow SEEN */
 145                        for (i = 0; i < found.nr; i++)
 146                                found.objects[i].item->flags |= SEEN;
 147                }
 148        }
 149        /* clear flags from the objects we traversed */
 150        for (i = 0; i < found.nr; i++)
 151                found.objects[i].item->flags &= ~STUDYING;
 152        if (is_incomplete)
 153                commit->object.flags |= INCOMPLETE;
 154        else {
 155                /*
 156                 * If we come here, we have (1) traversed the ancestry chain
 157                 * from the "commit" until we reach SEEN commits (which are
 158                 * known to be complete), and (2) made sure that the commits
 159                 * encountered during the above traversal refer to trees that
 160                 * are complete.  Which means that we know *all* the commits
 161                 * we have seen during this process are complete.
 162                 */
 163                for (i = 0; i < found.nr; i++)
 164                        found.objects[i].item->flags |= SEEN;
 165        }
 166        /* free object arrays */
 167        free(study.objects);
 168        free(found.objects);
 169        return !is_incomplete;
 170}
 171
 172static int keep_entry(struct commit **it, unsigned char *sha1)
 173{
 174        struct commit *commit;
 175
 176        *it = NULL;
 177        if (is_null_sha1(sha1))
 178                return 1;
 179        commit = lookup_commit_reference_gently(sha1, 1);
 180        if (!commit)
 181                return 0;
 182
 183        /*
 184         * Make sure everything in this commit exists.
 185         *
 186         * We have walked all the objects reachable from the refs
 187         * and cache earlier.  The commits reachable by this commit
 188         * must meet SEEN commits -- and then we should mark them as
 189         * SEEN as well.
 190         */
 191        if (!commit_is_complete(commit))
 192                return 0;
 193        *it = commit;
 194        return 1;
 195}
 196
 197static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 198                const char *email, unsigned long timestamp, int tz,
 199                const char *message, void *cb_data)
 200{
 201        struct expire_reflog_cb *cb = cb_data;
 202        struct commit *old, *new;
 203
 204        if (timestamp < cb->cmd->expire_total)
 205                goto prune;
 206
 207        if (cb->cmd->stalefix &&
 208            (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
 209                goto prune;
 210
 211        if ((timestamp < cb->cmd->expire_unreachable) &&
 212            (!cb->ref_commit ||
 213             (old && !in_merge_bases(old, cb->ref_commit)) ||
 214             (new && !in_merge_bases(new, cb->ref_commit))))
 215                goto prune;
 216
 217        if (cb->newlog) {
 218                char sign = (tz < 0) ? '-' : '+';
 219                int zone = (tz < 0) ? (-tz) : tz;
 220                fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
 221                        sha1_to_hex(osha1), sha1_to_hex(nsha1),
 222                        email, timestamp, sign, zone,
 223                        message);
 224        }
 225        if (cb->cmd->verbose)
 226                printf("keep %s", message);
 227        return 0;
 228 prune:
 229        if (!cb->newlog || cb->cmd->verbose)
 230                printf("%sprune %s", cb->newlog ? "" : "would ", message);
 231        return 0;
 232}
 233
 234static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
 235{
 236        struct cmd_reflog_expire_cb *cmd = cb_data;
 237        struct expire_reflog_cb cb;
 238        struct ref_lock *lock;
 239        char *newlog_path = NULL;
 240        int status = 0;
 241
 242        if (strncmp(ref, "refs/", 5))
 243                return error("not a ref '%s'", ref);
 244
 245        memset(&cb, 0, sizeof(cb));
 246        /* we take the lock for the ref itself to prevent it from
 247         * getting updated.
 248         */
 249        lock = lock_ref_sha1(ref + 5, sha1);
 250        if (!lock)
 251                return error("cannot lock ref '%s'", ref);
 252        if (!file_exists(lock->log_file))
 253                goto finish;
 254        if (!cmd->dry_run) {
 255                newlog_path = xstrdup(git_path("logs/%s.lock", ref));
 256                cb.newlog = fopen(newlog_path, "w");
 257        }
 258
 259        cb.ref_commit = lookup_commit_reference_gently(sha1, 1);
 260        if (!cb.ref_commit)
 261                fprintf(stderr,
 262                        "warning: ref '%s' does not point at a commit\n", ref);
 263        cb.ref = ref;
 264        cb.cmd = cmd;
 265        for_each_reflog_ent(ref, expire_reflog_ent, &cb);
 266 finish:
 267        if (cb.newlog) {
 268                if (fclose(cb.newlog))
 269                        status |= error("%s: %s", strerror(errno),
 270                                        newlog_path);
 271                if (rename(newlog_path, lock->log_file)) {
 272                        status |= error("cannot rename %s to %s",
 273                                        newlog_path, lock->log_file);
 274                        unlink(newlog_path);
 275                }
 276        }
 277        free(newlog_path);
 278        unlock_ref(lock);
 279        return status;
 280}
 281
 282static int reflog_expire_config(const char *var, const char *value)
 283{
 284        if (!strcmp(var, "gc.reflogexpire"))
 285                default_reflog_expire = approxidate(value);
 286        else if (!strcmp(var, "gc.reflogexpireunreachable"))
 287                default_reflog_expire_unreachable = approxidate(value);
 288        else
 289                return git_default_config(var, value);
 290        return 0;
 291}
 292
 293static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
 294{
 295        struct cmd_reflog_expire_cb cb;
 296        unsigned long now = time(NULL);
 297        int i, status, do_all;
 298
 299        git_config(reflog_expire_config);
 300
 301        save_commit_buffer = 0;
 302        do_all = status = 0;
 303        memset(&cb, 0, sizeof(cb));
 304
 305        if (!default_reflog_expire_unreachable)
 306                default_reflog_expire_unreachable = now - 30 * 24 * 3600;
 307        if (!default_reflog_expire)
 308                default_reflog_expire = now - 90 * 24 * 3600;
 309        cb.expire_total = default_reflog_expire;
 310        cb.expire_unreachable = default_reflog_expire_unreachable;
 311
 312        /*
 313         * We can trust the commits and objects reachable from refs
 314         * even in older repository.  We cannot trust what's reachable
 315         * from reflog if the repository was pruned with older git.
 316         */
 317
 318        for (i = 1; i < argc; i++) {
 319                const char *arg = argv[i];
 320                if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
 321                        cb.dry_run = 1;
 322                else if (!strncmp(arg, "--expire=", 9))
 323                        cb.expire_total = approxidate(arg + 9);
 324                else if (!strncmp(arg, "--expire-unreachable=", 21))
 325                        cb.expire_unreachable = approxidate(arg + 21);
 326                else if (!strcmp(arg, "--stale-fix"))
 327                        cb.stalefix = 1;
 328                else if (!strcmp(arg, "--all"))
 329                        do_all = 1;
 330                else if (!strcmp(arg, "--verbose"))
 331                        cb.verbose = 1;
 332                else if (!strcmp(arg, "--")) {
 333                        i++;
 334                        break;
 335                }
 336                else if (arg[0] == '-')
 337                        usage(reflog_expire_usage);
 338                else
 339                        break;
 340        }
 341        if (cb.stalefix) {
 342                init_revisions(&cb.revs, prefix);
 343                if (cb.verbose)
 344                        printf("Marking reachable objects...");
 345                mark_reachable_objects(&cb.revs, 0);
 346                if (cb.verbose)
 347                        putchar('\n');
 348        }
 349
 350        if (do_all)
 351                status |= for_each_ref(expire_reflog, &cb);
 352        while (i < argc) {
 353                const char *ref = argv[i++];
 354                unsigned char sha1[20];
 355                if (!resolve_ref(ref, sha1, 1, NULL)) {
 356                        status |= error("%s points nowhere!", ref);
 357                        continue;
 358                }
 359                status |= expire_reflog(ref, sha1, 0, &cb);
 360        }
 361        return status;
 362}
 363
 364/*
 365 * main "reflog"
 366 */
 367
 368static const char reflog_usage[] =
 369"git-reflog (expire | ...)";
 370
 371int cmd_reflog(int argc, const char **argv, const char *prefix)
 372{
 373        if (argc < 2)
 374                usage(reflog_usage);
 375        else if (!strcmp(argv[1], "expire"))
 376                return cmd_reflog_expire(argc - 1, argv + 1, prefix);
 377        else
 378                usage(reflog_usage);
 379}