builtin / reflog.con commit builtin/fetch: rewrite to take an object_id argument (0e0b7de)
   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>...";
  16
  17static unsigned long default_reflog_expire;
  18static unsigned long default_reflog_expire_unreachable;
  19
  20struct cmd_reflog_expire_cb {
  21        struct rev_info revs;
  22        int stalefix;
  23        unsigned long expire_total;
  24        unsigned long expire_unreachable;
  25        int recno;
  26};
  27
  28struct expire_reflog_policy_cb {
  29        enum {
  30                UE_NORMAL,
  31                UE_ALWAYS,
  32                UE_HEAD
  33        } unreachable_expire_kind;
  34        struct commit_list *mark_list;
  35        unsigned long mark_limit;
  36        struct cmd_reflog_expire_cb cmd;
  37        struct commit *tip_commit;
  38        struct commit_list *tips;
  39};
  40
  41struct collected_reflog {
  42        unsigned char sha1[20];
  43        char reflog[FLEX_ARRAY];
  44};
  45
  46struct collect_reflog_cb {
  47        struct collected_reflog **e;
  48        int alloc;
  49        int nr;
  50};
  51
  52#define INCOMPLETE      (1u<<10)
  53#define STUDYING        (1u<<11)
  54#define REACHABLE       (1u<<12)
  55
  56static int tree_is_complete(const unsigned char *sha1)
  57{
  58        struct tree_desc desc;
  59        struct name_entry entry;
  60        int complete;
  61        struct tree *tree;
  62
  63        tree = lookup_tree(sha1);
  64        if (!tree)
  65                return 0;
  66        if (tree->object.flags & SEEN)
  67                return 1;
  68        if (tree->object.flags & INCOMPLETE)
  69                return 0;
  70
  71        if (!tree->buffer) {
  72                enum object_type type;
  73                unsigned long size;
  74                void *data = read_sha1_file(sha1, &type, &size);
  75                if (!data) {
  76                        tree->object.flags |= INCOMPLETE;
  77                        return 0;
  78                }
  79                tree->buffer = data;
  80                tree->size = size;
  81        }
  82        init_tree_desc(&desc, tree->buffer, tree->size);
  83        complete = 1;
  84        while (tree_entry(&desc, &entry)) {
  85                if (!has_sha1_file(entry.sha1) ||
  86                    (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
  87                        tree->object.flags |= INCOMPLETE;
  88                        complete = 0;
  89                }
  90        }
  91        free_tree_buffer(tree);
  92
  93        if (complete)
  94                tree->object.flags |= SEEN;
  95        return complete;
  96}
  97
  98static int commit_is_complete(struct commit *commit)
  99{
 100        struct object_array study;
 101        struct object_array found;
 102        int is_incomplete = 0;
 103        int i;
 104
 105        /* early return */
 106        if (commit->object.flags & SEEN)
 107                return 1;
 108        if (commit->object.flags & INCOMPLETE)
 109                return 0;
 110        /*
 111         * Find all commits that are reachable and are not marked as
 112         * SEEN.  Then make sure the trees and blobs contained are
 113         * complete.  After that, mark these commits also as SEEN.
 114         * If some of the objects that are needed to complete this
 115         * commit are missing, mark this commit as INCOMPLETE.
 116         */
 117        memset(&study, 0, sizeof(study));
 118        memset(&found, 0, sizeof(found));
 119        add_object_array(&commit->object, NULL, &study);
 120        add_object_array(&commit->object, NULL, &found);
 121        commit->object.flags |= STUDYING;
 122        while (study.nr) {
 123                struct commit *c;
 124                struct commit_list *parent;
 125
 126                c = (struct commit *)study.objects[--study.nr].item;
 127                if (!c->object.parsed && !parse_object(c->object.sha1))
 128                        c->object.flags |= INCOMPLETE;
 129
 130                if (c->object.flags & INCOMPLETE) {
 131                        is_incomplete = 1;
 132                        break;
 133                }
 134                else if (c->object.flags & SEEN)
 135                        continue;
 136                for (parent = c->parents; parent; parent = parent->next) {
 137                        struct commit *p = parent->item;
 138                        if (p->object.flags & STUDYING)
 139                                continue;
 140                        p->object.flags |= STUDYING;
 141                        add_object_array(&p->object, NULL, &study);
 142                        add_object_array(&p->object, NULL, &found);
 143                }
 144        }
 145        if (!is_incomplete) {
 146                /*
 147                 * make sure all commits in "found" array have all the
 148                 * necessary objects.
 149                 */
 150                for (i = 0; i < found.nr; i++) {
 151                        struct commit *c =
 152                                (struct commit *)found.objects[i].item;
 153                        if (!tree_is_complete(c->tree->object.sha1)) {
 154                                is_incomplete = 1;
 155                                c->object.flags |= INCOMPLETE;
 156                        }
 157                }
 158                if (!is_incomplete) {
 159                        /* mark all found commits as complete, iow SEEN */
 160                        for (i = 0; i < found.nr; i++)
 161                                found.objects[i].item->flags |= SEEN;
 162                }
 163        }
 164        /* clear flags from the objects we traversed */
 165        for (i = 0; i < found.nr; i++)
 166                found.objects[i].item->flags &= ~STUDYING;
 167        if (is_incomplete)
 168                commit->object.flags |= INCOMPLETE;
 169        else {
 170                /*
 171                 * If we come here, we have (1) traversed the ancestry chain
 172                 * from the "commit" until we reach SEEN commits (which are
 173                 * known to be complete), and (2) made sure that the commits
 174                 * encountered during the above traversal refer to trees that
 175                 * are complete.  Which means that we know *all* the commits
 176                 * we have seen during this process are complete.
 177                 */
 178                for (i = 0; i < found.nr; i++)
 179                        found.objects[i].item->flags |= SEEN;
 180        }
 181        /* free object arrays */
 182        free(study.objects);
 183        free(found.objects);
 184        return !is_incomplete;
 185}
 186
 187static int keep_entry(struct commit **it, unsigned char *sha1)
 188{
 189        struct commit *commit;
 190
 191        if (is_null_sha1(sha1))
 192                return 1;
 193        commit = lookup_commit_reference_gently(sha1, 1);
 194        if (!commit)
 195                return 0;
 196
 197        /*
 198         * Make sure everything in this commit exists.
 199         *
 200         * We have walked all the objects reachable from the refs
 201         * and cache earlier.  The commits reachable by this commit
 202         * must meet SEEN commits -- and then we should mark them as
 203         * SEEN as well.
 204         */
 205        if (!commit_is_complete(commit))
 206                return 0;
 207        *it = commit;
 208        return 1;
 209}
 210
 211/*
 212 * Starting from commits in the cb->mark_list, mark commits that are
 213 * reachable from them.  Stop the traversal at commits older than
 214 * the expire_limit and queue them back, so that the caller can call
 215 * us again to restart the traversal with longer expire_limit.
 216 */
 217static void mark_reachable(struct expire_reflog_policy_cb *cb)
 218{
 219        struct commit *commit;
 220        struct commit_list *pending;
 221        unsigned long expire_limit = cb->mark_limit;
 222        struct commit_list *leftover = NULL;
 223
 224        for (pending = cb->mark_list; pending; pending = pending->next)
 225                pending->item->object.flags &= ~REACHABLE;
 226
 227        pending = cb->mark_list;
 228        while (pending) {
 229                struct commit_list *entry = pending;
 230                struct commit_list *parent;
 231                pending = entry->next;
 232                commit = entry->item;
 233                free(entry);
 234                if (commit->object.flags & REACHABLE)
 235                        continue;
 236                if (parse_commit(commit))
 237                        continue;
 238                commit->object.flags |= REACHABLE;
 239                if (commit->date < expire_limit) {
 240                        commit_list_insert(commit, &leftover);
 241                        continue;
 242                }
 243                commit->object.flags |= REACHABLE;
 244                parent = commit->parents;
 245                while (parent) {
 246                        commit = parent->item;
 247                        parent = parent->next;
 248                        if (commit->object.flags & REACHABLE)
 249                                continue;
 250                        commit_list_insert(commit, &pending);
 251                }
 252        }
 253        cb->mark_list = leftover;
 254}
 255
 256static int unreachable(struct expire_reflog_policy_cb *cb, struct commit *commit, unsigned char *sha1)
 257{
 258        /*
 259         * We may or may not have the commit yet - if not, look it
 260         * up using the supplied sha1.
 261         */
 262        if (!commit) {
 263                if (is_null_sha1(sha1))
 264                        return 0;
 265
 266                commit = lookup_commit_reference_gently(sha1, 1);
 267
 268                /* Not a commit -- keep it */
 269                if (!commit)
 270                        return 0;
 271        }
 272
 273        /* Reachable from the current ref?  Don't prune. */
 274        if (commit->object.flags & REACHABLE)
 275                return 0;
 276
 277        if (cb->mark_list && cb->mark_limit) {
 278                cb->mark_limit = 0; /* dig down to the root */
 279                mark_reachable(cb);
 280        }
 281
 282        return !(commit->object.flags & REACHABLE);
 283}
 284
 285/*
 286 * Return true iff the specified reflog entry should be expired.
 287 */
 288static int should_expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 289                                    const char *email, unsigned long timestamp, int tz,
 290                                    const char *message, void *cb_data)
 291{
 292        struct expire_reflog_policy_cb *cb = cb_data;
 293        struct commit *old, *new;
 294
 295        if (timestamp < cb->cmd.expire_total)
 296                return 1;
 297
 298        old = new = NULL;
 299        if (cb->cmd.stalefix &&
 300            (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
 301                return 1;
 302
 303        if (timestamp < cb->cmd.expire_unreachable) {
 304                if (cb->unreachable_expire_kind == UE_ALWAYS)
 305                        return 1;
 306                if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1))
 307                        return 1;
 308        }
 309
 310        if (cb->cmd.recno && --(cb->cmd.recno) == 0)
 311                return 1;
 312
 313        return 0;
 314}
 315
 316static int push_tip_to_list(const char *refname, const unsigned char *sha1,
 317                            int flags, void *cb_data)
 318{
 319        struct commit_list **list = cb_data;
 320        struct commit *tip_commit;
 321        if (flags & REF_ISSYMREF)
 322                return 0;
 323        tip_commit = lookup_commit_reference_gently(sha1, 1);
 324        if (!tip_commit)
 325                return 0;
 326        commit_list_insert(tip_commit, list);
 327        return 0;
 328}
 329
 330static void reflog_expiry_prepare(const char *refname,
 331                                  const unsigned char *sha1,
 332                                  void *cb_data)
 333{
 334        struct expire_reflog_policy_cb *cb = cb_data;
 335
 336        if (!cb->cmd.expire_unreachable || !strcmp(refname, "HEAD")) {
 337                cb->tip_commit = NULL;
 338                cb->unreachable_expire_kind = UE_HEAD;
 339        } else {
 340                cb->tip_commit = lookup_commit_reference_gently(sha1, 1);
 341                if (!cb->tip_commit)
 342                        cb->unreachable_expire_kind = UE_ALWAYS;
 343                else
 344                        cb->unreachable_expire_kind = UE_NORMAL;
 345        }
 346
 347        if (cb->cmd.expire_unreachable <= cb->cmd.expire_total)
 348                cb->unreachable_expire_kind = UE_ALWAYS;
 349
 350        cb->mark_list = NULL;
 351        cb->tips = NULL;
 352        if (cb->unreachable_expire_kind != UE_ALWAYS) {
 353                if (cb->unreachable_expire_kind == UE_HEAD) {
 354                        struct commit_list *elem;
 355                        struct each_ref_fn_sha1_adapter wrapped_push_tip_to_list =
 356                                {push_tip_to_list, &cb->tips};
 357
 358                        for_each_ref(each_ref_fn_adapter, &wrapped_push_tip_to_list);
 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                struct each_ref_fn_sha1_adapter wrapped_collect_reflog =
 595                        {collect_reflog, &collected};
 596
 597                memset(&collected, 0, sizeof(collected));
 598                for_each_reflog(each_ref_fn_adapter, &wrapped_collect_reflog);
 599                for (i = 0; i < collected.nr; i++) {
 600                        struct collected_reflog *e = collected.e[i];
 601                        set_reflog_expiry_param(&cb.cmd, explicit_expiry, e->reflog);
 602                        status |= reflog_expire(e->reflog, e->sha1, flags,
 603                                                reflog_expiry_prepare,
 604                                                should_expire_reflog_ent,
 605                                                reflog_expiry_cleanup,
 606                                                &cb);
 607                        free(e);
 608                }
 609                free(collected.e);
 610        }
 611
 612        for (; i < argc; i++) {
 613                char *ref;
 614                unsigned char sha1[20];
 615                if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) {
 616                        status |= error("%s points nowhere!", argv[i]);
 617                        continue;
 618                }
 619                set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref);
 620                status |= reflog_expire(ref, sha1, flags,
 621                                        reflog_expiry_prepare,
 622                                        should_expire_reflog_ent,
 623                                        reflog_expiry_cleanup,
 624                                        &cb);
 625        }
 626        return status;
 627}
 628
 629static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 630                const char *email, unsigned long timestamp, int tz,
 631                const char *message, void *cb_data)
 632{
 633        struct expire_reflog_policy_cb *cb = cb_data;
 634        if (!cb->cmd.expire_total || timestamp < cb->cmd.expire_total)
 635                cb->cmd.recno++;
 636        return 0;
 637}
 638
 639static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
 640{
 641        struct expire_reflog_policy_cb cb;
 642        int i, status = 0;
 643        unsigned int flags = 0;
 644
 645        memset(&cb, 0, sizeof(cb));
 646
 647        for (i = 1; i < argc; i++) {
 648                const char *arg = argv[i];
 649                if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
 650                        flags |= EXPIRE_REFLOGS_DRY_RUN;
 651                else if (!strcmp(arg, "--rewrite"))
 652                        flags |= EXPIRE_REFLOGS_REWRITE;
 653                else if (!strcmp(arg, "--updateref"))
 654                        flags |= EXPIRE_REFLOGS_UPDATE_REF;
 655                else if (!strcmp(arg, "--verbose"))
 656                        flags |= EXPIRE_REFLOGS_VERBOSE;
 657                else if (!strcmp(arg, "--")) {
 658                        i++;
 659                        break;
 660                }
 661                else if (arg[0] == '-')
 662                        usage(reflog_delete_usage);
 663                else
 664                        break;
 665        }
 666
 667        if (argc - i < 1)
 668                return error("Nothing to delete?");
 669
 670        for ( ; i < argc; i++) {
 671                const char *spec = strstr(argv[i], "@{");
 672                unsigned char sha1[20];
 673                char *ep, *ref;
 674                int recno;
 675
 676                if (!spec) {
 677                        status |= error("Not a reflog: %s", argv[i]);
 678                        continue;
 679                }
 680
 681                if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) {
 682                        status |= error("no reflog for '%s'", argv[i]);
 683                        continue;
 684                }
 685
 686                recno = strtoul(spec + 2, &ep, 10);
 687                if (*ep == '}') {
 688                        cb.cmd.recno = -recno;
 689                        for_each_reflog_ent(ref, count_reflog_ent, &cb);
 690                } else {
 691                        cb.cmd.expire_total = approxidate(spec + 2);
 692                        for_each_reflog_ent(ref, count_reflog_ent, &cb);
 693                        cb.cmd.expire_total = 0;
 694                }
 695
 696                status |= reflog_expire(ref, sha1, flags,
 697                                        reflog_expiry_prepare,
 698                                        should_expire_reflog_ent,
 699                                        reflog_expiry_cleanup,
 700                                        &cb);
 701                free(ref);
 702        }
 703        return status;
 704}
 705
 706/*
 707 * main "reflog"
 708 */
 709
 710static const char reflog_usage[] =
 711"git reflog [ show | expire | delete ]";
 712
 713int cmd_reflog(int argc, const char **argv, const char *prefix)
 714{
 715        if (argc > 1 && !strcmp(argv[1], "-h"))
 716                usage(reflog_usage);
 717
 718        /* With no command, we default to showing it. */
 719        if (argc < 2 || *argv[1] == '-')
 720                return cmd_log_reflog(argc, argv, prefix);
 721
 722        if (!strcmp(argv[1], "show"))
 723                return cmd_log_reflog(argc - 1, argv + 1, prefix);
 724
 725        if (!strcmp(argv[1], "expire"))
 726                return cmd_reflog_expire(argc - 1, argv + 1, prefix);
 727
 728        if (!strcmp(argv[1], "delete"))
 729                return cmd_reflog_delete(argc - 1, argv + 1, prefix);
 730
 731        return cmd_log_reflog(argc, argv, prefix);
 732}