ident.con commit Documentation/config.txt: Correct info about subsection name (191453f)
   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(struct passwd *w, char *name, int sz)
  13{
  14        char *src, *dst;
  15        int 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
  46int setup_ident(void)
  47{
  48        int len;
  49        struct passwd *pw = getpwuid(getuid());
  50
  51        if (!pw)
  52                die("You don't exist. Go away!");
  53
  54        /* Get the name ("gecos") */
  55        copy_gecos(pw, git_default_name, sizeof(git_default_name));
  56
  57        /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
  58        len = strlen(pw->pw_name);
  59        if (len > sizeof(git_default_email)/2)
  60                die("Your sysadmin must hate you!");
  61        memcpy(git_default_email, pw->pw_name, len);
  62        git_default_email[len++] = '@';
  63        gethostname(git_default_email + len, sizeof(git_default_email) - len);
  64        if (!strchr(git_default_email+len, '.')) {
  65                struct hostent *he = gethostbyname(git_default_email + len);
  66                char *domainname;
  67
  68                len = strlen(git_default_email);
  69                git_default_email[len++] = '.';
  70                if (he && (domainname = strchr(he->h_name, '.')))
  71                        strlcpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
  72                else
  73                        strlcpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
  74        }
  75        /* And set the default date */
  76        datestamp(git_default_date, sizeof(git_default_date));
  77        return 0;
  78}
  79
  80static int add_raw(char *buf, int size, int offset, const char *str)
  81{
  82        int len = strlen(str);
  83        if (offset + len > size)
  84                return size;
  85        memcpy(buf + offset, str, len);
  86        return offset + len;
  87}
  88
  89static int crud(unsigned char c)
  90{
  91        static char crud_array[256];
  92        static int crud_array_initialized = 0;
  93
  94        if (!crud_array_initialized) {
  95                int k;
  96
  97                for (k = 0; k <= 31; ++k) crud_array[k] = 1;
  98                crud_array[' '] = 1;
  99                crud_array['.'] = 1;
 100                crud_array[','] = 1;
 101                crud_array[':'] = 1;
 102                crud_array[';'] = 1;
 103                crud_array['<'] = 1;
 104                crud_array['>'] = 1;
 105                crud_array['"'] = 1;
 106                crud_array['\''] = 1;
 107                crud_array_initialized = 1;
 108        }
 109        return crud_array[c];
 110}
 111
 112/*
 113 * Copy over a string to the destination, but avoid special
 114 * characters ('\n', '<' and '>') and remove crud at the end
 115 */
 116static int copy(char *buf, int size, int offset, const char *src)
 117{
 118        int i, len;
 119        unsigned char c;
 120
 121        /* Remove crud from the beginning.. */
 122        while ((c = *src) != 0) {
 123                if (!crud(c))
 124                        break;
 125                src++;
 126        }
 127
 128        /* Remove crud from the end.. */
 129        len = strlen(src);
 130        while (len > 0) {
 131                c = src[len-1];
 132                if (!crud(c))
 133                        break;
 134                --len;
 135        }
 136
 137        /*
 138         * Copy the rest to the buffer, but avoid the special
 139         * characters '\n' '<' and '>' that act as delimiters on
 140         * a identification line
 141         */
 142        for (i = 0; i < len; i++) {
 143                c = *src++;
 144                switch (c) {
 145                case '\n': case '<': case '>':
 146                        continue;
 147                }
 148                if (offset >= size)
 149                        return size;
 150                buf[offset++] = c;
 151        }
 152        return offset;
 153}
 154
 155static const char au_env[] = "GIT_AUTHOR_NAME";
 156static const char co_env[] = "GIT_COMMITTER_NAME";
 157static const char *env_hint =
 158"\n"
 159"*** Your name cannot be determined from your system services (gecos).\n"
 160"\n"
 161"Run\n"
 162"\n"
 163"  git repo-config user.email \"you@email.com\"\n"
 164"  git repo-config user.name \"Your Name\"\n"
 165"\n"
 166"To set the identity in this repository.\n"
 167"Add --global to set your account\'s default\n"
 168"\n";
 169
 170static const char *get_ident(const char *name, const char *email,
 171                             const char *date_str, int error_on_no_name)
 172{
 173        static char buffer[1000];
 174        char date[50];
 175        int i;
 176
 177        if (!name)
 178                name = git_default_name;
 179        if (!email)
 180                email = git_default_email;
 181
 182        if (!*name) {
 183                if (name == git_default_name && env_hint) {
 184                        fprintf(stderr, env_hint, au_env, co_env);
 185                        env_hint = NULL; /* warn only once, for "git-var -l" */
 186                }
 187                if (error_on_no_name)
 188                        die("empty ident %s <%s> not allowed", name, email);
 189        }
 190
 191        strcpy(date, git_default_date);
 192        if (date_str)
 193                parse_date(date_str, date, sizeof(date));
 194
 195        i = copy(buffer, sizeof(buffer), 0, name);
 196        i = add_raw(buffer, sizeof(buffer), i, " <");
 197        i = copy(buffer, sizeof(buffer), i, email);
 198        i = add_raw(buffer, sizeof(buffer), i, "> ");
 199        i = copy(buffer, sizeof(buffer), i, date);
 200        if (i >= sizeof(buffer))
 201                die("Impossibly long personal identifier");
 202        buffer[i] = 0;
 203        return buffer;
 204}
 205
 206const char *git_author_info(int error_on_no_name)
 207{
 208        return get_ident(getenv("GIT_AUTHOR_NAME"),
 209                         getenv("GIT_AUTHOR_EMAIL"),
 210                         getenv("GIT_AUTHOR_DATE"),
 211                         error_on_no_name);
 212}
 213
 214const char *git_committer_info(int error_on_no_name)
 215{
 216        return get_ident(getenv("GIT_COMMITTER_NAME"),
 217                         getenv("GIT_COMMITTER_EMAIL"),
 218                         getenv("GIT_COMMITTER_DATE"),
 219                         error_on_no_name);
 220}
 221
 222void ignore_missing_committer_name()
 223{
 224        /* If we did not get a name from the user's gecos entry then
 225         * git_default_name is empty; so instead load the username
 226         * into it as a 'good enough for now' approximation of who
 227         * this user is.
 228         */
 229        if (!*git_default_name) {
 230                struct passwd *pw = getpwuid(getuid());
 231                if (!pw)
 232                        die("You don't exist. Go away!");
 233                strlcpy(git_default_name, pw->pw_name, sizeof(git_default_name));
 234        }
 235}