Merge branch 'km/svn-1.8-serf-only'
[git] / builtin / check-mailmap.c
1 #include "builtin.h"
2 #include "mailmap.h"
3 #include "parse-options.h"
4 #include "string-list.h"
5
6 static int use_stdin;
7 static const char * const check_mailmap_usage[] = {
8 N_("git check-mailmap [options] <contact>..."),
9 NULL
10 };
11
12 static const struct option check_mailmap_options[] = {
13         OPT_BOOL(0, "stdin", &use_stdin, N_("also read contacts from stdin")),
14         OPT_END()
15 };
16
17 static 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
38 int 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;
57                 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
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 }