Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 1 | #include "tag.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 2 | #include "commit.h" |
| 3 | #include "cache.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 4 | |
| 5 | const char *commit_type = "commit"; |
| 6 | |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 7 | static 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 | |
| 17 | struct 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 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 28 | struct commit *lookup_commit(unsigned char *sha1) |
| 29 | { |
| 30 | struct object *obj = lookup_object(sha1); |
| 31 | if (!obj) { |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 32 | struct commit *ret = xmalloc(sizeof(struct commit)); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 33 | memset(ret, 0, sizeof(struct commit)); |
| 34 | created_object(sha1, &ret->object); |
Linus Torvalds | d32987b | 2005-04-24 14:17:13 -0700 | [diff] [blame] | 35 | ret->object.type = commit_type; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 36 | return ret; |
| 37 | } |
Nicolas Pitre | d1af002 | 2005-05-20 16:59:17 -0400 | [diff] [blame] | 38 | if (!obj->type) |
| 39 | obj->type = commit_type; |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 40 | return check_commit(obj, sha1); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static 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 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 61 | int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 62 | { |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 63 | void *bufptr = buffer; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 64 | unsigned char parent[20]; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 65 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 66 | if (item->object.parsed) |
| 67 | return 0; |
| 68 | item->object.parsed = 1; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 69 | get_sha1_hex(bufptr + 5, parent); |
| 70 | item->tree = lookup_tree(parent); |
Linus Torvalds | 235ac40 | 2005-04-24 14:31:57 -0700 | [diff] [blame] | 71 | if (item->tree) |
| 72 | add_ref(&item->object, &item->tree->object); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 73 | bufptr += 46; /* "tree " + "hex sha1" + "\n" */ |
| 74 | while (!memcmp(bufptr, "parent ", 7) && |
| 75 | !get_sha1_hex(bufptr + 7, parent)) { |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 76 | struct commit *new_parent = lookup_commit(parent); |
Linus Torvalds | 235ac40 | 2005-04-24 14:31:57 -0700 | [diff] [blame] | 77 | if (new_parent) { |
| 78 | commit_list_insert(new_parent, &item->parents); |
| 79 | add_ref(&item->object, &new_parent->object); |
| 80 | } |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 81 | bufptr += 48; |
| 82 | } |
| 83 | item->date = parse_commit_date(bufptr); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 87 | int 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 | free(buffer); |
| 107 | return ret; |
| 108 | } |
| 109 | |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 110 | void commit_list_insert(struct commit *item, struct commit_list **list_p) |
| 111 | { |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 112 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 113 | new_list->item = item; |
| 114 | new_list->next = *list_p; |
| 115 | *list_p = new_list; |
| 116 | } |
| 117 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 118 | void free_commit_list(struct commit_list *list) |
| 119 | { |
| 120 | while (list) { |
| 121 | struct commit_list *temp = list; |
| 122 | list = temp->next; |
| 123 | free(temp); |
| 124 | } |
| 125 | } |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 126 | |
| 127 | static void insert_by_date(struct commit_list **list, struct commit *item) |
| 128 | { |
| 129 | struct commit_list **pp = list; |
| 130 | struct commit_list *p; |
| 131 | while ((p = *pp) != NULL) { |
| 132 | if (p->item->date < item->date) { |
| 133 | break; |
| 134 | } |
| 135 | pp = &p->next; |
| 136 | } |
| 137 | commit_list_insert(item, pp); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | void sort_by_date(struct commit_list **list) |
| 142 | { |
| 143 | struct commit_list *ret = NULL; |
| 144 | while (*list) { |
| 145 | insert_by_date(&ret, (*list)->item); |
| 146 | *list = (*list)->next; |
| 147 | } |
| 148 | *list = ret; |
| 149 | } |
| 150 | |
Daniel Barkalow | 58e28af | 2005-04-23 20:29:22 -0700 | [diff] [blame] | 151 | struct commit *pop_most_recent_commit(struct commit_list **list, |
| 152 | unsigned int mark) |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 153 | { |
| 154 | struct commit *ret = (*list)->item; |
| 155 | struct commit_list *parents = ret->parents; |
| 156 | struct commit_list *old = *list; |
| 157 | |
| 158 | *list = (*list)->next; |
| 159 | free(old); |
| 160 | |
| 161 | while (parents) { |
Linus Torvalds | 4056c09 | 2005-04-23 19:21:28 -0700 | [diff] [blame] | 162 | struct commit *commit = parents->item; |
Daniel Barkalow | 58e28af | 2005-04-23 20:29:22 -0700 | [diff] [blame] | 163 | parse_commit(commit); |
| 164 | if (!(commit->object.flags & mark)) { |
| 165 | commit->object.flags |= mark; |
Linus Torvalds | 4056c09 | 2005-04-23 19:21:28 -0700 | [diff] [blame] | 166 | insert_by_date(list, commit); |
| 167 | } |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 168 | parents = parents->next; |
| 169 | } |
| 170 | return ret; |
| 171 | } |