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