ident.con commit clone: the given repository dir should be relative to $PWD (ced78b3)
   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
  10#include <pwd.h>
  11#include <netdb.h>
  12
  13static char git_default_date[50];
  14
  15static void copy_gecos(struct passwd *w, char *name, int sz)
  16{
  17        char *src, *dst;
  18        int len, nlen;
  19
  20        nlen = strlen(w->pw_name);
  21
  22        /* Traditionally GECOS field had office phone numbers etc, separated
  23         * with commas.  Also & stands for capitalized form of the login name.
  24         */
  25
  26        for (len = 0, dst = name, src = w->pw_gecos; len < sz; src++) {
  27                int ch = *src;
  28                if (ch != '&') {
  29                        *dst++ = ch;
  30                        if (ch == 0 || ch == ',')
  31                                break;
  32                        len++;
  33                        continue;
  34                }
  35                if (len + nlen < sz) {
  36                        /* Sorry, Mr. McDonald... */
  37                        *dst++ = toupper(*w->pw_name);
  38                        memcpy(dst, w->pw_name + 1, nlen - 1);
  39                        dst += nlen - 1;
  40                }
  41        }
  42        if (len < sz)
  43                name[len] = 0;
  44        else
  45                die("Your parents must have hated you!");
  46
  47}
  48
  49int setup_ident(void)
  50{
  51        int len;
  52        struct passwd *pw = getpwuid(getuid());
  53
  54        if (!pw)
  55                die("You don't exist. Go away!");
  56
  57        /* Get the name ("gecos") */
  58        copy_gecos(pw, git_default_name, sizeof(git_default_name));
  59
  60        /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
  61        len = strlen(pw->pw_name);
  62        if (len > sizeof(git_default_email)/2)
  63                die("Your sysadmin must hate you!");
  64        memcpy(git_default_email, pw->pw_name, len);
  65        git_default_email[len++] = '@';
  66        gethostname(git_default_email + len, sizeof(git_default_email) - len);
  67        if (!strchr(git_default_email+len, '.')) {
  68                struct hostent *he = gethostbyname(git_default_email + len);
  69                char *domainname;
  70
  71                len = strlen(git_default_email);
  72                git_default_email[len++] = '.';
  73                if (he && (domainname = strchr(he->h_name, '.')))
  74                        strlcpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
  75                else
  76                        strlcpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
  77        }
  78        /* And set the default date */
  79        datestamp(git_default_date, sizeof(git_default_date));
  80        return 0;
  81}
  82
  83static int add_raw(char *buf, int size, int offset, const char *str)
  84{
  85        int len = strlen(str);
  86        if (offset + len > size)
  87                return size;
  88        memcpy(buf + offset, str, len);
  89        return offset + len;
  90}
  91
  92static int crud(unsigned char c)
  93{
  94        static char crud_array[256];
  95        static int crud_array_initialized = 0;
  96
  97        if (!crud_array_initialized) {
  98                int k;
  99
 100                for (k = 0; k <= 31; ++k) crud_array[k] = 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['>'] = 1;
 108                crud_array['"'] = 1;
 109                crud_array['\''] = 1;
 110                crud_array_initialized = 1;
 111        }
 112        return crud_array[c];
 113}
 114
 115/*
 116 * Copy over a string to the destination, but avoid special
 117 * characters ('\n', '<' and '>') and remove crud at the end
 118 */
 119static int copy(char *buf, int size, int offset, const char *src)
 120{
 121        int i, len;
 122        unsigned char c;
 123
 124        /* Remove crud from the beginning.. */
 125        while ((c = *src) != 0) {
 126                if (!crud(c))
 127                        break;
 128                src++;
 129        }
 130
 131        /* Remove crud from the end.. */
 132        len = strlen(src);
 133        while (len > 0) {
 134                c = src[len-1];
 135                if (!crud(c))
 136                        break;
 137                --len;
 138        }
 139
 140        /*
 141         * Copy the rest to the buffer, but avoid the special
 142         * characters '\n' '<' and '>' that act as delimiters on
 143         * a identification line
 144         */
 145        for (i = 0; i < len; i++) {
 146                c = *src++;
 147                switch (c) {
 148                case '\n': case '<': case '>':
 149                        continue;
 150                }
 151                if (offset >= size)
 152                        return size;
 153                buf[offset++] = c;
 154        }
 155        return offset;
 156}
 157
 158static const char au_env[] = "GIT_AUTHOR_NAME";
 159static const char co_env[] = "GIT_COMMITTER_NAME";
 160static const char *env_hint =
 161"\n*** Environment problem:\n"
 162"*** Your name cannot be determined from your system services (gecos).\n"
 163"*** You would need to set %s and %s\n"
 164"*** environment variables; otherwise you won't be able to perform\n"
 165"*** certain operations because of \"empty ident\" errors.\n"
 166"*** Alternatively, you can use user.name configuration variable.\n\n";
 167
 168static const char *get_ident(const char *name, const char *email,
 169                             const char *date_str, int error_on_no_name)
 170{
 171        static char buffer[1000];
 172        char date[50];
 173        int i;
 174
 175        if (!name)
 176                name = git_default_name;
 177        if (!email)
 178                email = git_default_email;
 179
 180        if (!*name) {
 181                if (name == git_default_name && env_hint) {
 182                        fprintf(stderr, env_hint, au_env, co_env);
 183                        env_hint = NULL; /* warn only once, for "git-var -l" */
 184                }
 185                if (error_on_no_name)
 186                        die("empty ident %s <%s> not allowed", name, email);
 187        }
 188
 189        strcpy(date, git_default_date);
 190        if (date_str)
 191                parse_date(date_str, date, sizeof(date));
 192
 193        i = copy(buffer, sizeof(buffer), 0, name);
 194        i = add_raw(buffer, sizeof(buffer), i, " <");
 195        i = copy(buffer, sizeof(buffer), i, email);
 196        i = add_raw(buffer, sizeof(buffer), i, "> ");
 197        i = copy(buffer, sizeof(buffer), i, date);
 198        if (i >= sizeof(buffer))
 199                die("Impossibly long personal identifier");
 200        buffer[i] = 0;
 201        return buffer;
 202}
 203
 204const char *git_author_info(int error_on_no_name)
 205{
 206        return get_ident(getenv("GIT_AUTHOR_NAME"),
 207                         getenv("GIT_AUTHOR_EMAIL"),
 208                         getenv("GIT_AUTHOR_DATE"),
 209                         error_on_no_name);
 210}
 211
 212const char *git_committer_info(int error_on_no_name)
 213{
 214        return get_ident(getenv("GIT_COMMITTER_NAME"),
 215                         getenv("GIT_COMMITTER_EMAIL"),
 216                         getenv("GIT_COMMITTER_DATE"),
 217                         error_on_no_name);
 218}