t0410: make test description clearer
[git/gitster.git] / builtin / check-mailmap.c
blobdf00b5ee13adb87881b8c1e92cac256e6ad319d1
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "gettext.h"
5 #include "ident.h"
6 #include "mailmap.h"
7 #include "parse-options.h"
8 #include "strbuf.h"
9 #include "string-list.h"
10 #include "write-or-die.h"
12 static int use_stdin;
13 static const char *mailmap_file, *mailmap_blob;
14 static const char * const check_mailmap_usage[] = {
15 N_("git check-mailmap [<options>] <contact>..."),
16 NULL
19 static const struct option check_mailmap_options[] = {
20 OPT_BOOL(0, "stdin", &use_stdin, N_("also read contacts from stdin")),
21 OPT_FILENAME(0, "mailmap-file", &mailmap_file, N_("read additional mailmap entries from file")),
22 OPT_STRING(0, "mailmap-blob", &mailmap_blob, N_("blob"), N_("read additional mailmap entries from blob")),
23 OPT_END()
26 static void check_mailmap(struct string_list *mailmap, const char *contact)
28 const char *name, *mail;
29 size_t namelen, maillen;
30 struct ident_split ident;
32 if (!split_ident_line(&ident, contact, strlen(contact))) {
33 name = ident.name_begin;
34 namelen = ident.name_end - ident.name_begin;
35 mail = ident.mail_begin;
36 maillen = ident.mail_end - ident.mail_begin;
37 } else {
38 name = NULL;
39 namelen = 0;
40 mail = contact;
41 maillen = strlen(contact);
44 map_user(mailmap, &mail, &maillen, &name, &namelen);
46 if (namelen)
47 printf("%.*s ", (int)namelen, name);
48 printf("<%.*s>\n", (int)maillen, mail);
51 int cmd_check_mailmap(int argc,
52 const char **argv,
53 const char *prefix,
54 struct repository *repo UNUSED)
56 int i;
57 struct string_list mailmap = STRING_LIST_INIT_NODUP;
59 git_config(git_default_config, NULL);
60 argc = parse_options(argc, argv, prefix, check_mailmap_options,
61 check_mailmap_usage, 0);
62 if (argc == 0 && !use_stdin)
63 die(_("no contacts specified"));
65 read_mailmap(&mailmap);
66 if (mailmap_blob)
67 read_mailmap_blob(&mailmap, mailmap_blob);
68 if (mailmap_file)
69 read_mailmap_file(&mailmap, mailmap_file, 0);
71 for (i = 0; i < argc; ++i)
72 check_mailmap(&mailmap, argv[i]);
73 maybe_flush_or_die(stdout, "stdout");
75 if (use_stdin) {
76 struct strbuf buf = STRBUF_INIT;
77 while (strbuf_getline_lf(&buf, stdin) != EOF) {
78 check_mailmap(&mailmap, buf.buf);
79 maybe_flush_or_die(stdout, "stdout");
81 strbuf_release(&buf);
84 clear_mailmap(&mailmap);
85 return 0;