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