Skip to content

Commit 92338c4

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 45d93eb commit 92338c4

File tree

4 files changed

+66
-13
lines changed

4 files changed

+66
-13
lines changed

Documentation/git-shortlog.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,16 @@ OPTIONS
4747

4848
Each pretty-printed commit will be rewrapped before it is shown.
4949

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+
5057
-c::
5158
--committer::
52-
Collect and show committer identities instead of authors.
59+
This is an alias for `--group=committer`.
5360

5461
-w[<width>[,<indent1>[,<indent2>]]]::
5562
Linewrap the output by wrapping each line at `width`. The first

builtin/shortlog.c

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,17 @@ static void read_from_stdin(struct shortlog *log)
129129
static const char *committer_match[2] = { "Commit: ", "committer " };
130130
const char **match;
131131

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+
133143
while (strbuf_getline_lf(&ident, stdin) != EOF) {
134144
const char *v;
135145
if (!skip_prefix(ident.buf, match[0], &v) &&
@@ -158,27 +168,36 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
158168
struct strbuf ident = STRBUF_INIT;
159169
struct strbuf oneline = STRBUF_INIT;
160170
struct pretty_print_context ctx = {0};
161-
const char *fmt;
171+
const char *oneline_str;
162172

163173
ctx.fmt = CMIT_FMT_USERFORMAT;
164174
ctx.abbrev = log->abbrev;
165175
ctx.print_email_subject = 1;
166176
ctx.date_mode.type = DATE_NORMAL;
167177
ctx.output_encoding = get_log_output_encoding();
168178

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);
174179
if (!log->summary) {
175180
if (log->user_format)
176181
pretty_print_commit(&ctx, commit, &oneline);
177182
else
178183
format_commit_message(commit, "%s", &oneline, &ctx);
179184
}
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+
}
182201

183202
strbuf_release(&ident);
184203
strbuf_release(&oneline);
@@ -241,6 +260,21 @@ static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
241260
return 0;
242261
}
243262

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+
244278
void shortlog_init(struct shortlog *log)
245279
{
246280
memset(log, 0, sizeof(*log));
@@ -260,8 +294,9 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
260294
int nongit = !startup_info->have_repository;
261295

262296
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),
265300
OPT_BOOL('n', "numbered", &log.sort_by_number,
266301
N_("sort output according to the number of commits per author")),
267302
OPT_BOOL('s', "summary", &log.summary,
@@ -271,6 +306,8 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
271306
OPT_CALLBACK_F('w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
272307
N_("Linewrap output"), PARSE_OPT_OPTARG,
273308
&parse_wrap_args),
309+
OPT_CALLBACK(0, "group", &log, N_("field"),
310+
N_("Group by field"), parse_group_option),
274311
OPT_END(),
275312
};
276313

shortlog.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ struct shortlog {
1515
int in2;
1616
int user_format;
1717
int abbrev;
18-
int committer;
18+
19+
enum {
20+
SHORTLOG_GROUP_AUTHOR = 0,
21+
SHORTLOG_GROUP_COMMITTER,
22+
} group;
1923

2024
char *common_repo_prefix;
2125
int email;

t/t4201-shortlog.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,9 @@ test_expect_success 'shortlog --committer (external)' '
215215
test_cmp expect actual
216216
'
217217

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+
218223
test_done

0 commit comments

Comments
 (0)