ident.con commit Merge branch 'fixes' (2b2dabc)
   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 <time.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                len = strlen(git_default_email);
  69                git_default_email[len++] = '.';
  70                getdomainname(git_default_email+len, sizeof(git_default_email)-len);
  71        }
  72        /* And set the default date */
  73        datestamp(git_default_date, sizeof(git_default_date));
  74        return 0;
  75}
  76
  77static int add_raw(char *buf, int size, int offset, const char *str)
  78{
  79        int len = strlen(str);
  80        if (offset + len > size)
  81                return size;
  82        memcpy(buf + offset, str, len);
  83        return offset + len;
  84}
  85
  86static int crud(unsigned char c)
  87{
  88        static char crud_array[256];
  89        static int crud_array_initialized = 0;
  90
  91        if (!crud_array_initialized) {
  92                int k;
  93
  94                for (k = 0; k <= 31; ++k) crud_array[k] = 1;
  95                crud_array[' '] = 1;
  96                crud_array['.'] = 1;
  97                crud_array[','] = 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_initialized = 1;
 105        }
 106        return crud_array[c];
 107}
 108
 109/*
 110 * Copy over a string to the destination, but avoid special
 111 * characters ('\n', '<' and '>') and remove crud at the end
 112 */
 113static int copy(char *buf, int size, int offset, const char *src)
 114{
 115        int i, len;
 116        unsigned char c;
 117
 118        /* Remove crud from the beginning.. */
 119        while ((c = *src) != 0) {
 120                if (!crud(c))
 121                        break;
 122                src++;
 123        }
 124
 125        /* Remove crud from the end.. */
 126        len = strlen(src);
 127        while (len > 0) {
 128                c = src[len-1];
 129                if (!crud(c))
 130                        break;
 131                --len;
 132        }
 133
 134        /*
 135         * Copy the rest to the buffer, but avoid the special
 136         * characters '\n' '<' and '>' that act as delimeters on
 137         * a identification line
 138         */
 139        for (i = 0; i < len; i++) {
 140                c = *src++;
 141                switch (c) {
 142                case '\n': case '<': case '>':
 143                        continue;
 144                }
 145                if (offset >= size)
 146                        return size;
 147                buf[offset++] = c;
 148        }
 149        return offset;
 150}
 151
 152char *get_ident(const char *name, const char *email, const char *date_str)
 153{
 154        static char buffer[1000];
 155        char date[50];
 156        int i;
 157
 158        if (!name)
 159                name = git_default_name;
 160        if (!email)
 161                email = git_default_email;
 162        strcpy(date, git_default_date);
 163        if (date_str)
 164                parse_date(date_str, date, sizeof(date));
 165
 166        i = copy(buffer, sizeof(buffer), 0, name);
 167        i = add_raw(buffer, sizeof(buffer), i, " <");
 168        i = copy(buffer, sizeof(buffer), i, email);
 169        i = add_raw(buffer, sizeof(buffer), i, "> ");
 170        i = copy(buffer, sizeof(buffer), i, date);
 171        if (i >= sizeof(buffer))
 172                die("Impossibly long personal identifier");
 173        buffer[i] = 0;
 174        return buffer;
 175}
 176
 177char *git_author_info(void)
 178{
 179        return get_ident(getenv("GIT_AUTHOR_NAME"), getenv("GIT_AUTHOR_EMAIL"), getenv("GIT_AUTHOR_DATE"));
 180}
 181
 182char *git_committer_info(void)
 183{
 184        return get_ident(getenv("GIT_COMMITTER_NAME"), getenv("GIT_COMMITTER_EMAIL"), getenv("GIT_COMMITTER_DATE"));
 185}