Matthias Kestenholz | 25f38f0 | 2006-08-02 23:52:00 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 2 | #include "cache.h" |
| 3 | #include "commit.h" |
| 4 | #include "diff.h" |
| 5 | #include "revision.h" |
| 6 | #include "tag.h" |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 7 | #include "string-list.h" |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 8 | |
Pierre Habouzit | c8ef038 | 2008-10-02 14:59:18 +0200 | [diff] [blame] | 9 | static const char * const fmt_merge_msg_usage[] = { |
| 10 | "git fmt-merge-msg [--log|--no-log] [--file <file>]", |
| 11 | NULL |
| 12 | }; |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 13 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 14 | static int merge_summary; |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 15 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 16 | static int fmt_merge_msg_config(const char *key, const char *value, void *cb) |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 17 | { |
SZEDER Gábor | 6cd9cfe | 2008-04-06 03:23:45 +0200 | [diff] [blame] | 18 | static int found_merge_log = 0; |
| 19 | if (!strcmp("merge.log", key)) { |
| 20 | found_merge_log = 1; |
| 21 | merge_summary = git_config_bool(key, value); |
| 22 | } |
| 23 | if (!found_merge_log && !strcmp("merge.summary", key)) |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 24 | merge_summary = git_config_bool(key, value); |
| 25 | return 0; |
| 26 | } |
| 27 | |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 28 | struct src_data { |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 29 | struct string_list branch, tag, r_branch, generic; |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 30 | int head_status; |
| 31 | }; |
| 32 | |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 33 | void init_src_data(struct src_data *data) |
| 34 | { |
| 35 | data->branch.strdup_strings = 1; |
| 36 | data->tag.strdup_strings = 1; |
| 37 | data->r_branch.strdup_strings = 1; |
| 38 | data->generic.strdup_strings = 1; |
| 39 | } |
| 40 | |
| 41 | static struct string_list srcs = { NULL, 0, 0, 1 }; |
| 42 | static struct string_list origins = { NULL, 0, 0, 1 }; |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 43 | |
| 44 | static int handle_line(char *line) |
| 45 | { |
| 46 | int i, len = strlen(line); |
| 47 | unsigned char *sha1; |
| 48 | char *src, *origin; |
| 49 | struct src_data *src_data; |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 50 | struct string_list_item *item; |
Junio C Hamano | e918c6a | 2006-07-12 22:21:05 -0700 | [diff] [blame] | 51 | int pulling_head = 0; |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 52 | |
| 53 | if (len < 43 || line[40] != '\t') |
| 54 | return 1; |
| 55 | |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 56 | if (!prefixcmp(line + 41, "not-for-merge")) |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 57 | return 0; |
| 58 | |
| 59 | if (line[41] != '\t') |
| 60 | return 2; |
| 61 | |
| 62 | line[40] = 0; |
| 63 | sha1 = xmalloc(20); |
| 64 | i = get_sha1(line, sha1); |
| 65 | line[40] = '\t'; |
| 66 | if (i) |
| 67 | return 3; |
| 68 | |
| 69 | if (line[len - 1] == '\n') |
| 70 | line[len - 1] = 0; |
| 71 | line += 42; |
| 72 | |
| 73 | src = strstr(line, " of "); |
| 74 | if (src) { |
| 75 | *src = 0; |
| 76 | src += 4; |
Junio C Hamano | e918c6a | 2006-07-12 22:21:05 -0700 | [diff] [blame] | 77 | pulling_head = 0; |
| 78 | } else { |
| 79 | src = line; |
| 80 | pulling_head = 1; |
| 81 | } |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 82 | |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 83 | item = unsorted_string_list_lookup(&srcs, src); |
| 84 | if (!item) { |
| 85 | item = string_list_append(src, &srcs); |
| 86 | item->util = xcalloc(1, sizeof(struct src_data)); |
| 87 | init_src_data(item->util); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 88 | } |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 89 | src_data = item->util; |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 90 | |
Junio C Hamano | e918c6a | 2006-07-12 22:21:05 -0700 | [diff] [blame] | 91 | if (pulling_head) { |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 92 | origin = src; |
Junio C Hamano | e918c6a | 2006-07-12 22:21:05 -0700 | [diff] [blame] | 93 | src_data->head_status |= 1; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 94 | } else if (!prefixcmp(line, "branch ")) { |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 95 | origin = line + 7; |
| 96 | string_list_append(origin, &src_data->branch); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 97 | src_data->head_status |= 2; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 98 | } else if (!prefixcmp(line, "tag ")) { |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 99 | origin = line; |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 100 | string_list_append(origin + 4, &src_data->tag); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 101 | src_data->head_status |= 2; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 102 | } else if (!prefixcmp(line, "remote branch ")) { |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 103 | origin = line + 14; |
| 104 | string_list_append(origin, &src_data->r_branch); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 105 | src_data->head_status |= 2; |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 106 | } else { |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 107 | origin = src; |
| 108 | string_list_append(line, &src_data->generic); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 109 | src_data->head_status |= 2; |
| 110 | } |
| 111 | |
| 112 | if (!strcmp(".", src) || !strcmp(src, origin)) { |
| 113 | int len = strlen(origin); |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 114 | if (origin[0] == '\'' && origin[len - 1] == '\'') |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 115 | origin = xmemdupz(origin + 1, len - 2); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 116 | } else { |
Jonas Fonseca | 2d7320d | 2006-09-01 00:32:39 +0200 | [diff] [blame] | 117 | char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 118 | sprintf(new_origin, "%s of %s", origin, src); |
| 119 | origin = new_origin; |
| 120 | } |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 121 | string_list_append(origin, &origins)->util = sha1; |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static void print_joined(const char *singular, const char *plural, |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 126 | struct string_list *list, struct strbuf *out) |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 127 | { |
| 128 | if (list->nr == 0) |
| 129 | return; |
| 130 | if (list->nr == 1) { |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 131 | strbuf_addf(out, "%s%s", singular, list->items[0].string); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 132 | } else { |
| 133 | int i; |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 134 | strbuf_addstr(out, plural); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 135 | for (i = 0; i < list->nr - 1; i++) |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 136 | strbuf_addf(out, "%s%s", i > 0 ? ", " : "", |
| 137 | list->items[i].string); |
| 138 | strbuf_addf(out, " and %s", list->items[list->nr - 1].string); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | static void shortlog(const char *name, unsigned char *sha1, |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 143 | struct commit *head, struct rev_info *rev, int limit, |
| 144 | struct strbuf *out) |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 145 | { |
| 146 | int i, count = 0; |
| 147 | struct commit *commit; |
| 148 | struct object *branch; |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 149 | struct string_list subjects = { NULL, 0, 0, 1 }; |
Linus Torvalds | 7dc0fe3 | 2007-11-12 23:16:08 -0800 | [diff] [blame] | 150 | int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED; |
Stephen Boyd | 15cb500 | 2010-03-24 00:16:01 -0700 | [diff] [blame] | 151 | struct strbuf sb = STRBUF_INIT; |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 152 | |
| 153 | branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 154 | if (!branch || branch->type != OBJ_COMMIT) |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 155 | return; |
| 156 | |
| 157 | setup_revisions(0, NULL, rev, NULL); |
| 158 | rev->ignore_merges = 1; |
| 159 | add_pending_object(rev, branch, name); |
| 160 | add_pending_object(rev, &head->object, "^HEAD"); |
| 161 | head->object.flags |= UNINTERESTING; |
Martin Koegler | 3d51e1b | 2008-02-18 08:31:56 +0100 | [diff] [blame] | 162 | if (prepare_revision_walk(rev)) |
| 163 | die("revision walk setup failed"); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 164 | while ((commit = get_revision(rev)) != NULL) { |
Stephen Boyd | 15cb500 | 2010-03-24 00:16:01 -0700 | [diff] [blame] | 165 | struct pretty_print_context ctx = {0}; |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 166 | |
| 167 | /* ignore merges */ |
| 168 | if (commit->parents && commit->parents->next) |
| 169 | continue; |
| 170 | |
| 171 | count++; |
| 172 | if (subjects.nr > limit) |
| 173 | continue; |
| 174 | |
Stephen Boyd | 15cb500 | 2010-03-24 00:16:01 -0700 | [diff] [blame] | 175 | format_commit_message(commit, "%s", &sb, &ctx); |
| 176 | strbuf_ltrim(&sb); |
Linus Torvalds | 6a28518 | 2008-04-15 11:01:36 -0700 | [diff] [blame] | 177 | |
Stephen Boyd | 15cb500 | 2010-03-24 00:16:01 -0700 | [diff] [blame] | 178 | if (!sb.len) |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 179 | string_list_append(sha1_to_hex(commit->object.sha1), |
| 180 | &subjects); |
Stephen Boyd | 15cb500 | 2010-03-24 00:16:01 -0700 | [diff] [blame] | 181 | else |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 182 | string_list_append(strbuf_detach(&sb, NULL), &subjects); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | if (count > limit) |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 186 | strbuf_addf(out, "\n* %s: (%d commits)\n", name, count); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 187 | else |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 188 | strbuf_addf(out, "\n* %s:\n", name); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 189 | |
| 190 | for (i = 0; i < subjects.nr; i++) |
| 191 | if (i >= limit) |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 192 | strbuf_addf(out, " ...\n"); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 193 | else |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 194 | strbuf_addf(out, " %s\n", subjects.items[i].string); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 195 | |
| 196 | clear_commit_marks((struct commit *)branch, flags); |
| 197 | clear_commit_marks(head, flags); |
| 198 | free_commit_list(rev->commits); |
| 199 | rev->commits = NULL; |
| 200 | rev->pending.nr = 0; |
| 201 | |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 202 | string_list_clear(&subjects, 0); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Tay Ray Chuan | 403994e | 2010-05-11 01:17:50 +0800 | [diff] [blame] | 205 | static void do_fmt_merge_msg_title(struct strbuf *out, |
| 206 | const char *current_branch) { |
| 207 | int i = 0; |
Benjamin Kramer | fd13b21 | 2009-03-07 21:02:26 +0100 | [diff] [blame] | 208 | char *sep = ""; |
Stephen Boyd | 419fe5b | 2010-03-24 00:15:58 -0700 | [diff] [blame] | 209 | |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 210 | strbuf_addstr(out, "Merge "); |
| 211 | for (i = 0; i < srcs.nr; i++) { |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 212 | struct src_data *src_data = srcs.items[i].util; |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 213 | const char *subsep = ""; |
| 214 | |
| 215 | strbuf_addstr(out, sep); |
| 216 | sep = "; "; |
| 217 | |
| 218 | if (src_data->head_status == 1) { |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 219 | strbuf_addstr(out, srcs.items[i].string); |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 220 | continue; |
| 221 | } |
| 222 | if (src_data->head_status == 3) { |
| 223 | subsep = ", "; |
| 224 | strbuf_addstr(out, "HEAD"); |
| 225 | } |
| 226 | if (src_data->branch.nr) { |
| 227 | strbuf_addstr(out, subsep); |
| 228 | subsep = ", "; |
| 229 | print_joined("branch ", "branches ", &src_data->branch, |
| 230 | out); |
| 231 | } |
| 232 | if (src_data->r_branch.nr) { |
| 233 | strbuf_addstr(out, subsep); |
| 234 | subsep = ", "; |
| 235 | print_joined("remote branch ", "remote branches ", |
| 236 | &src_data->r_branch, out); |
| 237 | } |
| 238 | if (src_data->tag.nr) { |
| 239 | strbuf_addstr(out, subsep); |
| 240 | subsep = ", "; |
| 241 | print_joined("tag ", "tags ", &src_data->tag, out); |
| 242 | } |
| 243 | if (src_data->generic.nr) { |
| 244 | strbuf_addstr(out, subsep); |
| 245 | print_joined("commit ", "commits ", &src_data->generic, |
| 246 | out); |
| 247 | } |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 248 | if (strcmp(".", srcs.items[i].string)) |
| 249 | strbuf_addf(out, " of %s", srcs.items[i].string); |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | if (!strcmp("master", current_branch)) |
| 253 | strbuf_addch(out, '\n'); |
| 254 | else |
| 255 | strbuf_addf(out, " into %s\n", current_branch); |
Tay Ray Chuan | 403994e | 2010-05-11 01:17:50 +0800 | [diff] [blame] | 256 | } |
| 257 | |
Tay Ray Chuan | 8c6bdfd | 2010-05-11 01:17:51 +0800 | [diff] [blame] | 258 | static int do_fmt_merge_msg(int merge_title, int merge_summary, |
| 259 | struct strbuf *in, struct strbuf *out) { |
Tay Ray Chuan | 403994e | 2010-05-11 01:17:50 +0800 | [diff] [blame] | 260 | int limit = 20, i = 0, pos = 0; |
| 261 | unsigned char head_sha1[20]; |
| 262 | const char *current_branch; |
| 263 | |
| 264 | /* get current branch */ |
| 265 | current_branch = resolve_ref("HEAD", head_sha1, 1, NULL); |
| 266 | if (!current_branch) |
| 267 | die("No current branch"); |
| 268 | if (!prefixcmp(current_branch, "refs/heads/")) |
| 269 | current_branch += 11; |
| 270 | |
| 271 | /* get a line */ |
| 272 | while (pos < in->len) { |
| 273 | int len; |
| 274 | char *newline, *p = in->buf + pos; |
| 275 | |
| 276 | newline = strchr(p, '\n'); |
| 277 | len = newline ? newline - p : strlen(p); |
| 278 | pos += len + !!newline; |
| 279 | i++; |
| 280 | p[len] = 0; |
| 281 | if (handle_line(p)) |
| 282 | die ("Error in line %d: %.*s", i, len, p); |
| 283 | } |
| 284 | |
| 285 | if (!srcs.nr) |
| 286 | return 0; |
| 287 | |
Tay Ray Chuan | 8c6bdfd | 2010-05-11 01:17:51 +0800 | [diff] [blame] | 288 | if (merge_title) |
| 289 | do_fmt_merge_msg_title(out, current_branch); |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 290 | |
| 291 | if (merge_summary) { |
| 292 | struct commit *head; |
| 293 | struct rev_info rev; |
| 294 | |
| 295 | head = lookup_commit(head_sha1); |
| 296 | init_revisions(&rev, NULL); |
| 297 | rev.commit_format = CMIT_FMT_ONELINE; |
| 298 | rev.ignore_merges = 1; |
| 299 | rev.limited = 1; |
| 300 | |
Tay Ray Chuan | f0ecac2 | 2010-05-11 01:17:52 +0800 | [diff] [blame] | 301 | if (suffixcmp(out->buf, "\n")) |
| 302 | strbuf_addch(out, '\n'); |
| 303 | |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 304 | for (i = 0; i < origins.nr; i++) |
Stephen Boyd | fcb243f | 2010-03-24 00:16:03 -0700 | [diff] [blame] | 305 | shortlog(origins.items[i].string, origins.items[i].util, |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 306 | head, &rev, limit, out); |
| 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
Tay Ray Chuan | 2234ec5 | 2010-05-11 01:17:49 +0800 | [diff] [blame] | 311 | int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) { |
Tay Ray Chuan | 8c6bdfd | 2010-05-11 01:17:51 +0800 | [diff] [blame] | 312 | return do_fmt_merge_msg(1, merge_summary, in, out); |
| 313 | } |
| 314 | |
| 315 | int fmt_merge_msg_shortlog(struct strbuf *in, struct strbuf *out) { |
| 316 | return do_fmt_merge_msg(0, 1, in, out); |
Tay Ray Chuan | 2234ec5 | 2010-05-11 01:17:49 +0800 | [diff] [blame] | 317 | } |
| 318 | |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 319 | int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix) |
| 320 | { |
Pierre Habouzit | c8ef038 | 2008-10-02 14:59:18 +0200 | [diff] [blame] | 321 | const char *inpath = NULL; |
| 322 | struct option options[] = { |
| 323 | OPT_BOOLEAN(0, "log", &merge_summary, "populate log with the shortlog"), |
Stephen Boyd | 63e6715 | 2010-03-24 00:16:04 -0700 | [diff] [blame] | 324 | { OPTION_BOOLEAN, 0, "summary", &merge_summary, NULL, |
| 325 | "alias for --log (deprecated)", |
| 326 | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN }, |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 327 | OPT_FILENAME('F', "file", &inpath, "file to read from"), |
Pierre Habouzit | c8ef038 | 2008-10-02 14:59:18 +0200 | [diff] [blame] | 328 | OPT_END() |
| 329 | }; |
| 330 | |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 331 | FILE *in = stdin; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 332 | struct strbuf input = STRBUF_INIT, output = STRBUF_INIT; |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 333 | int ret; |
| 334 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 335 | git_config(fmt_merge_msg_config, NULL); |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 336 | argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage, |
| 337 | 0); |
Pierre Habouzit | c8ef038 | 2008-10-02 14:59:18 +0200 | [diff] [blame] | 338 | if (argc > 0) |
| 339 | usage_with_options(fmt_merge_msg_usage, options); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 340 | |
Pierre Habouzit | c8ef038 | 2008-10-02 14:59:18 +0200 | [diff] [blame] | 341 | if (inpath && strcmp(inpath, "-")) { |
| 342 | in = fopen(inpath, "r"); |
| 343 | if (!in) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 344 | die_errno("cannot open '%s'", inpath); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 347 | if (strbuf_read(&input, fileno(in), 0) < 0) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 348 | die_errno("could not read input file"); |
Miklos Vajna | 0b9a969 | 2008-06-27 18:21:59 +0200 | [diff] [blame] | 349 | ret = fmt_merge_msg(merge_summary, &input, &output); |
| 350 | if (ret) |
| 351 | return ret; |
Pierre Habouzit | c8ef038 | 2008-10-02 14:59:18 +0200 | [diff] [blame] | 352 | write_in_full(STDOUT_FILENO, output.buf, output.len); |
Johannes Schindelin | 00449f9 | 2006-07-03 17:18:43 +0200 | [diff] [blame] | 353 | return 0; |
| 354 | } |