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