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" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 4 | |
Linus Torvalds | 60ab26d | 2005-09-15 14:43:17 -0700 | [diff] [blame] | 5 | int save_commit_buffer = 1; |
| 6 | |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 7 | struct sort_node |
| 8 | { |
| 9 | /* |
Tilman Sauerbeck | 55c3eb4 | 2006-08-17 20:44:16 +0200 | [diff] [blame] | 10 | * the number of children of the associated commit |
| 11 | * that also occur in the list being sorted. |
| 12 | */ |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 13 | unsigned int indegree; |
| 14 | |
| 15 | /* |
Tilman Sauerbeck | 55c3eb4 | 2006-08-17 20:44:16 +0200 | [diff] [blame] | 16 | * reference to original list item that we will re-use |
| 17 | * on output. |
| 18 | */ |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 19 | struct commit_list * list_item; |
| 20 | |
| 21 | }; |
| 22 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 23 | const char *commit_type = "commit"; |
| 24 | |
Eric Wong | 6cdfd17 | 2006-05-14 17:20:46 -0700 | [diff] [blame] | 25 | struct cmt_fmt_map { |
| 26 | const char *n; |
| 27 | size_t cmp_len; |
| 28 | enum cmit_fmt v; |
| 29 | } cmt_fmts[] = { |
| 30 | { "raw", 1, CMIT_FMT_RAW }, |
| 31 | { "medium", 1, CMIT_FMT_MEDIUM }, |
| 32 | { "short", 1, CMIT_FMT_SHORT }, |
Junio C Hamano | 328b710 | 2006-05-21 01:34:54 -0700 | [diff] [blame] | 33 | { "email", 1, CMIT_FMT_EMAIL }, |
Eric Wong | 6cdfd17 | 2006-05-14 17:20:46 -0700 | [diff] [blame] | 34 | { "full", 5, CMIT_FMT_FULL }, |
| 35 | { "fuller", 5, CMIT_FMT_FULLER }, |
| 36 | { "oneline", 1, CMIT_FMT_ONELINE }, |
| 37 | }; |
| 38 | |
Linus Torvalds | 9b66ec0 | 2005-06-26 17:50:46 -0700 | [diff] [blame] | 39 | enum cmit_fmt get_commit_format(const char *arg) |
| 40 | { |
Eric Wong | 6cdfd17 | 2006-05-14 17:20:46 -0700 | [diff] [blame] | 41 | int i; |
| 42 | |
| 43 | if (!arg || !*arg) |
Linus Torvalds | 9b66ec0 | 2005-06-26 17:50:46 -0700 | [diff] [blame] | 44 | return CMIT_FMT_DEFAULT; |
Eric Wong | 6cdfd17 | 2006-05-14 17:20:46 -0700 | [diff] [blame] | 45 | if (*arg == '=') |
| 46 | arg++; |
| 47 | for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) { |
| 48 | if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len)) |
| 49 | return cmt_fmts[i].v; |
| 50 | } |
| 51 | |
| 52 | die("invalid --pretty format: %s", arg); |
Linus Torvalds | 9b66ec0 | 2005-06-26 17:50:46 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 55 | static struct commit *check_commit(struct object *obj, |
| 56 | const unsigned char *sha1, |
| 57 | int quiet) |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 58 | { |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 59 | if (obj->type != OBJ_COMMIT) { |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 60 | if (!quiet) |
| 61 | error("Object %s is a %s, not a commit", |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 62 | sha1_to_hex(sha1), typename(obj->type)); |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 63 | return NULL; |
| 64 | } |
| 65 | return (struct commit *) obj; |
| 66 | } |
| 67 | |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 68 | struct commit *lookup_commit_reference_gently(const unsigned char *sha1, |
| 69 | int quiet) |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 70 | { |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 71 | struct object *obj = deref_tag(parse_object(sha1), NULL, 0); |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 72 | |
| 73 | if (!obj) |
| 74 | return NULL; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 75 | return check_commit(obj, sha1, quiet); |
| 76 | } |
| 77 | |
| 78 | struct commit *lookup_commit_reference(const unsigned char *sha1) |
| 79 | { |
| 80 | return lookup_commit_reference_gently(sha1, 0); |
Linus Torvalds | 961784e | 2005-05-18 16:14:22 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 83 | struct commit *lookup_commit(const unsigned char *sha1) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 84 | { |
| 85 | struct object *obj = lookup_object(sha1); |
| 86 | if (!obj) { |
Linus Torvalds | 855419f | 2006-06-19 10:44:15 -0700 | [diff] [blame] | 87 | struct commit *ret = alloc_commit_node(); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 88 | created_object(sha1, &ret->object); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 89 | ret->object.type = OBJ_COMMIT; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 90 | return ret; |
| 91 | } |
Nicolas Pitre | d1af002 | 2005-05-20 16:59:17 -0400 | [diff] [blame] | 92 | if (!obj->type) |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 93 | obj->type = OBJ_COMMIT; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 94 | return check_commit(obj, sha1, 0); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static unsigned long parse_commit_date(const char *buf) |
| 98 | { |
| 99 | unsigned long date; |
| 100 | |
| 101 | if (memcmp(buf, "author", 6)) |
| 102 | return 0; |
| 103 | while (*buf++ != '\n') |
| 104 | /* nada */; |
| 105 | if (memcmp(buf, "committer", 9)) |
| 106 | return 0; |
| 107 | while (*buf++ != '>') |
| 108 | /* nada */; |
| 109 | date = strtoul(buf, NULL, 10); |
| 110 | if (date == ULONG_MAX) |
| 111 | date = 0; |
| 112 | return date; |
| 113 | } |
| 114 | |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 115 | static struct commit_graft **commit_graft; |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 116 | static int commit_graft_alloc, commit_graft_nr; |
| 117 | |
| 118 | static int commit_graft_pos(const unsigned char *sha1) |
| 119 | { |
| 120 | int lo, hi; |
| 121 | lo = 0; |
| 122 | hi = commit_graft_nr; |
| 123 | while (lo < hi) { |
| 124 | int mi = (lo + hi) / 2; |
| 125 | struct commit_graft *graft = commit_graft[mi]; |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 126 | int cmp = hashcmp(sha1, graft->sha1); |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 127 | if (!cmp) |
| 128 | return mi; |
| 129 | if (cmp < 0) |
| 130 | hi = mi; |
| 131 | else |
| 132 | lo = mi + 1; |
| 133 | } |
| 134 | return -lo - 1; |
| 135 | } |
| 136 | |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 137 | int register_commit_graft(struct commit_graft *graft, int ignore_dups) |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 138 | { |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 139 | int pos = commit_graft_pos(graft->sha1); |
| 140 | |
| 141 | if (0 <= pos) { |
| 142 | if (ignore_dups) |
| 143 | free(graft); |
| 144 | else { |
| 145 | free(commit_graft[pos]); |
| 146 | commit_graft[pos] = graft; |
| 147 | } |
| 148 | return 1; |
| 149 | } |
| 150 | pos = -pos - 1; |
| 151 | if (commit_graft_alloc <= ++commit_graft_nr) { |
| 152 | commit_graft_alloc = alloc_nr(commit_graft_alloc); |
| 153 | commit_graft = xrealloc(commit_graft, |
| 154 | sizeof(*commit_graft) * |
| 155 | commit_graft_alloc); |
| 156 | } |
| 157 | if (pos < commit_graft_nr) |
| 158 | memmove(commit_graft + pos + 1, |
| 159 | commit_graft + pos, |
| 160 | (commit_graft_nr - pos - 1) * |
| 161 | sizeof(*commit_graft)); |
| 162 | commit_graft[pos] = graft; |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | struct commit_graft *read_graft_line(char *buf, int len) |
| 167 | { |
| 168 | /* The format is just "Commit Parent1 Parent2 ...\n" */ |
| 169 | int i; |
| 170 | struct commit_graft *graft = NULL; |
| 171 | |
| 172 | if (buf[len-1] == '\n') |
| 173 | buf[--len] = 0; |
Yann Dirson | 360204c | 2006-04-17 13:41:49 +0200 | [diff] [blame] | 174 | if (buf[0] == '#' || buf[0] == '\0') |
Junio C Hamano | 5bc4ce5 | 2006-04-16 14:24:56 -0700 | [diff] [blame] | 175 | return NULL; |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 176 | if ((len + 1) % 41) { |
| 177 | bad_graft_data: |
| 178 | error("bad graft data: %s", buf); |
| 179 | free(graft); |
| 180 | return NULL; |
| 181 | } |
| 182 | i = (len + 1) / 41 - 1; |
| 183 | graft = xmalloc(sizeof(*graft) + 20 * i); |
| 184 | graft->nr_parent = i; |
| 185 | if (get_sha1_hex(buf, graft->sha1)) |
| 186 | goto bad_graft_data; |
| 187 | for (i = 40; i < len; i += 41) { |
| 188 | if (buf[i] != ' ') |
| 189 | goto bad_graft_data; |
| 190 | if (get_sha1_hex(buf + i + 1, graft->parent[i/41])) |
| 191 | goto bad_graft_data; |
| 192 | } |
| 193 | return graft; |
| 194 | } |
| 195 | |
| 196 | int read_graft_file(const char *graft_file) |
| 197 | { |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 198 | FILE *fp = fopen(graft_file, "r"); |
| 199 | char buf[1024]; |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 200 | if (!fp) |
| 201 | return -1; |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 202 | while (fgets(buf, sizeof(buf), fp)) { |
| 203 | /* The format is just "Commit Parent1 Parent2 ...\n" */ |
| 204 | int len = strlen(buf); |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 205 | struct commit_graft *graft = read_graft_line(buf, len); |
Junio C Hamano | 5bc4ce5 | 2006-04-16 14:24:56 -0700 | [diff] [blame] | 206 | if (!graft) |
| 207 | continue; |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 208 | if (register_commit_graft(graft, 1)) |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 209 | error("duplicate graft data: %s", buf); |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 210 | } |
| 211 | fclose(fp); |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static void prepare_commit_graft(void) |
| 216 | { |
| 217 | static int commit_graft_prepared; |
| 218 | char *graft_file; |
| 219 | |
| 220 | if (commit_graft_prepared) |
| 221 | return; |
| 222 | graft_file = get_graft_file(); |
| 223 | read_graft_file(graft_file); |
| 224 | commit_graft_prepared = 1; |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static struct commit_graft *lookup_commit_graft(const unsigned char *sha1) |
| 228 | { |
| 229 | int pos; |
Junio C Hamano | 5040f17 | 2006-04-06 23:58:51 -0700 | [diff] [blame] | 230 | prepare_commit_graft(); |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 231 | pos = commit_graft_pos(sha1); |
| 232 | if (pos < 0) |
| 233 | return NULL; |
| 234 | return commit_graft[pos]; |
| 235 | } |
| 236 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 237 | int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 238 | { |
Junio C Hamano | 3b44f15 | 2006-06-28 03:51:00 -0700 | [diff] [blame] | 239 | char *tail = buffer; |
Linus Torvalds | f7cc77d | 2005-07-27 15:12:48 -0700 | [diff] [blame] | 240 | char *bufptr = buffer; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 241 | unsigned char parent[20]; |
Linus Torvalds | 6c88be1 | 2005-06-20 20:26:03 -0700 | [diff] [blame] | 242 | struct commit_list **pptr; |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 243 | struct commit_graft *graft; |
Junio C Hamano | 27dedf0 | 2005-11-16 21:32:44 -0800 | [diff] [blame] | 244 | unsigned n_refs = 0; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 245 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 246 | if (item->object.parsed) |
| 247 | return 0; |
| 248 | item->object.parsed = 1; |
Junio C Hamano | 3b44f15 | 2006-06-28 03:51:00 -0700 | [diff] [blame] | 249 | tail += size; |
| 250 | if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5)) |
Linus Torvalds | f7cc77d | 2005-07-27 15:12:48 -0700 | [diff] [blame] | 251 | return error("bogus commit object %s", sha1_to_hex(item->object.sha1)); |
Junio C Hamano | 3b44f15 | 2006-06-28 03:51:00 -0700 | [diff] [blame] | 252 | if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0) |
Junio C Hamano | bd2afde | 2006-02-22 17:47:10 -0800 | [diff] [blame] | 253 | return error("bad tree pointer in commit %s", |
| 254 | sha1_to_hex(item->object.sha1)); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 255 | item->tree = lookup_tree(parent); |
Linus Torvalds | 235ac40 | 2005-04-24 14:31:57 -0700 | [diff] [blame] | 256 | if (item->tree) |
Junio C Hamano | 27dedf0 | 2005-11-16 21:32:44 -0800 | [diff] [blame] | 257 | n_refs++; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 258 | bufptr += 46; /* "tree " + "hex sha1" + "\n" */ |
Linus Torvalds | 6c88be1 | 2005-06-20 20:26:03 -0700 | [diff] [blame] | 259 | pptr = &item->parents; |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 260 | |
| 261 | graft = lookup_commit_graft(item->object.sha1); |
Junio C Hamano | 3b44f15 | 2006-06-28 03:51:00 -0700 | [diff] [blame] | 262 | while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) { |
Linus Torvalds | f7cc77d | 2005-07-27 15:12:48 -0700 | [diff] [blame] | 263 | struct commit *new_parent; |
| 264 | |
Junio C Hamano | 3b44f15 | 2006-06-28 03:51:00 -0700 | [diff] [blame] | 265 | if (tail <= bufptr + 48 || |
| 266 | get_sha1_hex(bufptr + 7, parent) || |
| 267 | bufptr[47] != '\n') |
Linus Torvalds | f7cc77d | 2005-07-27 15:12:48 -0700 | [diff] [blame] | 268 | 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] | 269 | bufptr += 48; |
| 270 | if (graft) |
| 271 | continue; |
Linus Torvalds | f7cc77d | 2005-07-27 15:12:48 -0700 | [diff] [blame] | 272 | new_parent = lookup_commit(parent); |
Linus Torvalds | 235ac40 | 2005-04-24 14:31:57 -0700 | [diff] [blame] | 273 | if (new_parent) { |
Linus Torvalds | 6c88be1 | 2005-06-20 20:26:03 -0700 | [diff] [blame] | 274 | pptr = &commit_list_insert(new_parent, pptr)->next; |
Junio C Hamano | 27dedf0 | 2005-11-16 21:32:44 -0800 | [diff] [blame] | 275 | n_refs++; |
Linus Torvalds | 235ac40 | 2005-04-24 14:31:57 -0700 | [diff] [blame] | 276 | } |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 277 | } |
| 278 | if (graft) { |
| 279 | int i; |
| 280 | struct commit *new_parent; |
| 281 | for (i = 0; i < graft->nr_parent; i++) { |
| 282 | new_parent = lookup_commit(graft->parent[i]); |
| 283 | if (!new_parent) |
| 284 | continue; |
| 285 | pptr = &commit_list_insert(new_parent, pptr)->next; |
Junio C Hamano | 27dedf0 | 2005-11-16 21:32:44 -0800 | [diff] [blame] | 286 | n_refs++; |
Junio C Hamano | 5da5c8f | 2005-07-30 00:58:28 -0700 | [diff] [blame] | 287 | } |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 288 | } |
| 289 | item->date = parse_commit_date(bufptr); |
Junio C Hamano | 27dedf0 | 2005-11-16 21:32:44 -0800 | [diff] [blame] | 290 | |
| 291 | if (track_object_refs) { |
| 292 | unsigned i = 0; |
| 293 | struct commit_list *p; |
| 294 | struct object_refs *refs = alloc_object_refs(n_refs); |
| 295 | if (item->tree) |
| 296 | refs->ref[i++] = &item->tree->object; |
| 297 | for (p = item->parents; p; p = p->next) |
| 298 | refs->ref[i++] = &p->item->object; |
| 299 | set_object_refs(&item->object, refs); |
| 300 | } |
| 301 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 305 | int parse_commit(struct commit *item) |
| 306 | { |
| 307 | char type[20]; |
| 308 | void *buffer; |
| 309 | unsigned long size; |
| 310 | int ret; |
| 311 | |
| 312 | if (item->object.parsed) |
| 313 | return 0; |
| 314 | buffer = read_sha1_file(item->object.sha1, type, &size); |
| 315 | if (!buffer) |
| 316 | return error("Could not read %s", |
| 317 | sha1_to_hex(item->object.sha1)); |
| 318 | if (strcmp(type, commit_type)) { |
| 319 | free(buffer); |
| 320 | return error("Object %s not a commit", |
| 321 | sha1_to_hex(item->object.sha1)); |
| 322 | } |
| 323 | ret = parse_commit_buffer(item, buffer, size); |
Linus Torvalds | 60ab26d | 2005-09-15 14:43:17 -0700 | [diff] [blame] | 324 | if (save_commit_buffer && !ret) { |
Linus Torvalds | 3ff1fbb | 2005-05-25 18:27:14 -0700 | [diff] [blame] | 325 | item->buffer = buffer; |
| 326 | return 0; |
| 327 | } |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 328 | free(buffer); |
| 329 | return ret; |
| 330 | } |
| 331 | |
Linus Torvalds | ac5155e | 2005-05-30 18:44:02 -0700 | [diff] [blame] | 332 | 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] | 333 | { |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 334 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 335 | new_list->item = item; |
| 336 | new_list->next = *list_p; |
| 337 | *list_p = new_list; |
Linus Torvalds | ac5155e | 2005-05-30 18:44:02 -0700 | [diff] [blame] | 338 | return new_list; |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 341 | void free_commit_list(struct commit_list *list) |
| 342 | { |
| 343 | while (list) { |
| 344 | struct commit_list *temp = list; |
| 345 | list = temp->next; |
| 346 | free(temp); |
| 347 | } |
| 348 | } |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 349 | |
Linus Torvalds | f755494 | 2005-07-06 09:31:17 -0700 | [diff] [blame] | 350 | struct commit_list * insert_by_date(struct commit *item, struct commit_list **list) |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 351 | { |
| 352 | struct commit_list **pp = list; |
| 353 | struct commit_list *p; |
| 354 | while ((p = *pp) != NULL) { |
| 355 | if (p->item->date < item->date) { |
| 356 | break; |
| 357 | } |
| 358 | pp = &p->next; |
| 359 | } |
Linus Torvalds | f755494 | 2005-07-06 09:31:17 -0700 | [diff] [blame] | 360 | return commit_list_insert(item, pp); |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | |
| 364 | void sort_by_date(struct commit_list **list) |
| 365 | { |
| 366 | struct commit_list *ret = NULL; |
| 367 | while (*list) { |
Linus Torvalds | f755494 | 2005-07-06 09:31:17 -0700 | [diff] [blame] | 368 | insert_by_date((*list)->item, &ret); |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 369 | *list = (*list)->next; |
| 370 | } |
| 371 | *list = ret; |
| 372 | } |
| 373 | |
Daniel Barkalow | 58e28af | 2005-04-23 20:29:22 -0700 | [diff] [blame] | 374 | struct commit *pop_most_recent_commit(struct commit_list **list, |
| 375 | unsigned int mark) |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 376 | { |
| 377 | struct commit *ret = (*list)->item; |
| 378 | struct commit_list *parents = ret->parents; |
| 379 | struct commit_list *old = *list; |
| 380 | |
| 381 | *list = (*list)->next; |
| 382 | free(old); |
| 383 | |
| 384 | while (parents) { |
Linus Torvalds | 4056c09 | 2005-04-23 19:21:28 -0700 | [diff] [blame] | 385 | struct commit *commit = parents->item; |
Daniel Barkalow | 58e28af | 2005-04-23 20:29:22 -0700 | [diff] [blame] | 386 | parse_commit(commit); |
| 387 | if (!(commit->object.flags & mark)) { |
| 388 | commit->object.flags |= mark; |
Linus Torvalds | f755494 | 2005-07-06 09:31:17 -0700 | [diff] [blame] | 389 | insert_by_date(commit, list); |
Linus Torvalds | 4056c09 | 2005-04-23 19:21:28 -0700 | [diff] [blame] | 390 | } |
Daniel Barkalow | dd97f85 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 391 | parents = parents->next; |
| 392 | } |
| 393 | return ret; |
| 394 | } |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 395 | |
Junio C Hamano | f8f9c73 | 2006-01-07 18:52:42 -0800 | [diff] [blame] | 396 | void clear_commit_marks(struct commit *commit, unsigned int mark) |
| 397 | { |
| 398 | struct commit_list *parents; |
| 399 | |
Junio C Hamano | f8f9c73 | 2006-01-07 18:52:42 -0800 | [diff] [blame] | 400 | commit->object.flags &= ~mark; |
Junio C Hamano | 58ecf5c | 2006-07-04 17:45:22 -0700 | [diff] [blame] | 401 | parents = commit->parents; |
Junio C Hamano | f8f9c73 | 2006-01-07 18:52:42 -0800 | [diff] [blame] | 402 | while (parents) { |
Junio C Hamano | 160b798 | 2006-07-03 03:05:20 -0700 | [diff] [blame] | 403 | struct commit *parent = parents->item; |
Junio C Hamano | 58ecf5c | 2006-07-04 17:45:22 -0700 | [diff] [blame] | 404 | |
| 405 | /* Have we already cleared this? */ |
| 406 | if (mark & parent->object.flags) |
Junio C Hamano | 160b798 | 2006-07-03 03:05:20 -0700 | [diff] [blame] | 407 | clear_commit_marks(parent, mark); |
Junio C Hamano | f8f9c73 | 2006-01-07 18:52:42 -0800 | [diff] [blame] | 408 | parents = parents->next; |
| 409 | } |
| 410 | } |
| 411 | |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 412 | /* |
| 413 | * Generic support for pretty-printing the header |
| 414 | */ |
| 415 | static int get_one_line(const char *msg, unsigned long len) |
| 416 | { |
| 417 | int ret = 0; |
| 418 | |
| 419 | while (len--) { |
| 420 | char c = *msg++; |
Linus Torvalds | 684958a | 2006-04-12 11:31:23 -0700 | [diff] [blame] | 421 | if (!c) |
| 422 | break; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 423 | ret++; |
| 424 | if (c == '\n') |
| 425 | break; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 426 | } |
| 427 | return ret; |
| 428 | } |
| 429 | |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 430 | static int is_rfc2047_special(char ch) |
| 431 | { |
| 432 | return ((ch & 0x80) || (ch == '=') || (ch == '?') || (ch == '_')); |
| 433 | } |
| 434 | |
| 435 | static int add_rfc2047(char *buf, const char *line, int len) |
| 436 | { |
| 437 | char *bp = buf; |
| 438 | int i, needquote; |
| 439 | static const char q_utf8[] = "=?utf-8?q?"; |
| 440 | |
| 441 | for (i = needquote = 0; !needquote && i < len; i++) { |
| 442 | unsigned ch = line[i]; |
| 443 | if (ch & 0x80) |
| 444 | needquote++; |
| 445 | if ((i + 1 < len) && |
| 446 | (ch == '=' && line[i+1] == '?')) |
| 447 | needquote++; |
| 448 | } |
| 449 | if (!needquote) |
| 450 | return sprintf(buf, "%.*s", len, line); |
| 451 | |
| 452 | memcpy(bp, q_utf8, sizeof(q_utf8)-1); |
| 453 | bp += sizeof(q_utf8)-1; |
| 454 | for (i = 0; i < len; i++) { |
Junio C Hamano | 0da4677 | 2006-06-19 15:00:17 -0700 | [diff] [blame] | 455 | unsigned ch = line[i] & 0xFF; |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 456 | if (is_rfc2047_special(ch)) { |
| 457 | sprintf(bp, "=%02X", ch); |
| 458 | bp += 3; |
| 459 | } |
| 460 | else if (ch == ' ') |
| 461 | *bp++ = '_'; |
| 462 | else |
| 463 | *bp++ = ch; |
| 464 | } |
| 465 | memcpy(bp, "?=", 2); |
| 466 | bp += 2; |
| 467 | return bp - buf; |
| 468 | } |
| 469 | |
Jonas Fonseca | 3dfb927 | 2006-08-28 15:52:13 +0200 | [diff] [blame] | 470 | static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, |
| 471 | const char *line, int relative_date) |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 472 | { |
| 473 | char *date; |
Junio C Hamano | 5de36bf | 2005-08-29 21:17:21 -0700 | [diff] [blame] | 474 | int namelen; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 475 | unsigned long time; |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 476 | int tz, ret; |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 477 | const char *filler = " "; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 478 | |
Junio C Hamano | d87449c | 2005-08-08 22:15:40 -0700 | [diff] [blame] | 479 | if (fmt == CMIT_FMT_ONELINE) |
| 480 | return 0; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 481 | date = strchr(line, '>'); |
| 482 | if (!date) |
| 483 | return 0; |
| 484 | namelen = ++date - line; |
| 485 | time = strtoul(date, &date, 10); |
| 486 | tz = strtol(date, NULL, 10); |
| 487 | |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 488 | if (fmt == CMIT_FMT_EMAIL) { |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 489 | char *name_tail = strchr(line, '<'); |
| 490 | int display_name_length; |
| 491 | if (!name_tail) |
| 492 | return 0; |
| 493 | while (line < name_tail && isspace(name_tail[-1])) |
| 494 | name_tail--; |
| 495 | display_name_length = name_tail - line; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 496 | filler = ""; |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 497 | strcpy(buf, "From: "); |
| 498 | ret = strlen(buf); |
| 499 | ret += add_rfc2047(buf + ret, line, display_name_length); |
| 500 | memcpy(buf + ret, name_tail, namelen - display_name_length); |
| 501 | ret += namelen - display_name_length; |
| 502 | buf[ret++] = '\n'; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 503 | } |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 504 | else { |
| 505 | ret = sprintf(buf, "%s: %.*s%.*s\n", what, |
| 506 | (fmt == CMIT_FMT_FULLER) ? 4 : 0, |
| 507 | filler, namelen, line); |
| 508 | } |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 509 | switch (fmt) { |
| 510 | case CMIT_FMT_MEDIUM: |
Jonas Fonseca | 3dfb927 | 2006-08-28 15:52:13 +0200 | [diff] [blame] | 511 | ret += sprintf(buf + ret, "Date: %s\n", |
| 512 | show_date(time, tz, relative_date)); |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 513 | break; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 514 | case CMIT_FMT_EMAIL: |
Junio C Hamano | 2a38704 | 2006-05-01 01:44:33 -0700 | [diff] [blame] | 515 | ret += sprintf(buf + ret, "Date: %s\n", |
| 516 | show_rfc2822_date(time, tz)); |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 517 | break; |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 518 | case CMIT_FMT_FULLER: |
Jonas Fonseca | 3dfb927 | 2006-08-28 15:52:13 +0200 | [diff] [blame] | 519 | ret += sprintf(buf + ret, "%sDate: %s\n", what, |
| 520 | show_date(time, tz, relative_date)); |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 521 | break; |
| 522 | default: |
| 523 | /* notin' */ |
| 524 | break; |
| 525 | } |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 526 | return ret; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 529 | static int is_empty_line(const char *line, int *len_p) |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 530 | { |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 531 | int len = *len_p; |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 532 | while (len && isspace(line[len-1])) |
| 533 | len--; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 534 | *len_p = len; |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 535 | return !len; |
| 536 | } |
| 537 | |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 538 | static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev) |
Linus Torvalds | 28342a5 | 2005-06-18 13:52:05 -0700 | [diff] [blame] | 539 | { |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 540 | struct commit_list *parent = commit->parents; |
| 541 | int offset; |
Junio C Hamano | d87449c | 2005-08-08 22:15:40 -0700 | [diff] [blame] | 542 | |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 543 | if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) || |
| 544 | !parent || !parent->next) |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 545 | return 0; |
| 546 | |
| 547 | offset = sprintf(buf, "Merge:"); |
| 548 | |
| 549 | while (parent) { |
| 550 | struct commit *p = parent->item; |
Junio C Hamano | 6bfb27a | 2006-02-02 17:52:19 -0800 | [diff] [blame] | 551 | const char *hex = abbrev |
| 552 | ? find_unique_abbrev(p->object.sha1, abbrev) |
| 553 | : sha1_to_hex(p->object.sha1); |
Timo Hirvonen | 554fe20 | 2006-06-28 12:04:39 +0300 | [diff] [blame] | 554 | const char *dots = (abbrev && strlen(hex) != 40) ? "..." : ""; |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 555 | parent = parent->next; |
| 556 | |
Junio C Hamano | 6bfb27a | 2006-02-02 17:52:19 -0800 | [diff] [blame] | 557 | offset += sprintf(buf + offset, " %s%s", hex, dots); |
Linus Torvalds | 28342a5 | 2005-06-18 13:52:05 -0700 | [diff] [blame] | 558 | } |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 559 | buf[offset++] = '\n'; |
Linus Torvalds | 28342a5 | 2005-06-18 13:52:05 -0700 | [diff] [blame] | 560 | return offset; |
| 561 | } |
| 562 | |
Jonas Fonseca | 3dfb927 | 2006-08-28 15:52:13 +0200 | [diff] [blame] | 563 | unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, |
| 564 | unsigned long len, char *buf, unsigned long space, |
| 565 | int abbrev, const char *subject, |
| 566 | const char *after_subject, int relative_date) |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 567 | { |
| 568 | int hdr = 1, body = 0; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 569 | unsigned long offset = 0; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 570 | int indent = 4; |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 571 | int parents_shown = 0; |
Junio C Hamano | 3815f42 | 2006-01-27 01:54:59 -0800 | [diff] [blame] | 572 | const char *msg = commit->buffer; |
Junio C Hamano | c831da6 | 2006-05-21 23:55:00 -0700 | [diff] [blame] | 573 | int plain_non_ascii = 0; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 574 | |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 575 | if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL) |
| 576 | indent = 0; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 577 | |
Junio C Hamano | c831da6 | 2006-05-21 23:55:00 -0700 | [diff] [blame] | 578 | /* After-subject is used to pass in Content-Type: multipart |
| 579 | * MIME header; in that case we do not have to do the |
| 580 | * plaintext content type even if the commit message has |
| 581 | * non 7-bit ASCII character. Otherwise, check if we need |
| 582 | * to say this is not a 7-bit ASCII. |
| 583 | */ |
| 584 | if (fmt == CMIT_FMT_EMAIL && !after_subject) { |
Junio C Hamano | 0da4677 | 2006-06-19 15:00:17 -0700 | [diff] [blame] | 585 | int i, ch, in_body; |
| 586 | |
| 587 | for (in_body = i = 0; (ch = msg[i]) && i < len; i++) { |
| 588 | if (!in_body) { |
| 589 | /* author could be non 7-bit ASCII but |
| 590 | * the log may so; skip over the |
| 591 | * header part first. |
| 592 | */ |
| 593 | if (ch == '\n' && |
| 594 | i + 1 < len && msg[i+1] == '\n') |
| 595 | in_body = 1; |
| 596 | } |
| 597 | else if (ch & 0x80) { |
Junio C Hamano | c831da6 | 2006-05-21 23:55:00 -0700 | [diff] [blame] | 598 | plain_non_ascii = 1; |
Junio C Hamano | 0da4677 | 2006-06-19 15:00:17 -0700 | [diff] [blame] | 599 | break; |
| 600 | } |
| 601 | } |
Junio C Hamano | c831da6 | 2006-05-21 23:55:00 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 604 | for (;;) { |
| 605 | const char *line = msg; |
| 606 | int linelen = get_one_line(msg, len); |
| 607 | |
| 608 | if (!linelen) |
| 609 | break; |
| 610 | |
| 611 | /* |
| 612 | * We want some slop for indentation and a possible |
| 613 | * final "...". Thus the "+ 20". |
| 614 | */ |
| 615 | if (offset + linelen + 20 > space) { |
| 616 | memcpy(buf + offset, " ...\n", 8); |
| 617 | offset += 8; |
| 618 | break; |
| 619 | } |
| 620 | |
| 621 | msg += linelen; |
| 622 | len -= linelen; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 623 | if (hdr) { |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 624 | if (linelen == 1) { |
| 625 | hdr = 0; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 626 | if ((fmt != CMIT_FMT_ONELINE) && !subject) |
Junio C Hamano | d87449c | 2005-08-08 22:15:40 -0700 | [diff] [blame] | 627 | buf[offset++] = '\n'; |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 628 | continue; |
| 629 | } |
| 630 | if (fmt == CMIT_FMT_RAW) { |
| 631 | memcpy(buf + offset, line, linelen); |
| 632 | offset += linelen; |
| 633 | continue; |
| 634 | } |
Linus Torvalds | 28342a5 | 2005-06-18 13:52:05 -0700 | [diff] [blame] | 635 | if (!memcmp(line, "parent ", 7)) { |
| 636 | if (linelen != 48) |
| 637 | die("bad parent line in commit"); |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 638 | continue; |
Linus Torvalds | 28342a5 | 2005-06-18 13:52:05 -0700 | [diff] [blame] | 639 | } |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 640 | |
Junio C Hamano | f2d4227 | 2006-01-27 02:17:19 -0800 | [diff] [blame] | 641 | if (!parents_shown) { |
| 642 | offset += add_merge_info(fmt, buf + offset, |
| 643 | commit, abbrev); |
| 644 | parents_shown = 1; |
| 645 | continue; |
| 646 | } |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 647 | /* |
| 648 | * MEDIUM == DEFAULT shows only author with dates. |
| 649 | * FULL shows both authors but not dates. |
| 650 | * FULLER shows both authors and dates. |
| 651 | */ |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 652 | if (!memcmp(line, "author ", 7)) |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 653 | offset += add_user_info("Author", fmt, |
| 654 | buf + offset, |
Jonas Fonseca | 3dfb927 | 2006-08-28 15:52:13 +0200 | [diff] [blame] | 655 | line + 7, |
| 656 | relative_date); |
Junio C Hamano | ff56fe1 | 2005-11-09 22:15:27 -0800 | [diff] [blame] | 657 | if (!memcmp(line, "committer ", 10) && |
| 658 | (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) |
| 659 | offset += add_user_info("Commit", fmt, |
| 660 | buf + offset, |
Jonas Fonseca | 3dfb927 | 2006-08-28 15:52:13 +0200 | [diff] [blame] | 661 | line + 10, |
| 662 | relative_date); |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 663 | continue; |
| 664 | } |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 665 | |
Robert Shearman | 19b3bd3 | 2006-07-13 23:17:22 +0100 | [diff] [blame] | 666 | if (!subject) |
| 667 | body = 1; |
| 668 | |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 669 | if (is_empty_line(line, &linelen)) { |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 670 | if (!body) |
| 671 | continue; |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 672 | if (subject) |
| 673 | continue; |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 674 | if (fmt == CMIT_FMT_SHORT) |
| 675 | break; |
Linus Torvalds | 000182e | 2005-06-05 09:02:03 -0700 | [diff] [blame] | 676 | } |
Junio C Hamano | d87449c | 2005-08-08 22:15:40 -0700 | [diff] [blame] | 677 | |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 678 | if (subject) { |
Junio C Hamano | 53f420e | 2006-04-22 03:06:13 -0700 | [diff] [blame] | 679 | int slen = strlen(subject); |
| 680 | memcpy(buf + offset, subject, slen); |
| 681 | offset += slen; |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 682 | offset += add_rfc2047(buf + offset, line, linelen); |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 683 | } |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 684 | else { |
| 685 | memset(buf + offset, ' ', indent); |
| 686 | memcpy(buf + offset + indent, line, linelen); |
| 687 | offset += linelen + indent; |
| 688 | } |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 689 | buf[offset++] = '\n'; |
Junio C Hamano | d87449c | 2005-08-08 22:15:40 -0700 | [diff] [blame] | 690 | if (fmt == CMIT_FMT_ONELINE) |
| 691 | break; |
Junio C Hamano | c831da6 | 2006-05-21 23:55:00 -0700 | [diff] [blame] | 692 | if (subject && plain_non_ascii) { |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 693 | static const char header[] = |
| 694 | "Content-Type: text/plain; charset=UTF-8\n" |
| 695 | "Content-Transfer-Encoding: 8bit\n"; |
| 696 | memcpy(buf + offset, header, sizeof(header)-1); |
| 697 | offset += sizeof(header)-1; |
Junio C Hamano | cdd406e | 2006-05-16 02:29:42 -0700 | [diff] [blame] | 698 | } |
Johannes Schindelin | 698ce6f | 2006-05-20 15:40:29 +0200 | [diff] [blame] | 699 | if (after_subject) { |
| 700 | int slen = strlen(after_subject); |
| 701 | if (slen > space - offset - 1) |
| 702 | slen = space - offset - 1; |
| 703 | memcpy(buf + offset, after_subject, slen); |
| 704 | offset += slen; |
| 705 | after_subject = NULL; |
| 706 | } |
Junio C Hamano | 3eefc18 | 2006-04-18 16:45:27 -0700 | [diff] [blame] | 707 | subject = NULL; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 708 | } |
Linus Torvalds | 40c2fe0 | 2006-04-14 21:20:51 -0700 | [diff] [blame] | 709 | while (offset && isspace(buf[offset-1])) |
| 710 | offset--; |
| 711 | /* Make sure there is an EOLN for the non-oneline case */ |
| 712 | if (fmt != CMIT_FMT_ONELINE) |
| 713 | buf[offset++] = '\n'; |
Robert Shearman | 19b3bd3 | 2006-07-13 23:17:22 +0100 | [diff] [blame] | 714 | /* |
| 715 | * make sure there is another EOLN to separate the headers from whatever |
| 716 | * body the caller appends if we haven't already written a body |
| 717 | */ |
| 718 | if (fmt == CMIT_FMT_EMAIL && !body) |
| 719 | buf[offset++] = '\n'; |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 720 | buf[offset] = '\0'; |
| 721 | return offset; |
| 722 | } |
jon@blackcubes.dyndns.org | a3437b8 | 2005-06-06 15:39:40 +0000 | [diff] [blame] | 723 | |
| 724 | struct commit *pop_commit(struct commit_list **stack) |
| 725 | { |
| 726 | struct commit_list *top = *stack; |
| 727 | struct commit *item = top ? top->item : NULL; |
| 728 | |
| 729 | if (top) { |
| 730 | *stack = top->next; |
| 731 | free(top); |
| 732 | } |
| 733 | return item; |
| 734 | } |
| 735 | |
| 736 | int count_parents(struct commit * commit) |
| 737 | { |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 738 | int count; |
jon@blackcubes.dyndns.org | a3437b8 | 2005-06-06 15:39:40 +0000 | [diff] [blame] | 739 | struct commit_list * parents = commit->parents; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 740 | for (count = 0; parents; parents = parents->next,count++) |
| 741 | ; |
jon@blackcubes.dyndns.org | a3437b8 | 2005-06-06 15:39:40 +0000 | [diff] [blame] | 742 | return count; |
| 743 | } |
| 744 | |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 745 | void topo_sort_default_setter(struct commit *c, void *data) |
| 746 | { |
Linus Torvalds | d3ff6f5 | 2006-06-17 18:26:18 -0700 | [diff] [blame] | 747 | c->util = data; |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | void *topo_sort_default_getter(struct commit *c) |
| 751 | { |
Linus Torvalds | d3ff6f5 | 2006-06-17 18:26:18 -0700 | [diff] [blame] | 752 | return c->util; |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 753 | } |
| 754 | |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 755 | /* |
| 756 | * Performs an in-place topological sort on the list supplied. |
| 757 | */ |
Junio C Hamano | 4c8725f | 2006-02-15 22:05:33 -0800 | [diff] [blame] | 758 | void sort_in_topological_order(struct commit_list ** list, int lifo) |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 759 | { |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 760 | sort_in_topological_order_fn(list, lifo, topo_sort_default_setter, |
| 761 | topo_sort_default_getter); |
| 762 | } |
| 763 | |
| 764 | void sort_in_topological_order_fn(struct commit_list ** list, int lifo, |
| 765 | topo_sort_set_fn_t setter, |
| 766 | topo_sort_get_fn_t getter) |
| 767 | { |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 768 | struct commit_list * next = *list; |
Linus Torvalds | 2ed0288 | 2005-11-14 10:01:26 -0800 | [diff] [blame] | 769 | struct commit_list * work = NULL, **insert; |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 770 | struct commit_list ** pptr = list; |
| 771 | struct sort_node * nodes; |
| 772 | struct sort_node * next_nodes; |
| 773 | int count = 0; |
| 774 | |
| 775 | /* determine the size of the list */ |
| 776 | while (next) { |
| 777 | next = next->next; |
| 778 | count++; |
| 779 | } |
Eric Wong | 7d6fb37 | 2005-12-24 04:12:43 -0800 | [diff] [blame] | 780 | |
| 781 | if (!count) |
| 782 | return; |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 783 | /* allocate an array to help sort the list */ |
| 784 | nodes = xcalloc(count, sizeof(*nodes)); |
| 785 | /* link the list to the array */ |
| 786 | next_nodes = nodes; |
| 787 | next=*list; |
| 788 | while (next) { |
| 789 | next_nodes->list_item = next; |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 790 | setter(next->item, next_nodes); |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 791 | next_nodes++; |
| 792 | next = next->next; |
| 793 | } |
| 794 | /* update the indegree */ |
| 795 | next=*list; |
| 796 | while (next) { |
| 797 | struct commit_list * parents = next->item->parents; |
| 798 | while (parents) { |
| 799 | struct commit * parent=parents->item; |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 800 | struct sort_node * pn = (struct sort_node *) getter(parent); |
| 801 | |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 802 | if (pn) |
| 803 | pn->indegree++; |
| 804 | parents=parents->next; |
| 805 | } |
| 806 | next=next->next; |
| 807 | } |
| 808 | /* |
| 809 | * find the tips |
| 810 | * |
| 811 | * tips are nodes not reachable from any other node in the list |
| 812 | * |
| 813 | * the tips serve as a starting set for the work queue. |
| 814 | */ |
| 815 | next=*list; |
Linus Torvalds | 2ed0288 | 2005-11-14 10:01:26 -0800 | [diff] [blame] | 816 | insert = &work; |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 817 | while (next) { |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 818 | struct sort_node * node = (struct sort_node *) getter(next->item); |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 819 | |
| 820 | if (node->indegree == 0) { |
Linus Torvalds | 2ed0288 | 2005-11-14 10:01:26 -0800 | [diff] [blame] | 821 | insert = &commit_list_insert(next->item, insert)->next; |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 822 | } |
| 823 | next=next->next; |
| 824 | } |
Junio C Hamano | 4c8725f | 2006-02-15 22:05:33 -0800 | [diff] [blame] | 825 | |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 826 | /* process the list in topological order */ |
Junio C Hamano | 4c8725f | 2006-02-15 22:05:33 -0800 | [diff] [blame] | 827 | if (!lifo) |
| 828 | sort_by_date(&work); |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 829 | while (work) { |
| 830 | struct commit * work_item = pop_commit(&work); |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 831 | struct sort_node * work_node = (struct sort_node *) getter(work_item); |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 832 | struct commit_list * parents = work_item->parents; |
| 833 | |
| 834 | while (parents) { |
| 835 | struct commit * parent=parents->item; |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 836 | struct sort_node * pn = (struct sort_node *) getter(parent); |
| 837 | |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 838 | if (pn) { |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 839 | /* |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 840 | * parents are only enqueued for emission |
| 841 | * when all their children have been emitted thereby |
| 842 | * guaranteeing topological order. |
| 843 | */ |
| 844 | pn->indegree--; |
Junio C Hamano | 4c8725f | 2006-02-15 22:05:33 -0800 | [diff] [blame] | 845 | if (!pn->indegree) { |
| 846 | if (!lifo) |
| 847 | insert_by_date(parent, &work); |
| 848 | else |
| 849 | commit_list_insert(parent, &work); |
| 850 | } |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 851 | } |
| 852 | parents=parents->next; |
| 853 | } |
| 854 | /* |
| 855 | * work_item is a commit all of whose children |
| 856 | * have already been emitted. we can emit it now. |
| 857 | */ |
| 858 | *pptr = work_node->list_item; |
| 859 | pptr = &(*pptr)->next; |
| 860 | *pptr = NULL; |
Fredrik Kuivinen | 6b6dcfc | 2006-03-10 10:21:37 +0100 | [diff] [blame] | 861 | setter(work_item, NULL); |
Jon Seymour | ab580ac | 2005-07-07 02:39:34 +1000 | [diff] [blame] | 862 | } |
| 863 | free(nodes); |
| 864 | } |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 865 | |
| 866 | /* merge-rebase stuff */ |
| 867 | |
Rene Scharfe | 31609c1 | 2006-07-02 01:29:26 +0200 | [diff] [blame] | 868 | /* bits #0..7 in revision.h */ |
| 869 | #define PARENT1 (1u<< 8) |
| 870 | #define PARENT2 (1u<< 9) |
Junio C Hamano | 542ccef | 2006-07-02 11:34:17 -0700 | [diff] [blame] | 871 | #define STALE (1u<<10) |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 872 | #define RESULT (1u<<11) |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 873 | |
| 874 | static struct commit *interesting(struct commit_list *list) |
| 875 | { |
| 876 | while (list) { |
| 877 | struct commit *commit = list->item; |
| 878 | list = list->next; |
Junio C Hamano | 542ccef | 2006-07-02 11:34:17 -0700 | [diff] [blame] | 879 | if (commit->object.flags & STALE) |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 880 | continue; |
| 881 | return commit; |
| 882 | } |
| 883 | return NULL; |
| 884 | } |
| 885 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 886 | static struct commit_list *merge_bases(struct commit *one, struct commit *two) |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 887 | { |
| 888 | struct commit_list *list = NULL; |
| 889 | struct commit_list *result = NULL; |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 890 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 891 | if (one == two) |
| 892 | /* We do not mark this even with RESULT so we do not |
| 893 | * have to clean it up. |
| 894 | */ |
| 895 | return commit_list_insert(one, &result); |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 896 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 897 | parse_commit(one); |
| 898 | parse_commit(two); |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 899 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 900 | one->object.flags |= PARENT1; |
| 901 | two->object.flags |= PARENT2; |
| 902 | insert_by_date(one, &list); |
| 903 | insert_by_date(two, &list); |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 904 | |
| 905 | while (interesting(list)) { |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 906 | struct commit *commit; |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 907 | struct commit_list *parents; |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 908 | struct commit_list *n; |
| 909 | int flags; |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 910 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 911 | commit = list->item; |
| 912 | n = list->next; |
| 913 | free(list); |
| 914 | list = n; |
| 915 | |
| 916 | flags = commit->object.flags & (PARENT1 | PARENT2 | STALE); |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 917 | if (flags == (PARENT1 | PARENT2)) { |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 918 | if (!(commit->object.flags & RESULT)) { |
| 919 | commit->object.flags |= RESULT; |
| 920 | insert_by_date(commit, &result); |
| 921 | } |
Junio C Hamano | 542ccef | 2006-07-02 11:34:17 -0700 | [diff] [blame] | 922 | /* Mark parents of a found merge stale */ |
| 923 | flags |= STALE; |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 924 | } |
| 925 | parents = commit->parents; |
| 926 | while (parents) { |
| 927 | struct commit *p = parents->item; |
| 928 | parents = parents->next; |
| 929 | if ((p->object.flags & flags) == flags) |
| 930 | continue; |
| 931 | parse_commit(p); |
| 932 | p->object.flags |= flags; |
| 933 | insert_by_date(p, &list); |
| 934 | } |
| 935 | } |
| 936 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 937 | /* Clean up the result to remove stale ones */ |
| 938 | list = result; result = NULL; |
| 939 | while (list) { |
| 940 | struct commit_list *n = list->next; |
| 941 | if (!(list->item->object.flags & STALE)) |
| 942 | insert_by_date(list->item, &result); |
| 943 | free(list); |
| 944 | list = n; |
| 945 | } |
| 946 | return result; |
| 947 | } |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 948 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 949 | struct commit_list *get_merge_bases(struct commit *one, |
| 950 | struct commit *two, |
| 951 | int cleanup) |
| 952 | { |
| 953 | const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT); |
| 954 | struct commit_list *list; |
| 955 | struct commit **rslt; |
| 956 | struct commit_list *result; |
| 957 | int cnt, i, j; |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 958 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 959 | result = merge_bases(one, two); |
| 960 | if (one == two) |
| 961 | return result; |
| 962 | if (!result || !result->next) { |
| 963 | if (cleanup) { |
| 964 | clear_commit_marks(one, all_flags); |
| 965 | clear_commit_marks(two, all_flags); |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 966 | } |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 967 | return result; |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 968 | } |
| 969 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 970 | /* There are more than one */ |
| 971 | cnt = 0; |
| 972 | list = result; |
| 973 | while (list) { |
| 974 | list = list->next; |
| 975 | cnt++; |
| 976 | } |
| 977 | rslt = xcalloc(cnt, sizeof(*rslt)); |
| 978 | for (list = result, i = 0; list; list = list->next) |
| 979 | rslt[i++] = list->item; |
| 980 | free_commit_list(result); |
| 981 | |
| 982 | clear_commit_marks(one, all_flags); |
| 983 | clear_commit_marks(two, all_flags); |
| 984 | for (i = 0; i < cnt - 1; i++) { |
| 985 | for (j = i+1; j < cnt; j++) { |
| 986 | if (!rslt[i] || !rslt[j]) |
| 987 | continue; |
| 988 | result = merge_bases(rslt[i], rslt[j]); |
| 989 | clear_commit_marks(rslt[i], all_flags); |
| 990 | clear_commit_marks(rslt[j], all_flags); |
| 991 | for (list = result; list; list = list->next) { |
| 992 | if (rslt[i] == list->item) |
| 993 | rslt[i] = NULL; |
| 994 | if (rslt[j] == list->item) |
| 995 | rslt[j] = NULL; |
| 996 | } |
| 997 | } |
Junio C Hamano | 58ecf5c | 2006-07-04 17:45:22 -0700 | [diff] [blame] | 998 | } |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 999 | |
Junio C Hamano | f324943 | 2006-07-04 18:46:42 -0700 | [diff] [blame] | 1000 | /* Surviving ones in rslt[] are the independent results */ |
| 1001 | result = NULL; |
| 1002 | for (i = 0; i < cnt; i++) { |
| 1003 | if (rslt[i]) |
| 1004 | insert_by_date(rslt[i], &result); |
| 1005 | } |
| 1006 | free(rslt); |
Johannes Schindelin | 7c6f8aa | 2006-06-29 15:17:32 +0200 | [diff] [blame] | 1007 | return result; |
| 1008 | } |