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