]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/check-mailmap.c
rebase: allow overriding the maximal length of the generated labels
[thirdparty/git.git] / builtin / check-mailmap.c
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"
9
10 static int use_stdin;
11 static const char * const check_mailmap_usage[] = {
12 N_("git check-mailmap [<options>] <contact>..."),
13 NULL
14 };
15
16 static const struct option check_mailmap_options[] = {
17 OPT_BOOL(0, "stdin", &use_stdin, N_("also read contacts from stdin")),
18 OPT_END()
19 };
20
21 static void check_mailmap(struct string_list *mailmap, const char *contact)
22 {
23 const char *name, *mail;
24 size_t namelen, maillen;
25 struct ident_split ident;
26
27 if (split_ident_line(&ident, contact, strlen(contact)))
28 die(_("unable to parse contact: %s"), contact);
29
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;
34
35 map_user(mailmap, &mail, &maillen, &name, &namelen);
36
37 if (namelen)
38 printf("%.*s ", (int)namelen, name);
39 printf("<%.*s>\n", (int)maillen, mail);
40 }
41
42 int cmd_check_mailmap(int argc, const char **argv, const char *prefix)
43 {
44 int i;
45 struct string_list mailmap = STRING_LIST_INIT_NODUP;
46
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"));
52
53 read_mailmap(&mailmap);
54
55 for (i = 0; i < argc; ++i)
56 check_mailmap(&mailmap, argv[i]);
57 maybe_flush_or_die(stdout, "stdout");
58
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");
64 }
65 strbuf_release(&buf);
66 }
67
68 clear_mailmap(&mailmap);
69 return 0;
70 }