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