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