ident.con commit gitweb: Handle invalid regexp in regexp search (36612e4)
   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 char git_default_date[50];
  11
  12#ifdef NO_GECOS_IN_PWENT
  13#define get_gecos(ignored) "&"
  14#else
  15#define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
  16#endif
  17
  18static void copy_gecos(const struct passwd *w, char *name, size_t sz)
  19{
  20        char *src, *dst;
  21        size_t len, nlen;
  22
  23        nlen = strlen(w->pw_name);
  24
  25        /* Traditionally GECOS field had office phone numbers etc, separated
  26         * with commas.  Also & stands for capitalized form of the login name.
  27         */
  28
  29        for (len = 0, dst = name, src = get_gecos(w); len < sz; src++) {
  30                int ch = *src;
  31                if (ch != '&') {
  32                        *dst++ = ch;
  33                        if (ch == 0 || ch == ',')
  34                                break;
  35                        len++;
  36                        continue;
  37                }
  38                if (len + nlen < sz) {
  39                        /* Sorry, Mr. McDonald... */
  40                        *dst++ = toupper(*w->pw_name);
  41                        memcpy(dst, w->pw_name + 1, nlen - 1);
  42                        dst += nlen - 1;
  43                        len += nlen;
  44                }
  45        }
  46        if (len < sz)
  47                name[len] = 0;
  48        else
  49                die("Your parents must have hated you!");
  50
  51}
  52
  53static void copy_email(const struct passwd *pw)
  54{
  55        /*
  56         * Make up a fake email address
  57         * (name + '@' + hostname [+ '.' + domainname])
  58         */
  59        size_t len = strlen(pw->pw_name);
  60        if (len > sizeof(git_default_email)/2)
  61                die("Your sysadmin must hate you!");
  62        memcpy(git_default_email, pw->pw_name, len);
  63        git_default_email[len++] = '@';
  64        gethostname(git_default_email + len, sizeof(git_default_email) - len);
  65        if (!strchr(git_default_email+len, '.')) {
  66                struct hostent *he = gethostbyname(git_default_email + len);
  67                char *domainname;
  68
  69                len = strlen(git_default_email);
  70                git_default_email[len++] = '.';
  71                if (he && (domainname = strchr(he->h_name, '.')))
  72                        strlcpy(git_default_email + len, domainname + 1,
  73                                sizeof(git_default_email) - len);
  74                else
  75                        strlcpy(git_default_email + len, "(none)",
  76                                sizeof(git_default_email) - len);
  77        }
  78}
  79
  80static void setup_ident(void)
  81{
  82        struct passwd *pw = NULL;
  83
  84        /* Get the name ("gecos") */
  85        if (!git_default_name[0]) {
  86                pw = getpwuid(getuid());
  87                if (!pw)
  88                        die("You don't exist. Go away!");
  89                copy_gecos(pw, git_default_name, sizeof(git_default_name));
  90        }
  91
  92        if (!git_default_email[0]) {
  93                const char *email = getenv("EMAIL");
  94
  95                if (email && email[0]) {
  96                        strlcpy(git_default_email, email,
  97                                sizeof(git_default_email));
  98                        user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
  99                } else {
 100                        if (!pw)
 101                                pw = getpwuid(getuid());
 102                        if (!pw)
 103                                die("You don't exist. Go away!");
 104                        copy_email(pw);
 105                }
 106        }
 107
 108        /* And set the default date */
 109        if (!git_default_date[0])
 110                datestamp(git_default_date, sizeof(git_default_date));
 111}
 112
 113static int add_raw(char *buf, size_t size, int offset, const char *str)
 114{
 115        size_t len = strlen(str);
 116        if (offset + len > size)
 117                return size;
 118        memcpy(buf + offset, str, len);
 119        return offset + len;
 120}
 121
 122static int crud(unsigned char c)
 123{
 124        return  c <= 32  ||
 125                c == '.' ||
 126                c == ',' ||
 127                c == ':' ||
 128                c == ';' ||
 129                c == '<' ||
 130                c == '>' ||
 131                c == '"' ||
 132                c == '\\' ||
 133                c == '\'';
 134}
 135
 136/*
 137 * Copy over a string to the destination, but avoid special
 138 * characters ('\n', '<' and '>') and remove crud at the end
 139 */
 140static int copy(char *buf, size_t size, int offset, const char *src)
 141{
 142        size_t i, len;
 143        unsigned char c;
 144
 145        /* Remove crud from the beginning.. */
 146        while ((c = *src) != 0) {
 147                if (!crud(c))
 148                        break;
 149                src++;
 150        }
 151
 152        /* Remove crud from the end.. */
 153        len = strlen(src);
 154        while (len > 0) {
 155                c = src[len-1];
 156                if (!crud(c))
 157                        break;
 158                --len;
 159        }
 160
 161        /*
 162         * Copy the rest to the buffer, but avoid the special
 163         * characters '\n' '<' and '>' that act as delimiters on
 164         * an identification line
 165         */
 166        for (i = 0; i < len; i++) {
 167                c = *src++;
 168                switch (c) {
 169                case '\n': case '<': case '>':
 170                        continue;
 171                }
 172                if (offset >= size)
 173                        return size;
 174                buf[offset++] = c;
 175        }
 176        return offset;
 177}
 178
 179static const char *env_hint =
 180"\n"
 181"*** Please tell me who you are.\n"
 182"\n"
 183"Run\n"
 184"\n"
 185"  git config --global user.email \"you@example.com\"\n"
 186"  git config --global user.name \"Your Name\"\n"
 187"\n"
 188"to set your account\'s default identity.\n"
 189"Omit --global to set the identity only in this repository.\n"
 190"\n";
 191
 192const char *fmt_ident(const char *name, const char *email,
 193                      const char *date_str, int flag)
 194{
 195        static char buffer[1000];
 196        char date[50];
 197        int i;
 198        int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME);
 199        int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME);
 200        int name_addr_only = (flag & IDENT_NO_DATE);
 201
 202        setup_ident();
 203        if (!name)
 204                name = git_default_name;
 205        if (!email)
 206                email = git_default_email;
 207
 208        if (!*name) {
 209                struct passwd *pw;
 210
 211                if ((warn_on_no_name || error_on_no_name) &&
 212                    name == git_default_name && env_hint) {
 213                        fputs(env_hint, stderr);
 214                        env_hint = NULL; /* warn only once */
 215                }
 216                if (error_on_no_name)
 217                        die("empty ident %s <%s> not allowed", name, email);
 218                pw = getpwuid(getuid());
 219                if (!pw)
 220                        die("You don't exist. Go away!");
 221                strlcpy(git_default_name, pw->pw_name,
 222                        sizeof(git_default_name));
 223                name = git_default_name;
 224        }
 225
 226        strcpy(date, git_default_date);
 227        if (!name_addr_only && date_str && date_str[0]) {
 228                if (parse_date(date_str, date, sizeof(date)) < 0)
 229                        die("invalid date format: %s", date_str);
 230        }
 231
 232        i = copy(buffer, sizeof(buffer), 0, name);
 233        i = add_raw(buffer, sizeof(buffer), i, " <");
 234        i = copy(buffer, sizeof(buffer), i, email);
 235        if (!name_addr_only) {
 236                i = add_raw(buffer, sizeof(buffer), i,  "> ");
 237                i = copy(buffer, sizeof(buffer), i, date);
 238        } else {
 239                i = add_raw(buffer, sizeof(buffer), i, ">");
 240        }
 241        if (i >= sizeof(buffer))
 242                die("Impossibly long personal identifier");
 243        buffer[i] = 0;
 244        return buffer;
 245}
 246
 247const char *fmt_name(const char *name, const char *email)
 248{
 249        return fmt_ident(name, email, NULL, IDENT_ERROR_ON_NO_NAME | IDENT_NO_DATE);
 250}
 251
 252const char *git_author_info(int flag)
 253{
 254        return fmt_ident(getenv("GIT_AUTHOR_NAME"),
 255                         getenv("GIT_AUTHOR_EMAIL"),
 256                         getenv("GIT_AUTHOR_DATE"),
 257                         flag);
 258}
 259
 260const char *git_committer_info(int flag)
 261{
 262        if (getenv("GIT_COMMITTER_NAME"))
 263                user_ident_explicitly_given |= IDENT_NAME_GIVEN;
 264        if (getenv("GIT_COMMITTER_EMAIL"))
 265                user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
 266        return fmt_ident(getenv("GIT_COMMITTER_NAME"),
 267                         getenv("GIT_COMMITTER_EMAIL"),
 268                         getenv("GIT_COMMITTER_DATE"),
 269                         flag);
 270}
 271
 272int user_ident_sufficiently_given(void)
 273{
 274#ifndef WINDOWS
 275        return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
 276#else
 277        return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
 278#endif
 279}