dfff5f2fe8ce047a2d717c6631e8efc74b92ed0a
   1#include "builtin.h"
   2#include "lockfile.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] [--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        enum {
  38                UE_NORMAL,
  39                UE_ALWAYS,
  40                UE_HEAD
  41        } unreachable_expire_kind;
  42        struct commit_list *mark_list;
  43        unsigned long mark_limit;
  44        struct cmd_reflog_expire_cb *cmd;
  45        unsigned char last_kept_sha1[20];
  46        struct commit *tip_commit;
  47        struct commit_list *tips;
  48};
  49
  50struct collected_reflog {
  51        unsigned char sha1[20];
  52        char reflog[FLEX_ARRAY];
  53};
  54struct collect_reflog_cb {
  55        struct collected_reflog **e;
  56        int alloc;
  57        int nr;
  58};
  59
  60#define INCOMPLETE      (1u<<10)
  61#define STUDYING        (1u<<11)
  62#define REACHABLE       (1u<<12)
  63
  64static int tree_is_complete(const unsigned char *sha1)
  65{
  66        struct tree_desc desc;
  67        struct name_entry entry;
  68        int complete;
  69        struct tree *tree;
  70
  71        tree = lookup_tree(sha1);
  72        if (!tree)
  73                return 0;
  74        if (tree->object.flags & SEEN)
  75                return 1;
  76        if (tree->object.flags & INCOMPLETE)
  77                return 0;
  78
  79        if (!tree->buffer) {
  80                enum object_type type;
  81                unsigned long size;
  82                void *data = read_sha1_file(sha1, &type, &size);
  83                if (!data) {
  84                        tree->object.flags |= INCOMPLETE;
  85                        return 0;
  86                }
  87                tree->buffer = data;
  88                tree->size = size;
  89        }
  90        init_tree_desc(&desc, tree->buffer, tree->size);
  91        complete = 1;
  92        while (tree_entry(&desc, &entry)) {
  93                if (!has_sha1_file(entry.sha1) ||
  94                    (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
  95                        tree->object.flags |= INCOMPLETE;
  96                        complete = 0;
  97                }
  98        }
  99        free_tree_buffer(tree);
 100
 101        if (complete)
 102                tree->object.flags |= SEEN;
 103        return complete;
 104}
 105
 106static int commit_is_complete(struct commit *commit)
 107{
 108        struct object_array study;
 109        struct object_array found;
 110        int is_incomplete = 0;
 111        int i;
 112
 113        /* early return */
 114        if (commit->object.flags & SEEN)
 115                return 1;
 116        if (commit->object.flags & INCOMPLETE)
 117                return 0;
 118        /*
 119         * Find all commits that are reachable and are not marked as
 120         * SEEN.  Then make sure the trees and blobs contained are
 121         * complete.  After that, mark these commits also as SEEN.
 122         * If some of the objects that are needed to complete this
 123         * commit are missing, mark this commit as INCOMPLETE.
 124         */
 125        memset(&study, 0, sizeof(study));
 126        memset(&found, 0, sizeof(found));
 127        add_object_array(&commit->object, NULL, &study);
 128        add_object_array(&commit->object, NULL, &found);
 129        commit->object.flags |= STUDYING;
 130        while (study.nr) {
 131                struct commit *c;
 132                struct commit_list *parent;
 133
 134                c = (struct commit *)study.objects[--study.nr].item;
 135                if (!c->object.parsed && !parse_object(c->object.sha1))
 136                        c->object.flags |= INCOMPLETE;
 137
 138                if (c->object.flags & INCOMPLETE) {
 139                        is_incomplete = 1;
 140                        break;
 141                }
 142                else if (c->object.flags & SEEN)
 143                        continue;
 144                for (parent = c->parents; parent; parent = parent->next) {
 145                        struct commit *p = parent->item;
 146                        if (p->object.flags & STUDYING)
 147                                continue;
 148                        p->object.flags |= STUDYING;
 149                        add_object_array(&p->object, NULL, &study);
 150                        add_object_array(&p->object, NULL, &found);
 151                }
 152        }
 153        if (!is_incomplete) {
 154                /*
 155                 * make sure all commits in "found" array have all the
 156                 * necessary objects.
 157                 */
 158                for (i = 0; i < found.nr; i++) {
 159                        struct commit *c =
 160                                (struct commit *)found.objects[i].item;
 161                        if (!tree_is_complete(c->tree->object.sha1)) {
 162                                is_incomplete = 1;
 163                                c->object.flags |= INCOMPLETE;
 164                        }
 165                }
 166                if (!is_incomplete) {
 167                        /* mark all found commits as complete, iow SEEN */
 168                        for (i = 0; i < found.nr; i++)
 169                                found.objects[i].item->flags |= SEEN;
 170                }
 171        }
 172        /* clear flags from the objects we traversed */
 173        for (i = 0; i < found.nr; i++)
 174                found.objects[i].item->flags &= ~STUDYING;
 175        if (is_incomplete)
 176                commit->object.flags |= INCOMPLETE;
 177        else {
 178                /*
 179                 * If we come here, we have (1) traversed the ancestry chain
 180                 * from the "commit" until we reach SEEN commits (which are
 181                 * known to be complete), and (2) made sure that the commits
 182                 * encountered during the above traversal refer to trees that
 183                 * are complete.  Which means that we know *all* the commits
 184                 * we have seen during this process are complete.
 185                 */
 186                for (i = 0; i < found.nr; i++)
 187                        found.objects[i].item->flags |= SEEN;
 188        }
 189        /* free object arrays */
 190        free(study.objects);
 191        free(found.objects);
 192        return !is_incomplete;
 193}
 194
 195static int keep_entry(struct commit **it, unsigned char *sha1)
 196{
 197        struct commit *commit;
 198
 199        if (is_null_sha1(sha1))
 200                return 1;
 201        commit = lookup_commit_reference_gently(sha1, 1);
 202        if (!commit)
 203                return 0;
 204
 205        /*
 206         * Make sure everything in this commit exists.
 207         *
 208         * We have walked all the objects reachable from the refs
 209         * and cache earlier.  The commits reachable by this commit
 210         * must meet SEEN commits -- and then we should mark them as
 211         * SEEN as well.
 212         */
 213        if (!commit_is_complete(commit))
 214                return 0;
 215        *it = commit;
 216        return 1;
 217}
 218
 219/*
 220 * Starting from commits in the cb->mark_list, mark commits that are
 221 * reachable from them.  Stop the traversal at commits older than
 222 * the expire_limit and queue them back, so that the caller can call
 223 * us again to restart the traversal with longer expire_limit.
 224 */
 225static void mark_reachable(struct expire_reflog_cb *cb)
 226{
 227        struct commit *commit;
 228        struct commit_list *pending;
 229        unsigned long expire_limit = cb->mark_limit;
 230        struct commit_list *leftover = NULL;
 231
 232        for (pending = cb->mark_list; pending; pending = pending->next)
 233                pending->item->object.flags &= ~REACHABLE;
 234
 235        pending = cb->mark_list;
 236        while (pending) {
 237                struct commit_list *entry = pending;
 238                struct commit_list *parent;
 239                pending = entry->next;
 240                commit = entry->item;
 241                free(entry);
 242                if (commit->object.flags & REACHABLE)
 243                        continue;
 244                if (parse_commit(commit))
 245                        continue;
 246                commit->object.flags |= REACHABLE;
 247                if (commit->date < expire_limit) {
 248                        commit_list_insert(commit, &leftover);
 249                        continue;
 250                }
 251                commit->object.flags |= REACHABLE;
 252                parent = commit->parents;
 253                while (parent) {
 254                        commit = parent->item;
 255                        parent = parent->next;
 256                        if (commit->object.flags & REACHABLE)
 257                                continue;
 258                        commit_list_insert(commit, &pending);
 259                }
 260        }
 261        cb->mark_list = leftover;
 262}
 263
 264static int unreachable(struct expire_reflog_cb *cb, struct commit *commit, unsigned char *sha1)
 265{
 266        /*
 267         * We may or may not have the commit yet - if not, look it
 268         * up using the supplied sha1.
 269         */
 270        if (!commit) {
 271                if (is_null_sha1(sha1))
 272                        return 0;
 273
 274                commit = lookup_commit_reference_gently(sha1, 1);
 275
 276                /* Not a commit -- keep it */
 277                if (!commit)
 278                        return 0;
 279        }
 280
 281        /* Reachable from the current ref?  Don't prune. */
 282        if (commit->object.flags & REACHABLE)
 283                return 0;
 284
 285        if (cb->mark_list && cb->mark_limit) {
 286                cb->mark_limit = 0; /* dig down to the root */
 287                mark_reachable(cb);
 288        }
 289
 290        return !(commit->object.flags & REACHABLE);
 291}
 292
 293/*
 294 * Return true iff the specified reflog entry should be expired.
 295 */
 296static int should_expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 297                                    const char *email, unsigned long timestamp, int tz,
 298                                    const char *message, void *cb_data)
 299{
 300        struct expire_reflog_cb *cb = cb_data;
 301        struct commit *old, *new;
 302
 303        if (timestamp < cb->cmd->expire_total)
 304                return 1;
 305
 306        old = new = NULL;
 307        if (cb->cmd->stalefix &&
 308            (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
 309                return 1;
 310
 311        if (timestamp < cb->cmd->expire_unreachable) {
 312                if (cb->unreachable_expire_kind == UE_ALWAYS)
 313                        return 1;
 314                if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1))
 315                        return 1;
 316        }
 317
 318        if (cb->cmd->recno && --(cb->cmd->recno) == 0)
 319                return 1;
 320
 321        return 0;
 322}
 323
 324static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 325                const char *email, unsigned long timestamp, int tz,
 326                const char *message, void *cb_data)
 327{
 328        struct expire_reflog_cb *cb = cb_data;
 329
 330        if (cb->cmd->rewrite)
 331                osha1 = cb->last_kept_sha1;
 332
 333        if (should_expire_reflog_ent(osha1, nsha1, email, timestamp, tz,
 334                                     message, cb_data)) {
 335                if (!cb->newlog)
 336                        printf("would prune %s", message);
 337                else if (cb->cmd->verbose)
 338                        printf("prune %s", message);
 339        } else {
 340                if (cb->newlog) {
 341                        char sign = (tz < 0) ? '-' : '+';
 342                        int zone = (tz < 0) ? (-tz) : tz;
 343                        fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
 344                                sha1_to_hex(osha1), sha1_to_hex(nsha1),
 345                                email, timestamp, sign, zone,
 346                                message);
 347                        hashcpy(cb->last_kept_sha1, nsha1);
 348                }
 349                if (cb->cmd->verbose)
 350                        printf("keep %s", message);
 351        }
 352        return 0;
 353}
 354
 355static int push_tip_to_list(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
 356{
 357        struct commit_list **list = cb_data;
 358        struct commit *tip_commit;
 359        if (flags & REF_ISSYMREF)
 360                return 0;
 361        tip_commit = lookup_commit_reference_gently(sha1, 1);
 362        if (!tip_commit)
 363                return 0;
 364        commit_list_insert(tip_commit, list);
 365        return 0;
 366}
 367
 368static void reflog_expiry_prepare(const char *refname,
 369                                  const unsigned char *sha1,
 370                                  struct expire_reflog_cb *cb)
 371{
 372        if (!cb->cmd->expire_unreachable || !strcmp(refname, "HEAD")) {
 373                cb->tip_commit = NULL;
 374                cb->unreachable_expire_kind = UE_HEAD;
 375        } else {
 376                cb->tip_commit = lookup_commit_reference_gently(sha1, 1);
 377                if (!cb->tip_commit)
 378                        cb->unreachable_expire_kind = UE_ALWAYS;
 379                else
 380                        cb->unreachable_expire_kind = UE_NORMAL;
 381        }
 382
 383        if (cb->cmd->expire_unreachable <= cb->cmd->expire_total)
 384                cb->unreachable_expire_kind = UE_ALWAYS;
 385
 386        cb->mark_list = NULL;
 387        cb->tips = NULL;
 388        if (cb->unreachable_expire_kind != UE_ALWAYS) {
 389                if (cb->unreachable_expire_kind == UE_HEAD) {
 390                        struct commit_list *elem;
 391                        for_each_ref(push_tip_to_list, &cb->tips);
 392                        for (elem = cb->tips; elem; elem = elem->next)
 393                                commit_list_insert(elem->item, &cb->mark_list);
 394                } else {
 395                        commit_list_insert(cb->tip_commit, &cb->mark_list);
 396                }
 397                cb->mark_limit = cb->cmd->expire_total;
 398                mark_reachable(cb);
 399        }
 400}
 401
 402static void reflog_expiry_cleanup(struct expire_reflog_cb *cb)
 403{
 404        if (cb->unreachable_expire_kind != UE_ALWAYS) {
 405                if (cb->unreachable_expire_kind == UE_HEAD) {
 406                        struct commit_list *elem;
 407                        for (elem = cb->tips; elem; elem = elem->next)
 408                                clear_commit_marks(elem->item, REACHABLE);
 409                        free_commit_list(cb->tips);
 410                } else {
 411                        clear_commit_marks(cb->tip_commit, REACHABLE);
 412                }
 413        }
 414}
 415
 416static int expire_reflog(const char *refname, const unsigned char *sha1,
 417                         unsigned int flags, struct cmd_reflog_expire_cb *cmd)
 418{
 419        static struct lock_file reflog_lock;
 420        struct expire_reflog_cb cb;
 421        struct ref_lock *lock;
 422        char *log_file;
 423        int status = 0;
 424
 425        memset(&cb, 0, sizeof(cb));
 426
 427        /*
 428         * The reflog file is locked by holding the lock on the
 429         * reference itself, plus we might need to update the
 430         * reference if --updateref was specified:
 431         */
 432        lock = lock_any_ref_for_update(refname, sha1, 0, NULL);
 433        if (!lock)
 434                return error("cannot lock ref '%s'", refname);
 435        if (!reflog_exists(refname)) {
 436                unlock_ref(lock);
 437                return 0;
 438        }
 439
 440        log_file = git_pathdup("logs/%s", refname);
 441        if (!cmd->dry_run) {
 442                /*
 443                 * Even though holding $GIT_DIR/logs/$reflog.lock has
 444                 * no locking implications, we use the lock_file
 445                 * machinery here anyway because it does a lot of the
 446                 * work we need, including cleaning up if the program
 447                 * exits unexpectedly.
 448                 */
 449                if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
 450                        struct strbuf err = STRBUF_INIT;
 451                        unable_to_lock_message(log_file, errno, &err);
 452                        error("%s", err.buf);
 453                        strbuf_release(&err);
 454                        goto failure;
 455                }
 456                cb.newlog = fdopen_lock_file(&reflog_lock, "w");
 457                if (!cb.newlog) {
 458                        error("cannot fdopen %s (%s)",
 459                              reflog_lock.filename.buf, strerror(errno));
 460                        goto failure;
 461                }
 462        }
 463
 464        cb.cmd = cmd;
 465
 466        reflog_expiry_prepare(refname, sha1, &cb);
 467        for_each_reflog_ent(refname, expire_reflog_ent, &cb);
 468        reflog_expiry_cleanup(&cb);
 469
 470        if (cb.newlog) {
 471                if (close_lock_file(&reflog_lock)) {
 472                        status |= error("couldn't write %s: %s", log_file,
 473                                        strerror(errno));
 474                } else if (cmd->updateref &&
 475                        (write_in_full(lock->lock_fd,
 476                                sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
 477                         write_str_in_full(lock->lock_fd, "\n") != 1 ||
 478                         close_ref(lock) < 0)) {
 479                        status |= error("couldn't write %s",
 480                                        lock->lk->filename.buf);
 481                        rollback_lock_file(&reflog_lock);
 482                } else if (commit_lock_file(&reflog_lock)) {
 483                        status |= error("unable to commit reflog '%s' (%s)",
 484                                        log_file, strerror(errno));
 485                } else if (cmd->updateref && commit_ref(lock)) {
 486                        status |= error("couldn't set %s", lock->ref_name);
 487                }
 488        }
 489        free(log_file);
 490        unlock_ref(lock);
 491        return status;
 492
 493 failure:
 494        rollback_lock_file(&reflog_lock);
 495        free(log_file);
 496        unlock_ref(lock);
 497        return -1;
 498}
 499
 500static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
 501{
 502        struct collected_reflog *e;
 503        struct collect_reflog_cb *cb = cb_data;
 504        size_t namelen = strlen(ref);
 505
 506        e = xmalloc(sizeof(*e) + namelen + 1);
 507        hashcpy(e->sha1, sha1);
 508        memcpy(e->reflog, ref, namelen + 1);
 509        ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
 510        cb->e[cb->nr++] = e;
 511        return 0;
 512}
 513
 514static struct reflog_expire_cfg {
 515        struct reflog_expire_cfg *next;
 516        unsigned long expire_total;
 517        unsigned long expire_unreachable;
 518        size_t len;
 519        char pattern[FLEX_ARRAY];
 520} *reflog_expire_cfg, **reflog_expire_cfg_tail;
 521
 522static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
 523{
 524        struct reflog_expire_cfg *ent;
 525
 526        if (!reflog_expire_cfg_tail)
 527                reflog_expire_cfg_tail = &reflog_expire_cfg;
 528
 529        for (ent = reflog_expire_cfg; ent; ent = ent->next)
 530                if (ent->len == len &&
 531                    !memcmp(ent->pattern, pattern, len))
 532                        return ent;
 533
 534        ent = xcalloc(1, (sizeof(*ent) + len));
 535        memcpy(ent->pattern, pattern, len);
 536        ent->len = len;
 537        *reflog_expire_cfg_tail = ent;
 538        reflog_expire_cfg_tail = &(ent->next);
 539        return ent;
 540}
 541
 542static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire)
 543{
 544        if (!value)
 545                return config_error_nonbool(var);
 546        if (parse_expiry_date(value, expire))
 547                return error(_("%s' for '%s' is not a valid timestamp"),
 548                             value, var);
 549        return 0;
 550}
 551
 552/* expiry timer slot */
 553#define EXPIRE_TOTAL   01
 554#define EXPIRE_UNREACH 02
 555
 556static int reflog_expire_config(const char *var, const char *value, void *cb)
 557{
 558        const char *pattern, *key;
 559        int pattern_len;
 560        unsigned long expire;
 561        int slot;
 562        struct reflog_expire_cfg *ent;
 563
 564        if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
 565                return git_default_config(var, value, cb);
 566
 567        if (!strcmp(key, "reflogexpire")) {
 568                slot = EXPIRE_TOTAL;
 569                if (parse_expire_cfg_value(var, value, &expire))
 570                        return -1;
 571        } else if (!strcmp(key, "reflogexpireunreachable")) {
 572                slot = EXPIRE_UNREACH;
 573                if (parse_expire_cfg_value(var, value, &expire))
 574                        return -1;
 575        } else
 576                return git_default_config(var, value, cb);
 577
 578        if (!pattern) {
 579                switch (slot) {
 580                case EXPIRE_TOTAL:
 581                        default_reflog_expire = expire;
 582                        break;
 583                case EXPIRE_UNREACH:
 584                        default_reflog_expire_unreachable = expire;
 585                        break;
 586                }
 587                return 0;
 588        }
 589
 590        ent = find_cfg_ent(pattern, pattern_len);
 591        if (!ent)
 592                return -1;
 593        switch (slot) {
 594        case EXPIRE_TOTAL:
 595                ent->expire_total = expire;
 596                break;
 597        case EXPIRE_UNREACH:
 598                ent->expire_unreachable = expire;
 599                break;
 600        }
 601        return 0;
 602}
 603
 604static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
 605{
 606        struct reflog_expire_cfg *ent;
 607
 608        if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
 609                return; /* both given explicitly -- nothing to tweak */
 610
 611        for (ent = reflog_expire_cfg; ent; ent = ent->next) {
 612                if (!wildmatch(ent->pattern, ref, 0, NULL)) {
 613                        if (!(slot & EXPIRE_TOTAL))
 614                                cb->expire_total = ent->expire_total;
 615                        if (!(slot & EXPIRE_UNREACH))
 616                                cb->expire_unreachable = ent->expire_unreachable;
 617                        return;
 618                }
 619        }
 620
 621        /*
 622         * If unconfigured, make stash never expire
 623         */
 624        if (!strcmp(ref, "refs/stash")) {
 625                if (!(slot & EXPIRE_TOTAL))
 626                        cb->expire_total = 0;
 627                if (!(slot & EXPIRE_UNREACH))
 628                        cb->expire_unreachable = 0;
 629                return;
 630        }
 631
 632        /* Nothing matched -- use the default value */
 633        if (!(slot & EXPIRE_TOTAL))
 634                cb->expire_total = default_reflog_expire;
 635        if (!(slot & EXPIRE_UNREACH))
 636                cb->expire_unreachable = default_reflog_expire_unreachable;
 637}
 638
 639static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
 640{
 641        struct cmd_reflog_expire_cb cb;
 642        unsigned long now = time(NULL);
 643        int i, status, do_all;
 644        int explicit_expiry = 0;
 645        unsigned int flags = 0;
 646
 647        default_reflog_expire_unreachable = now - 30 * 24 * 3600;
 648        default_reflog_expire = now - 90 * 24 * 3600;
 649        git_config(reflog_expire_config, NULL);
 650
 651        save_commit_buffer = 0;
 652        do_all = status = 0;
 653        memset(&cb, 0, sizeof(cb));
 654
 655        cb.expire_total = default_reflog_expire;
 656        cb.expire_unreachable = default_reflog_expire_unreachable;
 657
 658        for (i = 1; i < argc; i++) {
 659                const char *arg = argv[i];
 660                if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
 661                        cb.dry_run = 1;
 662                else if (starts_with(arg, "--expire=")) {
 663                        if (parse_expiry_date(arg + 9, &cb.expire_total))
 664                                die(_("'%s' is not a valid timestamp"), arg);
 665                        explicit_expiry |= EXPIRE_TOTAL;
 666                }
 667                else if (starts_with(arg, "--expire-unreachable=")) {
 668                        if (parse_expiry_date(arg + 21, &cb.expire_unreachable))
 669                                die(_("'%s' is not a valid timestamp"), arg);
 670                        explicit_expiry |= EXPIRE_UNREACH;
 671                }
 672                else if (!strcmp(arg, "--stale-fix"))
 673                        cb.stalefix = 1;
 674                else if (!strcmp(arg, "--rewrite"))
 675                        cb.rewrite = 1;
 676                else if (!strcmp(arg, "--updateref"))
 677                        cb.updateref = 1;
 678                else if (!strcmp(arg, "--all"))
 679                        do_all = 1;
 680                else if (!strcmp(arg, "--verbose"))
 681                        cb.verbose = 1;
 682                else if (!strcmp(arg, "--")) {
 683                        i++;
 684                        break;
 685                }
 686                else if (arg[0] == '-')
 687                        usage(reflog_expire_usage);
 688                else
 689                        break;
 690        }
 691
 692        /*
 693         * We can trust the commits and objects reachable from refs
 694         * even in older repository.  We cannot trust what's reachable
 695         * from reflog if the repository was pruned with older git.
 696         */
 697        if (cb.stalefix) {
 698                init_revisions(&cb.revs, prefix);
 699                if (cb.verbose)
 700                        printf("Marking reachable objects...");
 701                mark_reachable_objects(&cb.revs, 0, 0, NULL);
 702                if (cb.verbose)
 703                        putchar('\n');
 704        }
 705
 706        if (do_all) {
 707                struct collect_reflog_cb collected;
 708                int i;
 709
 710                memset(&collected, 0, sizeof(collected));
 711                for_each_reflog(collect_reflog, &collected);
 712                for (i = 0; i < collected.nr; i++) {
 713                        struct collected_reflog *e = collected.e[i];
 714                        set_reflog_expiry_param(&cb, explicit_expiry, e->reflog);
 715                        status |= expire_reflog(e->reflog, e->sha1, flags, &cb);
 716                        free(e);
 717                }
 718                free(collected.e);
 719        }
 720
 721        for (; i < argc; i++) {
 722                char *ref;
 723                unsigned char sha1[20];
 724                if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) {
 725                        status |= error("%s points nowhere!", argv[i]);
 726                        continue;
 727                }
 728                set_reflog_expiry_param(&cb, explicit_expiry, ref);
 729                status |= expire_reflog(ref, sha1, flags, &cb);
 730        }
 731        return status;
 732}
 733
 734static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 735                const char *email, unsigned long timestamp, int tz,
 736                const char *message, void *cb_data)
 737{
 738        struct cmd_reflog_expire_cb *cb = cb_data;
 739        if (!cb->expire_total || timestamp < cb->expire_total)
 740                cb->recno++;
 741        return 0;
 742}
 743
 744static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
 745{
 746        struct cmd_reflog_expire_cb cb;
 747        int i, status = 0;
 748        unsigned int flags = 0;
 749
 750        memset(&cb, 0, sizeof(cb));
 751
 752        for (i = 1; i < argc; i++) {
 753                const char *arg = argv[i];
 754                if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
 755                        cb.dry_run = 1;
 756                else if (!strcmp(arg, "--rewrite"))
 757                        cb.rewrite = 1;
 758                else if (!strcmp(arg, "--updateref"))
 759                        cb.updateref = 1;
 760                else if (!strcmp(arg, "--verbose"))
 761                        cb.verbose = 1;
 762                else if (!strcmp(arg, "--")) {
 763                        i++;
 764                        break;
 765                }
 766                else if (arg[0] == '-')
 767                        usage(reflog_delete_usage);
 768                else
 769                        break;
 770        }
 771
 772        if (argc - i < 1)
 773                return error("Nothing to delete?");
 774
 775        for ( ; i < argc; i++) {
 776                const char *spec = strstr(argv[i], "@{");
 777                unsigned char sha1[20];
 778                char *ep, *ref;
 779                int recno;
 780
 781                if (!spec) {
 782                        status |= error("Not a reflog: %s", argv[i]);
 783                        continue;
 784                }
 785
 786                if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) {
 787                        status |= error("no reflog for '%s'", argv[i]);
 788                        continue;
 789                }
 790
 791                recno = strtoul(spec + 2, &ep, 10);
 792                if (*ep == '}') {
 793                        cb.recno = -recno;
 794                        for_each_reflog_ent(ref, count_reflog_ent, &cb);
 795                } else {
 796                        cb.expire_total = approxidate(spec + 2);
 797                        for_each_reflog_ent(ref, count_reflog_ent, &cb);
 798                        cb.expire_total = 0;
 799                }
 800
 801                status |= expire_reflog(ref, sha1, flags, &cb);
 802                free(ref);
 803        }
 804        return status;
 805}
 806
 807/*
 808 * main "reflog"
 809 */
 810
 811static const char reflog_usage[] =
 812"git reflog [ show | expire | delete ]";
 813
 814int cmd_reflog(int argc, const char **argv, const char *prefix)
 815{
 816        if (argc > 1 && !strcmp(argv[1], "-h"))
 817                usage(reflog_usage);
 818
 819        /* With no command, we default to showing it. */
 820        if (argc < 2 || *argv[1] == '-')
 821                return cmd_log_reflog(argc, argv, prefix);
 822
 823        if (!strcmp(argv[1], "show"))
 824                return cmd_log_reflog(argc - 1, argv + 1, prefix);
 825
 826        if (!strcmp(argv[1], "expire"))
 827                return cmd_reflog_expire(argc - 1, argv + 1, prefix);
 828
 829        if (!strcmp(argv[1], "delete"))
 830                return cmd_reflog_delete(argc - 1, argv + 1, prefix);
 831
 832        return cmd_log_reflog(argc, argv, prefix);
 833}