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