strbuf: introduce strbuf_getline_{lf,nul}()
[git/git.git] / builtin / check-mailmap.c
CommitLineData
226ad348
ES
1#include "builtin.h"
2#include "mailmap.h"
3#include "parse-options.h"
4#include "string-list.h"
5
6static int use_stdin;
7static const char * const check_mailmap_usage[] = {
9c9b4f2f 8N_("git check-mailmap [<options>] <contact>..."),
226ad348
ES
9NULL
10};
11
12static const struct option check_mailmap_options[] = {
13 OPT_BOOL(0, "stdin", &use_stdin, N_("also read contacts from stdin")),
14 OPT_END()
15};
16
17static void check_mailmap(struct string_list *mailmap, const char *contact)
18{
19 const char *name, *mail;
20 size_t namelen, maillen;
21 struct ident_split ident;
22
23 if (split_ident_line(&ident, contact, strlen(contact)))
24 die(_("unable to parse contact: %s"), contact);
25
26 name = ident.name_begin;
27 namelen = ident.name_end - ident.name_begin;
28 mail = ident.mail_begin;
29 maillen = ident.mail_end - ident.mail_begin;
30
31 map_user(mailmap, &mail, &maillen, &name, &namelen);
32
33 if (namelen)
34 printf("%.*s ", (int)namelen, name);
35 printf("<%.*s>\n", (int)maillen, mail);
36}
37
38int cmd_check_mailmap(int argc, const char **argv, const char *prefix)
39{
40 int i;
41 struct string_list mailmap = STRING_LIST_INIT_NODUP;
42
43 git_config(git_default_config, NULL);
44 argc = parse_options(argc, argv, prefix, check_mailmap_options,
45 check_mailmap_usage, 0);
46 if (argc == 0 && !use_stdin)
47 die(_("no contacts specified"));
48
49 read_mailmap(&mailmap, NULL);
50
51 for (i = 0; i < argc; ++i)
52 check_mailmap(&mailmap, argv[i]);
53 maybe_flush_or_die(stdout, "stdout");
54
55 if (use_stdin) {
56 struct strbuf buf = STRBUF_INIT;
8f309aeb 57 while (strbuf_getline_lf(&buf, stdin) != EOF) {
226ad348
ES
58 check_mailmap(&mailmap, buf.buf);
59 maybe_flush_or_die(stdout, "stdout");
60 }
61 strbuf_release(&buf);
62 }
63
64 clear_mailmap(&mailmap);
65 return 0;
66}