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