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