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