blob: b8a05b8e07b523953dfe9e8d6e572346a27d7791 [file] [log] [blame]
Eric Sunshine226ad342013-07-12 20:53:10 -04001#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00003#include "gettext.h"
Elijah Newrenb5fa6082023-02-24 00:09:29 +00004#include "ident.h"
Eric Sunshine226ad342013-07-12 20:53:10 -04005#include "mailmap.h"
6#include "parse-options.h"
Elijah Newren88e4e182023-05-16 06:34:02 +00007#include "strbuf.h"
Eric Sunshine226ad342013-07-12 20:53:10 -04008#include "string-list.h"
Elijah Newrend48be352023-03-21 06:26:07 +00009#include "write-or-die.h"
Eric Sunshine226ad342013-07-12 20:53:10 -040010
11static int use_stdin;
12static const char * const check_mailmap_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070013N_("git check-mailmap [<options>] <contact>..."),
Eric Sunshine226ad342013-07-12 20:53:10 -040014NULL
15};
16
17static const struct option check_mailmap_options[] = {
18 OPT_BOOL(0, "stdin", &use_stdin, N_("also read contacts from stdin")),
19 OPT_END()
20};
21
22static void check_mailmap(struct string_list *mailmap, const char *contact)
23{
24 const char *name, *mail;
25 size_t namelen, maillen;
26 struct ident_split ident;
27
28 if (split_ident_line(&ident, contact, strlen(contact)))
29 die(_("unable to parse contact: %s"), contact);
30
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;
35
36 map_user(mailmap, &mail, &maillen, &name, &namelen);
37
38 if (namelen)
39 printf("%.*s ", (int)namelen, name);
40 printf("<%.*s>\n", (int)maillen, mail);
41}
42
43int cmd_check_mailmap(int argc, const char **argv, const char *prefix)
44{
45 int i;
46 struct string_list mailmap = STRING_LIST_INIT_NODUP;
47
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"));
53
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +010054 read_mailmap(&mailmap);
Eric Sunshine226ad342013-07-12 20:53:10 -040055
56 for (i = 0; i < argc; ++i)
57 check_mailmap(&mailmap, argv[i]);
58 maybe_flush_or_die(stdout, "stdout");
59
60 if (use_stdin) {
61 struct strbuf buf = STRBUF_INIT;
Junio C Hamano8f309ae2016-01-13 15:31:17 -080062 while (strbuf_getline_lf(&buf, stdin) != EOF) {
Eric Sunshine226ad342013-07-12 20:53:10 -040063 check_mailmap(&mailmap, buf.buf);
64 maybe_flush_or_die(stdout, "stdout");
65 }
66 strbuf_release(&buf);
67 }
68
69 clear_mailmap(&mailmap);
70 return 0;
71}