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