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