[PATCH] Docs - tag object, git- prefix and s/changeset/commit/g
[git/gitweb.git] / commit.c
blob3f2235ecf87b63bc5d16342ebcf7f8faf098530a
1 #include "tag.h"
2 #include "commit.h"
3 #include "cache.h"
4 #include <string.h>
5 #include <limits.h>
7 const char *commit_type = "commit";
9 static struct commit *check_commit(struct object *obj, unsigned char *sha1)
11 if (obj->type != commit_type) {
12 error("Object %s is a %s, not a commit",
13 sha1_to_hex(sha1), obj->type);
14 return NULL;
16 return (struct commit *) obj;
19 struct commit *lookup_commit_reference(unsigned char *sha1)
21 struct object *obj = parse_object(sha1);
23 if (!obj)
24 return NULL;
25 if (obj->type == tag_type)
26 obj = ((struct tag *)obj)->tagged;
27 return check_commit(obj, sha1);
30 struct commit *lookup_commit(unsigned char *sha1)
32 struct object *obj = lookup_object(sha1);
33 if (!obj) {
34 struct commit *ret = xmalloc(sizeof(struct commit));
35 memset(ret, 0, sizeof(struct commit));
36 created_object(sha1, &ret->object);
37 ret->object.type = commit_type;
38 return ret;
40 if (!obj->type)
41 obj->type = commit_type;
42 return check_commit(obj, sha1);
45 static unsigned long parse_commit_date(const char *buf)
47 unsigned long date;
49 if (memcmp(buf, "author", 6))
50 return 0;
51 while (*buf++ != '\n')
52 /* nada */;
53 if (memcmp(buf, "committer", 9))
54 return 0;
55 while (*buf++ != '>')
56 /* nada */;
57 date = strtoul(buf, NULL, 10);
58 if (date == ULONG_MAX)
59 date = 0;
60 return date;
63 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
65 void *bufptr = buffer;
66 unsigned char parent[20];
68 if (item->object.parsed)
69 return 0;
70 item->object.parsed = 1;
71 get_sha1_hex(bufptr + 5, parent);
72 item->tree = lookup_tree(parent);
73 if (item->tree)
74 add_ref(&item->object, &item->tree->object);
75 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
76 while (!memcmp(bufptr, "parent ", 7) &&
77 !get_sha1_hex(bufptr + 7, parent)) {
78 struct commit *new_parent = lookup_commit(parent);
79 if (new_parent) {
80 commit_list_insert(new_parent, &item->parents);
81 add_ref(&item->object, &new_parent->object);
83 bufptr += 48;
85 item->date = parse_commit_date(bufptr);
86 return 0;
89 int parse_commit(struct commit *item)
91 char type[20];
92 void *buffer;
93 unsigned long size;
94 int ret;
96 if (item->object.parsed)
97 return 0;
98 buffer = read_sha1_file(item->object.sha1, type, &size);
99 if (!buffer)
100 return error("Could not read %s",
101 sha1_to_hex(item->object.sha1));
102 if (strcmp(type, commit_type)) {
103 free(buffer);
104 return error("Object %s not a commit",
105 sha1_to_hex(item->object.sha1));
107 ret = parse_commit_buffer(item, buffer, size);
108 free(buffer);
109 return ret;
112 void commit_list_insert(struct commit *item, struct commit_list **list_p)
114 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
115 new_list->item = item;
116 new_list->next = *list_p;
117 *list_p = new_list;
120 void free_commit_list(struct commit_list *list)
122 while (list) {
123 struct commit_list *temp = list;
124 list = temp->next;
125 free(temp);
129 static void insert_by_date(struct commit_list **list, struct commit *item)
131 struct commit_list **pp = list;
132 struct commit_list *p;
133 while ((p = *pp) != NULL) {
134 if (p->item->date < item->date) {
135 break;
137 pp = &p->next;
139 commit_list_insert(item, pp);
143 void sort_by_date(struct commit_list **list)
145 struct commit_list *ret = NULL;
146 while (*list) {
147 insert_by_date(&ret, (*list)->item);
148 *list = (*list)->next;
150 *list = ret;
153 struct commit *pop_most_recent_commit(struct commit_list **list,
154 unsigned int mark)
156 struct commit *ret = (*list)->item;
157 struct commit_list *parents = ret->parents;
158 struct commit_list *old = *list;
160 *list = (*list)->next;
161 free(old);
163 while (parents) {
164 struct commit *commit = parents->item;
165 parse_commit(commit);
166 if (!(commit->object.flags & mark)) {
167 commit->object.flags |= mark;
168 insert_by_date(list, commit);
170 parents = parents->next;
172 return ret;