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