shortlog: add grouping option
In preparation for adding more grouping types, let's refactor the committer/author grouping code and add a user-facing option that binds them together. In particular: - the main option is now "--group", to make it clear that the various group types are mutually exclusive. The "--committer" option is an alias for "--group=committer". - we keep an enum rather than a binary flag, to prepare for more values - we prefer switch statements to ternary assignment, since other group types will need more custom code Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Sep 27, 2020 at 04:39 UTC
92338c450bc41e8e3da31a10d2ab73844e8b0634
4 files changed
+66
-13
Documentation/git-shortlog.txt
+8
-1
@@ -47,9 +47,16 @@ OPTIONS
47
48
Each pretty-printed commit will be rewrapped before it is shown.
49
50
+--group=<type>::
51
+ Group commits based on `<type>`. If no `--group` option is
52
+ specified, the default is `author`. `<type>` is one of:
53
++
54
+ - `author`, commits are grouped by author
55
+ - `committer`, commits are grouped by committer (the same as `-c`)
56
+
57
-c::
58
--committer::
52
- Collect and show committer identities instead of authors.
59
+ This is an alias for `--group=committer`.
60
61
-w[<width>[,<indent1>[,<indent2>]]]::
62
Linewrap the output by wrapping each line at `width`. The first
builtin/shortlog.c
+48
-11
@@ -129,7 +129,17 @@ static void read_from_stdin(struct shortlog *log)
129
static const char *committer_match[2] = { "Commit: ", "committer " };
130
const char **match;
131
132
- match = log->committer ? committer_match : author_match;
132
+ switch (log->group) {
133
+ case SHORTLOG_GROUP_AUTHOR:
134
+ match = author_match;
135
+ break;
136
+ case SHORTLOG_GROUP_COMMITTER:
137
+ match = committer_match;
138
+ break;
139
+ default:
140
+ BUG("unhandled shortlog group");
141
+ }
142
+
143
while (strbuf_getline_lf(&ident, stdin) != EOF) {
144
const char *v;
145
if (!skip_prefix(ident.buf, match[0], &v) &&
@@ -158,7 +168,7 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
168
struct strbuf ident = STRBUF_INIT;
169
struct strbuf oneline = STRBUF_INIT;
170
struct pretty_print_context ctx = {0};
161
- const char *fmt;
171
+ const char *oneline_str;
172
173
ctx.fmt = CMIT_FMT_USERFORMAT;
174
ctx.abbrev = log->abbrev;
@@ -166,19 +176,28 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
176
ctx.date_mode.type = DATE_NORMAL;
177
ctx.output_encoding = get_log_output_encoding();
178
169
- fmt = log->committer ?
170
- (log->email ? "%cN <%cE>" : "%cN") :
171
- (log->email ? "%aN <%aE>" : "%aN");
172
-
173
- format_commit_message(commit, fmt, &ident, &ctx);
179
if (!log->summary) {
180
if (log->user_format)
181
pretty_print_commit(&ctx, commit, &oneline);
182
else
183
format_commit_message(commit, "%s", &oneline, &ctx);
184
}
180
-
181
- insert_one_record(log, ident.buf, oneline.len ? oneline.buf : "<none>");
185
+ oneline_str = oneline.len ? oneline.buf : "<none>";
186
+
187
+ switch (log->group) {
188
+ case SHORTLOG_GROUP_AUTHOR:
189
+ format_commit_message(commit,
190
+ log->email ? "%aN <%aE>" : "%aN",
191
+ &ident, &ctx);
192
+ insert_one_record(log, ident.buf, oneline_str);
193
+ break;
194
+ case SHORTLOG_GROUP_COMMITTER:
195
+ format_commit_message(commit,
196
+ log->email ? "%cN <%cE>" : "%cN",
197
+ &ident, &ctx);
198
+ insert_one_record(log, ident.buf, oneline_str);
199
+ break;
200
+ }
201
202
strbuf_release(&ident);
203
strbuf_release(&oneline);
@@ -241,6 +260,21 @@ static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
260
return 0;
261
}
262
263
+static int parse_group_option(const struct option *opt, const char *arg, int unset)
264
+{
265
+ struct shortlog *log = opt->value;
266
+
267
+ if (unset || !strcasecmp(arg, "author"))
268
+ log->group = SHORTLOG_GROUP_AUTHOR;
269
+ else if (!strcasecmp(arg, "committer"))
270
+ log->group = SHORTLOG_GROUP_COMMITTER;
271
+ else
272
+ return error(_("unknown group type: %s"), arg);
273
+
274
+ return 0;
275
+}
276
+
277
+
278
void shortlog_init(struct shortlog *log)
279
{
280
memset(log, 0, sizeof(*log));
@@ -260,8 +294,9 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
294
int nongit = !startup_info->have_repository;
295
296
const struct option options[] = {
263
- OPT_BOOL('c', "committer", &log.committer,
264
- N_("Group by committer rather than author")),
297
+ OPT_SET_INT('c', "committer", &log.group,
298
+ N_("Group by committer rather than author"),
299
+ SHORTLOG_GROUP_COMMITTER),
300
OPT_BOOL('n', "numbered", &log.sort_by_number,
301
N_("sort output according to the number of commits per author")),
302
OPT_BOOL('s', "summary", &log.summary,
@@ -271,6 +306,8 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
306
OPT_CALLBACK_F('w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
307
N_("Linewrap output"), PARSE_OPT_OPTARG,
308
&parse_wrap_args),
309
+ OPT_CALLBACK(0, "group", &log, N_("field"),
310
+ N_("Group by field"), parse_group_option),
311
OPT_END(),
312
};
313
shortlog.h
+5
-1
@@ -15,7 +15,11 @@ struct shortlog {
15
int in2;
16
int user_format;
17
int abbrev;
18
- int committer;
18
+
19
+ enum {
20
+ SHORTLOG_GROUP_AUTHOR = 0,
21
+ SHORTLOG_GROUP_COMMITTER,
22
+ } group;
23
24
char *common_repo_prefix;
25
int email;
t/t4201-shortlog.sh
+5
@@ -215,4 +215,9 @@ test_expect_success 'shortlog --committer (external)' '
215
test_cmp expect actual
216
'
217
218
+test_expect_success '--group=committer is the same as --committer' '
219
+ git shortlog -ns --group=committer HEAD >actual &&
220
+ test_cmp expect actual
221
+'
222
+
223
test_done