ident.con commit rebase -i --autosquash: auto-squash commits (f59baa5)
   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        return  c <= 32  ||
 117                c == '.' ||
 118                c == ',' ||
 119                c == ':' ||
 120                c == ';' ||
 121                c == '<' ||
 122                c == '>' ||
 123                c == '"' ||
 124                c == '\\' ||
 125                c == '\'';
 126}
 127
 128/*
 129 * Copy over a string to the destination, but avoid special
 130 * characters ('\n', '<' and '>') and remove crud at the end
 131 */
 132static int copy(char *buf, size_t size, int offset, const char *src)
 133{
 134        size_t i, len;
 135        unsigned char c;
 136
 137        /* Remove crud from the beginning.. */
 138        while ((c = *src) != 0) {
 139                if (!crud(c))
 140                        break;
 141                src++;
 142        }
 143
 144        /* Remove crud from the end.. */
 145        len = strlen(src);
 146        while (len > 0) {
 147                c = src[len-1];
 148                if (!crud(c))
 149                        break;
 150                --len;
 151        }
 152
 153        /*
 154         * Copy the rest to the buffer, but avoid the special
 155         * characters '\n' '<' and '>' that act as delimiters on
 156         * an identification line
 157         */
 158        for (i = 0; i < len; i++) {
 159                c = *src++;
 160                switch (c) {
 161                case '\n': case '<': case '>':
 162                        continue;
 163                }
 164                if (offset >= size)
 165                        return size;
 166                buf[offset++] = c;
 167        }
 168        return offset;
 169}
 170
 171static const char au_env[] = "GIT_AUTHOR_NAME";
 172static const char co_env[] = "GIT_COMMITTER_NAME";
 173static const char *env_hint =
 174"\n"
 175"*** Please tell me who you are.\n"
 176"\n"
 177"Run\n"
 178"\n"
 179"  git config --global user.email \"you@example.com\"\n"
 180"  git config --global user.name \"Your Name\"\n"
 181"\n"
 182"to set your account\'s default identity.\n"
 183"Omit --global to set the identity only in this repository.\n"
 184"\n";
 185
 186const char *fmt_ident(const char *name, const char *email,
 187                      const char *date_str, int flag)
 188{
 189        static char buffer[1000];
 190        char date[50];
 191        int i;
 192        int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME);
 193        int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME);
 194        int name_addr_only = (flag & IDENT_NO_DATE);
 195
 196        setup_ident();
 197        if (!name)
 198                name = git_default_name;
 199        if (!email)
 200                email = git_default_email;
 201
 202        if (!*name) {
 203                struct passwd *pw;
 204
 205                if ((warn_on_no_name || error_on_no_name) &&
 206                    name == git_default_name && env_hint) {
 207                        fprintf(stderr, env_hint, au_env, co_env);
 208                        env_hint = NULL; /* warn only once */
 209                }
 210                if (error_on_no_name)
 211                        die("empty ident %s <%s> not allowed", name, email);
 212                pw = getpwuid(getuid());
 213                if (!pw)
 214                        die("You don't exist. Go away!");
 215                strlcpy(git_default_name, pw->pw_name,
 216                        sizeof(git_default_name));
 217                name = git_default_name;
 218        }
 219
 220        strcpy(date, git_default_date);
 221        if (!name_addr_only && date_str)
 222                parse_date(date_str, date, sizeof(date));
 223
 224        i = copy(buffer, sizeof(buffer), 0, name);
 225        i = add_raw(buffer, sizeof(buffer), i, " <");
 226        i = copy(buffer, sizeof(buffer), i, email);
 227        if (!name_addr_only) {
 228                i = add_raw(buffer, sizeof(buffer), i,  "> ");
 229                i = copy(buffer, sizeof(buffer), i, date);
 230        } else {
 231                i = add_raw(buffer, sizeof(buffer), i, ">");
 232        }
 233        if (i >= sizeof(buffer))
 234                die("Impossibly long personal identifier");
 235        buffer[i] = 0;
 236        return buffer;
 237}
 238
 239const char *fmt_name(const char *name, const char *email)
 240{
 241        return fmt_ident(name, email, NULL, IDENT_ERROR_ON_NO_NAME | IDENT_NO_DATE);
 242}
 243
 244const char *git_author_info(int flag)
 245{
 246        return fmt_ident(getenv("GIT_AUTHOR_NAME"),
 247                         getenv("GIT_AUTHOR_EMAIL"),
 248                         getenv("GIT_AUTHOR_DATE"),
 249                         flag);
 250}
 251
 252const char *git_committer_info(int flag)
 253{
 254        if (getenv("GIT_COMMITTER_NAME") &&
 255            getenv("GIT_COMMITTER_EMAIL"))
 256                user_ident_explicitly_given = 1;
 257        return fmt_ident(getenv("GIT_COMMITTER_NAME"),
 258                         getenv("GIT_COMMITTER_EMAIL"),
 259                         getenv("GIT_COMMITTER_DATE"),
 260                         flag);
 261}