ident.con commit Merge branch 'tg/worktree-add-existing-branch' (10174da)
   1/*
   2 * ident.c
   3 *
   4 * create git identifier lines of the form "name <email> date"
   5 *
   6 * Copyright (C) 2005 Linus Torvalds
   7 */
   8#include "cache.h"
   9#include "config.h"
  10
  11static struct strbuf git_default_name = STRBUF_INIT;
  12static struct strbuf git_default_email = STRBUF_INIT;
  13static struct strbuf git_default_date = STRBUF_INIT;
  14static int default_email_is_bogus;
  15static int default_name_is_bogus;
  16
  17static int ident_use_config_only;
  18
  19#define IDENT_NAME_GIVEN 01
  20#define IDENT_MAIL_GIVEN 02
  21#define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
  22static int committer_ident_explicitly_given;
  23static int author_ident_explicitly_given;
  24static int ident_config_given;
  25
  26#ifdef NO_GECOS_IN_PWENT
  27#define get_gecos(ignored) "&"
  28#else
  29#define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
  30#endif
  31
  32static struct passwd *xgetpwuid_self(int *is_bogus)
  33{
  34        struct passwd *pw;
  35
  36        errno = 0;
  37        pw = getpwuid(getuid());
  38        if (!pw) {
  39                static struct passwd fallback;
  40                fallback.pw_name = "unknown";
  41#ifndef NO_GECOS_IN_PWENT
  42                fallback.pw_gecos = "Unknown";
  43#endif
  44                pw = &fallback;
  45                if (is_bogus)
  46                        *is_bogus = 1;
  47        }
  48        return pw;
  49}
  50
  51static void copy_gecos(const struct passwd *w, struct strbuf *name)
  52{
  53        char *src;
  54
  55        /* Traditionally GECOS field had office phone numbers etc, separated
  56         * with commas.  Also & stands for capitalized form of the login name.
  57         */
  58
  59        for (src = get_gecos(w); *src && *src != ','; src++) {
  60                int ch = *src;
  61                if (ch != '&')
  62                        strbuf_addch(name, ch);
  63                else {
  64                        /* Sorry, Mr. McDonald... */
  65                        strbuf_addch(name, toupper(*w->pw_name));
  66                        strbuf_addstr(name, w->pw_name + 1);
  67                }
  68        }
  69}
  70
  71static int add_mailname_host(struct strbuf *buf)
  72{
  73        FILE *mailname;
  74        struct strbuf mailnamebuf = STRBUF_INIT;
  75
  76        mailname = fopen_or_warn("/etc/mailname", "r");
  77        if (!mailname)
  78                return -1;
  79
  80        if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
  81                if (ferror(mailname))
  82                        warning_errno("cannot read /etc/mailname");
  83                strbuf_release(&mailnamebuf);
  84                fclose(mailname);
  85                return -1;
  86        }
  87        /* success! */
  88        strbuf_addbuf(buf, &mailnamebuf);
  89        strbuf_release(&mailnamebuf);
  90        fclose(mailname);
  91        return 0;
  92}
  93
  94static int canonical_name(const char *host, struct strbuf *out)
  95{
  96        int status = -1;
  97
  98#ifndef NO_IPV6
  99        struct addrinfo hints, *ai;
 100        memset (&hints, '\0', sizeof (hints));
 101        hints.ai_flags = AI_CANONNAME;
 102        if (!getaddrinfo(host, NULL, &hints, &ai)) {
 103                if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) {
 104                        strbuf_addstr(out, ai->ai_canonname);
 105                        status = 0;
 106                }
 107                freeaddrinfo(ai);
 108        }
 109#else
 110        struct hostent *he = gethostbyname(host);
 111        if (he && strchr(he->h_name, '.')) {
 112                strbuf_addstr(out, he->h_name);
 113                status = 0;
 114        }
 115#endif /* NO_IPV6 */
 116
 117        return status;
 118}
 119
 120static void add_domainname(struct strbuf *out, int *is_bogus)
 121{
 122        char buf[HOST_NAME_MAX + 1];
 123
 124        if (xgethostname(buf, sizeof(buf))) {
 125                warning_errno("cannot get host name");
 126                strbuf_addstr(out, "(none)");
 127                *is_bogus = 1;
 128                return;
 129        }
 130        if (strchr(buf, '.'))
 131                strbuf_addstr(out, buf);
 132        else if (canonical_name(buf, out) < 0) {
 133                strbuf_addf(out, "%s.(none)", buf);
 134                *is_bogus = 1;
 135        }
 136}
 137
 138static void copy_email(const struct passwd *pw, struct strbuf *email,
 139                       int *is_bogus)
 140{
 141        /*
 142         * Make up a fake email address
 143         * (name + '@' + hostname [+ '.' + domainname])
 144         */
 145        strbuf_addstr(email, pw->pw_name);
 146        strbuf_addch(email, '@');
 147
 148        if (!add_mailname_host(email))
 149                return; /* read from "/etc/mailname" (Debian) */
 150        add_domainname(email, is_bogus);
 151}
 152
 153const char *ident_default_name(void)
 154{
 155        if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) {
 156                copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
 157                strbuf_trim(&git_default_name);
 158        }
 159        return git_default_name.buf;
 160}
 161
 162const char *ident_default_email(void)
 163{
 164        if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) {
 165                const char *email = getenv("EMAIL");
 166
 167                if (email && email[0]) {
 168                        strbuf_addstr(&git_default_email, email);
 169                        committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
 170                        author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
 171                } else
 172                        copy_email(xgetpwuid_self(&default_email_is_bogus),
 173                                   &git_default_email, &default_email_is_bogus);
 174                strbuf_trim(&git_default_email);
 175        }
 176        return git_default_email.buf;
 177}
 178
 179static const char *ident_default_date(void)
 180{
 181        if (!git_default_date.len)
 182                datestamp(&git_default_date);
 183        return git_default_date.buf;
 184}
 185
 186void reset_ident_date(void)
 187{
 188        strbuf_reset(&git_default_date);
 189}
 190
 191static int crud(unsigned char c)
 192{
 193        return  c <= 32  ||
 194                c == '.' ||
 195                c == ',' ||
 196                c == ':' ||
 197                c == ';' ||
 198                c == '<' ||
 199                c == '>' ||
 200                c == '"' ||
 201                c == '\\' ||
 202                c == '\'';
 203}
 204
 205static int has_non_crud(const char *str)
 206{
 207        for (; *str; str++) {
 208                if (!crud(*str))
 209                        return 1;
 210        }
 211        return 0;
 212}
 213
 214/*
 215 * Copy over a string to the destination, but avoid special
 216 * characters ('\n', '<' and '>') and remove crud at the end
 217 */
 218static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
 219{
 220        size_t i, len;
 221        unsigned char c;
 222
 223        /* Remove crud from the beginning.. */
 224        while ((c = *src) != 0) {
 225                if (!crud(c))
 226                        break;
 227                src++;
 228        }
 229
 230        /* Remove crud from the end.. */
 231        len = strlen(src);
 232        while (len > 0) {
 233                c = src[len-1];
 234                if (!crud(c))
 235                        break;
 236                --len;
 237        }
 238
 239        /*
 240         * Copy the rest to the buffer, but avoid the special
 241         * characters '\n' '<' and '>' that act as delimiters on
 242         * an identification line. We can only remove crud, never add it,
 243         * so 'len' is our maximum.
 244         */
 245        strbuf_grow(sb, len);
 246        for (i = 0; i < len; i++) {
 247                c = *src++;
 248                switch (c) {
 249                case '\n': case '<': case '>':
 250                        continue;
 251                }
 252                sb->buf[sb->len++] = c;
 253        }
 254        sb->buf[sb->len] = '\0';
 255}
 256
 257/*
 258 * Reverse of fmt_ident(); given an ident line, split the fields
 259 * to allow the caller to parse it.
 260 * Signal a success by returning 0, but date/tz fields of the result
 261 * can still be NULL if the input line only has the name/email part
 262 * (e.g. reading from a reflog entry).
 263 */
 264int split_ident_line(struct ident_split *split, const char *line, int len)
 265{
 266        const char *cp;
 267        size_t span;
 268        int status = -1;
 269
 270        memset(split, 0, sizeof(*split));
 271
 272        split->name_begin = line;
 273        for (cp = line; *cp && cp < line + len; cp++)
 274                if (*cp == '<') {
 275                        split->mail_begin = cp + 1;
 276                        break;
 277                }
 278        if (!split->mail_begin)
 279                return status;
 280
 281        for (cp = split->mail_begin - 2; line <= cp; cp--)
 282                if (!isspace(*cp)) {
 283                        split->name_end = cp + 1;
 284                        break;
 285                }
 286        if (!split->name_end) {
 287                /* no human readable name */
 288                split->name_end = split->name_begin;
 289        }
 290
 291        for (cp = split->mail_begin; cp < line + len; cp++)
 292                if (*cp == '>') {
 293                        split->mail_end = cp;
 294                        break;
 295                }
 296        if (!split->mail_end)
 297                return status;
 298
 299        /*
 300         * Look from the end-of-line to find the trailing ">" of the mail
 301         * address, even though we should already know it as split->mail_end.
 302         * This can help in cases of broken idents with an extra ">" somewhere
 303         * in the email address.  Note that we are assuming the timestamp will
 304         * never have a ">" in it.
 305         *
 306         * Note that we will always find some ">" before going off the front of
 307         * the string, because will always hit the split->mail_end closing
 308         * bracket.
 309         */
 310        for (cp = line + len - 1; *cp != '>'; cp--)
 311                ;
 312
 313        for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
 314                ;
 315        if (line + len <= cp)
 316                goto person_only;
 317        split->date_begin = cp;
 318        span = strspn(cp, "0123456789");
 319        if (!span)
 320                goto person_only;
 321        split->date_end = split->date_begin + span;
 322        for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
 323                ;
 324        if (line + len <= cp || (*cp != '+' && *cp != '-'))
 325                goto person_only;
 326        split->tz_begin = cp;
 327        span = strspn(cp + 1, "0123456789");
 328        if (!span)
 329                goto person_only;
 330        split->tz_end = split->tz_begin + 1 + span;
 331        return 0;
 332
 333person_only:
 334        split->date_begin = NULL;
 335        split->date_end = NULL;
 336        split->tz_begin = NULL;
 337        split->tz_end = NULL;
 338        return 0;
 339}
 340
 341static const char *env_hint =
 342N_("\n"
 343   "*** Please tell me who you are.\n"
 344   "\n"
 345   "Run\n"
 346   "\n"
 347   "  git config --global user.email \"you@example.com\"\n"
 348   "  git config --global user.name \"Your Name\"\n"
 349   "\n"
 350   "to set your account\'s default identity.\n"
 351   "Omit --global to set the identity only in this repository.\n"
 352   "\n");
 353
 354const char *fmt_ident(const char *name, const char *email,
 355                      const char *date_str, int flag)
 356{
 357        static struct strbuf ident = STRBUF_INIT;
 358        int strict = (flag & IDENT_STRICT);
 359        int want_date = !(flag & IDENT_NO_DATE);
 360        int want_name = !(flag & IDENT_NO_NAME);
 361
 362        if (!email) {
 363                if (strict && ident_use_config_only
 364                    && !(ident_config_given & IDENT_MAIL_GIVEN)) {
 365                        fputs(_(env_hint), stderr);
 366                        die(_("no email was given and auto-detection is disabled"));
 367                }
 368                email = ident_default_email();
 369                if (strict && default_email_is_bogus) {
 370                        fputs(_(env_hint), stderr);
 371                        die(_("unable to auto-detect email address (got '%s')"), email);
 372                }
 373        }
 374
 375        if (want_name) {
 376                int using_default = 0;
 377                if (!name) {
 378                        if (strict && ident_use_config_only
 379                            && !(ident_config_given & IDENT_NAME_GIVEN)) {
 380                                fputs(_(env_hint), stderr);
 381                                die(_("no name was given and auto-detection is disabled"));
 382                        }
 383                        name = ident_default_name();
 384                        using_default = 1;
 385                        if (strict && default_name_is_bogus) {
 386                                fputs(_(env_hint), stderr);
 387                                die(_("unable to auto-detect name (got '%s')"), name);
 388                        }
 389                }
 390                if (!*name) {
 391                        struct passwd *pw;
 392                        if (strict) {
 393                                if (using_default)
 394                                        fputs(_(env_hint), stderr);
 395                                die(_("empty ident name (for <%s>) not allowed"), email);
 396                        }
 397                        pw = xgetpwuid_self(NULL);
 398                        name = pw->pw_name;
 399                }
 400                if (strict && !has_non_crud(name))
 401                        die(_("name consists only of disallowed characters: %s"), name);
 402        }
 403
 404        strbuf_reset(&ident);
 405        if (want_name) {
 406                strbuf_addstr_without_crud(&ident, name);
 407                strbuf_addstr(&ident, " <");
 408        }
 409        strbuf_addstr_without_crud(&ident, email);
 410        if (want_name)
 411                        strbuf_addch(&ident, '>');
 412        if (want_date) {
 413                strbuf_addch(&ident, ' ');
 414                if (date_str && date_str[0]) {
 415                        if (parse_date(date_str, &ident) < 0)
 416                                die(_("invalid date format: %s"), date_str);
 417                }
 418                else
 419                        strbuf_addstr(&ident, ident_default_date());
 420        }
 421
 422        return ident.buf;
 423}
 424
 425const char *fmt_name(const char *name, const char *email)
 426{
 427        return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
 428}
 429
 430const char *git_author_info(int flag)
 431{
 432        if (getenv("GIT_AUTHOR_NAME"))
 433                author_ident_explicitly_given |= IDENT_NAME_GIVEN;
 434        if (getenv("GIT_AUTHOR_EMAIL"))
 435                author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
 436        return fmt_ident(getenv("GIT_AUTHOR_NAME"),
 437                         getenv("GIT_AUTHOR_EMAIL"),
 438                         getenv("GIT_AUTHOR_DATE"),
 439                         flag);
 440}
 441
 442const char *git_committer_info(int flag)
 443{
 444        if (getenv("GIT_COMMITTER_NAME"))
 445                committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
 446        if (getenv("GIT_COMMITTER_EMAIL"))
 447                committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
 448        return fmt_ident(getenv("GIT_COMMITTER_NAME"),
 449                         getenv("GIT_COMMITTER_EMAIL"),
 450                         getenv("GIT_COMMITTER_DATE"),
 451                         flag);
 452}
 453
 454static int ident_is_sufficient(int user_ident_explicitly_given)
 455{
 456#ifndef WINDOWS
 457        return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
 458#else
 459        return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
 460#endif
 461}
 462
 463int committer_ident_sufficiently_given(void)
 464{
 465        return ident_is_sufficient(committer_ident_explicitly_given);
 466}
 467
 468int author_ident_sufficiently_given(void)
 469{
 470        return ident_is_sufficient(author_ident_explicitly_given);
 471}
 472
 473int git_ident_config(const char *var, const char *value, void *data)
 474{
 475        if (!strcmp(var, "user.useconfigonly")) {
 476                ident_use_config_only = git_config_bool(var, value);
 477                return 0;
 478        }
 479
 480        if (!strcmp(var, "user.name")) {
 481                if (!value)
 482                        return config_error_nonbool(var);
 483                strbuf_reset(&git_default_name);
 484                strbuf_addstr(&git_default_name, value);
 485                committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
 486                author_ident_explicitly_given |= IDENT_NAME_GIVEN;
 487                ident_config_given |= IDENT_NAME_GIVEN;
 488                return 0;
 489        }
 490
 491        if (!strcmp(var, "user.email")) {
 492                if (!value)
 493                        return config_error_nonbool(var);
 494                strbuf_reset(&git_default_email);
 495                strbuf_addstr(&git_default_email, value);
 496                committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
 497                author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
 498                ident_config_given |= IDENT_MAIL_GIVEN;
 499                return 0;
 500        }
 501
 502        return 0;
 503}
 504
 505static int buf_cmp(const char *a_begin, const char *a_end,
 506                   const char *b_begin, const char *b_end)
 507{
 508        int a_len = a_end - a_begin;
 509        int b_len = b_end - b_begin;
 510        int min = a_len < b_len ? a_len : b_len;
 511        int cmp;
 512
 513        cmp = memcmp(a_begin, b_begin, min);
 514        if (cmp)
 515                return cmp;
 516
 517        return a_len - b_len;
 518}
 519
 520int ident_cmp(const struct ident_split *a,
 521              const struct ident_split *b)
 522{
 523        int cmp;
 524
 525        cmp = buf_cmp(a->mail_begin, a->mail_end,
 526                      b->mail_begin, b->mail_end);
 527        if (cmp)
 528                return cmp;
 529
 530        return buf_cmp(a->name_begin, a->name_end,
 531                       b->name_begin, b->name_end);
 532}