Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 1 | #include <ctype.h> |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 2 | #include "tag.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 3 | #include "commit.h" |
| 4 | #include "cache.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 5 | |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 6 | struct sort_node |
| 7 | { |
| 8 | /* |
| 9 | * the number of children of the associated commit |
| 10 | * that also occur in the list being sorted. |
| 11 | */ |
| 12 | unsigned int indegree; |
| 13 | |
| 14 | /* |
| 15 | * reference to original list item that we will re-use |
| 16 | * on output. |
| 17 | */ |
| 18 | struct commit_list * list_item; |
| 19 | |
| 20 | }; |
| 21 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 22 | const char *commit_type = "commit"; |
| 23 | |
Linus Torvalds | 9b66ec0 | 2005-06-26 17:50:46 -0700 | [diff] [blame] | 24 | enum cmit_fmt get_commit_format(const char *arg) |
| 25 | { |
| 26 | if (!*arg) |
| 27 | return CMIT_FMT_DEFAULT; |
| 28 | if (!strcmp(arg, "=raw")) |
| 29 | return CMIT_FMT_RAW; |
| 30 | if (!strcmp(arg, "=medium")) |
| 31 | return CMIT_FMT_MEDIUM; |
| 32 | if (!strcmp(arg, "=short")) |
| 33 | return CMIT_FMT_SHORT; |
| 34 | if (!strcmp(arg, "=full")) |
| 35 | return CMIT_FMT_FULL; |
| 36 | die("invalid --pretty format"); |
| 37 | } |
| 38 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 39 | static struct commit *check_commit(struct object *obj, const unsigned char *sha1) |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 40 | { |
| 41 | if (obj->type != commit_type) { |
| 42 | error("Object %s is a %s, not a commit", |
| 43 | sha1_to_hex(sha1), obj->type); |
| 44 | return NULL; |
| 45 | } |
| 46 | return (struct commit *) obj; |
| 47 | } |
| 48 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 49 | struct commit *lookup_commit_reference(const unsigned char *sha1) |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 50 | { |
| 51 | struct object *obj = parse_object(sha1); |
| 52 | |
| 53 | if (!obj) |
| 54 | return NULL; |
Junio C Hamano | 013aab8 | 2005-07-10 23:55:56 -0700 | [diff] [blame] | 55 | while (obj->type == tag_type) |
| 56 | obj = parse_object(((struct tag *)obj)->tagged->sha1); |
| 57 | |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 58 | return check_commit(obj, sha1); |
| 59 | } |
| 60 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 61 | struct commit *lookup_commit(const unsigned char *sha1) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 62 | { |
| 63 | struct object *obj = lookup_object(sha1); |
| 64 | if (!obj) { |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 65 | struct commit *ret = xmalloc(sizeof(struct commit)); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 66 | memset(ret, 0, sizeof(struct commit)); |
| 67 | created_object(sha1, &ret->object); |
Linus Torvalds | d32987b | 2005-04-24 14:17:13 -0700 | [diff] [blame] | 68 | ret->object.type = commit_type; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 69 | return ret; |
| 70 | } |
Nicolas Pitre | d1af002 | 2005-05-20 16:59:17 -0400 | [diff] [blame] | 71 | if (!obj->type) |
| 72 | obj->type = commit_type; |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 73 | return check_commit(obj, sha1); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static unsigned long parse_commit_date(const char *buf) |
| 77 | { |
| 78 | unsigned long date; |
| 79 | |
| 80 | if (memcmp(buf, "author", 6)) |
| 81 | return 0; |
| 82 | while (*buf++ != '\n') |
| 83 | /* nada */; |
| 84 | if (memcmp(buf, "committer", 9)) |
| 85 | return 0; |
| 86 | while (*buf++ != '>') |
| 87 | /* nada */; |
| 88 | date = strtoul(buf, NULL, 10); |
| 89 | if (date == ULONG_MAX) |
| 90 | date = 0; |
| 91 | return date; |
| 92 | } |
| 93 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 94 | int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 95 | { |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 96 | void *bufptr = buffer; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 97 | unsigned char parent[20]; |
Linus Torvalds | 6c88be1 | 2005-06-20 20:26:03 -0700 | [diff] [blame] | 98 | struct commit_list **pptr; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 99 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 100 | if (item->object.parsed) |
| 101 | return 0; |
| 102 | item->object.parsed = 1; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 103 | get_sha1_hex(bufptr + 5, parent); |
| 104 | item->tree = lookup_tree(parent); |
Linus Torvalds | 235ac40 | 2005-04-24 14:31:57 -0700 | [diff] [blame] | 105 | if (item->tree) |
| 106 | add_ref(&item->object, &item->tree->object); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 107 | bufptr += 46; /* "tree " + "hex sha1" + "\n" */ |
Linus Torvalds | 6c88be1 | 2005-06-20 20:26:03 -0700 | [diff] [blame] | 108 | pptr = &item->parents; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 109 | while (!memcmp(bufptr, "parent ", 7) && |
| 110 | !get_sha1_hex(bufptr + 7, parent)) { |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 111 | struct commit *new_parent = lookup_commit(parent); |
Linus Torvalds | 235ac40 | 2005-04-24 14:31:57 -0700 | [diff] [blame] | 112 | if (new_parent) { |
Linus Torvalds | 6c88be1 | 2005-06-20 20:26:03 -0700 | [diff] [blame] | 113 | pptr = &commit_list_insert(new_parent, pptr)->next; |
Linus Torvalds | 235ac40 | 2005-04-24 14:31:57 -0700 | [diff] [blame] | 114 | add_ref(&item->object, &new_parent->object); |
| 115 | } |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 116 | bufptr += 48; |
| 117 | } |
| 118 | item->date = parse_commit_date(bufptr); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 122 | int parse_commit(struct commit *item) |
| 123 | { |
| 124 | char type[20]; |
| 125 | void *buffer; |
| 126 | unsigned long size; |
| 127 | int ret; |
| 128 | |
| 129 | if (item->object.parsed) |
| 130 | return 0; |
| 131 | buffer = read_sha1_file(item->object.sha1, type, &size); |
| 132 | if (!buffer) |
| 133 | return error("Could not read %s", |
| 134 | sha1_to_hex(item->object.sha1)); |
| 135 | if (strcmp(type, commit_type)) { |
| 136 | free(buffer); |
| 137 | return error("Object %s not a commit", |
| 138 | sha1_to_hex(item->object.sha1)); |
| 139 | } |
| 140 | ret = parse_commit_buffer(item, buffer, size); |
Linus Torvalds | 3ff1fbb | 2005-05-25 18:27:14 -0700 | [diff] [blame] | 141 | if (!ret) { |
| 142 | item->buffer = buffer; |
| 143 | return 0; |
| 144 | } |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 145 | free(buffer); |
| 146 | return ret; |
| 147 | } |
| 148 | |
Linus Torvalds | ac5155e | 2005-05-30 18:44:02 -0700 | [diff] [blame] | 149 | struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p) |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 150 | { |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 151 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 152 | new_list->item = item; |
| 153 | new_list->next = *list_p; |
| 154 | *list_p = new_list; |
Linus Torvalds | ac5155e | 2005-05-30 18:44:02 -0700 | [diff] [blame] | 155 | return new_list; |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 158 | void free_commit_list(struct commit_list *list) |
| 159 | { |
| 160 | while (list) { |
| 161 | struct commit_list *temp = list; |
| 162 | list = temp->next; |
| 163 | free(temp); |
| 164 | } |
| 165 | } |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 166 | |
Linus Torvalds | f755494 | 2005-07-06 09:31:17 -0700 | [diff] [blame] | 167 | struct commit_list * insert_by_date(struct commit *item, struct commit_list **list) |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 168 | { |
| 169 | struct commit_list **pp = list; |
| 170 | struct commit_list *p; |
| 171 | while ((p = *pp) != NULL) { |
| 172 | if (p->item->date < item->date) { |
| 173 | break; |
| 174 | } |
| 175 | pp = &p->next; |
| 176 | } |
Linus Torvalds | f755494 | 2005-07-06 09:31:17 -0700 | [diff] [blame] | 177 | return commit_list_insert(item, pp); |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
| 181 | void sort_by_date(struct commit_list **list) |
| 182 | { |
| 183 | struct commit_list *ret = NULL; |
| 184 | while (*list) { |
Linus Torvalds | f755494 | 2005-07-06 09:31:17 -0700 | [diff] [blame] | 185 | insert_by_date((*list)->item, &ret); |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 186 | *list = (*list)->next; |
| 187 | } |
| 188 | *list = ret; |
| 189 | } |
| 190 | |
Daniel Barkalow | 58e28af | 2005-04-23 20:29:22 -0700 | [diff] [blame] | 191 | struct commit *pop_most_recent_commit(struct commit_list **list, |
| 192 | unsigned int mark) |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 193 | { |
| 194 | struct commit *ret = (*list)->item; |
| 195 | struct commit_list *parents = ret->parents; |
| 196 | struct commit_list *old = *list; |
| 197 | |
| 198 | *list = (*list)->next; |
| 199 | free(old); |
| 200 | |
| 201 | while (parents) { |
Linus Torvalds | 4056c09 | 2005-04-23 19:21:28 -0700 | [diff] [blame] | 202 | struct commit *commit = parents->item; |
Daniel Barkalow | 58e28af | 2005-04-23 20:29:22 -0700 | [diff] [blame] | 203 | parse_commit(commit); |
| 204 | if (!(commit->object.flags & mark)) { |
| 205 | commit->object.flags |= mark; |
Linus Torvalds | f755494 | 2005-07-06 09:31:17 -0700 | [diff] [blame] | 206 | insert_by_date(commit, list); |
Linus Torvalds | 4056c09 | 2005-04-23 19:21:28 -0700 | [diff] [blame] | 207 | } |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 208 | parents = parents->next; |
| 209 | } |
| 210 | return ret; |
| 211 | } |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * Generic support for pretty-printing the header |
| 215 | */ |
| 216 | static int get_one_line(const char *msg, unsigned long len) |
| 217 | { |
| 218 | int ret = 0; |
| 219 | |
| 220 | while (len--) { |
| 221 | char c = *msg++; |
| 222 | ret++; |
| 223 | if (c == '\n') |
| 224 | break; |
| 225 | if (!c) |
| 226 | return 0; |
| 227 | } |
| 228 | return ret; |
| 229 | } |
| 230 | |
Linus Torvalds | 9b66ec0 | 2005-06-26 17:50:46 -0700 | [diff] [blame] | 231 | static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line) |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 232 | { |
| 233 | char *date; |
| 234 | unsigned int namelen; |
| 235 | unsigned long time; |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 236 | int tz, ret; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 237 | |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 238 | date = strchr(line, '>'); |
| 239 | if (!date) |
| 240 | return 0; |
| 241 | namelen = ++date - line; |
| 242 | time = strtoul(date, &date, 10); |
| 243 | tz = strtol(date, NULL, 10); |
| 244 | |
Linus Torvalds | 9b66ec0 | 2005-06-26 17:50:46 -0700 | [diff] [blame] | 245 | ret = sprintf(buf, "%s: %.*s\n", what, namelen, line); |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 246 | if (fmt == CMIT_FMT_MEDIUM) |
| 247 | ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz)); |
| 248 | return ret; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 251 | static int is_empty_line(const char *line, int len) |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 252 | { |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 253 | while (len && isspace(line[len-1])) |
| 254 | len--; |
| 255 | return !len; |
| 256 | } |
| 257 | |
Linus Torvalds | 28342a5 | 2005-06-18 13:52:05 -0700 | [diff] [blame] | 258 | static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents) |
| 259 | { |
| 260 | int offset = 0; |
| 261 | switch (parents) { |
| 262 | case 1: |
| 263 | break; |
| 264 | case 2: |
| 265 | /* Go back to the previous line: 40 characters of previous parent, and one '\n' */ |
| 266 | offset = sprintf(buf, "Merge: %.40s\n", line-41); |
| 267 | /* Fallthrough */ |
| 268 | default: |
| 269 | /* Replace the previous '\n' with a space */ |
| 270 | buf[offset-1] = ' '; |
| 271 | offset += sprintf(buf + offset, "%.40s\n", line+7); |
| 272 | } |
| 273 | return offset; |
| 274 | } |
| 275 | |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 276 | unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space) |
| 277 | { |
| 278 | int hdr = 1, body = 0; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 279 | unsigned long offset = 0; |
Linus Torvalds | 28342a5 | 2005-06-18 13:52:05 -0700 | [diff] [blame] | 280 | int parents = 0; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 281 | |
| 282 | for (;;) { |
| 283 | const char *line = msg; |
| 284 | int linelen = get_one_line(msg, len); |
| 285 | |
| 286 | if (!linelen) |
| 287 | break; |
| 288 | |
| 289 | /* |
| 290 | * We want some slop for indentation and a possible |
| 291 | * final "...". Thus the "+ 20". |
| 292 | */ |
| 293 | if (offset + linelen + 20 > space) { |
| 294 | memcpy(buf + offset, " ...\n", 8); |
| 295 | offset += 8; |
| 296 | break; |
| 297 | } |
| 298 | |
| 299 | msg += linelen; |
| 300 | len -= linelen; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 301 | if (hdr) { |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 302 | if (linelen == 1) { |
| 303 | hdr = 0; |
| 304 | buf[offset++] = '\n'; |
| 305 | continue; |
| 306 | } |
| 307 | if (fmt == CMIT_FMT_RAW) { |
| 308 | memcpy(buf + offset, line, linelen); |
| 309 | offset += linelen; |
| 310 | continue; |
| 311 | } |
Linus Torvalds | 28342a5 | 2005-06-18 13:52:05 -0700 | [diff] [blame] | 312 | if (!memcmp(line, "parent ", 7)) { |
| 313 | if (linelen != 48) |
| 314 | die("bad parent line in commit"); |
| 315 | offset += add_parent_info(fmt, buf + offset, line, ++parents); |
| 316 | } |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 317 | if (!memcmp(line, "author ", 7)) |
Linus Torvalds | 9b66ec0 | 2005-06-26 17:50:46 -0700 | [diff] [blame] | 318 | offset += add_user_info("Author", fmt, buf + offset, line + 7); |
| 319 | if (fmt == CMIT_FMT_FULL) { |
| 320 | if (!memcmp(line, "committer ", 10)) |
| 321 | offset += add_user_info("Commit", fmt, buf + offset, line + 10); |
| 322 | } |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 323 | continue; |
| 324 | } |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 325 | |
| 326 | if (is_empty_line(line, linelen)) { |
| 327 | if (!body) |
| 328 | continue; |
| 329 | if (fmt == CMIT_FMT_SHORT) |
| 330 | break; |
| 331 | } else { |
| 332 | body = 1; |
| 333 | } |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 334 | memset(buf + offset, ' ', 4); |
| 335 | memcpy(buf + offset + 4, line, linelen); |
| 336 | offset += linelen + 4; |
| 337 | } |
| 338 | /* Make sure there is an EOLN */ |
| 339 | if (buf[offset - 1] != '\n') |
| 340 | buf[offset++] = '\n'; |
| 341 | buf[offset] = '\0'; |
| 342 | return offset; |
| 343 | } |
jon@blackcubes.dyndns.org | a3437b8 | 2005-06-06 15:39:40 +0000 | [diff] [blame] | 344 | |
| 345 | struct commit *pop_commit(struct commit_list **stack) |
| 346 | { |
| 347 | struct commit_list *top = *stack; |
| 348 | struct commit *item = top ? top->item : NULL; |
| 349 | |
| 350 | if (top) { |
| 351 | *stack = top->next; |
| 352 | free(top); |
| 353 | } |
| 354 | return item; |
| 355 | } |
| 356 | |
| 357 | int count_parents(struct commit * commit) |
| 358 | { |
| 359 | int count = 0; |
| 360 | struct commit_list * parents = commit->parents; |
| 361 | for (count=0;parents; parents=parents->next,count++) |
| 362 | ; |
| 363 | return count; |
| 364 | } |
| 365 | |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 366 | /* |
| 367 | * Performs an in-place topological sort on the list supplied. |
| 368 | */ |
| 369 | void sort_in_topological_order(struct commit_list ** list) |
| 370 | { |
| 371 | struct commit_list * next = *list; |
| 372 | struct commit_list * work = NULL; |
| 373 | struct commit_list ** pptr = list; |
| 374 | struct sort_node * nodes; |
| 375 | struct sort_node * next_nodes; |
| 376 | int count = 0; |
| 377 | |
| 378 | /* determine the size of the list */ |
| 379 | while (next) { |
| 380 | next = next->next; |
| 381 | count++; |
| 382 | } |
| 383 | /* allocate an array to help sort the list */ |
| 384 | nodes = xcalloc(count, sizeof(*nodes)); |
| 385 | /* link the list to the array */ |
| 386 | next_nodes = nodes; |
| 387 | next=*list; |
| 388 | while (next) { |
| 389 | next_nodes->list_item = next; |
| 390 | next->item->object.util = next_nodes; |
| 391 | next_nodes++; |
| 392 | next = next->next; |
| 393 | } |
| 394 | /* update the indegree */ |
| 395 | next=*list; |
| 396 | while (next) { |
| 397 | struct commit_list * parents = next->item->parents; |
| 398 | while (parents) { |
| 399 | struct commit * parent=parents->item; |
| 400 | struct sort_node * pn = (struct sort_node *)parent->object.util; |
| 401 | |
| 402 | if (pn) |
| 403 | pn->indegree++; |
| 404 | parents=parents->next; |
| 405 | } |
| 406 | next=next->next; |
| 407 | } |
| 408 | /* |
| 409 | * find the tips |
| 410 | * |
| 411 | * tips are nodes not reachable from any other node in the list |
| 412 | * |
| 413 | * the tips serve as a starting set for the work queue. |
| 414 | */ |
| 415 | next=*list; |
| 416 | while (next) { |
| 417 | struct sort_node * node = (struct sort_node *)next->item->object.util; |
| 418 | |
| 419 | if (node->indegree == 0) { |
| 420 | commit_list_insert(next->item, &work); |
| 421 | } |
| 422 | next=next->next; |
| 423 | } |
| 424 | /* process the list in topological order */ |
| 425 | while (work) { |
| 426 | struct commit * work_item = pop_commit(&work); |
| 427 | struct sort_node * work_node = (struct sort_node *)work_item->object.util; |
| 428 | struct commit_list * parents = work_item->parents; |
| 429 | |
| 430 | while (parents) { |
| 431 | struct commit * parent=parents->item; |
| 432 | struct sort_node * pn = (struct sort_node *)parent->object.util; |
| 433 | |
| 434 | if (pn) { |
| 435 | /* |
| 436 | * parents are only enqueued for emission |
| 437 | * when all their children have been emitted thereby |
| 438 | * guaranteeing topological order. |
| 439 | */ |
| 440 | pn->indegree--; |
| 441 | if (!pn->indegree) |
| 442 | commit_list_insert(parent, &work); |
| 443 | } |
| 444 | parents=parents->next; |
| 445 | } |
| 446 | /* |
| 447 | * work_item is a commit all of whose children |
| 448 | * have already been emitted. we can emit it now. |
| 449 | */ |
| 450 | *pptr = work_node->list_item; |
| 451 | pptr = &(*pptr)->next; |
| 452 | *pptr = NULL; |
| 453 | work_item->object.util = NULL; |
| 454 | } |
| 455 | free(nodes); |
| 456 | } |