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