]> git.ipfire.org Git - thirdparty/git.git/blame - commit.c
[PATCH] Simplify building of programs
[thirdparty/git.git] / commit.c
CommitLineData
175785e5
DB
1#include "commit.h"
2#include "cache.h"
3#include <string.h>
b2c00718 4#include <limits.h>
175785e5
DB
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 = 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
25static 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
43int 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_list *new_parent =
67 malloc(sizeof(struct commit_list));
68 new_parent->next = item->parents;
69 new_parent->item = lookup_commit(parent);
70 add_ref(&item->object, &new_parent->item->object);
71 item->parents = new_parent;
72 bufptr += 48;
73 }
74 item->date = parse_commit_date(bufptr);
75 free(buffer);
76 return 0;
77}
78
79void free_commit_list(struct commit_list *list)
80{
81 while (list) {
82 struct commit_list *temp = list;
83 list = temp->next;
84 free(temp);
85 }
86}