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