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