80bffb0a00a10363358a792f52388477e8ac3354
   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                unsigned char osha1[20], nsha1[20];
  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(unsigned char *osha1, unsigned char *nsha1,
  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        if (array->nr >= array->alloc) {
  30                array->alloc = alloc_nr(array->nr + 1);
  31                array->items = xrealloc(array->items, array->alloc *
  32                        sizeof(struct reflog_info));
  33        }
  34        item = array->items + array->nr;
  35        memcpy(item->osha1, osha1, 20);
  36        memcpy(item->nsha1, nsha1, 20);
  37        item->email = xstrdup(email);
  38        item->timestamp = timestamp;
  39        item->tz = tz;
  40        item->message = xstrdup(message);
  41        array->nr++;
  42        return 0;
  43}
  44
  45static struct complete_reflogs *read_complete_reflog(const char *ref)
  46{
  47        struct complete_reflogs *reflogs =
  48                xcalloc(sizeof(struct complete_reflogs), 1);
  49        reflogs->ref = xstrdup(ref);
  50        for_each_reflog_ent(ref, read_one_reflog, reflogs);
  51        if (reflogs->nr == 0) {
  52                unsigned char sha1[20];
  53                const char *name = resolve_ref(ref, sha1, 1, NULL);
  54                if (name)
  55                        for_each_reflog_ent(name, read_one_reflog, reflogs);
  56        }
  57        if (reflogs->nr == 0) {
  58                int len = strlen(ref);
  59                char *refname = xmalloc(len + 12);
  60                sprintf(refname, "refs/%s", ref);
  61                for_each_reflog_ent(refname, read_one_reflog, reflogs);
  62                if (reflogs->nr == 0) {
  63                        sprintf(refname, "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        if (lifo->nr >= lifo->alloc) {
 114                lifo->alloc = alloc_nr(lifo->nr + 1);
 115                lifo->items = xrealloc(lifo->items,
 116                        lifo->alloc * sizeof(struct commit_info));
 117        }
 118        info = lifo->items + lifo->nr;
 119        info->commit = commit;
 120        info->util = util;
 121        lifo->nr++;
 122}
 123
 124struct commit_reflog {
 125        int recno;
 126        enum selector_type {
 127                SELECTOR_NONE,
 128                SELECTOR_INDEX,
 129                SELECTOR_DATE
 130        } selector;
 131        struct complete_reflogs *reflogs;
 132};
 133
 134struct reflog_walk_info {
 135        struct commit_info_lifo reflogs;
 136        struct string_list complete_reflogs;
 137        struct commit_reflog *last_commit_reflog;
 138};
 139
 140void init_reflog_walk(struct reflog_walk_info** info)
 141{
 142        *info = xcalloc(sizeof(struct reflog_walk_info), 1);
 143}
 144
 145int add_reflog_for_walk(struct reflog_walk_info *info,
 146                struct commit *commit, const char *name)
 147{
 148        unsigned long timestamp = 0;
 149        int recno = -1;
 150        struct string_list_item *item;
 151        struct complete_reflogs *reflogs;
 152        char *branch, *at = strchr(name, '@');
 153        struct commit_reflog *commit_reflog;
 154        enum selector_type selector = SELECTOR_NONE;
 155
 156        if (commit->object.flags & UNINTERESTING)
 157                die ("Cannot walk reflogs for %s", name);
 158
 159        branch = xstrdup(name);
 160        if (at && at[1] == '{') {
 161                char *ep;
 162                branch[at - name] = '\0';
 163                recno = strtoul(at + 2, &ep, 10);
 164                if (*ep != '}') {
 165                        recno = -1;
 166                        timestamp = approxidate(at + 2);
 167                        selector = SELECTOR_DATE;
 168                }
 169                else
 170                        selector = SELECTOR_INDEX;
 171        } else
 172                recno = 0;
 173
 174        item = string_list_lookup(&info->complete_reflogs, branch);
 175        if (item)
 176                reflogs = item->util;
 177        else {
 178                if (*branch == '\0') {
 179                        unsigned char sha1[20];
 180                        const char *head = resolve_ref("HEAD", sha1, 0, NULL);
 181                        if (!head)
 182                                die ("No current branch");
 183                        free(branch);
 184                        branch = xstrdup(head);
 185                }
 186                reflogs = read_complete_reflog(branch);
 187                if (!reflogs || reflogs->nr == 0) {
 188                        unsigned char sha1[20];
 189                        char *b;
 190                        if (dwim_log(branch, strlen(branch), sha1, &b) == 1) {
 191                                if (reflogs) {
 192                                        free(reflogs->ref);
 193                                        free(reflogs);
 194                                }
 195                                free(branch);
 196                                branch = b;
 197                                reflogs = read_complete_reflog(branch);
 198                        }
 199                }
 200                if (!reflogs || reflogs->nr == 0)
 201                        return -1;
 202                string_list_insert(&info->complete_reflogs, branch)->util
 203                        = reflogs;
 204        }
 205
 206        commit_reflog = xcalloc(sizeof(struct commit_reflog), 1);
 207        if (recno < 0) {
 208                commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
 209                if (commit_reflog->recno < 0) {
 210                        free(branch);
 211                        free(commit_reflog);
 212                        return -1;
 213                }
 214        } else
 215                commit_reflog->recno = reflogs->nr - recno - 1;
 216        commit_reflog->selector = selector;
 217        commit_reflog->reflogs = reflogs;
 218
 219        add_commit_info(commit, commit_reflog, &info->reflogs);
 220        return 0;
 221}
 222
 223void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
 224{
 225        struct commit_info *commit_info =
 226                get_commit_info(commit, &info->reflogs, 0);
 227        struct commit_reflog *commit_reflog;
 228        struct reflog_info *reflog;
 229
 230        info->last_commit_reflog = NULL;
 231        if (!commit_info)
 232                return;
 233
 234        commit_reflog = commit_info->util;
 235        if (commit_reflog->recno < 0) {
 236                commit->parents = NULL;
 237                return;
 238        }
 239
 240        reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
 241        info->last_commit_reflog = commit_reflog;
 242        commit_reflog->recno--;
 243        commit_info->commit = (struct commit *)parse_object(reflog->osha1);
 244        if (!commit_info->commit) {
 245                commit->parents = NULL;
 246                return;
 247        }
 248
 249        commit->parents = xcalloc(sizeof(struct commit_list), 1);
 250        commit->parents->item = commit_info->commit;
 251}
 252
 253void get_reflog_selector(struct strbuf *sb,
 254                         struct reflog_walk_info *reflog_info,
 255                         enum date_mode dmode,
 256                         int shorten)
 257{
 258        struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
 259        struct reflog_info *info;
 260        const char *printed_ref;
 261
 262        if (!commit_reflog)
 263                return;
 264
 265        if (shorten) {
 266                if (!commit_reflog->reflogs->short_ref)
 267                        commit_reflog->reflogs->short_ref
 268                                = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0);
 269                printed_ref = commit_reflog->reflogs->short_ref;
 270        } else {
 271                printed_ref = commit_reflog->reflogs->ref;
 272        }
 273
 274        strbuf_addf(sb, "%s@{", printed_ref);
 275        if (commit_reflog->selector == SELECTOR_DATE || dmode) {
 276                info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
 277                strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
 278        } else {
 279                strbuf_addf(sb, "%d", commit_reflog->reflogs->nr
 280                            - 2 - commit_reflog->recno);
 281        }
 282
 283        strbuf_addch(sb, '}');
 284}
 285
 286void get_reflog_message(struct strbuf *sb,
 287                        struct reflog_walk_info *reflog_info)
 288{
 289        struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
 290        struct reflog_info *info;
 291        size_t len;
 292
 293        if (!commit_reflog)
 294                return;
 295
 296        info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
 297        len = strlen(info->message);
 298        if (len > 0)
 299                len--; /* strip away trailing newline */
 300        strbuf_add(sb, info->message, len);
 301}
 302
 303void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
 304        enum date_mode dmode)
 305{
 306        if (reflog_info && reflog_info->last_commit_reflog) {
 307                struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
 308                struct reflog_info *info;
 309                struct strbuf selector = STRBUF_INIT;
 310
 311                info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
 312                get_reflog_selector(&selector, reflog_info, dmode, 0);
 313                if (oneline) {
 314                        printf("%s: %s", selector.buf, info->message);
 315                }
 316                else {
 317                        printf("Reflog: %s (%s)\nReflog message: %s",
 318                               selector.buf, info->email, info->message);
 319                }
 320
 321                strbuf_release(&selector);
 322        }
 323}