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