]>
Commit | Line | Data |
---|---|---|
b8ec5923 JS |
1 | #include "builtin.h" |
2 | #include "cache.h" | |
b2141fc1 | 3 | #include "config.h" |
b8ec5923 JS |
4 | #include "commit.h" |
5 | #include "diff.h" | |
c455c87c | 6 | #include "string-list.h" |
b8ec5923 | 7 | #include "revision.h" |
3714e7c8 | 8 | #include "utf8.h" |
7c1c6782 | 9 | #include "mailmap.h" |
552bcac3 | 10 | #include "shortlog.h" |
14ec9cbd | 11 | #include "parse-options.h" |
47beb37b | 12 | #include "trailer.h" |
b8ec5923 | 13 | |
14ec9cbd | 14 | static char const * const shortlog_usage[] = { |
cd56d4e5 MÅ |
15 | N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"), |
16 | N_("git log --pretty=short | git shortlog [<options>]"), | |
14ec9cbd PH |
17 | NULL |
18 | }; | |
b8ec5923 | 19 | |
9b21a34a JK |
20 | /* |
21 | * The util field of our string_list_items will contain one of two things: | |
22 | * | |
23 | * - if --summary is not in use, it will point to a string list of the | |
24 | * oneline subjects assigned to this author | |
25 | * | |
26 | * - if --summary is in use, we don't need that list; we only need to know | |
27 | * its size. So we abuse the pointer slot to store our integer counter. | |
28 | * | |
29 | * This macro accesses the latter. | |
30 | */ | |
31 | #define UTIL_TO_INT(x) ((intptr_t)(x)->util) | |
32 | ||
33 | static int compare_by_counter(const void *a1, const void *a2) | |
34 | { | |
35 | const struct string_list_item *i1 = a1, *i2 = a2; | |
36 | return UTIL_TO_INT(i2) - UTIL_TO_INT(i1); | |
37 | } | |
38 | ||
39 | static int compare_by_list(const void *a1, const void *a2) | |
b8ec5923 | 40 | { |
c455c87c JS |
41 | const struct string_list_item *i1 = a1, *i2 = a2; |
42 | const struct string_list *l1 = i1->util, *l2 = i2->util; | |
b8ec5923 JS |
43 | |
44 | if (l1->nr < l2->nr) | |
6d6ab610 | 45 | return 1; |
b8ec5923 JS |
46 | else if (l1->nr == l2->nr) |
47 | return 0; | |
48 | else | |
6d6ab610 | 49 | return -1; |
b8ec5923 JS |
50 | } |
51 | ||
552bcac3 | 52 | static void insert_one_record(struct shortlog *log, |
45d93eb8 | 53 | const char *ident, |
1e931cb4 | 54 | const char *oneline) |
b8ec5923 | 55 | { |
c455c87c | 56 | struct string_list_item *item; |
1e931cb4 | 57 | |
45d93eb8 | 58 | item = string_list_insert(&log->list, ident); |
b8ec5923 | 59 | |
ed7eba90 | 60 | if (log->summary) |
9b21a34a | 61 | item->util = (void *)(UTIL_TO_INT(item) + 1); |
ed7eba90 JK |
62 | else { |
63 | const char *dot3 = log->common_repo_prefix; | |
64 | char *buffer, *p; | |
65 | struct strbuf subject = STRBUF_INIT; | |
66 | const char *eol; | |
67 | ||
68 | /* Skip any leading whitespace, including any blank lines. */ | |
69 | while (*oneline && isspace(*oneline)) | |
70 | oneline++; | |
71 | eol = strchr(oneline, '\n'); | |
72 | if (!eol) | |
73 | eol = oneline + strlen(oneline); | |
74 | if (starts_with(oneline, "[PATCH")) { | |
75 | char *eob = strchr(oneline, ']'); | |
76 | if (eob && (!eol || eob < eol)) | |
77 | oneline = eob + 1; | |
78 | } | |
79 | while (*oneline && isspace(*oneline) && *oneline != '\n') | |
80 | oneline++; | |
81 | format_subject(&subject, oneline, " "); | |
82 | buffer = strbuf_detach(&subject, NULL); | |
b8ec5923 | 83 | |
ed7eba90 JK |
84 | if (dot3) { |
85 | int dot3len = strlen(dot3); | |
86 | if (dot3len > 5) { | |
87 | while ((p = strstr(buffer, dot3)) != NULL) { | |
88 | int taillen = strlen(p) - dot3len; | |
89 | memcpy(p, "/.../", 5); | |
90 | memmove(p + 5, p + dot3len, taillen + 1); | |
91 | } | |
c95044d4 JH |
92 | } |
93 | } | |
b8ec5923 | 94 | |
9b21a34a JK |
95 | if (item->util == NULL) |
96 | item->util = xcalloc(1, sizeof(struct string_list)); | |
ed7eba90 JK |
97 | string_list_append(item->util, buffer); |
98 | } | |
b8ec5923 JS |
99 | } |
100 | ||
87abb962 JK |
101 | static int parse_ident(struct shortlog *log, |
102 | struct strbuf *out, const char *in) | |
1ab03a57 JK |
103 | { |
104 | const char *mailbuf, *namebuf; | |
105 | size_t namelen, maillen; | |
106 | struct ident_split ident; | |
107 | ||
108 | if (split_ident_line(&ident, in, strlen(in))) | |
109 | return -1; | |
110 | ||
111 | namebuf = ident.name_begin; | |
112 | mailbuf = ident.mail_begin; | |
113 | namelen = ident.name_end - ident.name_begin; | |
114 | maillen = ident.mail_end - ident.mail_begin; | |
115 | ||
116 | map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen); | |
117 | strbuf_add(out, namebuf, namelen); | |
118 | if (log->email) | |
119 | strbuf_addf(out, " <%.*s>", (int)maillen, mailbuf); | |
120 | ||
121 | return 0; | |
122 | } | |
123 | ||
552bcac3 | 124 | static void read_from_stdin(struct shortlog *log) |
b8ec5923 | 125 | { |
45d93eb8 JK |
126 | struct strbuf ident = STRBUF_INIT; |
127 | struct strbuf mapped_ident = STRBUF_INIT; | |
50250491 | 128 | struct strbuf oneline = STRBUF_INIT; |
fbfda15f LT |
129 | static const char *author_match[2] = { "Author: ", "author " }; |
130 | static const char *committer_match[2] = { "Commit: ", "committer " }; | |
131 | const char **match; | |
1e931cb4 | 132 | |
63d24fa0 JK |
133 | if (HAS_MULTI_BITS(log->groups)) |
134 | die(_("using multiple --group options with stdin is not supported")); | |
135 | ||
136 | switch (log->groups) { | |
92338c45 JK |
137 | case SHORTLOG_GROUP_AUTHOR: |
138 | match = author_match; | |
139 | break; | |
140 | case SHORTLOG_GROUP_COMMITTER: | |
141 | match = committer_match; | |
142 | break; | |
47beb37b JK |
143 | case SHORTLOG_GROUP_TRAILER: |
144 | die(_("using --group=trailer with stdin is not supported")); | |
92338c45 JK |
145 | default: |
146 | BUG("unhandled shortlog group"); | |
147 | } | |
148 | ||
45d93eb8 | 149 | while (strbuf_getline_lf(&ident, stdin) != EOF) { |
5c3894c3 | 150 | const char *v; |
45d93eb8 JK |
151 | if (!skip_prefix(ident.buf, match[0], &v) && |
152 | !skip_prefix(ident.buf, match[1], &v)) | |
1e931cb4 | 153 | continue; |
a1c5405a | 154 | while (strbuf_getline_lf(&oneline, stdin) != EOF && |
50250491 | 155 | oneline.len) |
1e931cb4 | 156 | ; /* discard headers */ |
a1c5405a | 157 | while (strbuf_getline_lf(&oneline, stdin) != EOF && |
50250491 | 158 | !oneline.len) |
1e931cb4 | 159 | ; /* discard blanks */ |
1ab03a57 | 160 | |
45d93eb8 | 161 | strbuf_reset(&mapped_ident); |
87abb962 | 162 | if (parse_ident(log, &mapped_ident, v) < 0) |
1ab03a57 JK |
163 | continue; |
164 | ||
45d93eb8 | 165 | insert_one_record(log, mapped_ident.buf, oneline.buf); |
b8ec5923 | 166 | } |
45d93eb8 JK |
167 | strbuf_release(&ident); |
168 | strbuf_release(&mapped_ident); | |
50250491 | 169 | strbuf_release(&oneline); |
b8ec5923 JS |
170 | } |
171 | ||
f17b0b99 JK |
172 | struct strset_item { |
173 | struct hashmap_entry ent; | |
174 | char value[FLEX_ARRAY]; | |
175 | }; | |
176 | ||
177 | struct strset { | |
178 | struct hashmap map; | |
179 | }; | |
180 | ||
181 | #define STRSET_INIT { { NULL } } | |
182 | ||
183 | static int strset_item_hashcmp(const void *hash_data, | |
184 | const struct hashmap_entry *entry, | |
185 | const struct hashmap_entry *entry_or_key, | |
186 | const void *keydata) | |
187 | { | |
188 | const struct strset_item *a, *b; | |
189 | ||
190 | a = container_of(entry, const struct strset_item, ent); | |
191 | if (keydata) | |
192 | return strcmp(a->value, keydata); | |
193 | ||
194 | b = container_of(entry_or_key, const struct strset_item, ent); | |
195 | return strcmp(a->value, b->value); | |
196 | } | |
197 | ||
198 | /* | |
199 | * Adds "str" to the set if it was not already present; returns true if it was | |
200 | * already there. | |
201 | */ | |
202 | static int strset_check_and_add(struct strset *ss, const char *str) | |
203 | { | |
204 | unsigned int hash = strhash(str); | |
205 | struct strset_item *item; | |
206 | ||
207 | if (!ss->map.table) | |
208 | hashmap_init(&ss->map, strset_item_hashcmp, NULL, 0); | |
209 | ||
210 | if (hashmap_get_from_hash(&ss->map, hash, str)) | |
211 | return 1; | |
212 | ||
213 | FLEX_ALLOC_STR(item, value, str); | |
214 | hashmap_entry_init(&item->ent, hash); | |
215 | hashmap_add(&ss->map, &item->ent); | |
216 | return 0; | |
217 | } | |
218 | ||
219 | static void strset_clear(struct strset *ss) | |
220 | { | |
221 | if (!ss->map.table) | |
222 | return; | |
6da1a258 | 223 | hashmap_clear_and_free(&ss->map, struct strset_item, ent); |
f17b0b99 JK |
224 | } |
225 | ||
47beb37b | 226 | static void insert_records_from_trailers(struct shortlog *log, |
63d24fa0 | 227 | struct strset *dups, |
47beb37b JK |
228 | struct commit *commit, |
229 | struct pretty_print_context *ctx, | |
230 | const char *oneline) | |
231 | { | |
232 | struct trailer_iterator iter; | |
233 | const char *commit_buffer, *body; | |
56d5dde7 | 234 | struct strbuf ident = STRBUF_INIT; |
47beb37b JK |
235 | |
236 | /* | |
237 | * Using format_commit_message("%B") would be simpler here, but | |
238 | * this saves us copying the message. | |
239 | */ | |
240 | commit_buffer = logmsg_reencode(commit, NULL, ctx->output_encoding); | |
241 | body = strstr(commit_buffer, "\n\n"); | |
242 | if (!body) | |
243 | return; | |
244 | ||
245 | trailer_iterator_init(&iter, body); | |
246 | while (trailer_iterator_advance(&iter)) { | |
247 | const char *value = iter.val.buf; | |
248 | ||
63d24fa0 | 249 | if (!string_list_has_string(&log->trailers, iter.key.buf)) |
47beb37b JK |
250 | continue; |
251 | ||
56d5dde7 JK |
252 | strbuf_reset(&ident); |
253 | if (!parse_ident(log, &ident, value)) | |
254 | value = ident.buf; | |
255 | ||
63d24fa0 | 256 | if (strset_check_and_add(dups, value)) |
f17b0b99 | 257 | continue; |
47beb37b JK |
258 | insert_one_record(log, value, oneline); |
259 | } | |
260 | trailer_iterator_release(&iter); | |
261 | ||
56d5dde7 | 262 | strbuf_release(&ident); |
47beb37b JK |
263 | unuse_commit_buffer(commit, commit_buffer); |
264 | } | |
265 | ||
552bcac3 | 266 | void shortlog_add_commit(struct shortlog *log, struct commit *commit) |
b8ec5923 | 267 | { |
45d93eb8 | 268 | struct strbuf ident = STRBUF_INIT; |
2db6b83d | 269 | struct strbuf oneline = STRBUF_INIT; |
63d24fa0 | 270 | struct strset dups = STRSET_INIT; |
2db6b83d | 271 | struct pretty_print_context ctx = {0}; |
92338c45 | 272 | const char *oneline_str; |
2db6b83d JK |
273 | |
274 | ctx.fmt = CMIT_FMT_USERFORMAT; | |
275 | ctx.abbrev = log->abbrev; | |
6d167fd7 | 276 | ctx.print_email_subject = 1; |
2db6b83d JK |
277 | ctx.date_mode.type = DATE_NORMAL; |
278 | ctx.output_encoding = get_log_output_encoding(); | |
279 | ||
4e1d1a2e JK |
280 | if (!log->summary) { |
281 | if (log->user_format) | |
282 | pretty_print_commit(&ctx, commit, &oneline); | |
552bcac3 | 283 | else |
4e1d1a2e | 284 | format_commit_message(commit, "%s", &oneline, &ctx); |
cd4f09e3 | 285 | } |
92338c45 JK |
286 | oneline_str = oneline.len ? oneline.buf : "<none>"; |
287 | ||
63d24fa0 JK |
288 | if (log->groups & SHORTLOG_GROUP_AUTHOR) { |
289 | strbuf_reset(&ident); | |
92338c45 JK |
290 | format_commit_message(commit, |
291 | log->email ? "%aN <%aE>" : "%aN", | |
292 | &ident, &ctx); | |
63d24fa0 JK |
293 | if (!HAS_MULTI_BITS(log->groups) || |
294 | !strset_check_and_add(&dups, ident.buf)) | |
295 | insert_one_record(log, ident.buf, oneline_str); | |
296 | } | |
297 | if (log->groups & SHORTLOG_GROUP_COMMITTER) { | |
298 | strbuf_reset(&ident); | |
92338c45 JK |
299 | format_commit_message(commit, |
300 | log->email ? "%cN <%cE>" : "%cN", | |
301 | &ident, &ctx); | |
63d24fa0 JK |
302 | if (!HAS_MULTI_BITS(log->groups) || |
303 | !strset_check_and_add(&dups, ident.buf)) | |
304 | insert_one_record(log, ident.buf, oneline_str); | |
305 | } | |
306 | if (log->groups & SHORTLOG_GROUP_TRAILER) { | |
307 | insert_records_from_trailers(log, &dups, commit, &ctx, oneline_str); | |
92338c45 | 308 | } |
2db6b83d | 309 | |
63d24fa0 | 310 | strset_clear(&dups); |
45d93eb8 | 311 | strbuf_release(&ident); |
2db6b83d | 312 | strbuf_release(&oneline); |
552bcac3 DB |
313 | } |
314 | ||
315 | static void get_from_rev(struct rev_info *rev, struct shortlog *log) | |
316 | { | |
317 | struct commit *commit; | |
318 | ||
319 | if (prepare_revision_walk(rev)) | |
ab8b53bb | 320 | die(_("revision walk setup failed")); |
552bcac3 DB |
321 | while ((commit = get_revision(rev)) != NULL) |
322 | shortlog_add_commit(log, commit); | |
b8ec5923 JS |
323 | } |
324 | ||
14ec9cbd | 325 | static int parse_uint(char const **arg, int comma, int defval) |
3d711d97 JH |
326 | { |
327 | unsigned long ul; | |
328 | int ret; | |
329 | char *endp; | |
330 | ||
331 | ul = strtoul(*arg, &endp, 10); | |
14ec9cbd | 332 | if (*endp && *endp != comma) |
3d711d97 | 333 | return -1; |
14ec9cbd | 334 | if (ul > INT_MAX) |
3d711d97 | 335 | return -1; |
14ec9cbd PH |
336 | ret = *arg == endp ? defval : (int)ul; |
337 | *arg = *endp ? endp + 1 : endp; | |
3d711d97 JH |
338 | return ret; |
339 | } | |
340 | ||
341 | static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]"; | |
342 | #define DEFAULT_WRAPLEN 76 | |
343 | #define DEFAULT_INDENT1 6 | |
344 | #define DEFAULT_INDENT2 9 | |
345 | ||
14ec9cbd | 346 | static int parse_wrap_args(const struct option *opt, const char *arg, int unset) |
3d711d97 | 347 | { |
14ec9cbd PH |
348 | struct shortlog *log = opt->value; |
349 | ||
350 | log->wrap_lines = !unset; | |
351 | if (unset) | |
352 | return 0; | |
353 | if (!arg) { | |
354 | log->wrap = DEFAULT_WRAPLEN; | |
355 | log->in1 = DEFAULT_INDENT1; | |
356 | log->in2 = DEFAULT_INDENT2; | |
357 | return 0; | |
358 | } | |
359 | ||
360 | log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN); | |
361 | log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1); | |
362 | log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2); | |
363 | if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0) | |
364 | return error(wrap_arg_usage); | |
365 | if (log->wrap && | |
366 | ((log->in1 && log->wrap <= log->in1) || | |
367 | (log->in2 && log->wrap <= log->in2))) | |
368 | return error(wrap_arg_usage); | |
369 | return 0; | |
3d711d97 JH |
370 | } |
371 | ||
92338c45 JK |
372 | static int parse_group_option(const struct option *opt, const char *arg, int unset) |
373 | { | |
374 | struct shortlog *log = opt->value; | |
47beb37b | 375 | const char *field; |
92338c45 | 376 | |
63d24fa0 JK |
377 | if (unset) { |
378 | log->groups = 0; | |
379 | string_list_clear(&log->trailers, 0); | |
380 | } else if (!strcasecmp(arg, "author")) | |
381 | log->groups |= SHORTLOG_GROUP_AUTHOR; | |
92338c45 | 382 | else if (!strcasecmp(arg, "committer")) |
63d24fa0 | 383 | log->groups |= SHORTLOG_GROUP_COMMITTER; |
47beb37b | 384 | else if (skip_prefix(arg, "trailer:", &field)) { |
63d24fa0 JK |
385 | log->groups |= SHORTLOG_GROUP_TRAILER; |
386 | string_list_append(&log->trailers, field); | |
47beb37b | 387 | } else |
92338c45 JK |
388 | return error(_("unknown group type: %s"), arg); |
389 | ||
390 | return 0; | |
391 | } | |
392 | ||
393 | ||
552bcac3 DB |
394 | void shortlog_init(struct shortlog *log) |
395 | { | |
396 | memset(log, 0, sizeof(*log)); | |
397 | ||
d551a488 | 398 | read_mailmap(&log->mailmap, &log->common_repo_prefix); |
552bcac3 | 399 | |
c455c87c | 400 | log->list.strdup_strings = 1; |
552bcac3 DB |
401 | log->wrap = DEFAULT_WRAPLEN; |
402 | log->in1 = DEFAULT_INDENT1; | |
403 | log->in2 = DEFAULT_INDENT2; | |
63d24fa0 JK |
404 | log->trailers.strdup_strings = 1; |
405 | log->trailers.cmp = strcasecmp; | |
552bcac3 DB |
406 | } |
407 | ||
b8ec5923 JS |
408 | int cmd_shortlog(int argc, const char **argv, const char *prefix) |
409 | { | |
64093fc0 JK |
410 | struct shortlog log = { STRING_LIST_INIT_NODUP }; |
411 | struct rev_info rev; | |
773b69bf | 412 | int nongit = !startup_info->have_repository; |
552bcac3 | 413 | |
64093fc0 | 414 | const struct option options[] = { |
63d24fa0 JK |
415 | OPT_BIT('c', "committer", &log.groups, |
416 | N_("Group by committer rather than author"), | |
417 | SHORTLOG_GROUP_COMMITTER), | |
d5d09d47 SB |
418 | OPT_BOOL('n', "numbered", &log.sort_by_number, |
419 | N_("sort output according to the number of commits per author")), | |
420 | OPT_BOOL('s', "summary", &log.summary, | |
421 | N_("Suppress commit descriptions, only provides commit count")), | |
422 | OPT_BOOL('e', "email", &log.email, | |
423 | N_("Show the email address of each author")), | |
203c8533 | 424 | OPT_CALLBACK_F('w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"), |
5f0df44c | 425 | N_("Linewrap output"), PARSE_OPT_OPTARG, |
203c8533 | 426 | &parse_wrap_args), |
92338c45 JK |
427 | OPT_CALLBACK(0, "group", &log, N_("field"), |
428 | N_("Group by field"), parse_group_option), | |
14ec9cbd PH |
429 | OPT_END(), |
430 | }; | |
431 | ||
432 | struct parse_opt_ctx_t ctx; | |
433 | ||
d551a488 | 434 | git_config(git_default_config, NULL); |
552bcac3 | 435 | shortlog_init(&log); |
2abf3503 | 436 | repo_init_revisions(the_repository, &rev, prefix); |
9ca1169f SB |
437 | parse_options_start(&ctx, argc, argv, prefix, options, |
438 | PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0); | |
14ec9cbd PH |
439 | |
440 | for (;;) { | |
14ec9cbd PH |
441 | switch (parse_options_step(&ctx, options, shortlog_usage)) { |
442 | case PARSE_OPT_HELP: | |
3bb0923f | 443 | case PARSE_OPT_ERROR: |
14ec9cbd | 444 | exit(129); |
a92ec7ef NTND |
445 | case PARSE_OPT_COMPLETE: |
446 | exit(0); | |
14ec9cbd PH |
447 | case PARSE_OPT_DONE: |
448 | goto parse_done; | |
3d711d97 | 449 | } |
6b61ec05 | 450 | parse_revision_opt(&rev, &ctx, options, shortlog_usage); |
14ec9cbd PH |
451 | } |
452 | parse_done: | |
453 | argc = parse_options_end(&ctx); | |
454 | ||
4aa0161e MÅ |
455 | if (nongit && argc > 1) { |
456 | error(_("too many arguments given outside repository")); | |
457 | usage_with_options(shortlog_usage, options); | |
458 | } | |
459 | ||
14ec9cbd | 460 | if (setup_revisions(argc, argv, &rev, NULL) != 1) { |
ab8b53bb | 461 | error(_("unrecognized argument: %s"), argv[1]); |
14ec9cbd | 462 | usage_with_options(shortlog_usage, options); |
b8ec5923 JS |
463 | } |
464 | ||
b526f8ed | 465 | log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT; |
c1977021 | 466 | log.abbrev = rev.abbrev; |
7f7d712b | 467 | log.file = rev.diffopt.file; |
b526f8ed | 468 | |
63d24fa0 JK |
469 | if (!log.groups) |
470 | log.groups = SHORTLOG_GROUP_AUTHOR; | |
471 | string_list_sort(&log.trailers); | |
472 | ||
3384a2df | 473 | /* assume HEAD if from a tty */ |
abe549e1 | 474 | if (!nongit && !rev.pending.nr && isatty(0)) |
3384a2df | 475 | add_head_to_pending(&rev); |
0497c620 | 476 | if (rev.pending.nr == 0) { |
37314495 | 477 | if (isatty(0)) |
ab8b53bb | 478 | fprintf(stderr, _("(reading log message from standard input)\n")); |
552bcac3 | 479 | read_from_stdin(&log); |
0497c620 | 480 | } |
b8ec5923 | 481 | else |
552bcac3 | 482 | get_from_rev(&rev, &log); |
b8ec5923 | 483 | |
552bcac3 | 484 | shortlog_output(&log); |
7f7d712b JS |
485 | if (log.file != stdout) |
486 | fclose(log.file); | |
552bcac3 DB |
487 | return 0; |
488 | } | |
b8ec5923 | 489 | |
bb96a2c9 RS |
490 | static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s, |
491 | const struct shortlog *log) | |
492 | { | |
5b597082 SP |
493 | strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap); |
494 | strbuf_addch(sb, '\n'); | |
bb96a2c9 RS |
495 | } |
496 | ||
552bcac3 DB |
497 | void shortlog_output(struct shortlog *log) |
498 | { | |
499 | int i, j; | |
bb96a2c9 RS |
500 | struct strbuf sb = STRBUF_INIT; |
501 | ||
552bcac3 | 502 | if (log->sort_by_number) |
1b5294de | 503 | QSORT(log->list.items, log->list.nr, |
9b21a34a | 504 | log->summary ? compare_by_counter : compare_by_list); |
552bcac3 | 505 | for (i = 0; i < log->list.nr; i++) { |
9b21a34a | 506 | const struct string_list_item *item = &log->list.items[i]; |
552bcac3 | 507 | if (log->summary) { |
0a7b3577 JS |
508 | fprintf(log->file, "%6d\t%s\n", |
509 | (int)UTIL_TO_INT(item), item->string); | |
ac60c94d | 510 | } else { |
9b21a34a | 511 | struct string_list *onelines = item->util; |
0a7b3577 JS |
512 | fprintf(log->file, "%s (%d):\n", |
513 | item->string, onelines->nr); | |
3714e7c8 | 514 | for (j = onelines->nr - 1; j >= 0; j--) { |
c455c87c | 515 | const char *msg = onelines->items[j].string; |
3d711d97 | 516 | |
552bcac3 | 517 | if (log->wrap_lines) { |
bb96a2c9 RS |
518 | strbuf_reset(&sb); |
519 | add_wrapped_shortlog_msg(&sb, msg, log); | |
0a7b3577 | 520 | fwrite(sb.buf, sb.len, 1, log->file); |
3d711d97 JH |
521 | } |
522 | else | |
0a7b3577 | 523 | fprintf(log->file, " %s\n", msg); |
3714e7c8 | 524 | } |
0a7b3577 | 525 | putc('\n', log->file); |
9b21a34a JK |
526 | onelines->strdup_strings = 1; |
527 | string_list_clear(onelines, 0); | |
528 | free(onelines); | |
b8ec5923 JS |
529 | } |
530 | ||
552bcac3 | 531 | log->list.items[i].util = NULL; |
b8ec5923 JS |
532 | } |
533 | ||
bb96a2c9 | 534 | strbuf_release(&sb); |
c455c87c JS |
535 | log->list.strdup_strings = 1; |
536 | string_list_clear(&log->list, 1); | |
d20d654f | 537 | clear_mailmap(&log->mailmap); |
b8ec5923 | 538 | } |