Start the 2.46 cycle
[git.git] / builtin / check-mailmap.c
blobb8a05b8e07b523953dfe9e8d6e572346a27d7791
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 "strbuf.h"
8 #include "string-list.h"
9 #include "write-or-die.h"
11 static int use_stdin;
12 static const char * const check_mailmap_usage[] = {
13 N_("git check-mailmap [<options>] <contact>..."),
14 NULL
17 static const struct option check_mailmap_options[] = {
18 OPT_BOOL(0, "stdin", &use_stdin, N_("also read contacts from stdin")),
19 OPT_END()
22 static void check_mailmap(struct string_list *mailmap, const char *contact)
24 const char *name, *mail;
25 size_t namelen, maillen;
26 struct ident_split ident;
28 if (split_ident_line(&ident, contact, strlen(contact)))
29 die(_("unable to parse contact: %s"), contact);
31 name = ident.name_begin;
32 namelen = ident.name_end - ident.name_begin;
33 mail = ident.mail_begin;
34 maillen = ident.mail_end - ident.mail_begin;
36 map_user(mailmap, &mail, &maillen, &name, &namelen);
38 if (namelen)
39 printf("%.*s ", (int)namelen, name);
40 printf("<%.*s>\n", (int)maillen, mail);
43 int cmd_check_mailmap(int argc, const char **argv, const char *prefix)
45 int i;
46 struct string_list mailmap = STRING_LIST_INIT_NODUP;
48 git_config(git_default_config, NULL);
49 argc = parse_options(argc, argv, prefix, check_mailmap_options,
50 check_mailmap_usage, 0);
51 if (argc == 0 && !use_stdin)
52 die(_("no contacts specified"));
54 read_mailmap(&mailmap);
56 for (i = 0; i < argc; ++i)
57 check_mailmap(&mailmap, argv[i]);
58 maybe_flush_or_die(stdout, "stdout");
60 if (use_stdin) {
61 struct strbuf buf = STRBUF_INIT;
62 while (strbuf_getline_lf(&buf, stdin) != EOF) {
63 check_mailmap(&mailmap, buf.buf);
64 maybe_flush_or_die(stdout, "stdout");
66 strbuf_release(&buf);
69 clear_mailmap(&mailmap);
70 return 0;