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