reflog-walk.con commit apply: demonstrate a problem applying svn diffs (f16ef7b)
   1#include "cache.h"
   2#include "commit.h"
   3#include "refs.h"
   4#include "diff.h"
   5#include "revision.h"
   6#include "string-list.h"
   7#include "reflog-walk.h"
   8
   9struct complete_reflogs {
  10        char *ref;
  11        const char *short_ref;
  12        struct reflog_info {
  13                struct object_id ooid, noid;
  14                char *email;
  15                timestamp_t timestamp;
  16                int tz;
  17                char *message;
  18        } *items;
  19        int nr, alloc;
  20};
  21
  22static int read_one_reflog(struct object_id *ooid, struct object_id *noid,
  23                const char *email, timestamp_t timestamp, int tz,
  24                const char *message, void *cb_data)
  25{
  26        struct complete_reflogs *array = cb_data;
  27        struct reflog_info *item;
  28
  29        ALLOC_GROW(array->items, array->nr + 1, array->alloc);
  30        item = array->items + array->nr;
  31        oidcpy(&item->ooid, ooid);
  32        oidcpy(&item->noid, noid);
  33        item->email = xstrdup(email);
  34        item->timestamp = timestamp;
  35        item->tz = tz;
  36        item->message = xstrdup(message);
  37        array->nr++;
  38        return 0;
  39}
  40
  41static void free_complete_reflog(struct complete_reflogs *array)
  42{
  43        int i;
  44
  45        if (!array)
  46                return;
  47
  48        for (i = 0; i < array->nr; i++) {
  49                free(array->items[i].email);
  50                free(array->items[i].message);
  51        }
  52        free(array->items);
  53        free(array->ref);
  54        free(array);
  55}
  56
  57static struct complete_reflogs *read_complete_reflog(const char *ref)
  58{
  59        struct complete_reflogs *reflogs =
  60                xcalloc(1, sizeof(struct complete_reflogs));
  61        reflogs->ref = xstrdup(ref);
  62        for_each_reflog_ent(ref, read_one_reflog, reflogs);
  63        if (reflogs->nr == 0) {
  64                struct object_id oid;
  65                const char *name;
  66                void *name_to_free;
  67                name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
  68                                                     oid.hash, NULL);
  69                if (name) {
  70                        for_each_reflog_ent(name, read_one_reflog, reflogs);
  71                        free(name_to_free);
  72                }
  73        }
  74        if (reflogs->nr == 0) {
  75                char *refname = xstrfmt("refs/%s", ref);
  76                for_each_reflog_ent(refname, read_one_reflog, reflogs);
  77                if (reflogs->nr == 0) {
  78                        free(refname);
  79                        refname = xstrfmt("refs/heads/%s", ref);
  80                        for_each_reflog_ent(refname, read_one_reflog, reflogs);
  81                }
  82                free(refname);
  83        }
  84        return reflogs;
  85}
  86
  87static int get_reflog_recno_by_time(struct complete_reflogs *array,
  88        timestamp_t timestamp)
  89{
  90        int i;
  91        for (i = array->nr - 1; i >= 0; i--)
  92                if (timestamp >= array->items[i].timestamp)
  93                        return i;
  94        return -1;
  95}
  96
  97struct commit_reflog {
  98        int recno;
  99        enum selector_type {
 100                SELECTOR_NONE,
 101                SELECTOR_INDEX,
 102                SELECTOR_DATE
 103        } selector;
 104        struct complete_reflogs *reflogs;
 105};
 106
 107struct reflog_walk_info {
 108        struct commit_reflog **logs;
 109        size_t nr, alloc;
 110        struct string_list complete_reflogs;
 111        struct commit_reflog *last_commit_reflog;
 112};
 113
 114void init_reflog_walk(struct reflog_walk_info **info)
 115{
 116        *info = xcalloc(1, sizeof(struct reflog_walk_info));
 117        (*info)->complete_reflogs.strdup_strings = 1;
 118}
 119
 120int add_reflog_for_walk(struct reflog_walk_info *info,
 121                struct commit *commit, const char *name)
 122{
 123        timestamp_t timestamp = 0;
 124        int recno = -1;
 125        struct string_list_item *item;
 126        struct complete_reflogs *reflogs;
 127        char *branch, *at = strchr(name, '@');
 128        struct commit_reflog *commit_reflog;
 129        enum selector_type selector = SELECTOR_NONE;
 130
 131        if (commit->object.flags & UNINTERESTING)
 132                die ("Cannot walk reflogs for %s", name);
 133
 134        branch = xstrdup(name);
 135        if (at && at[1] == '{') {
 136                char *ep;
 137                branch[at - name] = '\0';
 138                recno = strtoul(at + 2, &ep, 10);
 139                if (*ep != '}') {
 140                        recno = -1;
 141                        timestamp = approxidate(at + 2);
 142                        selector = SELECTOR_DATE;
 143                }
 144                else
 145                        selector = SELECTOR_INDEX;
 146        } else
 147                recno = 0;
 148
 149        item = string_list_lookup(&info->complete_reflogs, branch);
 150        if (item)
 151                reflogs = item->util;
 152        else {
 153                if (*branch == '\0') {
 154                        struct object_id oid;
 155                        free(branch);
 156                        branch = resolve_refdup("HEAD", 0, oid.hash, NULL);
 157                        if (!branch)
 158                                die ("No current branch");
 159
 160                }
 161                reflogs = read_complete_reflog(branch);
 162                if (!reflogs || reflogs->nr == 0) {
 163                        struct object_id oid;
 164                        char *b;
 165                        int ret = dwim_log(branch, strlen(branch),
 166                                           oid.hash, &b);
 167                        if (ret > 1)
 168                                free(b);
 169                        else if (ret == 1) {
 170                                free_complete_reflog(reflogs);
 171                                free(branch);
 172                                branch = b;
 173                                reflogs = read_complete_reflog(branch);
 174                        }
 175                }
 176                if (!reflogs || reflogs->nr == 0) {
 177                        free_complete_reflog(reflogs);
 178                        free(branch);
 179                        return -1;
 180                }
 181                string_list_insert(&info->complete_reflogs, branch)->util
 182                        = reflogs;
 183        }
 184        free(branch);
 185
 186        commit_reflog = xcalloc(1, sizeof(struct commit_reflog));
 187        if (recno < 0) {
 188                commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
 189                if (commit_reflog->recno < 0) {
 190                        free(commit_reflog);
 191                        return -1;
 192                }
 193        } else
 194                commit_reflog->recno = reflogs->nr - recno - 1;
 195        commit_reflog->selector = selector;
 196        commit_reflog->reflogs = reflogs;
 197
 198        ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
 199        info->logs[info->nr++] = commit_reflog;
 200
 201        return 0;
 202}
 203
 204void get_reflog_selector(struct strbuf *sb,
 205                         struct reflog_walk_info *reflog_info,
 206                         const struct date_mode *dmode, int force_date,
 207                         int shorten)
 208{
 209        struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
 210        struct reflog_info *info;
 211        const char *printed_ref;
 212
 213        if (!commit_reflog)
 214                return;
 215
 216        if (shorten) {
 217                if (!commit_reflog->reflogs->short_ref)
 218                        commit_reflog->reflogs->short_ref
 219                                = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
 220                printed_ref = commit_reflog->reflogs->short_ref;
 221        } else {
 222                printed_ref = commit_reflog->reflogs->ref;
 223        }
 224
 225        strbuf_addf(sb, "%s@{", printed_ref);
 226        if (commit_reflog->selector == SELECTOR_DATE ||
 227            (commit_reflog->selector == SELECTOR_NONE && force_date)) {
 228                info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
 229                strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
 230        } else {
 231                strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
 232                            - 2 - commit_reflog->recno);
 233        }
 234
 235        strbuf_addch(sb, '}');
 236}
 237
 238void get_reflog_message(struct strbuf *sb,
 239                        struct reflog_walk_info *reflog_info)
 240{
 241        struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
 242        struct reflog_info *info;
 243        size_t len;
 244
 245        if (!commit_reflog)
 246                return;
 247
 248        info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
 249        len = strlen(info->message);
 250        if (len > 0)
 251                len--; /* strip away trailing newline */
 252        strbuf_add(sb, info->message, len);
 253}
 254
 255const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
 256{
 257        struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
 258        struct reflog_info *info;
 259
 260        if (!commit_reflog)
 261                return NULL;
 262
 263        info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
 264        return info->email;
 265}
 266
 267timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
 268{
 269        struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
 270        struct reflog_info *info;
 271
 272        if (!commit_reflog)
 273                return 0;
 274
 275        info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
 276        return info->timestamp;
 277}
 278
 279void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
 280                         const struct date_mode *dmode, int force_date)
 281{
 282        if (reflog_info && reflog_info->last_commit_reflog) {
 283                struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
 284                struct reflog_info *info;
 285                struct strbuf selector = STRBUF_INIT;
 286
 287                info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
 288                get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
 289                if (oneline) {
 290                        printf("%s: %s", selector.buf, info->message);
 291                }
 292                else {
 293                        printf("Reflog: %s (%s)\nReflog message: %s",
 294                               selector.buf, info->email, info->message);
 295                }
 296
 297                strbuf_release(&selector);
 298        }
 299}
 300
 301int reflog_walk_empty(struct reflog_walk_info *info)
 302{
 303        return !info || !info->nr;
 304}
 305
 306static struct commit *next_reflog_commit(struct commit_reflog *log)
 307{
 308        for (; log->recno >= 0; log->recno--) {
 309                struct reflog_info *entry = &log->reflogs->items[log->recno];
 310                struct object *obj = parse_object(&entry->noid);
 311
 312                if (obj && obj->type == OBJ_COMMIT)
 313                        return (struct commit *)obj;
 314        }
 315        return NULL;
 316}
 317
 318static timestamp_t log_timestamp(struct commit_reflog *log)
 319{
 320        return log->reflogs->items[log->recno].timestamp;
 321}
 322
 323struct commit *next_reflog_entry(struct reflog_walk_info *walk)
 324{
 325        struct commit_reflog *best = NULL;
 326        struct commit *best_commit = NULL;
 327        size_t i;
 328
 329        for (i = 0; i < walk->nr; i++) {
 330                struct commit_reflog *log = walk->logs[i];
 331                struct commit *commit = next_reflog_commit(log);
 332
 333                if (!commit)
 334                        continue;
 335
 336                if (!best || log_timestamp(log) > log_timestamp(best)) {
 337                        best = log;
 338                        best_commit = commit;
 339                }
 340        }
 341
 342        if (best) {
 343                best->recno--;
 344                walk->last_commit_reflog = best;
 345                return best_commit;
 346        }
 347
 348        return NULL;
 349}