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