builtin-reflog.con commit Per-ref reflog expiry configuration (3cb22b8)
   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 (show|expire) [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
  17static const char reflog_delete_usage[] =
  18"git-reflog delete [--verbose] [--dry-run] [--rewrite] [--updateref] <refs>...";
  19
  20static unsigned long default_reflog_expire;
  21static unsigned long default_reflog_expire_unreachable;
  22
  23struct cmd_reflog_expire_cb {
  24        struct rev_info revs;
  25        int dry_run;
  26        int stalefix;
  27        int rewrite;
  28        int updateref;
  29        int verbose;
  30        unsigned long expire_total;
  31        unsigned long expire_unreachable;
  32        int recno;
  33};
  34
  35struct expire_reflog_cb {
  36        FILE *newlog;
  37        const char *ref;
  38        struct commit *ref_commit;
  39        struct cmd_reflog_expire_cb *cmd;
  40        unsigned char last_kept_sha1[20];
  41};
  42
  43struct collected_reflog {
  44        unsigned char sha1[20];
  45        char reflog[FLEX_ARRAY];
  46};
  47struct collect_reflog_cb {
  48        struct collected_reflog **e;
  49        int alloc;
  50        int nr;
  51};
  52
  53#define INCOMPLETE      (1u<<10)
  54#define STUDYING        (1u<<11)
  55
  56static int tree_is_complete(const unsigned char *sha1)
  57{
  58        struct tree_desc desc;
  59        struct name_entry entry;
  60        int complete;
  61        struct tree *tree;
  62
  63        tree = lookup_tree(sha1);
  64        if (!tree)
  65                return 0;
  66        if (tree->object.flags & SEEN)
  67                return 1;
  68        if (tree->object.flags & INCOMPLETE)
  69                return 0;
  70
  71        if (!tree->buffer) {
  72                enum object_type type;
  73                unsigned long size;
  74                void *data = read_sha1_file(sha1, &type, &size);
  75                if (!data) {
  76                        tree->object.flags |= INCOMPLETE;
  77                        return 0;
  78                }
  79                tree->buffer = data;
  80                tree->size = size;
  81        }
  82        init_tree_desc(&desc, tree->buffer, tree->size);
  83        complete = 1;
  84        while (tree_entry(&desc, &entry)) {
  85                if (!has_sha1_file(entry.sha1) ||
  86                    (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
  87                        tree->object.flags |= INCOMPLETE;
  88                        complete = 0;
  89                }
  90        }
  91        free(tree->buffer);
  92        tree->buffer = NULL;
  93
  94        if (complete)
  95                tree->object.flags |= SEEN;
  96        return complete;
  97}
  98
  99static int commit_is_complete(struct commit *commit)
 100{
 101        struct object_array study;
 102        struct object_array found;
 103        int is_incomplete = 0;
 104        int i;
 105
 106        /* early return */
 107        if (commit->object.flags & SEEN)
 108                return 1;
 109        if (commit->object.flags & INCOMPLETE)
 110                return 0;
 111        /*
 112         * Find all commits that are reachable and are not marked as
 113         * SEEN.  Then make sure the trees and blobs contained are
 114         * complete.  After that, mark these commits also as SEEN.
 115         * If some of the objects that are needed to complete this
 116         * commit are missing, mark this commit as INCOMPLETE.
 117         */
 118        memset(&study, 0, sizeof(study));
 119        memset(&found, 0, sizeof(found));
 120        add_object_array(&commit->object, NULL, &study);
 121        add_object_array(&commit->object, NULL, &found);
 122        commit->object.flags |= STUDYING;
 123        while (study.nr) {
 124                struct commit *c;
 125                struct commit_list *parent;
 126
 127                c = (struct commit *)study.objects[--study.nr].item;
 128                if (!c->object.parsed && !parse_object(c->object.sha1))
 129                        c->object.flags |= INCOMPLETE;
 130
 131                if (c->object.flags & INCOMPLETE) {
 132                        is_incomplete = 1;
 133                        break;
 134                }
 135                else if (c->object.flags & SEEN)
 136                        continue;
 137                for (parent = c->parents; parent; parent = parent->next) {
 138                        struct commit *p = parent->item;
 139                        if (p->object.flags & STUDYING)
 140                                continue;
 141                        p->object.flags |= STUDYING;
 142                        add_object_array(&p->object, NULL, &study);
 143                        add_object_array(&p->object, NULL, &found);
 144                }
 145        }
 146        if (!is_incomplete) {
 147                /*
 148                 * make sure all commits in "found" array have all the
 149                 * necessary objects.
 150                 */
 151                for (i = 0; i < found.nr; i++) {
 152                        struct commit *c =
 153                                (struct commit *)found.objects[i].item;
 154                        if (!tree_is_complete(c->tree->object.sha1)) {
 155                                is_incomplete = 1;
 156                                c->object.flags |= INCOMPLETE;
 157                        }
 158                }
 159                if (!is_incomplete) {
 160                        /* mark all found commits as complete, iow SEEN */
 161                        for (i = 0; i < found.nr; i++)
 162                                found.objects[i].item->flags |= SEEN;
 163                }
 164        }
 165        /* clear flags from the objects we traversed */
 166        for (i = 0; i < found.nr; i++)
 167                found.objects[i].item->flags &= ~STUDYING;
 168        if (is_incomplete)
 169                commit->object.flags |= INCOMPLETE;
 170        else {
 171                /*
 172                 * If we come here, we have (1) traversed the ancestry chain
 173                 * from the "commit" until we reach SEEN commits (which are
 174                 * known to be complete), and (2) made sure that the commits
 175                 * encountered during the above traversal refer to trees that
 176                 * are complete.  Which means that we know *all* the commits
 177                 * we have seen during this process are complete.
 178                 */
 179                for (i = 0; i < found.nr; i++)
 180                        found.objects[i].item->flags |= SEEN;
 181        }
 182        /* free object arrays */
 183        free(study.objects);
 184        free(found.objects);
 185        return !is_incomplete;
 186}
 187
 188static int keep_entry(struct commit **it, unsigned char *sha1)
 189{
 190        struct commit *commit;
 191
 192        if (is_null_sha1(sha1))
 193                return 1;
 194        commit = lookup_commit_reference_gently(sha1, 1);
 195        if (!commit)
 196                return 0;
 197
 198        /*
 199         * Make sure everything in this commit exists.
 200         *
 201         * We have walked all the objects reachable from the refs
 202         * and cache earlier.  The commits reachable by this commit
 203         * must meet SEEN commits -- and then we should mark them as
 204         * SEEN as well.
 205         */
 206        if (!commit_is_complete(commit))
 207                return 0;
 208        *it = commit;
 209        return 1;
 210}
 211
 212static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 213                const char *email, unsigned long timestamp, int tz,
 214                const char *message, void *cb_data)
 215{
 216        struct expire_reflog_cb *cb = cb_data;
 217        struct commit *old, *new;
 218
 219        if (timestamp < cb->cmd->expire_total)
 220                goto prune;
 221
 222        if (cb->cmd->rewrite)
 223                osha1 = cb->last_kept_sha1;
 224
 225        old = new = NULL;
 226        if (cb->cmd->stalefix &&
 227            (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
 228                goto prune;
 229
 230        if (timestamp < cb->cmd->expire_unreachable) {
 231                if (!cb->ref_commit)
 232                        goto prune;
 233                if (!old && !is_null_sha1(osha1))
 234                        old = lookup_commit_reference_gently(osha1, 1);
 235                if (!new && !is_null_sha1(nsha1))
 236                        new = lookup_commit_reference_gently(nsha1, 1);
 237                if ((old && !in_merge_bases(old, &cb->ref_commit, 1)) ||
 238                    (new && !in_merge_bases(new, &cb->ref_commit, 1)))
 239                        goto prune;
 240        }
 241
 242        if (cb->cmd->recno && --(cb->cmd->recno) == 0)
 243                goto prune;
 244
 245        if (cb->newlog) {
 246                char sign = (tz < 0) ? '-' : '+';
 247                int zone = (tz < 0) ? (-tz) : tz;
 248                fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
 249                        sha1_to_hex(osha1), sha1_to_hex(nsha1),
 250                        email, timestamp, sign, zone,
 251                        message);
 252                hashcpy(cb->last_kept_sha1, nsha1);
 253        }
 254        if (cb->cmd->verbose)
 255                printf("keep %s", message);
 256        return 0;
 257 prune:
 258        if (!cb->newlog || cb->cmd->verbose)
 259                printf("%sprune %s", cb->newlog ? "" : "would ", message);
 260        return 0;
 261}
 262
 263static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
 264{
 265        struct cmd_reflog_expire_cb *cmd = cb_data;
 266        struct expire_reflog_cb cb;
 267        struct ref_lock *lock;
 268        char *log_file, *newlog_path = NULL;
 269        int status = 0;
 270
 271        memset(&cb, 0, sizeof(cb));
 272
 273        /*
 274         * we take the lock for the ref itself to prevent it from
 275         * getting updated.
 276         */
 277        lock = lock_any_ref_for_update(ref, sha1, 0);
 278        if (!lock)
 279                return error("cannot lock ref '%s'", ref);
 280        log_file = xstrdup(git_path("logs/%s", ref));
 281        if (!file_exists(log_file))
 282                goto finish;
 283        if (!cmd->dry_run) {
 284                newlog_path = xstrdup(git_path("logs/%s.lock", ref));
 285                cb.newlog = fopen(newlog_path, "w");
 286        }
 287
 288        cb.ref_commit = lookup_commit_reference_gently(sha1, 1);
 289        cb.ref = ref;
 290        cb.cmd = cmd;
 291        for_each_reflog_ent(ref, expire_reflog_ent, &cb);
 292 finish:
 293        if (cb.newlog) {
 294                if (fclose(cb.newlog)) {
 295                        status |= error("%s: %s", strerror(errno),
 296                                        newlog_path);
 297                        unlink(newlog_path);
 298                } else if (cmd->updateref &&
 299                        (write_in_full(lock->lock_fd,
 300                                sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
 301                         write_in_full(lock->lock_fd, "\n", 1) != 1 ||
 302                         close_ref(lock) < 0)) {
 303                        status |= error("Couldn't write %s",
 304                                lock->lk->filename);
 305                        unlink(newlog_path);
 306                } else if (rename(newlog_path, log_file)) {
 307                        status |= error("cannot rename %s to %s",
 308                                        newlog_path, log_file);
 309                        unlink(newlog_path);
 310                } else if (cmd->updateref && commit_ref(lock)) {
 311                        status |= error("Couldn't set %s", lock->ref_name);
 312                } else {
 313                        adjust_shared_perm(log_file);
 314                }
 315        }
 316        free(newlog_path);
 317        free(log_file);
 318        unlock_ref(lock);
 319        return status;
 320}
 321
 322static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
 323{
 324        struct collected_reflog *e;
 325        struct collect_reflog_cb *cb = cb_data;
 326        size_t namelen = strlen(ref);
 327
 328        e = xmalloc(sizeof(*e) + namelen + 1);
 329        hashcpy(e->sha1, sha1);
 330        memcpy(e->reflog, ref, namelen + 1);
 331        ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
 332        cb->e[cb->nr++] = e;
 333        return 0;
 334}
 335
 336static struct reflog_expire_cfg {
 337        struct reflog_expire_cfg *next;
 338        unsigned long expire_total;
 339        unsigned long expire_unreachable;
 340        size_t len;
 341        char pattern[FLEX_ARRAY];
 342} *reflog_expire_cfg, **reflog_expire_cfg_tail;
 343
 344static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
 345{
 346        struct reflog_expire_cfg *ent;
 347
 348        if (!reflog_expire_cfg_tail)
 349                reflog_expire_cfg_tail = &reflog_expire_cfg;
 350
 351        for (ent = reflog_expire_cfg; ent; ent = ent->next)
 352                if (ent->len == len &&
 353                    !memcmp(ent->pattern, pattern, len))
 354                        return ent;
 355
 356        ent = xcalloc(1, (sizeof(*ent) + len));
 357        memcpy(ent->pattern, pattern, len);
 358        ent->len = len;
 359        *reflog_expire_cfg_tail = ent;
 360        reflog_expire_cfg_tail = &(ent->next);
 361        return ent;
 362}
 363
 364static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire)
 365{
 366        if (!value)
 367                return config_error_nonbool(var);
 368        if (!strcmp(value, "never") || !strcmp(value, "false")) {
 369                *expire = 0;
 370                return 0;
 371        }
 372        *expire = approxidate(value);
 373        return 0;
 374}
 375
 376/* expiry timer slot */
 377#define EXPIRE_TOTAL   01
 378#define EXPIRE_UNREACH 02
 379
 380static int reflog_expire_config(const char *var, const char *value, void *cb)
 381{
 382        const char *lastdot = strrchr(var, '.');
 383        unsigned long expire;
 384        int slot;
 385        struct reflog_expire_cfg *ent;
 386
 387        if (!lastdot || prefixcmp(var, "gc."))
 388                return git_default_config(var, value, cb);
 389
 390        if (!strcmp(lastdot, ".reflogexpire")) {
 391                slot = EXPIRE_TOTAL;
 392                if (parse_expire_cfg_value(var, value, &expire))
 393                        return -1;
 394        } else if (!strcmp(lastdot, ".reflogexpireunreachable")) {
 395                slot = EXPIRE_UNREACH;
 396                if (parse_expire_cfg_value(var, value, &expire))
 397                        return -1;
 398        } else
 399                return git_default_config(var, value, cb);
 400
 401        if (lastdot == var + 2) {
 402                switch (slot) {
 403                case EXPIRE_TOTAL:
 404                        default_reflog_expire = expire;
 405                        break;
 406                case EXPIRE_UNREACH:
 407                        default_reflog_expire_unreachable = expire;
 408                        break;
 409                }
 410                return 0;
 411        }
 412
 413        ent = find_cfg_ent(var + 3, lastdot - (var+3));
 414        if (!ent)
 415                return -1;
 416        switch (slot) {
 417        case EXPIRE_TOTAL:
 418                ent->expire_total = expire;
 419                break;
 420        case EXPIRE_UNREACH:
 421                ent->expire_unreachable = expire;
 422                break;
 423        }
 424        return 0;
 425}
 426
 427static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
 428{
 429        struct reflog_expire_cfg *ent;
 430
 431        if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
 432                return; /* both given explicitly -- nothing to tweak */
 433
 434        for (ent = reflog_expire_cfg; ent; ent = ent->next) {
 435                if (!fnmatch(ent->pattern, ref, 0)) {
 436                        if (!(slot & EXPIRE_TOTAL))
 437                                cb->expire_total = ent->expire_total;
 438                        if (!(slot & EXPIRE_UNREACH))
 439                                cb->expire_unreachable = ent->expire_unreachable;
 440                        return;
 441                }
 442        }
 443
 444        /* Nothing matched -- use the default value */
 445        if (!(slot & EXPIRE_TOTAL))
 446                cb->expire_total = default_reflog_expire;
 447        if (!(slot & EXPIRE_UNREACH))
 448                cb->expire_unreachable = default_reflog_expire_unreachable;
 449}
 450
 451static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
 452{
 453        struct cmd_reflog_expire_cb cb;
 454        unsigned long now = time(NULL);
 455        int i, status, do_all;
 456        int explicit_expiry = 0;
 457
 458        git_config(reflog_expire_config, NULL);
 459
 460        save_commit_buffer = 0;
 461        do_all = status = 0;
 462        memset(&cb, 0, sizeof(cb));
 463
 464        if (!default_reflog_expire_unreachable)
 465                default_reflog_expire_unreachable = now - 30 * 24 * 3600;
 466        if (!default_reflog_expire)
 467                default_reflog_expire = now - 90 * 24 * 3600;
 468        cb.expire_total = default_reflog_expire;
 469        cb.expire_unreachable = default_reflog_expire_unreachable;
 470
 471        for (i = 1; i < argc; i++) {
 472                const char *arg = argv[i];
 473                if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
 474                        cb.dry_run = 1;
 475                else if (!prefixcmp(arg, "--expire=")) {
 476                        cb.expire_total = approxidate(arg + 9);
 477                        explicit_expiry |= EXPIRE_TOTAL;
 478                }
 479                else if (!prefixcmp(arg, "--expire-unreachable=")) {
 480                        cb.expire_unreachable = approxidate(arg + 21);
 481                        explicit_expiry |= EXPIRE_UNREACH;
 482                }
 483                else if (!strcmp(arg, "--stale-fix"))
 484                        cb.stalefix = 1;
 485                else if (!strcmp(arg, "--rewrite"))
 486                        cb.rewrite = 1;
 487                else if (!strcmp(arg, "--updateref"))
 488                        cb.updateref = 1;
 489                else if (!strcmp(arg, "--all"))
 490                        do_all = 1;
 491                else if (!strcmp(arg, "--verbose"))
 492                        cb.verbose = 1;
 493                else if (!strcmp(arg, "--")) {
 494                        i++;
 495                        break;
 496                }
 497                else if (arg[0] == '-')
 498                        usage(reflog_expire_usage);
 499                else
 500                        break;
 501        }
 502
 503        /*
 504         * We can trust the commits and objects reachable from refs
 505         * even in older repository.  We cannot trust what's reachable
 506         * from reflog if the repository was pruned with older git.
 507         */
 508        if (cb.stalefix) {
 509                init_revisions(&cb.revs, prefix);
 510                if (cb.verbose)
 511                        printf("Marking reachable objects...");
 512                mark_reachable_objects(&cb.revs, 0);
 513                if (cb.verbose)
 514                        putchar('\n');
 515        }
 516
 517        if (do_all) {
 518                struct collect_reflog_cb collected;
 519                int i;
 520
 521                memset(&collected, 0, sizeof(collected));
 522                for_each_reflog(collect_reflog, &collected);
 523                for (i = 0; i < collected.nr; i++) {
 524                        struct collected_reflog *e = collected.e[i];
 525                        set_reflog_expiry_param(&cb, explicit_expiry, e->reflog);
 526                        status |= expire_reflog(e->reflog, e->sha1, 0, &cb);
 527                        free(e);
 528                }
 529                free(collected.e);
 530        }
 531
 532        while (i < argc) {
 533                const char *ref = argv[i++];
 534                unsigned char sha1[20];
 535                if (!resolve_ref(ref, sha1, 1, NULL)) {
 536                        status |= error("%s points nowhere!", ref);
 537                        continue;
 538                }
 539                set_reflog_expiry_param(&cb, explicit_expiry, ref);
 540                status |= expire_reflog(ref, sha1, 0, &cb);
 541        }
 542        return status;
 543}
 544
 545static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 546                const char *email, unsigned long timestamp, int tz,
 547                const char *message, void *cb_data)
 548{
 549        struct cmd_reflog_expire_cb *cb = cb_data;
 550        if (!cb->expire_total || timestamp < cb->expire_total)
 551                cb->recno++;
 552        return 0;
 553}
 554
 555static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
 556{
 557        struct cmd_reflog_expire_cb cb;
 558        int i, status = 0;
 559
 560        memset(&cb, 0, sizeof(cb));
 561
 562        for (i = 1; i < argc; i++) {
 563                const char *arg = argv[i];
 564                if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
 565                        cb.dry_run = 1;
 566                else if (!strcmp(arg, "--rewrite"))
 567                        cb.rewrite = 1;
 568                else if (!strcmp(arg, "--updateref"))
 569                        cb.updateref = 1;
 570                else if (!strcmp(arg, "--verbose"))
 571                        cb.verbose = 1;
 572                else if (!strcmp(arg, "--")) {
 573                        i++;
 574                        break;
 575                }
 576                else if (arg[0] == '-')
 577                        usage(reflog_delete_usage);
 578                else
 579                        break;
 580        }
 581
 582        if (argc - i < 1)
 583                return error("Nothing to delete?");
 584
 585        for ( ; i < argc; i++) {
 586                const char *spec = strstr(argv[i], "@{");
 587                unsigned char sha1[20];
 588                char *ep, *ref;
 589                int recno;
 590
 591                if (!spec) {
 592                        status |= error("Not a reflog: %s", argv[i]);
 593                        continue;
 594                }
 595
 596                if (!dwim_ref(argv[i], spec - argv[i], sha1, &ref)) {
 597                        status |= error("%s points nowhere!", argv[i]);
 598                        continue;
 599                }
 600
 601                recno = strtoul(spec + 2, &ep, 10);
 602                if (*ep == '}') {
 603                        cb.recno = -recno;
 604                        for_each_reflog_ent(ref, count_reflog_ent, &cb);
 605                } else {
 606                        cb.expire_total = approxidate(spec + 2);
 607                        for_each_reflog_ent(ref, count_reflog_ent, &cb);
 608                        cb.expire_total = 0;
 609                }
 610
 611                status |= expire_reflog(ref, sha1, 0, &cb);
 612                free(ref);
 613        }
 614        return status;
 615}
 616
 617/*
 618 * main "reflog"
 619 */
 620
 621static const char reflog_usage[] =
 622"git-reflog (expire | ...)";
 623
 624int cmd_reflog(int argc, const char **argv, const char *prefix)
 625{
 626        /* With no command, we default to showing it. */
 627        if (argc < 2 || *argv[1] == '-')
 628                return cmd_log_reflog(argc, argv, prefix);
 629
 630        if (!strcmp(argv[1], "show"))
 631                return cmd_log_reflog(argc - 1, argv + 1, prefix);
 632
 633        if (!strcmp(argv[1], "expire"))
 634                return cmd_reflog_expire(argc - 1, argv + 1, prefix);
 635
 636        if (!strcmp(argv[1], "delete"))
 637                return cmd_reflog_delete(argc - 1, argv + 1, prefix);
 638
 639        /* Not a recognized reflog command..*/
 640        usage(reflog_usage);
 641}