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