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