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