commit.con commit [PATCH] Add git-local-pull. (dfcb405)
   1#include "commit.h"
   2#include "cache.h"
   3#include <string.h>
   4#include <limits.h>
   5
   6const char *commit_type = "commit";
   7
   8struct commit *lookup_commit(unsigned char *sha1)
   9{
  10        struct object *obj = lookup_object(sha1);
  11        if (!obj) {
  12                struct commit *ret = xmalloc(sizeof(struct commit));
  13                memset(ret, 0, sizeof(struct commit));
  14                created_object(sha1, &ret->object);
  15                ret->object.type = commit_type;
  16                return ret;
  17        }
  18        if (obj->type != commit_type) {
  19                error("Object %s is a %s, not a commit", 
  20                      sha1_to_hex(sha1), obj->type);
  21                return NULL;
  22        }
  23        return (struct commit *) obj;
  24}
  25
  26static unsigned long parse_commit_date(const char *buf)
  27{
  28        unsigned long date;
  29
  30        if (memcmp(buf, "author", 6))
  31                return 0;
  32        while (*buf++ != '\n')
  33                /* nada */;
  34        if (memcmp(buf, "committer", 9))
  35                return 0;
  36        while (*buf++ != '>')
  37                /* nada */;
  38        date = strtoul(buf, NULL, 10);
  39        if (date == ULONG_MAX)
  40                date = 0;
  41        return date;
  42}
  43
  44int parse_commit(struct commit *item)
  45{
  46        char type[20];
  47        void * buffer, *bufptr;
  48        unsigned long size;
  49        unsigned char parent[20];
  50        if (item->object.parsed)
  51                return 0;
  52        item->object.parsed = 1;
  53        buffer = bufptr = read_sha1_file(item->object.sha1, type, &size);
  54        if (!buffer)
  55                return error("Could not read %s",
  56                             sha1_to_hex(item->object.sha1));
  57        if (strcmp(type, commit_type))
  58                return error("Object %s not a commit",
  59                             sha1_to_hex(item->object.sha1));
  60        get_sha1_hex(bufptr + 5, parent);
  61        item->tree = lookup_tree(parent);
  62        if (item->tree)
  63                add_ref(&item->object, &item->tree->object);
  64        bufptr += 46; /* "tree " + "hex sha1" + "\n" */
  65        while (!memcmp(bufptr, "parent ", 7) &&
  66               !get_sha1_hex(bufptr + 7, parent)) {
  67                struct commit *new_parent = lookup_commit(parent);
  68                if (new_parent) {
  69                        commit_list_insert(new_parent, &item->parents);
  70                        add_ref(&item->object, &new_parent->object);
  71                }
  72                bufptr += 48;
  73        }
  74        item->date = parse_commit_date(bufptr);
  75        free(buffer);
  76        return 0;
  77}
  78
  79void commit_list_insert(struct commit *item, struct commit_list **list_p)
  80{
  81        struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
  82        new_list->item = item;
  83        new_list->next = *list_p;
  84        *list_p = new_list;
  85}
  86
  87void free_commit_list(struct commit_list *list)
  88{
  89        while (list) {
  90                struct commit_list *temp = list;
  91                list = temp->next;
  92                free(temp);
  93        }
  94}
  95
  96static void insert_by_date(struct commit_list **list, struct commit *item)
  97{
  98        struct commit_list **pp = list;
  99        struct commit_list *p;
 100        while ((p = *pp) != NULL) {
 101                if (p->item->date < item->date) {
 102                        break;
 103                }
 104                pp = &p->next;
 105        }
 106        commit_list_insert(item, pp);
 107}
 108
 109        
 110void sort_by_date(struct commit_list **list)
 111{
 112        struct commit_list *ret = NULL;
 113        while (*list) {
 114                insert_by_date(&ret, (*list)->item);
 115                *list = (*list)->next;
 116        }
 117        *list = ret;
 118}
 119
 120struct commit *pop_most_recent_commit(struct commit_list **list,
 121                                      unsigned int mark)
 122{
 123        struct commit *ret = (*list)->item;
 124        struct commit_list *parents = ret->parents;
 125        struct commit_list *old = *list;
 126
 127        *list = (*list)->next;
 128        free(old);
 129
 130        while (parents) {
 131                struct commit *commit = parents->item;
 132                parse_commit(commit);
 133                if (!(commit->object.flags & mark)) {
 134                        commit->object.flags |= mark;
 135                        insert_by_date(list, commit);
 136                }
 137                parents = parents->next;
 138        }
 139        return ret;
 140}