Junio C Hamano | 8f1d2e6 | 2006-01-07 01:33:54 -0800 | [diff] [blame] | 1 | #include "cache.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" |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 4 | #include "pkt-line.h" |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 5 | #include "utf8.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 6 | |
Linus Torvalds | 60ab26d | 2005-09-15 14:43:17 -0700 | [diff] [blame] | 7 | int save_commit_buffer = 1; |
| 8 | |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 9 | struct sort_node |
| 10 | { |
| 11 | /* |
Tilman Sauerbeck | 55c3eb4 | 2006-08-17 20:44:16 +0200 | [diff] [blame] | 12 | * the number of children of the associated commit |
| 13 | * that also occur in the list being sorted. |
| 14 | */ |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 15 | unsigned int indegree; |
| 16 | |
| 17 | /* |
Tilman Sauerbeck | 55c3eb4 | 2006-08-17 20:44:16 +0200 | [diff] [blame] | 18 | * reference to original list item that we will re-use |
| 19 | * on output. |
| 20 | */ |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 21 | struct commit_list * list_item; |
| 22 | |
| 23 | }; |
| 24 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 25 | const char *commit_type = "commit"; |
| 26 | |
Eric Wong | 6cdfd17 | 2006-05-14 17:20:46 -0700 | [diff] [blame] | 27 | struct cmt_fmt_map { |
| 28 | const char *n; |
| 29 | size_t cmp_len; |
| 30 | enum cmit_fmt v; |
| 31 | } cmt_fmts[] = { |
| 32 | { "raw", 1, CMIT_FMT_RAW }, |
| 33 | { "medium", 1, CMIT_FMT_MEDIUM }, |
| 34 | { "short", 1, CMIT_FMT_SHORT }, |
Junio C Hamano | 328b710 | 2006-05-21 01:34:54 -0700 | [diff] [blame] | 35 | { "email", 1, CMIT_FMT_EMAIL }, |
Eric Wong | 6cdfd17 | 2006-05-14 17:20:46 -0700 | [diff] [blame] | 36 | { "full", 5, CMIT_FMT_FULL }, |
| 37 | { "fuller", 5, CMIT_FMT_FULLER }, |
| 38 | { "oneline", 1, CMIT_FMT_ONELINE }, |
| 39 | }; |
| 40 | |
Linus Torvalds | 9b66ec0 | 2005-06-26 17:50:46 -0700 | [diff] [blame] | 41 | enum cmit_fmt get_commit_format(const char *arg) |
| 42 | { |
Eric Wong | 6cdfd17 | 2006-05-14 17:20:46 -0700 | [diff] [blame] | 43 | int i; |
| 44 | |
| 45 | if (!arg || !*arg) |
Linus Torvalds | 9b66ec0 | 2005-06-26 17:50:46 -0700 | [diff] [blame] | 46 | return CMIT_FMT_DEFAULT; |
Eric Wong | 6cdfd17 | 2006-05-14 17:20:46 -0700 | [diff] [blame] | 47 | if (*arg == '=') |
| 48 | arg++; |
| 49 | for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) { |
Eric Wong | b693620 | 2007-02-02 05:10:25 -0800 | [diff] [blame] | 50 | if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) && |
| 51 | !strncmp(arg, cmt_fmts[i].n, strlen(arg))) |
Eric Wong | 6cdfd17 | 2006-05-14 17:20:46 -0700 | [diff] [blame] | 52 | return cmt_fmts[i].v; |
| 53 | } |
| 54 | |
| 55 | die("invalid --pretty format: %s", arg); |
Linus Torvalds | 9b66ec0 | 2005-06-26 17:50:46 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 58 | static struct commit *check_commit(struct object *obj, |
| 59 | const unsigned char *sha1, |
| 60 | int quiet) |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 61 | { |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 62 | if (obj->type != OBJ_COMMIT) { |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 63 | if (!quiet) |
| 64 | error("Object %s is a %s, not a commit", |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 65 | sha1_to_hex(sha1), typename(obj->type)); |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 66 | return NULL; |
| 67 | } |
| 68 | return (struct commit *) obj; |
| 69 | } |
| 70 | |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 71 | struct commit *lookup_commit_reference_gently(const unsigned char *sha1, |
| 72 | int quiet) |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 73 | { |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 74 | struct object *obj = deref_tag(parse_object(sha1), NULL, 0); |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 75 | |
| 76 | if (!obj) |
| 77 | return NULL; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 78 | return check_commit(obj, sha1, quiet); |
| 79 | } |
| 80 | |
| 81 | struct commit *lookup_commit_reference(const unsigned char *sha1) |
| 82 | { |
| 83 | return lookup_commit_reference_gently(sha1, 0); |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 86 | struct commit *lookup_commit(const unsigned char *sha1) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 87 | { |
| 88 | struct object *obj = lookup_object(sha1); |
| 89 | if (!obj) { |
Linus Torvalds | 855419f | 2006-06-19 10:44:15 -0700 | [diff] [blame] | 90 | struct commit *ret = alloc_commit_node(); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 91 | created_object(sha1, &ret->object); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 92 | ret->object.type = OBJ_COMMIT; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 93 | return ret; |
| 94 | } |
Nicolas Pitre | d1af002 | 2005-05-20 16:59:17 -0400 | [diff] [blame] | 95 | if (!obj->type) |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 96 | obj->type = OBJ_COMMIT; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 97 | return check_commit(obj, sha1, 0); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static unsigned long parse_commit_date(const char *buf) |
| 101 | { |
| 102 | unsigned long date; |
| 103 | |
| 104 | if (memcmp(buf, "author", 6)) |
| 105 | return 0; |
| 106 | while (*buf++ != '\n') |
| 107 | /* nada */; |
| 108 | if (memcmp(buf, "committer", 9)) |
| 109 | return 0; |
| 110 | while (*buf++ != '>') |
| 111 | /* nada */; |
| 112 | date = strtoul(buf, NULL, 10); |
| 113 | if (date == ULONG_MAX) |
| 114 | date = 0; |
| 115 | return date; |
| 116 | } |
| 117 | |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 118 | static struct commit_graft **commit_graft; |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 119 | static int commit_graft_alloc, commit_graft_nr; |
| 120 | |
| 121 | static int commit_graft_pos(const unsigned char *sha1) |
| 122 | { |
| 123 | int lo, hi; |
| 124 | lo = 0; |
| 125 | hi = commit_graft_nr; |
| 126 | while (lo < hi) { |
| 127 | int mi = (lo + hi) / 2; |
| 128 | struct commit_graft *graft = commit_graft[mi]; |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 129 | int cmp = hashcmp(sha1, graft->sha1); |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 130 | if (!cmp) |
| 131 | return mi; |
| 132 | if (cmp < 0) |
| 133 | hi = mi; |
| 134 | else |
| 135 | lo = mi + 1; |
| 136 | } |
| 137 | return -lo - 1; |
| 138 | } |
| 139 | |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 140 | int register_commit_graft(struct commit_graft *graft, int ignore_dups) |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 141 | { |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 142 | int pos = commit_graft_pos(graft->sha1); |
| 143 | |
| 144 | if (0 <= pos) { |
| 145 | if (ignore_dups) |
| 146 | free(graft); |
| 147 | else { |
| 148 | free(commit_graft[pos]); |
| 149 | commit_graft[pos] = graft; |
| 150 | } |
| 151 | return 1; |
| 152 | } |
| 153 | pos = -pos - 1; |
| 154 | if (commit_graft_alloc <= ++commit_graft_nr) { |
| 155 | commit_graft_alloc = alloc_nr(commit_graft_alloc); |
| 156 | commit_graft = xrealloc(commit_graft, |
| 157 | sizeof(*commit_graft) * |
| 158 | commit_graft_alloc); |
| 159 | } |
| 160 | if (pos < commit_graft_nr) |
| 161 | memmove(commit_graft + pos + 1, |
| 162 | commit_graft + pos, |
| 163 | (commit_graft_nr - pos - 1) * |
| 164 | sizeof(*commit_graft)); |
| 165 | commit_graft[pos] = graft; |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | struct commit_graft *read_graft_line(char *buf, int len) |
| 170 | { |
| 171 | /* The format is just "Commit Parent1 Parent2 ...\n" */ |
| 172 | int i; |
| 173 | struct commit_graft *graft = NULL; |
| 174 | |
| 175 | if (buf[len-1] == '\n') |
| 176 | buf[--len] = 0; |
Yann Dirson | 360204c | 2006-04-17 13:41:49 +0200 | [diff] [blame] | 177 | if (buf[0] == '#' || buf[0] == '\0') |
Junio C Hamano | 5bc4ce5 | 2006-04-16 14:24:56 -0700 | [diff] [blame] | 178 | return NULL; |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 179 | if ((len + 1) % 41) { |
| 180 | bad_graft_data: |
| 181 | error("bad graft data: %s", buf); |
| 182 | free(graft); |
| 183 | return NULL; |
| 184 | } |
| 185 | i = (len + 1) / 41 - 1; |
| 186 | graft = xmalloc(sizeof(*graft) + 20 * i); |
| 187 | graft->nr_parent = i; |
| 188 | if (get_sha1_hex(buf, graft->sha1)) |
| 189 | goto bad_graft_data; |
| 190 | for (i = 40; i < len; i += 41) { |
| 191 | if (buf[i] != ' ') |
| 192 | goto bad_graft_data; |
| 193 | if (get_sha1_hex(buf + i + 1, graft->parent[i/41])) |
| 194 | goto bad_graft_data; |
| 195 | } |
| 196 | return graft; |
| 197 | } |
| 198 | |
| 199 | int read_graft_file(const char *graft_file) |
| 200 | { |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 201 | FILE *fp = fopen(graft_file, "r"); |
| 202 | char buf[1024]; |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 203 | if (!fp) |
| 204 | return -1; |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 205 | while (fgets(buf, sizeof(buf), fp)) { |
| 206 | /* The format is just "Commit Parent1 Parent2 ...\n" */ |
| 207 | int len = strlen(buf); |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 208 | struct commit_graft *graft = read_graft_line(buf, len); |
Junio C Hamano | 5bc4ce5 | 2006-04-16 14:24:56 -0700 | [diff] [blame] | 209 | if (!graft) |
| 210 | continue; |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 211 | if (register_commit_graft(graft, 1)) |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 212 | error("duplicate graft data: %s", buf); |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 213 | } |
| 214 | fclose(fp); |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static void prepare_commit_graft(void) |
| 219 | { |
| 220 | static int commit_graft_prepared; |
| 221 | char *graft_file; |
| 222 | |
| 223 | if (commit_graft_prepared) |
| 224 | return; |
| 225 | graft_file = get_graft_file(); |
| 226 | read_graft_file(graft_file); |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 227 | /* make sure shallows are read */ |
| 228 | is_repository_shallow(); |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 229 | commit_graft_prepared = 1; |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static struct commit_graft *lookup_commit_graft(const unsigned char *sha1) |
| 233 | { |
| 234 | int pos; |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 235 | prepare_commit_graft(); |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 236 | pos = commit_graft_pos(sha1); |
| 237 | if (pos < 0) |
| 238 | return NULL; |
| 239 | return commit_graft[pos]; |
| 240 | } |
| 241 | |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 242 | int write_shallow_commits(int fd, int use_pack_protocol) |
| 243 | { |
| 244 | int i, count = 0; |
| 245 | for (i = 0; i < commit_graft_nr; i++) |
| 246 | if (commit_graft[i]->nr_parent < 0) { |
| 247 | const char *hex = |
| 248 | sha1_to_hex(commit_graft[i]->sha1); |
| 249 | count++; |
| 250 | if (use_pack_protocol) |
| 251 | packet_write(fd, "shallow %s", hex); |
| 252 | else { |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 253 | if (write_in_full(fd, hex, 40) != 40) |
| 254 | break; |
| 255 | if (write_in_full(fd, "\n", 1) != 1) |
| 256 | break; |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | return count; |
| 260 | } |
| 261 | |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 262 | int unregister_shallow(const unsigned char *sha1) |
| 263 | { |
| 264 | int pos = commit_graft_pos(sha1); |
| 265 | if (pos < 0) |
| 266 | return -1; |
| 267 | if (pos + 1 < commit_graft_nr) |
| 268 | memcpy(commit_graft + pos, commit_graft + pos + 1, |
| 269 | sizeof(struct commit_graft *) |
| 270 | * (commit_graft_nr - pos - 1)); |
| 271 | commit_graft_nr--; |
| 272 | return 0; |
| 273 | } |
| 274 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 275 | int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 276 | { |
Junio C Hamano | 3b44f15 | 2006-06-28 03:51:00 -0700 | [diff] [blame] | 277 | char *tail = buffer; |
Linus Torvalds | f7cc77d | 2005-07-27 15:12:48 -0700 | [diff] [blame] | 278 | char *bufptr = buffer; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 279 | unsigned char parent[20]; |
Linus Torvalds | 6c88be1 | 2005-06-20 20:26:03 -0700 | [diff] [blame] | 280 | struct commit_list **pptr; |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 281 | struct commit_graft *graft; |
Junio C Hamano | 27dedf0 | 2005-11-16 21:32:44 -0800 | [diff] [blame] | 282 | unsigned n_refs = 0; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 283 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 284 | if (item->object.parsed) |
| 285 | return 0; |
| 286 | item->object.parsed = 1; |
Junio C Hamano | 3b44f15 | 2006-06-28 03:51:00 -0700 | [diff] [blame] | 287 | tail += size; |
| 288 | if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5)) |
Linus Torvalds | f7cc77d | 2005-07-27 15:12:48 -0700 | [diff] [blame] | 289 | return error("bogus commit object %s", sha1_to_hex(item->object.sha1)); |
Junio C Hamano | 3b44f15 | 2006-06-28 03:51:00 -0700 | [diff] [blame] | 290 | if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0) |
Junio C Hamano | bd2afde | 2006-02-22 17:47:10 -0800 | [diff] [blame] | 291 | return error("bad tree pointer in commit %s", |
| 292 | sha1_to_hex(item->object.sha1)); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 293 | item->tree = lookup_tree(parent); |
Linus Torvalds | 235ac40 | 2005-04-24 14:31:57 -0700 | [diff] [blame] | 294 | if (item->tree) |
Junio C Hamano | 27dedf0 | 2005-11-16 21:32:44 -0800 | [diff] [blame] | 295 | n_refs++; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 296 | bufptr += 46; /* "tree " + "hex sha1" + "\n" */ |
Linus Torvalds | 6c88be1 | 2005-06-20 20:26:03 -0700 | [diff] [blame] | 297 | pptr = &item->parents; |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 298 | |
| 299 | graft = lookup_commit_graft(item->object.sha1); |
Junio C Hamano | 3b44f15 | 2006-06-28 03:51:00 -0700 | [diff] [blame] | 300 | while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) { |
Linus Torvalds | f7cc77d | 2005-07-27 15:12:48 -0700 | [diff] [blame] | 301 | struct commit *new_parent; |
| 302 | |
Junio C Hamano | 3b44f15 | 2006-06-28 03:51:00 -0700 | [diff] [blame] | 303 | if (tail <= bufptr + 48 || |
| 304 | get_sha1_hex(bufptr + 7, parent) || |
| 305 | bufptr[47] != '\n') |
Linus Torvalds | f7cc77d | 2005-07-27 15:12:48 -0700 | [diff] [blame] | 306 | 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] | 307 | bufptr += 48; |
| 308 | if (graft) |
| 309 | continue; |
Linus Torvalds | f7cc77d | 2005-07-27 15:12:48 -0700 | [diff] [blame] | 310 | new_parent = lookup_commit(parent); |
Linus Torvalds | 235ac40 | 2005-04-24 14:31:57 -0700 | [diff] [blame] | 311 | if (new_parent) { |
Linus Torvalds | 6c88be1 | 2005-06-20 20:26:03 -0700 | [diff] [blame] | 312 | pptr = &commit_list_insert(new_parent, pptr)->next; |
Junio C Hamano | 27dedf0 | 2005-11-16 21:32:44 -0800 | [diff] [blame] | 313 | n_refs++; |
Linus Torvalds | 235ac40 | 2005-04-24 14:31:57 -0700 | [diff] [blame] | 314 | } |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 315 | } |
| 316 | if (graft) { |
| 317 | int i; |
| 318 | struct commit *new_parent; |
| 319 | for (i = 0; i < graft->nr_parent; i++) { |
| 320 | new_parent = lookup_commit(graft->parent[i]); |
| 321 | if (!new_parent) |
| 322 | continue; |
| 323 | pptr = &commit_list_insert(new_parent, pptr)->next; |
Junio C Hamano | 27dedf0 | 2005-11-16 21:32:44 -0800 | [diff] [blame] | 324 | n_refs++; |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 325 | } |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 326 | } |
| 327 | item->date = parse_commit_date(bufptr); |
Junio C Hamano | 27dedf0 | 2005-11-16 21:32:44 -0800 | [diff] [blame] | 328 | |
| 329 | if (track_object_refs) { |
| 330 | unsigned i = 0; |
| 331 | struct commit_list *p; |
| 332 | struct object_refs *refs = alloc_object_refs(n_refs); |
| 333 | if (item->tree) |
| 334 | refs->ref[i++] = &item->tree->object; |
| 335 | for (p = item->parents; p; p = p->next) |
| 336 | refs->ref[i++] = &p->item->object; |
| 337 | set_object_refs(&item->object, refs); |
| 338 | } |
| 339 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 343 | int parse_commit(struct commit *item) |
| 344 | { |
| 345 | char type[20]; |
| 346 | void *buffer; |
| 347 | unsigned long size; |
| 348 | int ret; |
| 349 | |
| 350 | if (item->object.parsed) |
| 351 | return 0; |
| 352 | buffer = read_sha1_file(item->object.sha1, type, &size); |
| 353 | if (!buffer) |
| 354 | return error("Could not read %s", |
| 355 | sha1_to_hex(item->object.sha1)); |
| 356 | if (strcmp(type, commit_type)) { |
| 357 | free(buffer); |
| 358 | return error("Object %s not a commit", |
| 359 | sha1_to_hex(item->object.sha1)); |
| 360 | } |
| 361 | ret = parse_commit_buffer(item, buffer, size); |
Linus Torvalds | 60ab26d | 2005-09-15 14:43:17 -0700 | [diff] [blame] | 362 | if (save_commit_buffer && !ret) { |
Linus Torvalds | 3ff1fbb | 2005-05-25 18:27:14 -0700 | [diff] [blame] | 363 | item->buffer = buffer; |
| 364 | return 0; |
| 365 | } |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 366 | free(buffer); |
| 367 | return ret; |
| 368 | } |
| 369 | |
Linus Torvalds | ac5155e | 2005-05-30 18:44:02 -0700 | [diff] [blame] | 370 | 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] | 371 | { |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 372 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 373 | new_list->item = item; |
| 374 | new_list->next = *list_p; |
| 375 | *list_p = new_list; |
Linus Torvalds | ac5155e | 2005-05-30 18:44:02 -0700 | [diff] [blame] | 376 | return new_list; |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 379 | void free_commit_list(struct commit_list *list) |
| 380 | { |
| 381 | while (list) { |
| 382 | struct commit_list *temp = list; |
| 383 | list = temp->next; |
| 384 | free(temp); |
| 385 | } |
| 386 | } |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 387 | |
Linus Torvalds | f755494 | 2005-07-06 09:31:17 -0700 | [diff] [blame] | 388 | 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] | 389 | { |
| 390 | struct commit_list **pp = list; |
| 391 | struct commit_list *p; |
| 392 | while ((p = *pp) != NULL) { |
| 393 | if (p->item->date < item->date) { |
| 394 | break; |
| 395 | } |
| 396 | pp = &p->next; |
| 397 | } |
Linus Torvalds | f755494 | 2005-07-06 09:31:17 -0700 | [diff] [blame] | 398 | return commit_list_insert(item, pp); |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | |
| 402 | void sort_by_date(struct commit_list **list) |
| 403 | { |
| 404 | struct commit_list *ret = NULL; |
| 405 | while (*list) { |
Linus Torvalds | f755494 | 2005-07-06 09:31:17 -0700 | [diff] [blame] | 406 | insert_by_date((*list)->item, &ret); |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 407 | *list = (*list)->next; |
| 408 | } |
| 409 | *list = ret; |
| 410 | } |
| 411 | |
Daniel Barkalow | 58e28af | 2005-04-23 20:29:22 -0700 | [diff] [blame] | 412 | struct commit *pop_most_recent_commit(struct commit_list **list, |
| 413 | unsigned int mark) |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 414 | { |
| 415 | struct commit *ret = (*list)->item; |
| 416 | struct commit_list *parents = ret->parents; |
| 417 | struct commit_list *old = *list; |
| 418 | |
| 419 | *list = (*list)->next; |
| 420 | free(old); |
| 421 | |
| 422 | while (parents) { |
Linus Torvalds | 4056c09 | 2005-04-23 19:21:28 -0700 | [diff] [blame] | 423 | struct commit *commit = parents->item; |
Daniel Barkalow | 58e28af | 2005-04-23 20:29:22 -0700 | [diff] [blame] | 424 | parse_commit(commit); |
| 425 | if (!(commit->object.flags & mark)) { |
| 426 | commit->object.flags |= mark; |
Linus Torvalds | f755494 | 2005-07-06 09:31:17 -0700 | [diff] [blame] | 427 | insert_by_date(commit, list); |
Linus Torvalds | 4056c09 | 2005-04-23 19:21:28 -0700 | [diff] [blame] | 428 | } |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 429 | parents = parents->next; |
| 430 | } |
| 431 | return ret; |
| 432 | } |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 433 | |
Junio C Hamano | f8f9c73 | 2006-01-07 18:52:42 -0800 | [diff] [blame] | 434 | void clear_commit_marks(struct commit *commit, unsigned int mark) |
| 435 | { |
| 436 | struct commit_list *parents; |
| 437 | |
Junio C Hamano | f8f9c73 | 2006-01-07 18:52:42 -0800 | [diff] [blame] | 438 | commit->object.flags &= ~mark; |
Junio C Hamano | 58ecf5c | 2006-07-04 17:45:22 -0700 | [diff] [blame] | 439 | parents = commit->parents; |
Junio C Hamano | f8f9c73 | 2006-01-07 18:52:42 -0800 | [diff] [blame] | 440 | while (parents) { |
Junio C Hamano | 160b798 | 2006-07-03 03:05:20 -0700 | [diff] [blame] | 441 | struct commit *parent = parents->item; |
Junio C Hamano | 58ecf5c | 2006-07-04 17:45:22 -0700 | [diff] [blame] | 442 | |
| 443 | /* Have we already cleared this? */ |
| 444 | if (mark & parent->object.flags) |
Junio C Hamano | 160b798 | 2006-07-03 03:05:20 -0700 | [diff] [blame] | 445 | clear_commit_marks(parent, mark); |
Junio C Hamano | f8f9c73 | 2006-01-07 18:52:42 -0800 | [diff] [blame] | 446 | parents = parents->next; |
| 447 | } |
| 448 | } |
| 449 | |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 450 | /* |
| 451 | * Generic support for pretty-printing the header |
| 452 | */ |
| 453 | static int get_one_line(const char *msg, unsigned long len) |
| 454 | { |
| 455 | int ret = 0; |
| 456 | |
| 457 | while (len--) { |
| 458 | char c = *msg++; |
Linus Torvalds | 684958a | 2006-04-12 11:31:23 -0700 | [diff] [blame] | 459 | if (!c) |
| 460 | break; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 461 | ret++; |
| 462 | if (c == '\n') |
| 463 | break; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 464 | } |
| 465 | return ret; |
| 466 | } |
| 467 | |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 468 | /* High bit set, or ISO-2022-INT */ |
| 469 | static int non_ascii(int ch) |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 470 | { |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 471 | ch = (ch & 0xff); |
| 472 | return ((ch & 0x80) || (ch == 0x1b)); |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 475 | static int is_rfc2047_special(char ch) |
| 476 | { |
| 477 | return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_')); |
| 478 | } |
| 479 | |
| 480 | static int add_rfc2047(char *buf, const char *line, int len, |
| 481 | const char *encoding) |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 482 | { |
| 483 | char *bp = buf; |
| 484 | int i, needquote; |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 485 | char q_encoding[128]; |
| 486 | const char *q_encoding_fmt = "=?%s?q?"; |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 487 | |
| 488 | for (i = needquote = 0; !needquote && i < len; i++) { |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 489 | int ch = line[i]; |
| 490 | if (non_ascii(ch)) |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 491 | needquote++; |
| 492 | if ((i + 1 < len) && |
| 493 | (ch == '=' && line[i+1] == '?')) |
| 494 | needquote++; |
| 495 | } |
| 496 | if (!needquote) |
| 497 | return sprintf(buf, "%.*s", len, line); |
| 498 | |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 499 | i = snprintf(q_encoding, sizeof(q_encoding), q_encoding_fmt, encoding); |
| 500 | if (sizeof(q_encoding) < i) |
| 501 | die("Insanely long encoding name %s", encoding); |
| 502 | memcpy(bp, q_encoding, i); |
| 503 | bp += i; |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 504 | for (i = 0; i < len; i++) { |
Junio C Hamano | 0da4677 | 2006-06-19 15:00:17 -0700 | [diff] [blame] | 505 | unsigned ch = line[i] & 0xFF; |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 506 | if (is_rfc2047_special(ch)) { |
| 507 | sprintf(bp, "=%02X", ch); |
| 508 | bp += 3; |
| 509 | } |
| 510 | else if (ch == ' ') |
| 511 | *bp++ = '_'; |
| 512 | else |
| 513 | *bp++ = ch; |
| 514 | } |
| 515 | memcpy(bp, "?=", 2); |
| 516 | bp += 2; |
| 517 | return bp - buf; |
| 518 | } |
| 519 | |
Jonas Fonseca | 3dfb927 | 2006-08-28 15:52:13 +0200 | [diff] [blame] | 520 | static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 521 | const char *line, int relative_date, |
| 522 | const char *encoding) |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 523 | { |
| 524 | char *date; |
Junio C Hamano | 5de36bf | 2005-08-29 21:17:21 -0700 | [diff] [blame] | 525 | int namelen; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 526 | unsigned long time; |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 527 | int tz, ret; |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 528 | const char *filler = " "; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 529 | |
Junio C Hamano | d87449c | 2005-08-08 22:15:40 -0700 | [diff] [blame] | 530 | if (fmt == CMIT_FMT_ONELINE) |
| 531 | return 0; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 532 | date = strchr(line, '>'); |
| 533 | if (!date) |
| 534 | return 0; |
| 535 | namelen = ++date - line; |
| 536 | time = strtoul(date, &date, 10); |
| 537 | tz = strtol(date, NULL, 10); |
| 538 | |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 539 | if (fmt == CMIT_FMT_EMAIL) { |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 540 | char *name_tail = strchr(line, '<'); |
| 541 | int display_name_length; |
| 542 | if (!name_tail) |
| 543 | return 0; |
| 544 | while (line < name_tail && isspace(name_tail[-1])) |
| 545 | name_tail--; |
| 546 | display_name_length = name_tail - line; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 547 | filler = ""; |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 548 | strcpy(buf, "From: "); |
| 549 | ret = strlen(buf); |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 550 | ret += add_rfc2047(buf + ret, line, display_name_length, |
| 551 | encoding); |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 552 | memcpy(buf + ret, name_tail, namelen - display_name_length); |
| 553 | ret += namelen - display_name_length; |
| 554 | buf[ret++] = '\n'; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 555 | } |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 556 | else { |
| 557 | ret = sprintf(buf, "%s: %.*s%.*s\n", what, |
| 558 | (fmt == CMIT_FMT_FULLER) ? 4 : 0, |
| 559 | filler, namelen, line); |
| 560 | } |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 561 | switch (fmt) { |
| 562 | case CMIT_FMT_MEDIUM: |
Jonas Fonseca | 3dfb927 | 2006-08-28 15:52:13 +0200 | [diff] [blame] | 563 | ret += sprintf(buf + ret, "Date: %s\n", |
| 564 | show_date(time, tz, relative_date)); |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 565 | break; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 566 | case CMIT_FMT_EMAIL: |
Junio C Hamano | 2a38704 | 2006-05-01 01:44:33 -0700 | [diff] [blame] | 567 | ret += sprintf(buf + ret, "Date: %s\n", |
| 568 | show_rfc2822_date(time, tz)); |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 569 | break; |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 570 | case CMIT_FMT_FULLER: |
Jonas Fonseca | 3dfb927 | 2006-08-28 15:52:13 +0200 | [diff] [blame] | 571 | ret += sprintf(buf + ret, "%sDate: %s\n", what, |
| 572 | show_date(time, tz, relative_date)); |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 573 | break; |
| 574 | default: |
| 575 | /* notin' */ |
| 576 | break; |
| 577 | } |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 578 | return ret; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 581 | static int is_empty_line(const char *line, int *len_p) |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 582 | { |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 583 | int len = *len_p; |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 584 | while (len && isspace(line[len-1])) |
| 585 | len--; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 586 | *len_p = len; |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 587 | return !len; |
| 588 | } |
| 589 | |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 590 | static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev) |
Linus Torvalds | 28342a5 | 2005-06-18 13:52:05 -0700 | [diff] [blame] | 591 | { |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 592 | struct commit_list *parent = commit->parents; |
| 593 | int offset; |
Junio C Hamano | d87449c | 2005-08-08 22:15:40 -0700 | [diff] [blame] | 594 | |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 595 | if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) || |
| 596 | !parent || !parent->next) |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 597 | return 0; |
| 598 | |
| 599 | offset = sprintf(buf, "Merge:"); |
| 600 | |
| 601 | while (parent) { |
| 602 | struct commit *p = parent->item; |
Eric Wong | 14763e7 | 2006-10-11 16:16:02 -0700 | [diff] [blame] | 603 | const char *hex = NULL; |
| 604 | const char *dots; |
| 605 | if (abbrev) |
| 606 | hex = find_unique_abbrev(p->object.sha1, abbrev); |
| 607 | if (!hex) |
| 608 | hex = sha1_to_hex(p->object.sha1); |
| 609 | dots = (abbrev && strlen(hex) != 40) ? "..." : ""; |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 610 | parent = parent->next; |
| 611 | |
Junio C Hamano | 6bfb27a | 2006-02-02 17:52:19 -0800 | [diff] [blame] | 612 | offset += sprintf(buf + offset, " %s%s", hex, dots); |
Linus Torvalds | 28342a5 | 2005-06-18 13:52:05 -0700 | [diff] [blame] | 613 | } |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 614 | buf[offset++] = '\n'; |
Linus Torvalds | 28342a5 | 2005-06-18 13:52:05 -0700 | [diff] [blame] | 615 | return offset; |
| 616 | } |
| 617 | |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 618 | static char *get_header(const struct commit *commit, const char *key) |
| 619 | { |
| 620 | int key_len = strlen(key); |
| 621 | const char *line = commit->buffer; |
| 622 | |
| 623 | for (;;) { |
| 624 | const char *eol = strchr(line, '\n'), *next; |
| 625 | |
| 626 | if (line == eol) |
| 627 | return NULL; |
| 628 | if (!eol) { |
| 629 | eol = line + strlen(line); |
| 630 | next = NULL; |
| 631 | } else |
| 632 | next = eol + 1; |
| 633 | if (!strncmp(line, key, key_len) && line[key_len] == ' ') { |
| 634 | int len = eol - line - key_len; |
| 635 | char *ret = xmalloc(len); |
| 636 | memcpy(ret, line + key_len + 1, len - 1); |
| 637 | ret[len - 1] = '\0'; |
| 638 | return ret; |
| 639 | } |
| 640 | line = next; |
| 641 | } |
| 642 | } |
| 643 | |
Junio C Hamano | 53af981 | 2006-12-30 15:49:32 -0800 | [diff] [blame] | 644 | static char *replace_encoding_header(char *buf, char *encoding) |
| 645 | { |
| 646 | char *encoding_header = strstr(buf, "\nencoding "); |
| 647 | char *end_of_encoding_header; |
| 648 | int encoding_header_pos; |
| 649 | int encoding_header_len; |
| 650 | int new_len; |
| 651 | int need_len; |
| 652 | int buflen = strlen(buf) + 1; |
| 653 | |
| 654 | if (!encoding_header) |
| 655 | return buf; /* should not happen but be defensive */ |
| 656 | encoding_header++; |
| 657 | end_of_encoding_header = strchr(encoding_header, '\n'); |
| 658 | if (!end_of_encoding_header) |
| 659 | return buf; /* should not happen but be defensive */ |
| 660 | end_of_encoding_header++; |
| 661 | |
| 662 | encoding_header_len = end_of_encoding_header - encoding_header; |
| 663 | encoding_header_pos = encoding_header - buf; |
| 664 | |
| 665 | if (is_encoding_utf8(encoding)) { |
| 666 | /* we have re-coded to UTF-8; drop the header */ |
| 667 | memmove(encoding_header, end_of_encoding_header, |
| 668 | buflen - (encoding_header_pos + encoding_header_len)); |
| 669 | return buf; |
| 670 | } |
| 671 | new_len = strlen(encoding); |
| 672 | need_len = new_len + strlen("encoding \n"); |
| 673 | if (encoding_header_len < need_len) { |
| 674 | buf = xrealloc(buf, buflen + (need_len - encoding_header_len)); |
| 675 | encoding_header = buf + encoding_header_pos; |
| 676 | end_of_encoding_header = encoding_header + encoding_header_len; |
| 677 | } |
| 678 | memmove(end_of_encoding_header + (need_len - encoding_header_len), |
| 679 | end_of_encoding_header, |
| 680 | buflen - (encoding_header_pos + encoding_header_len)); |
| 681 | memcpy(encoding_header + 9, encoding, strlen(encoding)); |
| 682 | encoding_header[9 + new_len] = '\n'; |
| 683 | return buf; |
| 684 | } |
| 685 | |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 686 | static char *logmsg_reencode(const struct commit *commit, |
| 687 | char *output_encoding) |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 688 | { |
Junio C Hamano | d2c11a3 | 2006-12-27 16:41:33 -0800 | [diff] [blame] | 689 | char *encoding; |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 690 | char *out; |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 691 | char *utf8 = "utf-8"; |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 692 | |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 693 | if (!*output_encoding) |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 694 | return NULL; |
Junio C Hamano | d2c11a3 | 2006-12-27 16:41:33 -0800 | [diff] [blame] | 695 | encoding = get_header(commit, "encoding"); |
Junio C Hamano | e90068a | 2006-12-31 18:18:23 -0800 | [diff] [blame] | 696 | if (!encoding) |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 697 | encoding = utf8; |
Junio C Hamano | e90068a | 2006-12-31 18:18:23 -0800 | [diff] [blame] | 698 | if (!strcmp(encoding, output_encoding)) |
| 699 | out = strdup(commit->buffer); |
| 700 | else |
| 701 | out = reencode_string(commit->buffer, |
| 702 | output_encoding, encoding); |
Junio C Hamano | 53af981 | 2006-12-30 15:49:32 -0800 | [diff] [blame] | 703 | if (out) |
| 704 | out = replace_encoding_header(out, output_encoding); |
| 705 | |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 706 | if (encoding != utf8) |
| 707 | free(encoding); |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 708 | if (!out) |
| 709 | return NULL; |
| 710 | return out; |
| 711 | } |
| 712 | |
| 713 | unsigned long pretty_print_commit(enum cmit_fmt fmt, |
| 714 | const struct commit *commit, |
| 715 | unsigned long len, |
| 716 | char *buf, unsigned long space, |
Jonas Fonseca | 3dfb927 | 2006-08-28 15:52:13 +0200 | [diff] [blame] | 717 | int abbrev, const char *subject, |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 718 | const char *after_subject, |
| 719 | int relative_date) |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 720 | { |
Lars Hjemli | f3a4740 | 2007-01-03 14:34:13 +0100 | [diff] [blame] | 721 | int hdr = 1, body = 0, seen_title = 0; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 722 | unsigned long offset = 0; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 723 | int indent = 4; |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 724 | int parents_shown = 0; |
Junio C Hamano | 3815f42 | 2006-01-27 01:54:59 -0800 | [diff] [blame] | 725 | const char *msg = commit->buffer; |
Junio C Hamano | c831da6 | 2006-05-21 23:55:00 -0700 | [diff] [blame] | 726 | int plain_non_ascii = 0; |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 727 | char *reencoded; |
| 728 | char *encoding; |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 729 | |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 730 | encoding = (git_log_output_encoding |
| 731 | ? git_log_output_encoding |
| 732 | : git_commit_encoding); |
| 733 | if (!encoding) |
| 734 | encoding = "utf-8"; |
| 735 | reencoded = logmsg_reencode(commit, encoding); |
Junio C Hamano | d2c11a3 | 2006-12-27 16:41:33 -0800 | [diff] [blame] | 736 | if (reencoded) |
| 737 | msg = reencoded; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 738 | |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 739 | if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL) |
| 740 | indent = 0; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 741 | |
Junio C Hamano | c831da6 | 2006-05-21 23:55:00 -0700 | [diff] [blame] | 742 | /* After-subject is used to pass in Content-Type: multipart |
| 743 | * MIME header; in that case we do not have to do the |
| 744 | * plaintext content type even if the commit message has |
| 745 | * non 7-bit ASCII character. Otherwise, check if we need |
| 746 | * to say this is not a 7-bit ASCII. |
| 747 | */ |
| 748 | if (fmt == CMIT_FMT_EMAIL && !after_subject) { |
Junio C Hamano | 0da4677 | 2006-06-19 15:00:17 -0700 | [diff] [blame] | 749 | int i, ch, in_body; |
| 750 | |
| 751 | for (in_body = i = 0; (ch = msg[i]) && i < len; i++) { |
| 752 | if (!in_body) { |
| 753 | /* author could be non 7-bit ASCII but |
Junio C Hamano | d2c11a3 | 2006-12-27 16:41:33 -0800 | [diff] [blame] | 754 | * the log may be so; skip over the |
Junio C Hamano | 0da4677 | 2006-06-19 15:00:17 -0700 | [diff] [blame] | 755 | * header part first. |
| 756 | */ |
| 757 | if (ch == '\n' && |
| 758 | i + 1 < len && msg[i+1] == '\n') |
| 759 | in_body = 1; |
| 760 | } |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 761 | else if (non_ascii(ch)) { |
Junio C Hamano | c831da6 | 2006-05-21 23:55:00 -0700 | [diff] [blame] | 762 | plain_non_ascii = 1; |
Junio C Hamano | 0da4677 | 2006-06-19 15:00:17 -0700 | [diff] [blame] | 763 | break; |
| 764 | } |
| 765 | } |
Junio C Hamano | c831da6 | 2006-05-21 23:55:00 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 768 | for (;;) { |
| 769 | const char *line = msg; |
| 770 | int linelen = get_one_line(msg, len); |
| 771 | |
| 772 | if (!linelen) |
| 773 | break; |
| 774 | |
| 775 | /* |
| 776 | * We want some slop for indentation and a possible |
| 777 | * final "...". Thus the "+ 20". |
| 778 | */ |
| 779 | if (offset + linelen + 20 > space) { |
| 780 | memcpy(buf + offset, " ...\n", 8); |
| 781 | offset += 8; |
| 782 | break; |
| 783 | } |
| 784 | |
| 785 | msg += linelen; |
| 786 | len -= linelen; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 787 | if (hdr) { |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 788 | if (linelen == 1) { |
| 789 | hdr = 0; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 790 | if ((fmt != CMIT_FMT_ONELINE) && !subject) |
Junio C Hamano | d87449c | 2005-08-08 22:15:40 -0700 | [diff] [blame] | 791 | buf[offset++] = '\n'; |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 792 | continue; |
| 793 | } |
| 794 | if (fmt == CMIT_FMT_RAW) { |
| 795 | memcpy(buf + offset, line, linelen); |
| 796 | offset += linelen; |
| 797 | continue; |
| 798 | } |
Linus Torvalds | 28342a5 | 2005-06-18 13:52:05 -0700 | [diff] [blame] | 799 | if (!memcmp(line, "parent ", 7)) { |
| 800 | if (linelen != 48) |
| 801 | die("bad parent line in commit"); |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 802 | continue; |
Linus Torvalds | 28342a5 | 2005-06-18 13:52:05 -0700 | [diff] [blame] | 803 | } |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 804 | |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 805 | if (!parents_shown) { |
| 806 | offset += add_merge_info(fmt, buf + offset, |
| 807 | commit, abbrev); |
| 808 | parents_shown = 1; |
| 809 | continue; |
| 810 | } |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 811 | /* |
| 812 | * MEDIUM == DEFAULT shows only author with dates. |
| 813 | * FULL shows both authors but not dates. |
| 814 | * FULLER shows both authors and dates. |
| 815 | */ |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 816 | if (!memcmp(line, "author ", 7)) |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 817 | offset += add_user_info("Author", fmt, |
| 818 | buf + offset, |
Jonas Fonseca | 3dfb927 | 2006-08-28 15:52:13 +0200 | [diff] [blame] | 819 | line + 7, |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 820 | relative_date, |
| 821 | encoding); |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 822 | if (!memcmp(line, "committer ", 10) && |
| 823 | (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) |
| 824 | offset += add_user_info("Commit", fmt, |
| 825 | buf + offset, |
Jonas Fonseca | 3dfb927 | 2006-08-28 15:52:13 +0200 | [diff] [blame] | 826 | line + 10, |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 827 | relative_date, |
| 828 | encoding); |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 829 | continue; |
| 830 | } |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 831 | |
Robert Shearman | 19b3bd3 | 2006-07-13 23:17:22 +0100 | [diff] [blame] | 832 | if (!subject) |
| 833 | body = 1; |
| 834 | |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 835 | if (is_empty_line(line, &linelen)) { |
Lars Hjemli | f3a4740 | 2007-01-03 14:34:13 +0100 | [diff] [blame] | 836 | if (!seen_title) |
| 837 | continue; |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 838 | if (!body) |
| 839 | continue; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 840 | if (subject) |
| 841 | continue; |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 842 | if (fmt == CMIT_FMT_SHORT) |
| 843 | break; |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 844 | } |
Junio C Hamano | d87449c | 2005-08-08 22:15:40 -0700 | [diff] [blame] | 845 | |
Lars Hjemli | f3a4740 | 2007-01-03 14:34:13 +0100 | [diff] [blame] | 846 | seen_title = 1; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 847 | if (subject) { |
Junio C Hamano | 53f420e | 2006-04-22 03:06:13 -0700 | [diff] [blame] | 848 | int slen = strlen(subject); |
| 849 | memcpy(buf + offset, subject, slen); |
| 850 | offset += slen; |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 851 | offset += add_rfc2047(buf + offset, line, linelen, |
| 852 | encoding); |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 853 | } |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 854 | else { |
| 855 | memset(buf + offset, ' ', indent); |
| 856 | memcpy(buf + offset + indent, line, linelen); |
| 857 | offset += linelen + indent; |
| 858 | } |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 859 | buf[offset++] = '\n'; |
Junio C Hamano | d87449c | 2005-08-08 22:15:40 -0700 | [diff] [blame] | 860 | if (fmt == CMIT_FMT_ONELINE) |
| 861 | break; |
Junio C Hamano | c831da6 | 2006-05-21 23:55:00 -0700 | [diff] [blame] | 862 | if (subject && plain_non_ascii) { |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 863 | int sz; |
| 864 | char header[512]; |
| 865 | const char *header_fmt = |
| 866 | "Content-Type: text/plain; charset=%s\n" |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 867 | "Content-Transfer-Encoding: 8bit\n"; |
Junio C Hamano | f7e68b2 | 2007-01-12 17:32:38 -0800 | [diff] [blame] | 868 | sz = snprintf(header, sizeof(header), header_fmt, |
| 869 | encoding); |
| 870 | if (sizeof(header) < sz) |
| 871 | die("Encoding name %s too long", encoding); |
| 872 | memcpy(buf + offset, header, sz); |
| 873 | offset += sz; |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 874 | } |
Johannes Schindelin | 698ce6f | 2006-05-20 15:40:29 +0200 | [diff] [blame] | 875 | if (after_subject) { |
| 876 | int slen = strlen(after_subject); |
| 877 | if (slen > space - offset - 1) |
| 878 | slen = space - offset - 1; |
| 879 | memcpy(buf + offset, after_subject, slen); |
| 880 | offset += slen; |
| 881 | after_subject = NULL; |
| 882 | } |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 883 | subject = NULL; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 884 | } |
Linus Torvalds | 40c2fe0 | 2006-04-14 21:20:51 -0700 | [diff] [blame] | 885 | while (offset && isspace(buf[offset-1])) |
| 886 | offset--; |
| 887 | /* Make sure there is an EOLN for the non-oneline case */ |
| 888 | if (fmt != CMIT_FMT_ONELINE) |
| 889 | buf[offset++] = '\n'; |
Robert Shearman | 19b3bd3 | 2006-07-13 23:17:22 +0100 | [diff] [blame] | 890 | /* |
| 891 | * make sure there is another EOLN to separate the headers from whatever |
| 892 | * body the caller appends if we haven't already written a body |
| 893 | */ |
| 894 | if (fmt == CMIT_FMT_EMAIL && !body) |
| 895 | buf[offset++] = '\n'; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 896 | buf[offset] = '\0'; |
Junio C Hamano | 52883fb | 2006-12-25 11:48:35 -0800 | [diff] [blame] | 897 | |
| 898 | free(reencoded); |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 899 | return offset; |
| 900 | } |
jon@blackcubes.dyndns.org | a3437b8 | 2005-06-06 15:39:40 +0000 | [diff] [blame] | 901 | |
| 902 | struct commit *pop_commit(struct commit_list **stack) |
| 903 | { |
| 904 | struct commit_list *top = *stack; |
| 905 | struct commit *item = top ? top->item : NULL; |
| 906 | |
| 907 | if (top) { |
| 908 | *stack = top->next; |
| 909 | free(top); |
| 910 | } |
| 911 | return item; |
| 912 | } |
| 913 | |
| 914 | int count_parents(struct commit * commit) |
| 915 | { |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 916 | int count; |
jon@blackcubes.dyndns.org | a3437b8 | 2005-06-06 15:39:40 +0000 | [diff] [blame] | 917 | struct commit_list * parents = commit->parents; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 918 | for (count = 0; parents; parents = parents->next,count++) |
| 919 | ; |
jon@blackcubes.dyndns.org | a3437b8 | 2005-06-06 15:39:40 +0000 | [diff] [blame] | 920 | return count; |
| 921 | } |
| 922 | |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 923 | void topo_sort_default_setter(struct commit *c, void *data) |
| 924 | { |
Linus Torvalds | d3ff6f5 | 2006-06-17 18:26:18 -0700 | [diff] [blame] | 925 | c->util = data; |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | void *topo_sort_default_getter(struct commit *c) |
| 929 | { |
Linus Torvalds | d3ff6f5 | 2006-06-17 18:26:18 -0700 | [diff] [blame] | 930 | return c->util; |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 931 | } |
| 932 | |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 933 | /* |
| 934 | * Performs an in-place topological sort on the list supplied. |
| 935 | */ |
Junio C Hamano | 4c8725f | 2006-02-15 22:05:33 -0800 | [diff] [blame] | 936 | void sort_in_topological_order(struct commit_list ** list, int lifo) |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 937 | { |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 938 | sort_in_topological_order_fn(list, lifo, topo_sort_default_setter, |
| 939 | topo_sort_default_getter); |
| 940 | } |
| 941 | |
| 942 | void sort_in_topological_order_fn(struct commit_list ** list, int lifo, |
| 943 | topo_sort_set_fn_t setter, |
| 944 | topo_sort_get_fn_t getter) |
| 945 | { |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 946 | struct commit_list * next = *list; |
Linus Torvalds | 2ed0288 | 2005-11-14 10:01:26 -0800 | [diff] [blame] | 947 | struct commit_list * work = NULL, **insert; |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 948 | struct commit_list ** pptr = list; |
| 949 | struct sort_node * nodes; |
| 950 | struct sort_node * next_nodes; |
| 951 | int count = 0; |
| 952 | |
| 953 | /* determine the size of the list */ |
| 954 | while (next) { |
| 955 | next = next->next; |
| 956 | count++; |
| 957 | } |
Eric Wong | 7d6fb37 | 2005-12-24 04:12:43 -0800 | [diff] [blame] | 958 | |
| 959 | if (!count) |
| 960 | return; |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 961 | /* allocate an array to help sort the list */ |
| 962 | nodes = xcalloc(count, sizeof(*nodes)); |
| 963 | /* link the list to the array */ |
| 964 | next_nodes = nodes; |
| 965 | next=*list; |
| 966 | while (next) { |
| 967 | next_nodes->list_item = next; |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 968 | setter(next->item, next_nodes); |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 969 | next_nodes++; |
| 970 | next = next->next; |
| 971 | } |
| 972 | /* update the indegree */ |
| 973 | next=*list; |
| 974 | while (next) { |
| 975 | struct commit_list * parents = next->item->parents; |
| 976 | while (parents) { |
| 977 | struct commit * parent=parents->item; |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 978 | struct sort_node * pn = (struct sort_node *) getter(parent); |
| 979 | |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 980 | if (pn) |
| 981 | pn->indegree++; |
| 982 | parents=parents->next; |
| 983 | } |
| 984 | next=next->next; |
| 985 | } |
| 986 | /* |
| 987 | * find the tips |
| 988 | * |
| 989 | * tips are nodes not reachable from any other node in the list |
| 990 | * |
| 991 | * the tips serve as a starting set for the work queue. |
| 992 | */ |
| 993 | next=*list; |
Linus Torvalds | 2ed0288 | 2005-11-14 10:01:26 -0800 | [diff] [blame] | 994 | insert = &work; |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 995 | while (next) { |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 996 | struct sort_node * node = (struct sort_node *) getter(next->item); |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 997 | |
| 998 | if (node->indegree == 0) { |
Linus Torvalds | 2ed0288 | 2005-11-14 10:01:26 -0800 | [diff] [blame] | 999 | insert = &commit_list_insert(next->item, insert)->next; |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 1000 | } |
| 1001 | next=next->next; |
| 1002 | } |
Junio C Hamano | 4c8725f | 2006-02-15 22:05:33 -0800 | [diff] [blame] | 1003 | |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 1004 | /* process the list in topological order */ |
Junio C Hamano | 4c8725f | 2006-02-15 22:05:33 -0800 | [diff] [blame] | 1005 | if (!lifo) |
| 1006 | sort_by_date(&work); |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 1007 | while (work) { |
| 1008 | struct commit * work_item = pop_commit(&work); |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 1009 | struct sort_node * work_node = (struct sort_node *) getter(work_item); |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 1010 | struct commit_list * parents = work_item->parents; |
| 1011 | |
| 1012 | while (parents) { |
| 1013 | struct commit * parent=parents->item; |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 1014 | struct sort_node * pn = (struct sort_node *) getter(parent); |
| 1015 | |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 1016 | if (pn) { |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 1017 | /* |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 1018 | * parents are only enqueued for emission |
| 1019 | * when all their children have been emitted thereby |
| 1020 | * guaranteeing topological order. |
| 1021 | */ |
| 1022 | pn->indegree--; |
Junio C Hamano | 4c8725f | 2006-02-15 22:05:33 -0800 | [diff] [blame] | 1023 | if (!pn->indegree) { |
| 1024 | if (!lifo) |
| 1025 | insert_by_date(parent, &work); |
| 1026 | else |
| 1027 | commit_list_insert(parent, &work); |
| 1028 | } |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 1029 | } |
| 1030 | parents=parents->next; |
| 1031 | } |
| 1032 | /* |
| 1033 | * work_item is a commit all of whose children |
| 1034 | * have already been emitted. we can emit it now. |
| 1035 | */ |
| 1036 | *pptr = work_node->list_item; |
| 1037 | pptr = &(*pptr)->next; |
| 1038 | *pptr = NULL; |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 1039 | setter(work_item, NULL); |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 1040 | } |
| 1041 | free(nodes); |
| 1042 | } |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1043 | |
Junio C Hamano | 1295228 | 2007-01-08 23:10:49 -0800 | [diff] [blame] | 1044 | /* merge-base stuff */ |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1045 | |
Junio C Hamano | 577ed5c | 2006-10-22 17:32:47 -0700 | [diff] [blame] | 1046 | /* bits #0..15 in revision.h */ |
| 1047 | #define PARENT1 (1u<<16) |
| 1048 | #define PARENT2 (1u<<17) |
| 1049 | #define STALE (1u<<18) |
| 1050 | #define RESULT (1u<<19) |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1051 | |
Junio C Hamano | 1295228 | 2007-01-08 23:10:49 -0800 | [diff] [blame] | 1052 | static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT); |
| 1053 | |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1054 | static struct commit *interesting(struct commit_list *list) |
| 1055 | { |
| 1056 | while (list) { |
| 1057 | struct commit *commit = list->item; |
| 1058 | list = list->next; |
Junio C Hamano | 542ccef | 2006-07-02 11:34:17 -0700 | [diff] [blame] | 1059 | if (commit->object.flags & STALE) |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1060 | continue; |
| 1061 | return commit; |
| 1062 | } |
| 1063 | return NULL; |
| 1064 | } |
| 1065 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1066 | static struct commit_list *merge_bases(struct commit *one, struct commit *two) |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1067 | { |
| 1068 | struct commit_list *list = NULL; |
| 1069 | struct commit_list *result = NULL; |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1070 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1071 | if (one == two) |
| 1072 | /* We do not mark this even with RESULT so we do not |
| 1073 | * have to clean it up. |
| 1074 | */ |
| 1075 | return commit_list_insert(one, &result); |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1076 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1077 | parse_commit(one); |
| 1078 | parse_commit(two); |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1079 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1080 | one->object.flags |= PARENT1; |
| 1081 | two->object.flags |= PARENT2; |
| 1082 | insert_by_date(one, &list); |
| 1083 | insert_by_date(two, &list); |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1084 | |
| 1085 | while (interesting(list)) { |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1086 | struct commit *commit; |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1087 | struct commit_list *parents; |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1088 | struct commit_list *n; |
| 1089 | int flags; |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1090 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1091 | commit = list->item; |
| 1092 | n = list->next; |
| 1093 | free(list); |
| 1094 | list = n; |
| 1095 | |
| 1096 | flags = commit->object.flags & (PARENT1 | PARENT2 | STALE); |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1097 | if (flags == (PARENT1 | PARENT2)) { |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1098 | if (!(commit->object.flags & RESULT)) { |
| 1099 | commit->object.flags |= RESULT; |
| 1100 | insert_by_date(commit, &result); |
| 1101 | } |
Junio C Hamano | 542ccef | 2006-07-02 11:34:17 -0700 | [diff] [blame] | 1102 | /* Mark parents of a found merge stale */ |
| 1103 | flags |= STALE; |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1104 | } |
| 1105 | parents = commit->parents; |
| 1106 | while (parents) { |
| 1107 | struct commit *p = parents->item; |
| 1108 | parents = parents->next; |
| 1109 | if ((p->object.flags & flags) == flags) |
| 1110 | continue; |
| 1111 | parse_commit(p); |
| 1112 | p->object.flags |= flags; |
| 1113 | insert_by_date(p, &list); |
| 1114 | } |
| 1115 | } |
| 1116 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1117 | /* Clean up the result to remove stale ones */ |
Junio C Hamano | 1295228 | 2007-01-08 23:10:49 -0800 | [diff] [blame] | 1118 | free_commit_list(list); |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1119 | list = result; result = NULL; |
| 1120 | while (list) { |
| 1121 | struct commit_list *n = list->next; |
| 1122 | if (!(list->item->object.flags & STALE)) |
| 1123 | insert_by_date(list->item, &result); |
| 1124 | free(list); |
| 1125 | list = n; |
| 1126 | } |
| 1127 | return result; |
| 1128 | } |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1129 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1130 | struct commit_list *get_merge_bases(struct commit *one, |
| 1131 | struct commit *two, |
| 1132 | int cleanup) |
| 1133 | { |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1134 | struct commit_list *list; |
| 1135 | struct commit **rslt; |
| 1136 | struct commit_list *result; |
| 1137 | int cnt, i, j; |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1138 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1139 | result = merge_bases(one, two); |
| 1140 | if (one == two) |
| 1141 | return result; |
| 1142 | if (!result || !result->next) { |
| 1143 | if (cleanup) { |
| 1144 | clear_commit_marks(one, all_flags); |
| 1145 | clear_commit_marks(two, all_flags); |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1146 | } |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1147 | return result; |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1150 | /* There are more than one */ |
| 1151 | cnt = 0; |
| 1152 | list = result; |
| 1153 | while (list) { |
| 1154 | list = list->next; |
| 1155 | cnt++; |
| 1156 | } |
| 1157 | rslt = xcalloc(cnt, sizeof(*rslt)); |
| 1158 | for (list = result, i = 0; list; list = list->next) |
| 1159 | rslt[i++] = list->item; |
| 1160 | free_commit_list(result); |
| 1161 | |
| 1162 | clear_commit_marks(one, all_flags); |
| 1163 | clear_commit_marks(two, all_flags); |
| 1164 | for (i = 0; i < cnt - 1; i++) { |
| 1165 | for (j = i+1; j < cnt; j++) { |
| 1166 | if (!rslt[i] || !rslt[j]) |
| 1167 | continue; |
| 1168 | result = merge_bases(rslt[i], rslt[j]); |
| 1169 | clear_commit_marks(rslt[i], all_flags); |
| 1170 | clear_commit_marks(rslt[j], all_flags); |
| 1171 | for (list = result; list; list = list->next) { |
| 1172 | if (rslt[i] == list->item) |
| 1173 | rslt[i] = NULL; |
| 1174 | if (rslt[j] == list->item) |
| 1175 | rslt[j] = NULL; |
| 1176 | } |
| 1177 | } |
Junio C Hamano | 58ecf5c | 2006-07-04 17:45:22 -0700 | [diff] [blame] | 1178 | } |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1179 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1180 | /* Surviving ones in rslt[] are the independent results */ |
| 1181 | result = NULL; |
| 1182 | for (i = 0; i < cnt; i++) { |
| 1183 | if (rslt[i]) |
| 1184 | insert_by_date(rslt[i], &result); |
| 1185 | } |
| 1186 | free(rslt); |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1187 | return result; |
| 1188 | } |
Junio C Hamano | 2ecd2bb | 2006-12-19 00:14:04 -0800 | [diff] [blame] | 1189 | |
| 1190 | int in_merge_bases(struct commit *rev1, struct commit *rev2) |
| 1191 | { |
| 1192 | struct commit_list *bases, *b; |
| 1193 | int ret = 0; |
| 1194 | |
| 1195 | bases = get_merge_bases(rev1, rev2, 1); |
| 1196 | for (b = bases; b; b = b->next) { |
| 1197 | if (!hashcmp(rev1->object.sha1, b->item->object.sha1)) { |
| 1198 | ret = 1; |
| 1199 | break; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | free_commit_list(bases); |
| 1204 | return ret; |
| 1205 | } |