1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-11-01 14:57:52 +01:00
git/builtin/check-mailmap.c
Junio C Hamano 8f309aeb82 strbuf: introduce strbuf_getline_{lf,nul}()
The strbuf_getline() interface allows a byte other than LF or NUL as
the line terminator, but this is only because I wrote these
codepaths anticipating that there might be a value other than NUL
and LF that could be useful when I introduced line_termination long
time ago.  No useful caller that uses other value has emerged.

By now, it is clear that the interface is overly broad without a
good reason.  Many codepaths have hardcoded preference to read
either LF terminated or NUL terminated records from their input, and
then call strbuf_getline() with LF or NUL as the third parameter.

This step introduces two thin wrappers around strbuf_getline(),
namely, strbuf_getline_lf() and strbuf_getline_nul(), and
mechanically rewrites these call sites to call either one of
them.  The changes contained in this patch are:

 * introduction of these two functions in strbuf.[ch]

 * mechanical conversion of all callers to strbuf_getline() with
   either '\n' or '\0' as the third parameter to instead call the
   respective thin wrapper.

After this step, output from "git grep 'strbuf_getline('" would
become a lot smaller.  An interim goal of this series is to make
this an empty set, so that we can have strbuf_getline_crlf() take
over the shorter name strbuf_getline().

Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-01-15 10:12:51 -08:00

66 lines
1.6 KiB
C

#include "builtin.h"
#include "mailmap.h"
#include "parse-options.h"
#include "string-list.h"
static int use_stdin;
static const char * const check_mailmap_usage[] = {
N_("git check-mailmap [<options>] <contact>..."),
NULL
};
static const struct option check_mailmap_options[] = {
OPT_BOOL(0, "stdin", &use_stdin, N_("also read contacts from stdin")),
OPT_END()
};
static void check_mailmap(struct string_list *mailmap, const char *contact)
{
const char *name, *mail;
size_t namelen, maillen;
struct ident_split ident;
if (split_ident_line(&ident, contact, strlen(contact)))
die(_("unable to parse contact: %s"), contact);
name = ident.name_begin;
namelen = ident.name_end - ident.name_begin;
mail = ident.mail_begin;
maillen = ident.mail_end - ident.mail_begin;
map_user(mailmap, &mail, &maillen, &name, &namelen);
if (namelen)
printf("%.*s ", (int)namelen, name);
printf("<%.*s>\n", (int)maillen, mail);
}
int cmd_check_mailmap(int argc, const char **argv, const char *prefix)
{
int i;
struct string_list mailmap = STRING_LIST_INIT_NODUP;
git_config(git_default_config, NULL);
argc = parse_options(argc, argv, prefix, check_mailmap_options,
check_mailmap_usage, 0);
if (argc == 0 && !use_stdin)
die(_("no contacts specified"));
read_mailmap(&mailmap, NULL);
for (i = 0; i < argc; ++i)
check_mailmap(&mailmap, argv[i]);
maybe_flush_or_die(stdout, "stdout");
if (use_stdin) {
struct strbuf buf = STRBUF_INIT;
while (strbuf_getline_lf(&buf, stdin) != EOF) {
check_mailmap(&mailmap, buf.buf);
maybe_flush_or_die(stdout, "stdout");
}
strbuf_release(&buf);
}
clear_mailmap(&mailmap);
return 0;
}