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