shortlog.c 9 KB
Newer Older
Johannes Schindelin's avatar
Johannes Schindelin committed
1 2 3 4
#include "builtin.h"
#include "cache.h"
#include "commit.h"
#include "diff.h"
5
#include "string-list.h"
Johannes Schindelin's avatar
Johannes Schindelin committed
6
#include "revision.h"
7
#include "utf8.h"
8
#include "mailmap.h"
Daniel Barkalow's avatar
Daniel Barkalow committed
9
#include "shortlog.h"
10
#include "parse-options.h"
Johannes Schindelin's avatar
Johannes Schindelin committed
11

12
static char const * const shortlog_usage[] = {
Junio C Hamano's avatar
Junio C Hamano committed
13
	"git shortlog [-n] [-s] [-e] [-w] [rev-opts] [--] [<commit-id>... ]",
14 15 16 17
	"",
	"[rev-opts] are documented in git-rev-list(1)",
	NULL
};
Johannes Schindelin's avatar
Johannes Schindelin committed
18 19 20

static int compare_by_number(const void *a1, const void *a2)
{
21 22
	const struct string_list_item *i1 = a1, *i2 = a2;
	const struct string_list *l1 = i1->util, *l2 = i2->util;
Johannes Schindelin's avatar
Johannes Schindelin committed
23 24

	if (l1->nr < l2->nr)
Johannes Schindelin's avatar
Johannes Schindelin committed
25
		return 1;
Johannes Schindelin's avatar
Johannes Schindelin committed
26 27 28
	else if (l1->nr == l2->nr)
		return 0;
	else
Johannes Schindelin's avatar
Johannes Schindelin committed
29
		return -1;
Johannes Schindelin's avatar
Johannes Schindelin committed
30 31
}

32 33 34
const char *format_subject(struct strbuf *sb, const char *msg,
			   const char *line_separator);

Daniel Barkalow's avatar
Daniel Barkalow committed
35
static void insert_one_record(struct shortlog *log,
36 37
			      const char *author,
			      const char *oneline)
Johannes Schindelin's avatar
Johannes Schindelin committed
38
{
Daniel Barkalow's avatar
Daniel Barkalow committed
39
	const char *dot3 = log->common_repo_prefix;
Johannes Schindelin's avatar
Johannes Schindelin committed
40
	char *buffer, *p;
41
	struct string_list_item *item;
42
	char namebuf[1024];
43
	char emailbuf[1024];
44 45 46
	size_t len;
	const char *eol;
	const char *boemail, *eoemail;
47
	struct strbuf subject = STRBUF_INIT;
48 49 50 51 52 53 54

	boemail = strchr(author, '<');
	if (!boemail)
		return;
	eoemail = strchr(boemail, '>');
	if (!eoemail)
		return;
55 56 57 58

	/* copy author name to namebuf, to support matching on both name and email */
	memcpy(namebuf, author, boemail - author);
	len = boemail - author;
59
	while (len > 0 && isspace(namebuf[len-1]))
60 61 62 63 64 65 66 67
		len--;
	namebuf[len] = 0;

	/* copy email name to emailbuf, to allow email replacement as well */
	memcpy(emailbuf, boemail+1, eoemail - boemail);
	emailbuf[eoemail - boemail - 1] = 0;

	if (!map_user(&log->mailmap, emailbuf, sizeof(emailbuf), namebuf, sizeof(namebuf))) {
68 69 70 71 72 73 74 75 76 77
		while (author < boemail && isspace(*author))
			author++;
		for (len = 0;
		     len < sizeof(namebuf) - 1 && author + len < boemail;
		     len++)
			namebuf[len] = author[len];
		while (0 < len && isspace(namebuf[len-1]))
			len--;
		namebuf[len] = '\0';
	}
78 79 80
	else
		len = strlen(namebuf);

Daniel Barkalow's avatar
Daniel Barkalow committed
81
	if (log->email) {
82
		size_t room = sizeof(namebuf) - len - 1;
83 84
		int maillen = strlen(emailbuf);
		snprintf(namebuf + len, room, " <%.*s>", maillen, emailbuf);
85
	}
Johannes Schindelin's avatar
Johannes Schindelin committed
86

87
	item = string_list_insert(&log->list, namebuf);
Johannes Schindelin's avatar
Johannes Schindelin committed
88
	if (item->util == NULL)
89
		item->util = xcalloc(1, sizeof(struct string_list));
Johannes Schindelin's avatar
Johannes Schindelin committed
90

91 92 93
	/* Skip any leading whitespace, including any blank lines. */
	while (*oneline && isspace(*oneline))
		oneline++;
94 95 96
	eol = strchr(oneline, '\n');
	if (!eol)
		eol = oneline + strlen(oneline);
97
	if (!prefixcmp(oneline, "[PATCH")) {
98
		char *eob = strchr(oneline, ']');
99 100
		if (eob && (!eol || eob < eol))
			oneline = eob + 1;
Johannes Schindelin's avatar
Johannes Schindelin committed
101
	}
102
	while (*oneline && isspace(*oneline) && *oneline != '\n')
Johannes Schindelin's avatar
Johannes Schindelin committed
103
		oneline++;
104 105
	format_subject(&subject, oneline, " ");
	buffer = strbuf_detach(&subject, NULL);
Johannes Schindelin's avatar
Johannes Schindelin committed
106

107 108 109 110 111 112 113 114 115
	if (dot3) {
		int dot3len = strlen(dot3);
		if (dot3len > 5) {
			while ((p = strstr(buffer, dot3)) != NULL) {
				int taillen = strlen(p) - dot3len;
				memcpy(p, "/.../", 5);
				memmove(p + 5, p + dot3len, taillen + 1);
			}
		}
Johannes Schindelin's avatar
Johannes Schindelin committed
116 117
	}

118
	string_list_append(item->util, buffer);
Johannes Schindelin's avatar
Johannes Schindelin committed
119 120
}

Daniel Barkalow's avatar
Daniel Barkalow committed
121
static void read_from_stdin(struct shortlog *log)
Johannes Schindelin's avatar
Johannes Schindelin committed
122
{
123 124 125 126 127 128 129 130 131 132 133 134
	char author[1024], oneline[1024];

	while (fgets(author, sizeof(author), stdin) != NULL) {
		if (!(author[0] == 'A' || author[0] == 'a') ||
		    prefixcmp(author + 1, "uthor: "))
			continue;
		while (fgets(oneline, sizeof(oneline), stdin) &&
		       oneline[0] != '\n')
			; /* discard headers */
		while (fgets(oneline, sizeof(oneline), stdin) &&
		       oneline[0] == '\n')
			; /* discard blanks */
Daniel Barkalow's avatar
Daniel Barkalow committed
135
		insert_one_record(log, author + 8, oneline);
Johannes Schindelin's avatar
Johannes Schindelin committed
136 137 138
	}
}

Daniel Barkalow's avatar
Daniel Barkalow committed
139
void shortlog_add_commit(struct shortlog *log, struct commit *commit)
Johannes Schindelin's avatar
Johannes Schindelin committed
140
{
Daniel Barkalow's avatar
Daniel Barkalow committed
141
	const char *author = NULL, *buffer;
142 143
	struct strbuf buf = STRBUF_INIT;
	struct strbuf ufbuf = STRBUF_INIT;
144
	struct pretty_print_context ctx = {0};
Johannes Schindelin's avatar
Johannes Schindelin committed
145

146
	pretty_print_commit(CMIT_FMT_RAW, commit, &buf, &ctx);
147
	buffer = buf.buf;
Daniel Barkalow's avatar
Daniel Barkalow committed
148 149
	while (*buffer && *buffer != '\n') {
		const char *eol = strchr(buffer, '\n');
Johannes Schindelin's avatar
Johannes Schindelin committed
150

Daniel Barkalow's avatar
Daniel Barkalow committed
151 152 153 154
		if (eol == NULL)
			eol = buffer + strlen(buffer);
		else
			eol++;
Johannes Schindelin's avatar
Johannes Schindelin committed
155

Daniel Barkalow's avatar
Daniel Barkalow committed
156 157 158
		if (!prefixcmp(buffer, "author "))
			author = buffer + 7;
		buffer = eol;
Johannes Schindelin's avatar
Johannes Schindelin committed
159
	}
Daniel Barkalow's avatar
Daniel Barkalow committed
160 161 162
	if (!author)
		die("Missing author: %s",
		    sha1_to_hex(commit->object.sha1));
163
	if (log->user_format) {
164
		struct pretty_print_context ctx = {0};
165
		ctx.abbrev = log->abbrev;
166 167 168
		ctx.subject = "";
		ctx.after_subject = "";
		ctx.date_mode = DATE_NORMAL;
169
		pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &ufbuf, &ctx);
170 171
		buffer = ufbuf.buf;
	} else if (*buffer) {
Daniel Barkalow's avatar
Daniel Barkalow committed
172
		buffer++;
173
	}
Daniel Barkalow's avatar
Daniel Barkalow committed
174
	insert_one_record(log, author, !*buffer ? "<none>" : buffer);
175 176
	strbuf_release(&ufbuf);
	strbuf_release(&buf);
Daniel Barkalow's avatar
Daniel Barkalow committed
177 178 179 180 181 182 183 184 185 186
}

static void get_from_rev(struct rev_info *rev, struct shortlog *log)
{
	struct commit *commit;

	if (prepare_revision_walk(rev))
		die("revision walk setup failed");
	while ((commit = get_revision(rev)) != NULL)
		shortlog_add_commit(log, commit);
Johannes Schindelin's avatar
Johannes Schindelin committed
187 188
}

189
static int parse_uint(char const **arg, int comma, int defval)
190 191 192 193 194 195
{
	unsigned long ul;
	int ret;
	char *endp;

	ul = strtoul(*arg, &endp, 10);
196
	if (*endp && *endp != comma)
197
		return -1;
198
	if (ul > INT_MAX)
199
		return -1;
200 201
	ret = *arg == endp ? defval : (int)ul;
	*arg = *endp ? endp + 1 : endp;
202 203 204 205 206 207 208 209
	return ret;
}

static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
#define DEFAULT_WRAPLEN 76
#define DEFAULT_INDENT1 6
#define DEFAULT_INDENT2 9

210
static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
211
{
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
	struct shortlog *log = opt->value;

	log->wrap_lines = !unset;
	if (unset)
		return 0;
	if (!arg) {
		log->wrap = DEFAULT_WRAPLEN;
		log->in1 = DEFAULT_INDENT1;
		log->in2 = DEFAULT_INDENT2;
		return 0;
	}

	log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
	log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
	log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
	if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
		return error(wrap_arg_usage);
	if (log->wrap &&
	    ((log->in1 && log->wrap <= log->in1) ||
	     (log->in2 && log->wrap <= log->in2)))
		return error(wrap_arg_usage);
	return 0;
234 235
}

Daniel Barkalow's avatar
Daniel Barkalow committed
236 237 238 239
void shortlog_init(struct shortlog *log)
{
	memset(log, 0, sizeof(*log));

240
	read_mailmap(&log->mailmap, &log->common_repo_prefix);
Daniel Barkalow's avatar
Daniel Barkalow committed
241

242
	log->list.strdup_strings = 1;
Daniel Barkalow's avatar
Daniel Barkalow committed
243 244 245 246 247
	log->wrap = DEFAULT_WRAPLEN;
	log->in1 = DEFAULT_INDENT1;
	log->in2 = DEFAULT_INDENT2;
}

Johannes Schindelin's avatar
Johannes Schindelin committed
248 249
int cmd_shortlog(int argc, const char **argv, const char *prefix)
{
250 251
	static struct shortlog log;
	static struct rev_info rev;
252
	int nongit = !startup_info->have_repository;
Daniel Barkalow's avatar
Daniel Barkalow committed
253

254 255 256 257 258 259 260 261 262 263 264 265 266 267
	static const struct option options[] = {
		OPT_BOOLEAN('n', "numbered", &log.sort_by_number,
			    "sort output according to the number of commits per author"),
		OPT_BOOLEAN('s', "summary", &log.summary,
			    "Suppress commit descriptions, only provides commit count"),
		OPT_BOOLEAN('e', "email", &log.email,
			    "Show the email address of each author"),
		{ OPTION_CALLBACK, 'w', NULL, &log, "w[,i1[,i2]]",
			"Linewrap output", PARSE_OPT_OPTARG, &parse_wrap_args },
		OPT_END(),
	};

	struct parse_opt_ctx_t ctx;

268
	git_config(git_default_config, NULL);
Daniel Barkalow's avatar
Daniel Barkalow committed
269
	shortlog_init(&log);
270
	init_revisions(&rev, prefix);
271
	parse_options_start(&ctx, argc, argv, prefix, PARSE_OPT_KEEP_DASHDASH |
272 273 274 275 276 277 278 279
			    PARSE_OPT_KEEP_ARGV0);

	for (;;) {
		switch (parse_options_step(&ctx, options, shortlog_usage)) {
		case PARSE_OPT_HELP:
			exit(129);
		case PARSE_OPT_DONE:
			goto parse_done;
280
		}
281
		parse_revision_opt(&rev, &ctx, options, shortlog_usage);
282 283 284 285 286 287 288
	}
parse_done:
	argc = parse_options_end(&ctx);

	if (setup_revisions(argc, argv, &rev, NULL) != 1) {
		error("unrecognized argument: %s", argv[1]);
		usage_with_options(shortlog_usage, options);
Johannes Schindelin's avatar
Johannes Schindelin committed
289 290
	}

291
	log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
292
	log.abbrev = rev.abbrev;
293

294
	/* assume HEAD if from a tty */
295
	if (!nongit && !rev.pending.nr && isatty(0))
296
		add_head_to_pending(&rev);
297
	if (rev.pending.nr == 0) {
298 299
		if (isatty(0))
			fprintf(stderr, "(reading log message from standard input)\n");
Daniel Barkalow's avatar
Daniel Barkalow committed
300
		read_from_stdin(&log);
301
	}
Johannes Schindelin's avatar
Johannes Schindelin committed
302
	else
Daniel Barkalow's avatar
Daniel Barkalow committed
303
		get_from_rev(&rev, &log);
Johannes Schindelin's avatar
Johannes Schindelin committed
304

Daniel Barkalow's avatar
Daniel Barkalow committed
305 306 307
	shortlog_output(&log);
	return 0;
}
Johannes Schindelin's avatar
Johannes Schindelin committed
308

309 310 311 312 313 314 315 316
static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
				     const struct shortlog *log)
{
	int col = strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
	if (col != log->wrap)
		strbuf_addch(sb, '\n');
}

Daniel Barkalow's avatar
Daniel Barkalow committed
317 318 319
void shortlog_output(struct shortlog *log)
{
	int i, j;
320 321
	struct strbuf sb = STRBUF_INIT;

Daniel Barkalow's avatar
Daniel Barkalow committed
322
	if (log->sort_by_number)
323
		qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
Daniel Barkalow's avatar
Daniel Barkalow committed
324 325
			compare_by_number);
	for (i = 0; i < log->list.nr; i++) {
326
		struct string_list *onelines = log->list.items[i].util;
Johannes Schindelin's avatar
Johannes Schindelin committed
327

Daniel Barkalow's avatar
Daniel Barkalow committed
328
		if (log->summary) {
329
			printf("%6d\t%s\n", onelines->nr, log->list.items[i].string);
330
		} else {
331
			printf("%s (%d):\n", log->list.items[i].string, onelines->nr);
332
			for (j = onelines->nr - 1; j >= 0; j--) {
333
				const char *msg = onelines->items[j].string;
334

Daniel Barkalow's avatar
Daniel Barkalow committed
335
				if (log->wrap_lines) {
336 337 338
					strbuf_reset(&sb);
					add_wrapped_shortlog_msg(&sb, msg, log);
					fwrite(sb.buf, sb.len, 1, stdout);
339 340 341
				}
				else
					printf("      %s\n", msg);
342 343
			}
			putchar('\n');
Johannes Schindelin's avatar
Johannes Schindelin committed
344 345
		}

346
		onelines->strdup_strings = 1;
347
		string_list_clear(onelines, 0);
Johannes Schindelin's avatar
Johannes Schindelin committed
348
		free(onelines);
Daniel Barkalow's avatar
Daniel Barkalow committed
349
		log->list.items[i].util = NULL;
Johannes Schindelin's avatar
Johannes Schindelin committed
350 351
	}

352
	strbuf_release(&sb);
353 354
	log->list.strdup_strings = 1;
	string_list_clear(&log->list, 1);
355
	clear_mailmap(&log->mailmap);
Johannes Schindelin's avatar
Johannes Schindelin committed
356
}