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