| Junio C Hamano | 8f1d2e6 | 2006-01-07 09:33:54 | [diff] [blame] | 1 | #include "cache.h" |
| Linus Torvalds | 961784e | 2005-05-18 23:14:22 | [diff] [blame] | 2 | #include "tag.h" |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 3 | #include "commit.h" |
| Johannes Schindelin | ed09aef | 2006-10-30 19:09:06 | [diff] [blame] | 4 | #include "pkt-line.h" |
| Junio C Hamano | 52883fb | 2006-12-25 19:48:35 | [diff] [blame] | 5 | #include "utf8.h" |
| Junio C Hamano | 199c45b | 2007-04-09 09:34:05 | [diff] [blame] | 6 | #include "diff.h" |
| 7 | #include "revision.h" |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 8 | |
| Linus Torvalds | 60ab26d | 2005-09-15 21:43:17 | [diff] [blame] | 9 | int save_commit_buffer = 1; |
| 10 | |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 11 | const char *commit_type = "commit"; |
| 12 | |
| Junio C Hamano | f76412e | 2005-08-21 09:51:10 | [diff] [blame] | 13 | static struct commit *check_commit(struct object *obj, |
| 14 | const unsigned char *sha1, |
| 15 | int quiet) |
| Linus Torvalds | 961784e | 2005-05-18 23:14:22 | [diff] [blame] | 16 | { |
| Linus Torvalds | 1974632 | 2006-07-12 03:45:31 | [diff] [blame] | 17 | if (obj->type != OBJ_COMMIT) { |
| Junio C Hamano | f76412e | 2005-08-21 09:51:10 | [diff] [blame] | 18 | if (!quiet) |
| 19 | error("Object %s is a %s, not a commit", |
| Linus Torvalds | 885a86a | 2006-06-14 23:45:13 | [diff] [blame] | 20 | sha1_to_hex(sha1), typename(obj->type)); |
| Linus Torvalds | 961784e | 2005-05-18 23:14:22 | [diff] [blame] | 21 | return NULL; |
| 22 | } |
| 23 | return (struct commit *) obj; |
| 24 | } |
| 25 | |
| Junio C Hamano | f76412e | 2005-08-21 09:51:10 | [diff] [blame] | 26 | struct commit *lookup_commit_reference_gently(const unsigned char *sha1, |
| 27 | int quiet) |
| Linus Torvalds | 961784e | 2005-05-18 23:14:22 | [diff] [blame] | 28 | { |
| Junio C Hamano | 9534f40 | 2005-11-02 23:19:13 | [diff] [blame] | 29 | struct object *obj = deref_tag(parse_object(sha1), NULL, 0); |
| Linus Torvalds | 961784e | 2005-05-18 23:14:22 | [diff] [blame] | 30 | |
| 31 | if (!obj) |
| 32 | return NULL; |
| Junio C Hamano | f76412e | 2005-08-21 09:51:10 | [diff] [blame] | 33 | return check_commit(obj, sha1, quiet); |
| 34 | } |
| 35 | |
| 36 | struct commit *lookup_commit_reference(const unsigned char *sha1) |
| 37 | { |
| 38 | return lookup_commit_reference_gently(sha1, 0); |
| Linus Torvalds | 961784e | 2005-05-18 23:14:22 | [diff] [blame] | 39 | } |
| 40 | |
| Jason McMullan | 5d6ccf5 | 2005-06-03 15:05:39 | [diff] [blame] | 41 | struct commit *lookup_commit(const unsigned char *sha1) |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 42 | { |
| 43 | struct object *obj = lookup_object(sha1); |
| Linus Torvalds | 100c5f3 | 2007-04-17 05:11:43 | [diff] [blame] | 44 | if (!obj) |
| 45 | return create_object(sha1, OBJ_COMMIT, alloc_commit_node()); |
| Nicolas Pitre | d1af002 | 2005-05-20 20:59:17 | [diff] [blame] | 46 | if (!obj->type) |
| Linus Torvalds | 1974632 | 2006-07-12 03:45:31 | [diff] [blame] | 47 | obj->type = OBJ_COMMIT; |
| Junio C Hamano | f76412e | 2005-08-21 09:51:10 | [diff] [blame] | 48 | return check_commit(obj, sha1, 0); |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 49 | } |
| 50 | |
| Martin Koegler | 0a61779 | 2008-01-19 17:35:23 | [diff] [blame] | 51 | static unsigned long parse_commit_date(const char *buf, const char *tail) |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 52 | { |
| Martin Koegler | 0a61779 | 2008-01-19 17:35:23 | [diff] [blame] | 53 | const char *dateptr; |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 54 | |
| Martin Koegler | 0a61779 | 2008-01-19 17:35:23 | [diff] [blame] | 55 | if (buf + 6 >= tail) |
| 56 | return 0; |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 57 | if (memcmp(buf, "author", 6)) |
| 58 | return 0; |
| Martin Koegler | 0a61779 | 2008-01-19 17:35:23 | [diff] [blame] | 59 | while (buf < tail && *buf++ != '\n') |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 60 | /* nada */; |
| Martin Koegler | 0a61779 | 2008-01-19 17:35:23 | [diff] [blame] | 61 | if (buf + 9 >= tail) |
| 62 | return 0; |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 63 | if (memcmp(buf, "committer", 9)) |
| 64 | return 0; |
| Martin Koegler | 0a61779 | 2008-01-19 17:35:23 | [diff] [blame] | 65 | while (buf < tail && *buf++ != '>') |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 66 | /* nada */; |
| Martin Koegler | 0a61779 | 2008-01-19 17:35:23 | [diff] [blame] | 67 | if (buf >= tail) |
| 68 | return 0; |
| 69 | dateptr = buf; |
| 70 | while (buf < tail && *buf++ != '\n') |
| 71 | /* nada */; |
| 72 | if (buf >= tail) |
| 73 | return 0; |
| 74 | /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */ |
| Eric Wong | f290974 | 2009-07-03 04:34:54 | [diff] [blame] | 75 | return strtoul(dateptr, NULL, 10); |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 76 | } |
| 77 | |
| Junio C Hamano | 5040f17 | 2006-04-07 06:58:51 | [diff] [blame] | 78 | static struct commit_graft **commit_graft; |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 79 | static int commit_graft_alloc, commit_graft_nr; |
| 80 | |
| 81 | static int commit_graft_pos(const unsigned char *sha1) |
| 82 | { |
| 83 | int lo, hi; |
| 84 | lo = 0; |
| 85 | hi = commit_graft_nr; |
| 86 | while (lo < hi) { |
| 87 | int mi = (lo + hi) / 2; |
| 88 | struct commit_graft *graft = commit_graft[mi]; |
| David Rientjes | a89fccd | 2006-08-17 18:54:57 | [diff] [blame] | 89 | int cmp = hashcmp(sha1, graft->sha1); |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 90 | if (!cmp) |
| 91 | return mi; |
| 92 | if (cmp < 0) |
| 93 | hi = mi; |
| 94 | else |
| 95 | lo = mi + 1; |
| 96 | } |
| 97 | return -lo - 1; |
| 98 | } |
| 99 | |
| Junio C Hamano | 5040f17 | 2006-04-07 06:58:51 | [diff] [blame] | 100 | int register_commit_graft(struct commit_graft *graft, int ignore_dups) |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 101 | { |
| Junio C Hamano | 5040f17 | 2006-04-07 06:58:51 | [diff] [blame] | 102 | int pos = commit_graft_pos(graft->sha1); |
| Junio C Hamano | a6080a0 | 2007-06-07 07:04:01 | [diff] [blame] | 103 | |
| Junio C Hamano | 5040f17 | 2006-04-07 06:58:51 | [diff] [blame] | 104 | if (0 <= pos) { |
| 105 | if (ignore_dups) |
| 106 | free(graft); |
| 107 | else { |
| 108 | free(commit_graft[pos]); |
| 109 | commit_graft[pos] = graft; |
| 110 | } |
| 111 | return 1; |
| 112 | } |
| 113 | pos = -pos - 1; |
| 114 | if (commit_graft_alloc <= ++commit_graft_nr) { |
| 115 | commit_graft_alloc = alloc_nr(commit_graft_alloc); |
| 116 | commit_graft = xrealloc(commit_graft, |
| 117 | sizeof(*commit_graft) * |
| 118 | commit_graft_alloc); |
| 119 | } |
| 120 | if (pos < commit_graft_nr) |
| 121 | memmove(commit_graft + pos + 1, |
| 122 | commit_graft + pos, |
| 123 | (commit_graft_nr - pos - 1) * |
| 124 | sizeof(*commit_graft)); |
| 125 | commit_graft[pos] = graft; |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | struct commit_graft *read_graft_line(char *buf, int len) |
| 130 | { |
| 131 | /* The format is just "Commit Parent1 Parent2 ...\n" */ |
| 132 | int i; |
| 133 | struct commit_graft *graft = NULL; |
| 134 | |
| 135 | if (buf[len-1] == '\n') |
| 136 | buf[--len] = 0; |
| Yann Dirson | 360204c | 2006-04-17 11:41:49 | [diff] [blame] | 137 | if (buf[0] == '#' || buf[0] == '\0') |
| Junio C Hamano | 5bc4ce5 | 2006-04-16 21:24:56 | [diff] [blame] | 138 | return NULL; |
| Junio C Hamano | 5040f17 | 2006-04-07 06:58:51 | [diff] [blame] | 139 | if ((len + 1) % 41) { |
| 140 | bad_graft_data: |
| 141 | error("bad graft data: %s", buf); |
| 142 | free(graft); |
| 143 | return NULL; |
| 144 | } |
| 145 | i = (len + 1) / 41 - 1; |
| 146 | graft = xmalloc(sizeof(*graft) + 20 * i); |
| 147 | graft->nr_parent = i; |
| 148 | if (get_sha1_hex(buf, graft->sha1)) |
| 149 | goto bad_graft_data; |
| 150 | for (i = 40; i < len; i += 41) { |
| 151 | if (buf[i] != ' ') |
| 152 | goto bad_graft_data; |
| 153 | if (get_sha1_hex(buf + i + 1, graft->parent[i/41])) |
| 154 | goto bad_graft_data; |
| 155 | } |
| 156 | return graft; |
| 157 | } |
| 158 | |
| Nanako Shiraishi | c5ae643 | 2008-10-02 10:14:30 | [diff] [blame] | 159 | static int read_graft_file(const char *graft_file) |
| Junio C Hamano | 5040f17 | 2006-04-07 06:58:51 | [diff] [blame] | 160 | { |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 161 | FILE *fp = fopen(graft_file, "r"); |
| 162 | char buf[1024]; |
| Junio C Hamano | 5040f17 | 2006-04-07 06:58:51 | [diff] [blame] | 163 | if (!fp) |
| 164 | return -1; |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 165 | while (fgets(buf, sizeof(buf), fp)) { |
| 166 | /* The format is just "Commit Parent1 Parent2 ...\n" */ |
| 167 | int len = strlen(buf); |
| Junio C Hamano | 5040f17 | 2006-04-07 06:58:51 | [diff] [blame] | 168 | struct commit_graft *graft = read_graft_line(buf, len); |
| Junio C Hamano | 5bc4ce5 | 2006-04-16 21:24:56 | [diff] [blame] | 169 | if (!graft) |
| 170 | continue; |
| Junio C Hamano | 5040f17 | 2006-04-07 06:58:51 | [diff] [blame] | 171 | if (register_commit_graft(graft, 1)) |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 172 | error("duplicate graft data: %s", buf); |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 173 | } |
| 174 | fclose(fp); |
| Junio C Hamano | 5040f17 | 2006-04-07 06:58:51 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static void prepare_commit_graft(void) |
| 179 | { |
| 180 | static int commit_graft_prepared; |
| 181 | char *graft_file; |
| 182 | |
| 183 | if (commit_graft_prepared) |
| 184 | return; |
| 185 | graft_file = get_graft_file(); |
| 186 | read_graft_file(graft_file); |
| Johannes Schindelin | ed09aef | 2006-10-30 19:09:06 | [diff] [blame] | 187 | /* make sure shallows are read */ |
| 188 | is_repository_shallow(); |
| Junio C Hamano | 5040f17 | 2006-04-07 06:58:51 | [diff] [blame] | 189 | commit_graft_prepared = 1; |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 190 | } |
| 191 | |
| Martin Koegler | 4516338 | 2008-02-25 21:46:07 | [diff] [blame] | 192 | struct commit_graft *lookup_commit_graft(const unsigned char *sha1) |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 193 | { |
| 194 | int pos; |
| Junio C Hamano | 5040f17 | 2006-04-07 06:58:51 | [diff] [blame] | 195 | prepare_commit_graft(); |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 196 | pos = commit_graft_pos(sha1); |
| 197 | if (pos < 0) |
| 198 | return NULL; |
| 199 | return commit_graft[pos]; |
| 200 | } |
| 201 | |
| Johannes Schindelin | ed09aef | 2006-10-30 19:09:06 | [diff] [blame] | 202 | int write_shallow_commits(int fd, int use_pack_protocol) |
| 203 | { |
| 204 | int i, count = 0; |
| 205 | for (i = 0; i < commit_graft_nr; i++) |
| 206 | if (commit_graft[i]->nr_parent < 0) { |
| 207 | const char *hex = |
| 208 | sha1_to_hex(commit_graft[i]->sha1); |
| 209 | count++; |
| 210 | if (use_pack_protocol) |
| 211 | packet_write(fd, "shallow %s", hex); |
| 212 | else { |
| Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 | [diff] [blame] | 213 | if (write_in_full(fd, hex, 40) != 40) |
| 214 | break; |
| 215 | if (write_in_full(fd, "\n", 1) != 1) |
| 216 | break; |
| Johannes Schindelin | ed09aef | 2006-10-30 19:09:06 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | return count; |
| 220 | } |
| 221 | |
| Johannes Schindelin | f53514b | 2006-10-30 19:09:53 | [diff] [blame] | 222 | int unregister_shallow(const unsigned char *sha1) |
| 223 | { |
| 224 | int pos = commit_graft_pos(sha1); |
| 225 | if (pos < 0) |
| 226 | return -1; |
| 227 | if (pos + 1 < commit_graft_nr) |
| 228 | memcpy(commit_graft + pos, commit_graft + pos + 1, |
| 229 | sizeof(struct commit_graft *) |
| 230 | * (commit_graft_nr - pos - 1)); |
| 231 | commit_graft_nr--; |
| 232 | return 0; |
| 233 | } |
| 234 | |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 235 | int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size) |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 236 | { |
| Junio C Hamano | 3b44f15 | 2006-06-28 10:51:00 | [diff] [blame] | 237 | char *tail = buffer; |
| Linus Torvalds | f7cc77d | 2005-07-27 22:12:48 | [diff] [blame] | 238 | char *bufptr = buffer; |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 239 | unsigned char parent[20]; |
| Linus Torvalds | 6c88be1 | 2005-06-21 03:26:03 | [diff] [blame] | 240 | struct commit_list **pptr; |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 241 | struct commit_graft *graft; |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 242 | |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 243 | if (item->object.parsed) |
| 244 | return 0; |
| 245 | item->object.parsed = 1; |
| Junio C Hamano | 3b44f15 | 2006-06-28 10:51:00 | [diff] [blame] | 246 | tail += size; |
| Martin Koegler | 0a61779 | 2008-01-19 17:35:23 | [diff] [blame] | 247 | if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n') |
| Linus Torvalds | f7cc77d | 2005-07-27 22:12:48 | [diff] [blame] | 248 | return error("bogus commit object %s", sha1_to_hex(item->object.sha1)); |
| Martin Koegler | 0a61779 | 2008-01-19 17:35:23 | [diff] [blame] | 249 | if (get_sha1_hex(bufptr + 5, parent) < 0) |
| Junio C Hamano | bd2afde | 2006-02-23 01:47:10 | [diff] [blame] | 250 | return error("bad tree pointer in commit %s", |
| 251 | sha1_to_hex(item->object.sha1)); |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 252 | item->tree = lookup_tree(parent); |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 253 | bufptr += 46; /* "tree " + "hex sha1" + "\n" */ |
| Linus Torvalds | 6c88be1 | 2005-06-21 03:26:03 | [diff] [blame] | 254 | pptr = &item->parents; |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 255 | |
| 256 | graft = lookup_commit_graft(item->object.sha1); |
| Junio C Hamano | 3b44f15 | 2006-06-28 10:51:00 | [diff] [blame] | 257 | while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) { |
| Linus Torvalds | f7cc77d | 2005-07-27 22:12:48 | [diff] [blame] | 258 | struct commit *new_parent; |
| 259 | |
| Junio C Hamano | 3b44f15 | 2006-06-28 10:51:00 | [diff] [blame] | 260 | if (tail <= bufptr + 48 || |
| 261 | get_sha1_hex(bufptr + 7, parent) || |
| 262 | bufptr[47] != '\n') |
| Linus Torvalds | f7cc77d | 2005-07-27 22:12:48 | [diff] [blame] | 263 | return error("bad parents in commit %s", sha1_to_hex(item->object.sha1)); |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 264 | bufptr += 48; |
| 265 | if (graft) |
| 266 | continue; |
| Linus Torvalds | f7cc77d | 2005-07-27 22:12:48 | [diff] [blame] | 267 | new_parent = lookup_commit(parent); |
| Miklos Vajna | 39468de | 2008-06-07 20:38:37 | [diff] [blame] | 268 | if (new_parent) |
| Linus Torvalds | 6c88be1 | 2005-06-21 03:26:03 | [diff] [blame] | 269 | pptr = &commit_list_insert(new_parent, pptr)->next; |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 270 | } |
| 271 | if (graft) { |
| 272 | int i; |
| 273 | struct commit *new_parent; |
| 274 | for (i = 0; i < graft->nr_parent; i++) { |
| 275 | new_parent = lookup_commit(graft->parent[i]); |
| 276 | if (!new_parent) |
| 277 | continue; |
| 278 | pptr = &commit_list_insert(new_parent, pptr)->next; |
| Junio C Hamano | 5da5c8f | 2005-07-30 07:58:28 | [diff] [blame] | 279 | } |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 280 | } |
| Martin Koegler | 0a61779 | 2008-01-19 17:35:23 | [diff] [blame] | 281 | item->date = parse_commit_date(bufptr, tail); |
| Junio C Hamano | 27dedf0 | 2005-11-17 05:32:44 | [diff] [blame] | 282 | |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 286 | int parse_commit(struct commit *item) |
| 287 | { |
| Nicolas Pitre | 21666f1 | 2007-02-26 19:55:59 | [diff] [blame] | 288 | enum object_type type; |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 289 | void *buffer; |
| 290 | unsigned long size; |
| 291 | int ret; |
| 292 | |
| Martin Koegler | 9786f68 | 2008-02-18 20:48:02 | [diff] [blame] | 293 | if (!item) |
| 294 | return -1; |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 295 | if (item->object.parsed) |
| 296 | return 0; |
| Nicolas Pitre | 21666f1 | 2007-02-26 19:55:59 | [diff] [blame] | 297 | buffer = read_sha1_file(item->object.sha1, &type, &size); |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 298 | if (!buffer) |
| 299 | return error("Could not read %s", |
| 300 | sha1_to_hex(item->object.sha1)); |
| Nicolas Pitre | 21666f1 | 2007-02-26 19:55:59 | [diff] [blame] | 301 | if (type != OBJ_COMMIT) { |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 302 | free(buffer); |
| 303 | return error("Object %s not a commit", |
| 304 | sha1_to_hex(item->object.sha1)); |
| 305 | } |
| 306 | ret = parse_commit_buffer(item, buffer, size); |
| Linus Torvalds | 60ab26d | 2005-09-15 21:43:17 | [diff] [blame] | 307 | if (save_commit_buffer && !ret) { |
| Linus Torvalds | 3ff1fbb | 2005-05-26 01:27:14 | [diff] [blame] | 308 | item->buffer = buffer; |
| 309 | return 0; |
| 310 | } |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 311 | free(buffer); |
| 312 | return ret; |
| 313 | } |
| 314 | |
| Linus Torvalds | ac5155e | 2005-05-31 01:44:02 | [diff] [blame] | 315 | struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p) |
| Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 316 | { |
| Christopher Li | 812666c | 2005-04-26 19:00:58 | [diff] [blame] | 317 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
| Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 318 | new_list->item = item; |
| 319 | new_list->next = *list_p; |
| 320 | *list_p = new_list; |
| Linus Torvalds | ac5155e | 2005-05-31 01:44:02 | [diff] [blame] | 321 | return new_list; |
| Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 322 | } |
| 323 | |
| Miklos Vajna | 6531947 | 2008-06-27 16:21:55 | [diff] [blame] | 324 | unsigned commit_list_count(const struct commit_list *l) |
| 325 | { |
| 326 | unsigned c = 0; |
| 327 | for (; l; l = l->next ) |
| 328 | c++; |
| 329 | return c; |
| 330 | } |
| 331 | |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 332 | void free_commit_list(struct commit_list *list) |
| 333 | { |
| 334 | while (list) { |
| 335 | struct commit_list *temp = list; |
| 336 | list = temp->next; |
| 337 | free(temp); |
| 338 | } |
| 339 | } |
| Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 340 | |
| Linus Torvalds | f755494 | 2005-07-06 16:31:17 | [diff] [blame] | 341 | struct commit_list * insert_by_date(struct commit *item, struct commit_list **list) |
| Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 342 | { |
| 343 | struct commit_list **pp = list; |
| 344 | struct commit_list *p; |
| 345 | while ((p = *pp) != NULL) { |
| 346 | if (p->item->date < item->date) { |
| 347 | break; |
| 348 | } |
| 349 | pp = &p->next; |
| 350 | } |
| Linus Torvalds | f755494 | 2005-07-06 16:31:17 | [diff] [blame] | 351 | return commit_list_insert(item, pp); |
| Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 352 | } |
| 353 | |
| Junio C Hamano | a6080a0 | 2007-06-07 07:04:01 | [diff] [blame] | 354 | |
| Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 355 | void sort_by_date(struct commit_list **list) |
| 356 | { |
| 357 | struct commit_list *ret = NULL; |
| 358 | while (*list) { |
| Linus Torvalds | f755494 | 2005-07-06 16:31:17 | [diff] [blame] | 359 | insert_by_date((*list)->item, &ret); |
| Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 360 | *list = (*list)->next; |
| 361 | } |
| 362 | *list = ret; |
| 363 | } |
| 364 | |
| Daniel Barkalow | 58e28af | 2005-04-24 03:29:22 | [diff] [blame] | 365 | struct commit *pop_most_recent_commit(struct commit_list **list, |
| 366 | unsigned int mark) |
| Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 367 | { |
| 368 | struct commit *ret = (*list)->item; |
| 369 | struct commit_list *parents = ret->parents; |
| 370 | struct commit_list *old = *list; |
| 371 | |
| 372 | *list = (*list)->next; |
| 373 | free(old); |
| 374 | |
| 375 | while (parents) { |
| Linus Torvalds | 4056c09 | 2005-04-24 02:21:28 | [diff] [blame] | 376 | struct commit *commit = parents->item; |
| Martin Koegler | dec38c8 | 2008-02-18 20:48:03 | [diff] [blame] | 377 | if (!parse_commit(commit) && !(commit->object.flags & mark)) { |
| Daniel Barkalow | 58e28af | 2005-04-24 03:29:22 | [diff] [blame] | 378 | commit->object.flags |= mark; |
| Linus Torvalds | f755494 | 2005-07-06 16:31:17 | [diff] [blame] | 379 | insert_by_date(commit, list); |
| Linus Torvalds | 4056c09 | 2005-04-24 02:21:28 | [diff] [blame] | 380 | } |
| Daniel Barkalow | dd97f85 | 2005-04-24 01:47:23 | [diff] [blame] | 381 | parents = parents->next; |
| 382 | } |
| 383 | return ret; |
| 384 | } |
| Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 385 | |
| Junio C Hamano | f8f9c73 | 2006-01-08 02:52:42 | [diff] [blame] | 386 | void clear_commit_marks(struct commit *commit, unsigned int mark) |
| 387 | { |
| Johannes Schindelin | 60fcc2e | 2007-10-10 22:14:35 | [diff] [blame] | 388 | while (commit) { |
| 389 | struct commit_list *parents; |
| Junio C Hamano | f8f9c73 | 2006-01-08 02:52:42 | [diff] [blame] | 390 | |
| Johannes Schindelin | 60fcc2e | 2007-10-10 22:14:35 | [diff] [blame] | 391 | if (!(mark & commit->object.flags)) |
| 392 | return; |
| Junio C Hamano | 58ecf5c | 2006-07-05 00:45:22 | [diff] [blame] | 393 | |
| Johannes Schindelin | 60fcc2e | 2007-10-10 22:14:35 | [diff] [blame] | 394 | commit->object.flags &= ~mark; |
| 395 | |
| 396 | parents = commit->parents; |
| 397 | if (!parents) |
| 398 | return; |
| 399 | |
| 400 | while ((parents = parents->next)) |
| 401 | clear_commit_marks(parents->item, mark); |
| 402 | |
| 403 | commit = commit->parents->item; |
| Junio C Hamano | f8f9c73 | 2006-01-08 02:52:42 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| jon@blackcubes.dyndns.org | a3437b8 | 2005-06-06 15:39:40 | [diff] [blame] | 407 | struct commit *pop_commit(struct commit_list **stack) |
| 408 | { |
| 409 | struct commit_list *top = *stack; |
| 410 | struct commit *item = top ? top->item : NULL; |
| 411 | |
| 412 | if (top) { |
| 413 | *stack = top->next; |
| 414 | free(top); |
| 415 | } |
| 416 | return item; |
| 417 | } |
| 418 | |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 419 | /* |
| 420 | * Performs an in-place topological sort on the list supplied. |
| 421 | */ |
| Junio C Hamano | 4c8725f | 2006-02-16 06:05:33 | [diff] [blame] | 422 | void sort_in_topological_order(struct commit_list ** list, int lifo) |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 423 | { |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 424 | struct commit_list *next, *orig = *list; |
| 425 | struct commit_list *work, **insert; |
| 426 | struct commit_list **pptr; |
| Fredrik Kuivinen | 6b6dcfc | 2006-03-10 09:21:37 | [diff] [blame] | 427 | |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 428 | if (!orig) |
| Eric Wong | 7d6fb37 | 2005-12-24 12:12:43 | [diff] [blame] | 429 | return; |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 430 | *list = NULL; |
| 431 | |
| 432 | /* Mark them and clear the indegree */ |
| 433 | for (next = orig; next; next = next->next) { |
| 434 | struct commit *commit = next->item; |
| Johannes Schindelin | e358f3c | 2008-07-23 00:51:36 | [diff] [blame] | 435 | commit->indegree = 1; |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 436 | } |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 437 | |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 438 | /* update the indegree */ |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 439 | for (next = orig; next; next = next->next) { |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 440 | struct commit_list * parents = next->item->parents; |
| 441 | while (parents) { |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 442 | struct commit *parent = parents->item; |
| Fredrik Kuivinen | 6b6dcfc | 2006-03-10 09:21:37 | [diff] [blame] | 443 | |
| Johannes Schindelin | e358f3c | 2008-07-23 00:51:36 | [diff] [blame] | 444 | if (parent->indegree) |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 445 | parent->indegree++; |
| 446 | parents = parents->next; |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 447 | } |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 448 | } |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 449 | |
| Junio C Hamano | a6080a0 | 2007-06-07 07:04:01 | [diff] [blame] | 450 | /* |
| Pierre Habouzit | 674d172 | 2007-09-10 10:35:06 | [diff] [blame] | 451 | * find the tips |
| 452 | * |
| 453 | * tips are nodes not reachable from any other node in the list |
| 454 | * |
| 455 | * the tips serve as a starting set for the work queue. |
| 456 | */ |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 457 | work = NULL; |
| Linus Torvalds | 2ed0288 | 2005-11-14 18:01:26 | [diff] [blame] | 458 | insert = &work; |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 459 | for (next = orig; next; next = next->next) { |
| 460 | struct commit *commit = next->item; |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 461 | |
| Johannes Schindelin | e358f3c | 2008-07-23 00:51:36 | [diff] [blame] | 462 | if (commit->indegree == 1) |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 463 | insert = &commit_list_insert(commit, insert)->next; |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 464 | } |
| Junio C Hamano | 4c8725f | 2006-02-16 06:05:33 | [diff] [blame] | 465 | |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 466 | /* process the list in topological order */ |
| Junio C Hamano | 4c8725f | 2006-02-16 06:05:33 | [diff] [blame] | 467 | if (!lifo) |
| 468 | sort_by_date(&work); |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 469 | |
| 470 | pptr = list; |
| 471 | *list = NULL; |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 472 | while (work) { |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 473 | struct commit *commit; |
| 474 | struct commit_list *parents, *work_item; |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 475 | |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 476 | work_item = work; |
| 477 | work = work_item->next; |
| 478 | work_item->next = NULL; |
| Fredrik Kuivinen | 6b6dcfc | 2006-03-10 09:21:37 | [diff] [blame] | 479 | |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 480 | commit = work_item->item; |
| 481 | for (parents = commit->parents; parents ; parents = parents->next) { |
| 482 | struct commit *parent=parents->item; |
| 483 | |
| Johannes Schindelin | e358f3c | 2008-07-23 00:51:36 | [diff] [blame] | 484 | if (!parent->indegree) |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 485 | continue; |
| 486 | |
| 487 | /* |
| 488 | * parents are only enqueued for emission |
| 489 | * when all their children have been emitted thereby |
| 490 | * guaranteeing topological order. |
| 491 | */ |
| Johannes Schindelin | e358f3c | 2008-07-23 00:51:36 | [diff] [blame] | 492 | if (--parent->indegree == 1) { |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 493 | if (!lifo) |
| 494 | insert_by_date(parent, &work); |
| 495 | else |
| 496 | commit_list_insert(parent, &work); |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 497 | } |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 498 | } |
| 499 | /* |
| Pierre Habouzit | 674d172 | 2007-09-10 10:35:06 | [diff] [blame] | 500 | * work_item is a commit all of whose children |
| 501 | * have already been emitted. we can emit it now. |
| 502 | */ |
| Johannes Schindelin | e358f3c | 2008-07-23 00:51:36 | [diff] [blame] | 503 | commit->indegree = 0; |
| Linus Torvalds | 23c17d4 | 2007-11-02 20:32:58 | [diff] [blame] | 504 | *pptr = work_item; |
| 505 | pptr = &work_item->next; |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 506 | } |
| Jon Seymour | ab580ac | 2005-07-06 16:39:34 | [diff] [blame] | 507 | } |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 508 | |
| Junio C Hamano | 1295228 | 2007-01-09 07:10:49 | [diff] [blame] | 509 | /* merge-base stuff */ |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 510 | |
| Junio C Hamano | 577ed5c | 2006-10-23 00:32:47 | [diff] [blame] | 511 | /* bits #0..15 in revision.h */ |
| 512 | #define PARENT1 (1u<<16) |
| 513 | #define PARENT2 (1u<<17) |
| 514 | #define STALE (1u<<18) |
| 515 | #define RESULT (1u<<19) |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 516 | |
| Junio C Hamano | 1295228 | 2007-01-09 07:10:49 | [diff] [blame] | 517 | static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT); |
| 518 | |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 519 | static struct commit *interesting(struct commit_list *list) |
| 520 | { |
| 521 | while (list) { |
| 522 | struct commit *commit = list->item; |
| 523 | list = list->next; |
| Junio C Hamano | 542ccef | 2006-07-02 18:34:17 | [diff] [blame] | 524 | if (commit->object.flags & STALE) |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 525 | continue; |
| 526 | return commit; |
| 527 | } |
| 528 | return NULL; |
| 529 | } |
| 530 | |
| Junio C Hamano | 6a93864 | 2008-06-27 16:22:02 | [diff] [blame] | 531 | static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos) |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 532 | { |
| 533 | struct commit_list *list = NULL; |
| 534 | struct commit_list *result = NULL; |
| Junio C Hamano | 6a93864 | 2008-06-27 16:22:02 | [diff] [blame] | 535 | int i; |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 536 | |
| Junio C Hamano | 6a93864 | 2008-06-27 16:22:02 | [diff] [blame] | 537 | for (i = 0; i < n; i++) { |
| 538 | if (one == twos[i]) |
| 539 | /* |
| 540 | * We do not mark this even with RESULT so we do not |
| 541 | * have to clean it up. |
| 542 | */ |
| 543 | return commit_list_insert(one, &result); |
| 544 | } |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 545 | |
| Martin Koegler | 172947e | 2008-02-18 20:47:57 | [diff] [blame] | 546 | if (parse_commit(one)) |
| 547 | return NULL; |
| Junio C Hamano | 6a93864 | 2008-06-27 16:22:02 | [diff] [blame] | 548 | for (i = 0; i < n; i++) { |
| 549 | if (parse_commit(twos[i])) |
| 550 | return NULL; |
| 551 | } |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 552 | |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 553 | one->object.flags |= PARENT1; |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 554 | insert_by_date(one, &list); |
| Junio C Hamano | 6a93864 | 2008-06-27 16:22:02 | [diff] [blame] | 555 | for (i = 0; i < n; i++) { |
| 556 | twos[i]->object.flags |= PARENT2; |
| 557 | insert_by_date(twos[i], &list); |
| 558 | } |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 559 | |
| 560 | while (interesting(list)) { |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 561 | struct commit *commit; |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 562 | struct commit_list *parents; |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 563 | struct commit_list *n; |
| 564 | int flags; |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 565 | |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 566 | commit = list->item; |
| 567 | n = list->next; |
| 568 | free(list); |
| 569 | list = n; |
| 570 | |
| 571 | flags = commit->object.flags & (PARENT1 | PARENT2 | STALE); |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 572 | if (flags == (PARENT1 | PARENT2)) { |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 573 | if (!(commit->object.flags & RESULT)) { |
| 574 | commit->object.flags |= RESULT; |
| 575 | insert_by_date(commit, &result); |
| 576 | } |
| Junio C Hamano | 542ccef | 2006-07-02 18:34:17 | [diff] [blame] | 577 | /* Mark parents of a found merge stale */ |
| 578 | flags |= STALE; |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 579 | } |
| 580 | parents = commit->parents; |
| 581 | while (parents) { |
| 582 | struct commit *p = parents->item; |
| 583 | parents = parents->next; |
| 584 | if ((p->object.flags & flags) == flags) |
| 585 | continue; |
| Martin Koegler | 172947e | 2008-02-18 20:47:57 | [diff] [blame] | 586 | if (parse_commit(p)) |
| 587 | return NULL; |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 588 | p->object.flags |= flags; |
| 589 | insert_by_date(p, &list); |
| 590 | } |
| 591 | } |
| 592 | |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 593 | /* Clean up the result to remove stale ones */ |
| Junio C Hamano | 1295228 | 2007-01-09 07:10:49 | [diff] [blame] | 594 | free_commit_list(list); |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 595 | list = result; result = NULL; |
| 596 | while (list) { |
| 597 | struct commit_list *n = list->next; |
| 598 | if (!(list->item->object.flags & STALE)) |
| 599 | insert_by_date(list->item, &result); |
| 600 | free(list); |
| 601 | list = n; |
| 602 | } |
| 603 | return result; |
| 604 | } |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 605 | |
| Miklos Vajna | 5240c9d | 2008-06-27 16:22:00 | [diff] [blame] | 606 | struct commit_list *get_octopus_merge_bases(struct commit_list *in) |
| 607 | { |
| 608 | struct commit_list *i, *j, *k, *ret = NULL; |
| 609 | struct commit_list **pptr = &ret; |
| 610 | |
| 611 | for (i = in; i; i = i->next) { |
| 612 | if (!ret) |
| 613 | pptr = &commit_list_insert(i->item, pptr)->next; |
| 614 | else { |
| 615 | struct commit_list *new = NULL, *end = NULL; |
| 616 | |
| 617 | for (j = ret; j; j = j->next) { |
| 618 | struct commit_list *bases; |
| 619 | bases = get_merge_bases(i->item, j->item, 1); |
| 620 | if (!new) |
| 621 | new = bases; |
| 622 | else |
| 623 | end->next = bases; |
| 624 | for (k = bases; k; k = k->next) |
| 625 | end = k; |
| 626 | } |
| 627 | ret = new; |
| 628 | } |
| 629 | } |
| 630 | return ret; |
| 631 | } |
| 632 | |
| Junio C Hamano | 6a93864 | 2008-06-27 16:22:02 | [diff] [blame] | 633 | struct commit_list *get_merge_bases_many(struct commit *one, |
| 634 | int n, |
| 635 | struct commit **twos, |
| 636 | int cleanup) |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 637 | { |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 638 | struct commit_list *list; |
| 639 | struct commit **rslt; |
| 640 | struct commit_list *result; |
| 641 | int cnt, i, j; |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 642 | |
| Junio C Hamano | 6a93864 | 2008-06-27 16:22:02 | [diff] [blame] | 643 | result = merge_bases_many(one, n, twos); |
| 644 | for (i = 0; i < n; i++) { |
| 645 | if (one == twos[i]) |
| 646 | return result; |
| 647 | } |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 648 | if (!result || !result->next) { |
| 649 | if (cleanup) { |
| 650 | clear_commit_marks(one, all_flags); |
| Junio C Hamano | 6a93864 | 2008-06-27 16:22:02 | [diff] [blame] | 651 | for (i = 0; i < n; i++) |
| 652 | clear_commit_marks(twos[i], all_flags); |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 653 | } |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 654 | return result; |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 655 | } |
| 656 | |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 657 | /* There are more than one */ |
| 658 | cnt = 0; |
| 659 | list = result; |
| 660 | while (list) { |
| 661 | list = list->next; |
| 662 | cnt++; |
| 663 | } |
| 664 | rslt = xcalloc(cnt, sizeof(*rslt)); |
| 665 | for (list = result, i = 0; list; list = list->next) |
| 666 | rslt[i++] = list->item; |
| 667 | free_commit_list(result); |
| 668 | |
| 669 | clear_commit_marks(one, all_flags); |
| Junio C Hamano | 6a93864 | 2008-06-27 16:22:02 | [diff] [blame] | 670 | for (i = 0; i < n; i++) |
| 671 | clear_commit_marks(twos[i], all_flags); |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 672 | for (i = 0; i < cnt - 1; i++) { |
| 673 | for (j = i+1; j < cnt; j++) { |
| 674 | if (!rslt[i] || !rslt[j]) |
| 675 | continue; |
| Junio C Hamano | 6a93864 | 2008-06-27 16:22:02 | [diff] [blame] | 676 | result = merge_bases_many(rslt[i], 1, &rslt[j]); |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 677 | clear_commit_marks(rslt[i], all_flags); |
| 678 | clear_commit_marks(rslt[j], all_flags); |
| 679 | for (list = result; list; list = list->next) { |
| 680 | if (rslt[i] == list->item) |
| 681 | rslt[i] = NULL; |
| 682 | if (rslt[j] == list->item) |
| 683 | rslt[j] = NULL; |
| 684 | } |
| 685 | } |
| Junio C Hamano | 58ecf5c | 2006-07-05 00:45:22 | [diff] [blame] | 686 | } |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 687 | |
| Junio C Hamano | f324943 | 2006-07-05 01:46:42 | [diff] [blame] | 688 | /* Surviving ones in rslt[] are the independent results */ |
| 689 | result = NULL; |
| 690 | for (i = 0; i < cnt; i++) { |
| 691 | if (rslt[i]) |
| 692 | insert_by_date(rslt[i], &result); |
| 693 | } |
| 694 | free(rslt); |
| Johannes Schindelin | 7c6f8aa | 2006-06-29 13:17:32 | [diff] [blame] | 695 | return result; |
| 696 | } |
| Junio C Hamano | 2ecd2bb | 2006-12-19 08:14:04 | [diff] [blame] | 697 | |
| Junio C Hamano | 6a93864 | 2008-06-27 16:22:02 | [diff] [blame] | 698 | struct commit_list *get_merge_bases(struct commit *one, struct commit *two, |
| 699 | int cleanup) |
| 700 | { |
| 701 | return get_merge_bases_many(one, 1, &two, cleanup); |
| 702 | } |
| 703 | |
| Jake Goulding | 7fcdb36 | 2009-01-26 14:13:24 | [diff] [blame] | 704 | int is_descendant_of(struct commit *commit, struct commit_list *with_commit) |
| 705 | { |
| 706 | if (!with_commit) |
| 707 | return 1; |
| 708 | while (with_commit) { |
| 709 | struct commit *other; |
| 710 | |
| 711 | other = with_commit->item; |
| 712 | with_commit = with_commit->next; |
| 713 | if (in_merge_bases(other, &commit, 1)) |
| 714 | return 1; |
| 715 | } |
| 716 | return 0; |
| 717 | } |
| 718 | |
| Junio C Hamano | 03840fc | 2007-01-09 07:22:31 | [diff] [blame] | 719 | int in_merge_bases(struct commit *commit, struct commit **reference, int num) |
| Junio C Hamano | 2ecd2bb | 2006-12-19 08:14:04 | [diff] [blame] | 720 | { |
| 721 | struct commit_list *bases, *b; |
| 722 | int ret = 0; |
| 723 | |
| Junio C Hamano | 03840fc | 2007-01-09 07:22:31 | [diff] [blame] | 724 | if (num == 1) |
| 725 | bases = get_merge_bases(commit, *reference, 1); |
| 726 | else |
| 727 | die("not yet"); |
| Junio C Hamano | 2ecd2bb | 2006-12-19 08:14:04 | [diff] [blame] | 728 | for (b = bases; b; b = b->next) { |
| Junio C Hamano | 03840fc | 2007-01-09 07:22:31 | [diff] [blame] | 729 | if (!hashcmp(commit->object.sha1, b->item->object.sha1)) { |
| Junio C Hamano | 2ecd2bb | 2006-12-19 08:14:04 | [diff] [blame] | 730 | ret = 1; |
| 731 | break; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | free_commit_list(bases); |
| 736 | return ret; |
| 737 | } |
| Junio C Hamano | 98cf9c3 | 2008-06-27 16:22:03 | [diff] [blame] | 738 | |
| 739 | struct commit_list *reduce_heads(struct commit_list *heads) |
| 740 | { |
| 741 | struct commit_list *p; |
| 742 | struct commit_list *result = NULL, **tail = &result; |
| 743 | struct commit **other; |
| 744 | size_t num_head, num_other; |
| 745 | |
| 746 | if (!heads) |
| 747 | return NULL; |
| 748 | |
| 749 | /* Avoid unnecessary reallocations */ |
| 750 | for (p = heads, num_head = 0; p; p = p->next) |
| 751 | num_head++; |
| 752 | other = xcalloc(sizeof(*other), num_head); |
| 753 | |
| 754 | /* For each commit, see if it can be reached by others */ |
| 755 | for (p = heads; p; p = p->next) { |
| 756 | struct commit_list *q, *base; |
| 757 | |
| Junio C Hamano | 711f6b2 | 2008-07-14 07:09:41 | [diff] [blame] | 758 | /* Do we already have this in the result? */ |
| 759 | for (q = result; q; q = q->next) |
| 760 | if (p->item == q->item) |
| 761 | break; |
| 762 | if (q) |
| 763 | continue; |
| 764 | |
| Junio C Hamano | 98cf9c3 | 2008-06-27 16:22:03 | [diff] [blame] | 765 | num_other = 0; |
| 766 | for (q = heads; q; q = q->next) { |
| Sverre Hvammen Johansen | 3d1dd47 | 2008-07-13 08:13:55 | [diff] [blame] | 767 | if (p->item == q->item) |
| Junio C Hamano | 98cf9c3 | 2008-06-27 16:22:03 | [diff] [blame] | 768 | continue; |
| 769 | other[num_other++] = q->item; |
| 770 | } |
| Junio C Hamano | 711f6b2 | 2008-07-14 07:09:41 | [diff] [blame] | 771 | if (num_other) |
| Junio C Hamano | 98cf9c3 | 2008-06-27 16:22:03 | [diff] [blame] | 772 | base = get_merge_bases_many(p->item, num_other, other, 1); |
| Junio C Hamano | 711f6b2 | 2008-07-14 07:09:41 | [diff] [blame] | 773 | else |
| Junio C Hamano | 98cf9c3 | 2008-06-27 16:22:03 | [diff] [blame] | 774 | base = NULL; |
| 775 | /* |
| 776 | * If p->item does not have anything common with other |
| 777 | * commits, there won't be any merge base. If it is |
| 778 | * reachable from some of the others, p->item will be |
| 779 | * the merge base. If its history is connected with |
| 780 | * others, but p->item is not reachable by others, we |
| 781 | * will get something other than p->item back. |
| 782 | */ |
| 783 | if (!base || (base->item != p->item)) |
| 784 | tail = &(commit_list_insert(p->item, tail)->next); |
| 785 | free_commit_list(base); |
| 786 | } |
| 787 | free(other); |
| 788 | return result; |
| 789 | } |