commit.con commit Add "git" and "git-log-script" helper scripts. (e764b8e)
   1#include "tag.h"
   2#include "commit.h"
   3#include "cache.h"
   4
   5const char *commit_type = "commit";
   6
   7static struct commit *check_commit(struct object *obj, unsigned char *sha1)
   8{
   9        if (obj->type != commit_type) {
  10                error("Object %s is a %s, not a commit", 
  11                      sha1_to_hex(sha1), obj->type);
  12                return NULL;
  13        }
  14        return (struct commit *) obj;
  15}
  16
  17struct commit *lookup_commit_reference(unsigned char *sha1)
  18{
  19        struct object *obj = parse_object(sha1);
  20
  21        if (!obj)
  22                return NULL;
  23        if (obj->type == tag_type)
  24                obj = ((struct tag *)obj)->tagged;
  25        return check_commit(obj, sha1);
  26}
  27
  28struct commit *lookup_commit(unsigned char *sha1)
  29{
  30        struct object *obj = lookup_object(sha1);
  31        if (!obj) {
  32                struct commit *ret = xmalloc(sizeof(struct commit));
  33                memset(ret, 0, sizeof(struct commit));
  34                created_object(sha1, &ret->object);
  35                ret->object.type = commit_type;
  36                return ret;
  37        }
  38        if (!obj->type)
  39                obj->type = commit_type;
  40        return check_commit(obj, sha1);
  41}
  42
  43static unsigned long parse_commit_date(const char *buf)
  44{
  45        unsigned long date;
  46
  47        if (memcmp(buf, "author", 6))
  48                return 0;
  49        while (*buf++ != '\n')
  50                /* nada */;
  51        if (memcmp(buf, "committer", 9))
  52                return 0;
  53        while (*buf++ != '>')
  54                /* nada */;
  55        date = strtoul(buf, NULL, 10);
  56        if (date == ULONG_MAX)
  57                date = 0;
  58        return date;
  59}
  60
  61int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
  62{
  63        void *bufptr = buffer;
  64        unsigned char parent[20];
  65
  66        if (item->object.parsed)
  67                return 0;
  68        item->object.parsed = 1;
  69        get_sha1_hex(bufptr + 5, parent);
  70        item->tree = lookup_tree(parent);
  71        if (item->tree)
  72                add_ref(&item->object, &item->tree->object);
  73        bufptr += 46; /* "tree " + "hex sha1" + "\n" */
  74        while (!memcmp(bufptr, "parent ", 7) &&
  75               !get_sha1_hex(bufptr + 7, parent)) {
  76                struct commit *new_parent = lookup_commit(parent);
  77                if (new_parent) {
  78                        commit_list_insert(new_parent, &item->parents);
  79                        add_ref(&item->object, &new_parent->object);
  80                }
  81                bufptr += 48;
  82        }
  83        item->date = parse_commit_date(bufptr);
  84        return 0;
  85}
  86
  87int parse_commit(struct commit *item)
  88{
  89        char type[20];
  90        void *buffer;
  91        unsigned long size;
  92        int ret;
  93
  94        if (item->object.parsed)
  95                return 0;
  96        buffer = read_sha1_file(item->object.sha1, type, &size);
  97        if (!buffer)
  98                return error("Could not read %s",
  99                             sha1_to_hex(item->object.sha1));
 100        if (strcmp(type, commit_type)) {
 101                free(buffer);
 102                return error("Object %s not a commit",
 103                             sha1_to_hex(item->object.sha1));
 104        }
 105        ret = parse_commit_buffer(item, buffer, size);
 106        if (!ret) {
 107                item->buffer = buffer;
 108                return 0;
 109        }
 110        free(buffer);
 111        return ret;
 112}
 113
 114struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
 115{
 116        struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
 117        new_list->item = item;
 118        new_list->next = *list_p;
 119        *list_p = new_list;
 120        return new_list;
 121}
 122
 123void free_commit_list(struct commit_list *list)
 124{
 125        while (list) {
 126                struct commit_list *temp = list;
 127                list = temp->next;
 128                free(temp);
 129        }
 130}
 131
 132static void insert_by_date(struct commit_list **list, struct commit *item)
 133{
 134        struct commit_list **pp = list;
 135        struct commit_list *p;
 136        while ((p = *pp) != NULL) {
 137                if (p->item->date < item->date) {
 138                        break;
 139                }
 140                pp = &p->next;
 141        }
 142        commit_list_insert(item, pp);
 143}
 144
 145        
 146void sort_by_date(struct commit_list **list)
 147{
 148        struct commit_list *ret = NULL;
 149        while (*list) {
 150                insert_by_date(&ret, (*list)->item);
 151                *list = (*list)->next;
 152        }
 153        *list = ret;
 154}
 155
 156struct commit *pop_most_recent_commit(struct commit_list **list,
 157                                      unsigned int mark)
 158{
 159        struct commit *ret = (*list)->item;
 160        struct commit_list *parents = ret->parents;
 161        struct commit_list *old = *list;
 162
 163        *list = (*list)->next;
 164        free(old);
 165
 166        while (parents) {
 167                struct commit *commit = parents->item;
 168                parse_commit(commit);
 169                if (!(commit->object.flags & mark)) {
 170                        commit->object.flags |= mark;
 171                        insert_by_date(list, commit);
 172                }
 173                parents = parents->next;
 174        }
 175        return ret;
 176}
 177
 178/*
 179 * Generic support for pretty-printing the header
 180 */
 181static int get_one_line(const char *msg, unsigned long len)
 182{
 183        int ret = 0;
 184
 185        while (len--) {
 186                char c = *msg++;
 187                ret++;
 188                if (c == '\n')
 189                        break;
 190                if (!c)
 191                        return 0;
 192        }
 193        return ret;
 194}
 195
 196static int add_author_info(char *buf, const char *line, int len)
 197{
 198        char *date;
 199        unsigned int namelen;
 200        unsigned long time;
 201        int tz;
 202
 203        line += strlen("author ");
 204        date = strchr(line, '>');
 205        if (!date)
 206                return 0;
 207        namelen = ++date - line;
 208        time = strtoul(date, &date, 10);
 209        tz = strtol(date, NULL, 10);
 210
 211        return sprintf(buf, "Author: %.*s\nDate:   %s\n",
 212                namelen, line,
 213                show_date(time, tz));
 214}
 215
 216unsigned long pretty_print_commit(const char *msg, unsigned long len, char *buf, unsigned long space)
 217{
 218        int hdr = 1;
 219        unsigned long offset = 0;
 220
 221        for (;;) {
 222                const char *line = msg;
 223                int linelen = get_one_line(msg, len);
 224
 225                if (!linelen)
 226                        break;
 227
 228                /*
 229                 * We want some slop for indentation and a possible
 230                 * final "...". Thus the "+ 20".
 231                 */
 232                if (offset + linelen + 20 > space) {
 233                        memcpy(buf + offset, "    ...\n", 8);
 234                        offset += 8;
 235                        break;
 236                }
 237
 238                msg += linelen;
 239                len -= linelen;
 240                if (linelen == 1)
 241                        hdr = 0;
 242                if (hdr) {
 243                        if (!memcmp(line, "author ", 7))
 244                                offset += add_author_info(buf + offset, line, linelen);
 245                        continue;
 246                }
 247                memset(buf + offset, ' ', 4);
 248                memcpy(buf + offset + 4, line, linelen);
 249                offset += linelen + 4;
 250        }
 251        /* Make sure there is an EOLN */
 252        if (buf[offset - 1] != '\n')
 253                buf[offset++] = '\n';
 254        buf[offset] = '\0';
 255        return offset;
 256}