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