Linus Torvalds | 000182e | 2005-06-05 16:02:03 | [diff] [blame] | 1 | #include <ctype.h> |
Linus Torvalds | 961784e | 2005-05-18 23:14:22 | [diff] [blame] | 2 | #include "tag.h" |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 3 | #include "commit.h" |
| 4 | #include "cache.h" |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 5 | |
Linus Torvalds | 60ab26d | 2005-09-15 21:43:17 | [diff] [blame] | 6 | int save_commit_buffer = 1; |
| 7 | |
Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 8 | struct sort_node |
| 9 | { |
| 10 | /* |
| 11 | * the number of children of the associated commit |
| 12 | * that also occur in the list being sorted. |
| 13 | */ |
| 14 | unsigned int indegree; |
| 15 | |
| 16 | /* |
| 17 | * reference to original list item that we will re-use |
| 18 | * on output. |
| 19 | */ |
| 20 | struct commit_list * list_item; |
| 21 | |
| 22 | }; |
| 23 | |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 24 | const char *commit_type = "commit"; |
| 25 | |
Linus Torvalds | 9b66ec0 | 2005-06-27 00:50:46 | [diff] [blame] | 26 | enum cmit_fmt get_commit_format(const char *arg) |
| 27 | { |
| 28 | if (!*arg) |
| 29 | return CMIT_FMT_DEFAULT; |
| 30 | if (!strcmp(arg, "=raw")) |
| 31 | return CMIT_FMT_RAW; |
| 32 | if (!strcmp(arg, "=medium")) |
| 33 | return CMIT_FMT_MEDIUM; |
| 34 | if (!strcmp(arg, "=short")) |
| 35 | return CMIT_FMT_SHORT; |
| 36 | if (!strcmp(arg, "=full")) |
| 37 | return CMIT_FMT_FULL; |
Junio C Hamano | d87449c | 2005-08-09 05:15:40 | [diff] [blame] | 38 | if (!strcmp(arg, "=oneline")) |
| 39 | return CMIT_FMT_ONELINE; |
Linus Torvalds | 9b66ec0 | 2005-06-27 00:50:46 | [diff] [blame] | 40 | die("invalid --pretty format"); |
| 41 | } |
| 42 | |
Junio C Hamano | f76412e | 2005-08-21 09:51:10 | [diff] [blame] | 43 | static struct commit *check_commit(struct object *obj, |
| 44 | const unsigned char *sha1, |
| 45 | int quiet) |
Linus Torvalds | 961784e | 2005-05-18 23:14:22 | [diff] [blame] | 46 | { |
| 47 | if (obj->type != commit_type) { |
Junio C Hamano | f76412e | 2005-08-21 09:51:10 | [diff] [blame] | 48 | if (!quiet) |
| 49 | error("Object %s is a %s, not a commit", |
| 50 | sha1_to_hex(sha1), obj->type); |
Linus Torvalds | 961784e | 2005-05-18 23:14:22 | [diff] [blame] | 51 | return NULL; |
| 52 | } |
| 53 | return (struct commit *) obj; |
| 54 | } |
| 55 | |
Junio C Hamano | f76412e | 2005-08-21 09:51:10 | [diff] [blame] | 56 | struct commit *lookup_commit_reference_gently(const unsigned char *sha1, |
| 57 | int quiet) |
Linus Torvalds | 961784e | 2005-05-18 23:14:22 | [diff] [blame] | 58 | { |
Junio C Hamano | 37fde87 | 2005-08-05 07:47:56 | [diff] [blame] | 59 | struct object *obj = deref_tag(parse_object(sha1)); |
Linus Torvalds | 961784e | 2005-05-18 23:14:22 | [diff] [blame] | 60 | |
| 61 | if (!obj) |
| 62 | return NULL; |
Junio C Hamano | f76412e | 2005-08-21 09:51:10 | [diff] [blame] | 63 | return check_commit(obj, sha1, quiet); |
| 64 | } |
| 65 | |
| 66 | struct commit *lookup_commit_reference(const unsigned char *sha1) |
| 67 | { |
| 68 | return lookup_commit_reference_gently(sha1, 0); |
Linus Torvalds | 961784e | 2005-05-18 23:14:22 | [diff] [blame] | 69 | } |
| 70 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 15:05:39 | [diff] [blame] | 71 | struct commit *lookup_commit(const unsigned char *sha1) |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 72 | { |
| 73 | struct object *obj = lookup_object(sha1); |
| 74 | if (!obj) { |
Christopher Li | 812666c | 2005-04-26 19:00:58 | [diff] [blame] | 75 | struct commit *ret = xmalloc(sizeof(struct commit)); |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 76 | memset(ret, 0, sizeof(struct commit)); |
| 77 | created_object(sha1, &ret->object); |
Linus Torvalds | d32987b | 2005-04-24 21:17:13 | [diff] [blame] | 78 | ret->object.type = commit_type; |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 79 | return ret; |
| 80 | } |
Nicolas Pitre | d1af002 | 2005-05-20 20:59:17 | [diff] [blame] | 81 | if (!obj->type) |
| 82 | obj->type = commit_type; |
Junio C Hamano | f76412e | 2005-08-21 09:51:10 | [diff] [blame] | 83 | return check_commit(obj, sha1, 0); |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static unsigned long parse_commit_date(const char *buf) |
| 87 | { |
| 88 | unsigned long date; |
| 89 | |
| 90 | if (memcmp(buf, "author", 6)) |
| 91 | return 0; |
| 92 | while (*buf++ != '\n') |
| 93 | /* nada */; |
| 94 | if (memcmp(buf, "committer", 9)) |
| 95 | return 0; |
| 96 | while (*buf++ != '>') |
| 97 | /* nada */; |
| 98 | date = strtoul(buf, NULL, 10); |
| 99 | if (date == ULONG_MAX) |
| 100 | date = 0; |
| 101 | return date; |
| 102 | } |
| 103 | |
Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 104 | static struct commit_graft { |
| 105 | unsigned char sha1[20]; |
| 106 | int nr_parent; |
Junio C Hamano | 2c04662 | 2005-08-29 19:41:03 | [diff] [blame] | 107 | unsigned char parent[0][20]; /* more */ |
Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 108 | } **commit_graft; |
| 109 | static int commit_graft_alloc, commit_graft_nr; |
| 110 | |
| 111 | static int commit_graft_pos(const unsigned char *sha1) |
| 112 | { |
| 113 | int lo, hi; |
| 114 | lo = 0; |
| 115 | hi = commit_graft_nr; |
| 116 | while (lo < hi) { |
| 117 | int mi = (lo + hi) / 2; |
| 118 | struct commit_graft *graft = commit_graft[mi]; |
| 119 | int cmp = memcmp(sha1, graft->sha1, 20); |
| 120 | if (!cmp) |
| 121 | return mi; |
| 122 | if (cmp < 0) |
| 123 | hi = mi; |
| 124 | else |
| 125 | lo = mi + 1; |
| 126 | } |
| 127 | return -lo - 1; |
| 128 | } |
| 129 | |
| 130 | static void prepare_commit_graft(void) |
| 131 | { |
| 132 | char *graft_file = get_graft_file(); |
| 133 | FILE *fp = fopen(graft_file, "r"); |
| 134 | char buf[1024]; |
| 135 | if (!fp) { |
| 136 | commit_graft = (struct commit_graft **) "hack"; |
| 137 | return; |
| 138 | } |
| 139 | while (fgets(buf, sizeof(buf), fp)) { |
| 140 | /* The format is just "Commit Parent1 Parent2 ...\n" */ |
| 141 | int len = strlen(buf); |
| 142 | int i; |
| 143 | struct commit_graft *graft = NULL; |
| 144 | |
| 145 | if (buf[len-1] == '\n') |
| 146 | buf[--len] = 0; |
| 147 | if (buf[0] == '#') |
| 148 | continue; |
| 149 | if ((len + 1) % 41) { |
| 150 | bad_graft_data: |
| 151 | error("bad graft data: %s", buf); |
| 152 | free(graft); |
| 153 | continue; |
| 154 | } |
| 155 | i = (len + 1) / 41 - 1; |
| 156 | graft = xmalloc(sizeof(*graft) + 20 * i); |
| 157 | graft->nr_parent = i; |
| 158 | if (get_sha1_hex(buf, graft->sha1)) |
| 159 | goto bad_graft_data; |
| 160 | for (i = 40; i < len; i += 41) { |
| 161 | if (buf[i] != ' ') |
| 162 | goto bad_graft_data; |
| 163 | if (get_sha1_hex(buf + i + 1, graft->parent[i/41])) |
| 164 | goto bad_graft_data; |
| 165 | } |
| 166 | i = commit_graft_pos(graft->sha1); |
| 167 | if (0 <= i) { |
| 168 | error("duplicate graft data: %s", buf); |
| 169 | free(graft); |
| 170 | continue; |
| 171 | } |
| 172 | i = -i - 1; |
| 173 | if (commit_graft_alloc <= ++commit_graft_nr) { |
| 174 | commit_graft_alloc = alloc_nr(commit_graft_alloc); |
| 175 | commit_graft = xrealloc(commit_graft, |
| 176 | sizeof(*commit_graft) * |
| 177 | commit_graft_alloc); |
| 178 | } |
| 179 | if (i < commit_graft_nr) |
| 180 | memmove(commit_graft + i + 1, |
| 181 | commit_graft + i, |
| 182 | (commit_graft_nr - i - 1) * |
| 183 | sizeof(*commit_graft)); |
| 184 | commit_graft[i] = graft; |
| 185 | } |
| 186 | fclose(fp); |
| 187 | } |
| 188 | |
| 189 | static struct commit_graft *lookup_commit_graft(const unsigned char *sha1) |
| 190 | { |
| 191 | int pos; |
| 192 | if (!commit_graft) |
| 193 | prepare_commit_graft(); |
| 194 | pos = commit_graft_pos(sha1); |
| 195 | if (pos < 0) |
| 196 | return NULL; |
| 197 | return commit_graft[pos]; |
| 198 | } |
| 199 | |
Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 200 | int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size) |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 201 | { |
Linus Torvalds | f7cc77d | 2005-07-27 22:12:48 | [diff] [blame] | 202 | char *bufptr = buffer; |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 203 | unsigned char parent[20]; |
Linus Torvalds | 6c88be1 | 2005-06-21 03:26:03 | [diff] [blame] | 204 | struct commit_list **pptr; |
Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 205 | struct commit_graft *graft; |
Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 206 | |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 207 | if (item->object.parsed) |
| 208 | return 0; |
| 209 | item->object.parsed = 1; |
Linus Torvalds | f7cc77d | 2005-07-27 22:12:48 | [diff] [blame] | 210 | if (memcmp(bufptr, "tree ", 5)) |
| 211 | return error("bogus commit object %s", sha1_to_hex(item->object.sha1)); |
| 212 | if (get_sha1_hex(bufptr + 5, parent) < 0) |
| 213 | return error("bad tree pointer in commit %s\n", sha1_to_hex(item->object.sha1)); |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 214 | item->tree = lookup_tree(parent); |
Linus Torvalds | 235ac40 | 2005-04-24 21:31:57 | [diff] [blame] | 215 | if (item->tree) |
| 216 | add_ref(&item->object, &item->tree->object); |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 217 | bufptr += 46; /* "tree " + "hex sha1" + "\n" */ |
Linus Torvalds | 6c88be1 | 2005-06-21 03:26:03 | [diff] [blame] | 218 | pptr = &item->parents; |
Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 219 | |
| 220 | graft = lookup_commit_graft(item->object.sha1); |
Linus Torvalds | f7cc77d | 2005-07-27 22:12:48 | [diff] [blame] | 221 | while (!memcmp(bufptr, "parent ", 7)) { |
| 222 | struct commit *new_parent; |
| 223 | |
| 224 | if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n') |
| 225 | return error("bad parents in commit %s", sha1_to_hex(item->object.sha1)); |
Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 226 | bufptr += 48; |
| 227 | if (graft) |
| 228 | continue; |
Linus Torvalds | f7cc77d | 2005-07-27 22:12:48 | [diff] [blame] | 229 | new_parent = lookup_commit(parent); |
Linus Torvalds | 235ac40 | 2005-04-24 21:31:57 | [diff] [blame] | 230 | if (new_parent) { |
Linus Torvalds | 6c88be1 | 2005-06-21 03:26:03 | [diff] [blame] | 231 | pptr = &commit_list_insert(new_parent, pptr)->next; |
Linus Torvalds | 235ac40 | 2005-04-24 21:31:57 | [diff] [blame] | 232 | add_ref(&item->object, &new_parent->object); |
| 233 | } |
Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 234 | } |
| 235 | if (graft) { |
| 236 | int i; |
| 237 | struct commit *new_parent; |
| 238 | for (i = 0; i < graft->nr_parent; i++) { |
| 239 | new_parent = lookup_commit(graft->parent[i]); |
| 240 | if (!new_parent) |
| 241 | continue; |
| 242 | pptr = &commit_list_insert(new_parent, pptr)->next; |
| 243 | add_ref(&item->object, &new_parent->object); |
| 244 | } |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 245 | } |
| 246 | item->date = parse_commit_date(bufptr); |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 250 | int parse_commit(struct commit *item) |
| 251 | { |
| 252 | char type[20]; |
| 253 | void *buffer; |
| 254 | unsigned long size; |
| 255 | int ret; |
| 256 | |
| 257 | if (item->object.parsed) |
| 258 | return 0; |
| 259 | buffer = read_sha1_file(item->object.sha1, type, &size); |
| 260 | if (!buffer) |
| 261 | return error("Could not read %s", |
| 262 | sha1_to_hex(item->object.sha1)); |
| 263 | if (strcmp(type, commit_type)) { |
| 264 | free(buffer); |
| 265 | return error("Object %s not a commit", |
| 266 | sha1_to_hex(item->object.sha1)); |
| 267 | } |
| 268 | ret = parse_commit_buffer(item, buffer, size); |
Linus Torvalds | 60ab26d | 2005-09-15 21:43:17 | [diff] [blame] | 269 | if (save_commit_buffer && !ret) { |
Linus Torvalds | 3ff1fbb | 2005-05-26 01:27:14 | [diff] [blame] | 270 | item->buffer = buffer; |
| 271 | return 0; |
| 272 | } |
Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 273 | free(buffer); |
| 274 | return ret; |
| 275 | } |
| 276 | |
Linus Torvalds | ac5155e | 2005-05-31 01:44:02 | [diff] [blame] | 277 | struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p) |
Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 278 | { |
Christopher Li | 812666c | 2005-04-26 19:00:58 | [diff] [blame] | 279 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 280 | new_list->item = item; |
| 281 | new_list->next = *list_p; |
| 282 | *list_p = new_list; |
Linus Torvalds | ac5155e | 2005-05-31 01:44:02 | [diff] [blame] | 283 | return new_list; |
Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 284 | } |
| 285 | |
Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 286 | void free_commit_list(struct commit_list *list) |
| 287 | { |
| 288 | while (list) { |
| 289 | struct commit_list *temp = list; |
| 290 | list = temp->next; |
| 291 | free(temp); |
| 292 | } |
| 293 | } |
Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 294 | |
Linus Torvalds | f755494 | 2005-07-06 16:31:17 | [diff] [blame] | 295 | struct commit_list * insert_by_date(struct commit *item, struct commit_list **list) |
Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 296 | { |
| 297 | struct commit_list **pp = list; |
| 298 | struct commit_list *p; |
| 299 | while ((p = *pp) != NULL) { |
| 300 | if (p->item->date < item->date) { |
| 301 | break; |
| 302 | } |
| 303 | pp = &p->next; |
| 304 | } |
Linus Torvalds | f755494 | 2005-07-06 16:31:17 | [diff] [blame] | 305 | return commit_list_insert(item, pp); |
Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
| 309 | void sort_by_date(struct commit_list **list) |
| 310 | { |
| 311 | struct commit_list *ret = NULL; |
| 312 | while (*list) { |
Linus Torvalds | f755494 | 2005-07-06 16:31:17 | [diff] [blame] | 313 | insert_by_date((*list)->item, &ret); |
Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 314 | *list = (*list)->next; |
| 315 | } |
| 316 | *list = ret; |
| 317 | } |
| 318 | |
Daniel Barkalow | 58e28af | 2005-04-24 03:29:22 | [diff] [blame] | 319 | struct commit *pop_most_recent_commit(struct commit_list **list, |
| 320 | unsigned int mark) |
Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 321 | { |
| 322 | struct commit *ret = (*list)->item; |
| 323 | struct commit_list *parents = ret->parents; |
| 324 | struct commit_list *old = *list; |
| 325 | |
| 326 | *list = (*list)->next; |
| 327 | free(old); |
| 328 | |
| 329 | while (parents) { |
Linus Torvalds | 4056c09 | 2005-04-24 02:21:28 | [diff] [blame] | 330 | struct commit *commit = parents->item; |
Daniel Barkalow | 58e28af | 2005-04-24 03:29:22 | [diff] [blame] | 331 | parse_commit(commit); |
| 332 | if (!(commit->object.flags & mark)) { |
| 333 | commit->object.flags |= mark; |
Linus Torvalds | f755494 | 2005-07-06 16:31:17 | [diff] [blame] | 334 | insert_by_date(commit, list); |
Linus Torvalds | 4056c09 | 2005-04-24 02:21:28 | [diff] [blame] | 335 | } |
Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 336 | parents = parents->next; |
| 337 | } |
| 338 | return ret; |
| 339 | } |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 340 | |
| 341 | /* |
| 342 | * Generic support for pretty-printing the header |
| 343 | */ |
| 344 | static int get_one_line(const char *msg, unsigned long len) |
| 345 | { |
| 346 | int ret = 0; |
| 347 | |
| 348 | while (len--) { |
| 349 | char c = *msg++; |
| 350 | ret++; |
| 351 | if (c == '\n') |
| 352 | break; |
| 353 | if (!c) |
| 354 | return 0; |
| 355 | } |
| 356 | return ret; |
| 357 | } |
| 358 | |
Linus Torvalds | 9b66ec0 | 2005-06-27 00:50:46 | [diff] [blame] | 359 | static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line) |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 360 | { |
| 361 | char *date; |
Junio C Hamano | 5de36bf | 2005-08-30 04:17:21 | [diff] [blame] | 362 | int namelen; |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 363 | unsigned long time; |
Linus Torvalds | 000182e | 2005-06-05 16:02:03 | [diff] [blame] | 364 | int tz, ret; |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 365 | |
Junio C Hamano | d87449c | 2005-08-09 05:15:40 | [diff] [blame] | 366 | if (fmt == CMIT_FMT_ONELINE) |
| 367 | return 0; |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 368 | date = strchr(line, '>'); |
| 369 | if (!date) |
| 370 | return 0; |
| 371 | namelen = ++date - line; |
| 372 | time = strtoul(date, &date, 10); |
| 373 | tz = strtol(date, NULL, 10); |
| 374 | |
Linus Torvalds | 9b66ec0 | 2005-06-27 00:50:46 | [diff] [blame] | 375 | ret = sprintf(buf, "%s: %.*s\n", what, namelen, line); |
Linus Torvalds | 000182e | 2005-06-05 16:02:03 | [diff] [blame] | 376 | if (fmt == CMIT_FMT_MEDIUM) |
| 377 | ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz)); |
| 378 | return ret; |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 379 | } |
| 380 | |
Linus Torvalds | 000182e | 2005-06-05 16:02:03 | [diff] [blame] | 381 | static int is_empty_line(const char *line, int len) |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 382 | { |
Linus Torvalds | 000182e | 2005-06-05 16:02:03 | [diff] [blame] | 383 | while (len && isspace(line[len-1])) |
| 384 | len--; |
| 385 | return !len; |
| 386 | } |
| 387 | |
Linus Torvalds | 28342a5 | 2005-06-18 20:52:05 | [diff] [blame] | 388 | static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents) |
| 389 | { |
| 390 | int offset = 0; |
Junio C Hamano | d87449c | 2005-08-09 05:15:40 | [diff] [blame] | 391 | |
| 392 | if (fmt == CMIT_FMT_ONELINE) |
| 393 | return offset; |
Linus Torvalds | 28342a5 | 2005-06-18 20:52:05 | [diff] [blame] | 394 | switch (parents) { |
| 395 | case 1: |
| 396 | break; |
| 397 | case 2: |
| 398 | /* Go back to the previous line: 40 characters of previous parent, and one '\n' */ |
| 399 | offset = sprintf(buf, "Merge: %.40s\n", line-41); |
| 400 | /* Fallthrough */ |
| 401 | default: |
| 402 | /* Replace the previous '\n' with a space */ |
| 403 | buf[offset-1] = ' '; |
| 404 | offset += sprintf(buf + offset, "%.40s\n", line+7); |
| 405 | } |
| 406 | return offset; |
| 407 | } |
| 408 | |
Linus Torvalds | 000182e | 2005-06-05 16:02:03 | [diff] [blame] | 409 | unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space) |
| 410 | { |
| 411 | int hdr = 1, body = 0; |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 412 | unsigned long offset = 0; |
Linus Torvalds | 28342a5 | 2005-06-18 20:52:05 | [diff] [blame] | 413 | int parents = 0; |
Junio C Hamano | d87449c | 2005-08-09 05:15:40 | [diff] [blame] | 414 | int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4; |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 415 | |
| 416 | for (;;) { |
| 417 | const char *line = msg; |
| 418 | int linelen = get_one_line(msg, len); |
| 419 | |
| 420 | if (!linelen) |
| 421 | break; |
| 422 | |
| 423 | /* |
| 424 | * We want some slop for indentation and a possible |
| 425 | * final "...". Thus the "+ 20". |
| 426 | */ |
| 427 | if (offset + linelen + 20 > space) { |
| 428 | memcpy(buf + offset, " ...\n", 8); |
| 429 | offset += 8; |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | msg += linelen; |
| 434 | len -= linelen; |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 435 | if (hdr) { |
Linus Torvalds | 000182e | 2005-06-05 16:02:03 | [diff] [blame] | 436 | if (linelen == 1) { |
| 437 | hdr = 0; |
Junio C Hamano | d87449c | 2005-08-09 05:15:40 | [diff] [blame] | 438 | if (fmt != CMIT_FMT_ONELINE) |
| 439 | buf[offset++] = '\n'; |
Linus Torvalds | 000182e | 2005-06-05 16:02:03 | [diff] [blame] | 440 | continue; |
| 441 | } |
| 442 | if (fmt == CMIT_FMT_RAW) { |
| 443 | memcpy(buf + offset, line, linelen); |
| 444 | offset += linelen; |
| 445 | continue; |
| 446 | } |
Linus Torvalds | 28342a5 | 2005-06-18 20:52:05 | [diff] [blame] | 447 | if (!memcmp(line, "parent ", 7)) { |
| 448 | if (linelen != 48) |
| 449 | die("bad parent line in commit"); |
| 450 | offset += add_parent_info(fmt, buf + offset, line, ++parents); |
| 451 | } |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 452 | if (!memcmp(line, "author ", 7)) |
Linus Torvalds | 9b66ec0 | 2005-06-27 00:50:46 | [diff] [blame] | 453 | offset += add_user_info("Author", fmt, buf + offset, line + 7); |
| 454 | if (fmt == CMIT_FMT_FULL) { |
| 455 | if (!memcmp(line, "committer ", 10)) |
| 456 | offset += add_user_info("Commit", fmt, buf + offset, line + 10); |
| 457 | } |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 458 | continue; |
| 459 | } |
Linus Torvalds | 000182e | 2005-06-05 16:02:03 | [diff] [blame] | 460 | |
| 461 | if (is_empty_line(line, linelen)) { |
| 462 | if (!body) |
| 463 | continue; |
| 464 | if (fmt == CMIT_FMT_SHORT) |
| 465 | break; |
| 466 | } else { |
| 467 | body = 1; |
| 468 | } |
Junio C Hamano | d87449c | 2005-08-09 05:15:40 | [diff] [blame] | 469 | |
| 470 | memset(buf + offset, ' ', indent); |
| 471 | memcpy(buf + offset + indent, line, linelen); |
| 472 | offset += linelen + indent; |
| 473 | if (fmt == CMIT_FMT_ONELINE) |
| 474 | break; |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 475 | } |
Junio C Hamano | d87449c | 2005-08-09 05:15:40 | [diff] [blame] | 476 | if (fmt == CMIT_FMT_ONELINE) { |
| 477 | /* We do not want the terminating newline */ |
| 478 | if (buf[offset - 1] == '\n') |
| 479 | offset--; |
| 480 | } |
| 481 | else { |
| 482 | /* Make sure there is an EOLN */ |
| 483 | if (buf[offset - 1] != '\n') |
| 484 | buf[offset++] = '\n'; |
| 485 | } |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 486 | buf[offset] = '\0'; |
| 487 | return offset; |
| 488 | } |
jon@blackcubes.dyndns.org | a3437b8 | 2005-06-06 15:39:40 | [diff] [blame] | 489 | |
| 490 | struct commit *pop_commit(struct commit_list **stack) |
| 491 | { |
| 492 | struct commit_list *top = *stack; |
| 493 | struct commit *item = top ? top->item : NULL; |
| 494 | |
| 495 | if (top) { |
| 496 | *stack = top->next; |
| 497 | free(top); |
| 498 | } |
| 499 | return item; |
| 500 | } |
| 501 | |
| 502 | int count_parents(struct commit * commit) |
| 503 | { |
| 504 | int count = 0; |
| 505 | struct commit_list * parents = commit->parents; |
| 506 | for (count=0;parents; parents=parents->next,count++) |
| 507 | ; |
| 508 | return count; |
| 509 | } |
| 510 | |
Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 511 | /* |
| 512 | * Performs an in-place topological sort on the list supplied. |
| 513 | */ |
| 514 | void sort_in_topological_order(struct commit_list ** list) |
| 515 | { |
| 516 | struct commit_list * next = *list; |
| 517 | struct commit_list * work = NULL; |
| 518 | struct commit_list ** pptr = list; |
| 519 | struct sort_node * nodes; |
| 520 | struct sort_node * next_nodes; |
| 521 | int count = 0; |
| 522 | |
| 523 | /* determine the size of the list */ |
| 524 | while (next) { |
| 525 | next = next->next; |
| 526 | count++; |
| 527 | } |
| 528 | /* allocate an array to help sort the list */ |
| 529 | nodes = xcalloc(count, sizeof(*nodes)); |
| 530 | /* link the list to the array */ |
| 531 | next_nodes = nodes; |
| 532 | next=*list; |
| 533 | while (next) { |
| 534 | next_nodes->list_item = next; |
| 535 | next->item->object.util = next_nodes; |
| 536 | next_nodes++; |
| 537 | next = next->next; |
| 538 | } |
| 539 | /* update the indegree */ |
| 540 | next=*list; |
| 541 | while (next) { |
| 542 | struct commit_list * parents = next->item->parents; |
| 543 | while (parents) { |
| 544 | struct commit * parent=parents->item; |
| 545 | struct sort_node * pn = (struct sort_node *)parent->object.util; |
| 546 | |
| 547 | if (pn) |
| 548 | pn->indegree++; |
| 549 | parents=parents->next; |
| 550 | } |
| 551 | next=next->next; |
| 552 | } |
| 553 | /* |
| 554 | * find the tips |
| 555 | * |
| 556 | * tips are nodes not reachable from any other node in the list |
| 557 | * |
| 558 | * the tips serve as a starting set for the work queue. |
| 559 | */ |
| 560 | next=*list; |
| 561 | while (next) { |
| 562 | struct sort_node * node = (struct sort_node *)next->item->object.util; |
| 563 | |
| 564 | if (node->indegree == 0) { |
| 565 | commit_list_insert(next->item, &work); |
| 566 | } |
| 567 | next=next->next; |
| 568 | } |
| 569 | /* process the list in topological order */ |
| 570 | while (work) { |
| 571 | struct commit * work_item = pop_commit(&work); |
| 572 | struct sort_node * work_node = (struct sort_node *)work_item->object.util; |
| 573 | struct commit_list * parents = work_item->parents; |
| 574 | |
| 575 | while (parents) { |
| 576 | struct commit * parent=parents->item; |
| 577 | struct sort_node * pn = (struct sort_node *)parent->object.util; |
| 578 | |
| 579 | if (pn) { |
| 580 | /* |
| 581 | * parents are only enqueued for emission |
| 582 | * when all their children have been emitted thereby |
| 583 | * guaranteeing topological order. |
| 584 | */ |
| 585 | pn->indegree--; |
| 586 | if (!pn->indegree) |
| 587 | commit_list_insert(parent, &work); |
| 588 | } |
| 589 | parents=parents->next; |
| 590 | } |
| 591 | /* |
| 592 | * work_item is a commit all of whose children |
| 593 | * have already been emitted. we can emit it now. |
| 594 | */ |
| 595 | *pptr = work_node->list_item; |
| 596 | pptr = &(*pptr)->next; |
| 597 | *pptr = NULL; |
| 598 | work_item->object.util = NULL; |
| 599 | } |
| 600 | free(nodes); |
| 601 | } |