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