ident.con commit [PATCH] Add "git grep" helper (f22cc3f)
   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#include <ctype.h>
  13
  14static char real_email[1000];
  15static char real_name[1000];
  16static char real_date[50];
  17
  18int setup_ident(void)
  19{
  20        int len;
  21        struct passwd *pw = getpwuid(getuid());
  22
  23        if (!pw)
  24                die("You don't exist. Go away!");
  25
  26        /* Get the name ("gecos") */
  27        len = strlen(pw->pw_gecos);
  28        if (len >= sizeof(real_name))
  29                die("Your parents must have hated you!");
  30        memcpy(real_name, pw->pw_gecos, len+1);
  31
  32        /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
  33        len = strlen(pw->pw_name);
  34        if (len > sizeof(real_email)/2)
  35                die("Your sysadmin must hate you!");
  36        memcpy(real_email, pw->pw_name, len);
  37        real_email[len++] = '@';
  38        gethostname(real_email + len, sizeof(real_email) - len);
  39#ifndef NO_GETDOMAINNAME
  40        if (!strchr(real_email+len, '.')) {
  41                len = strlen(real_email);
  42                real_email[len++] = '.';
  43                getdomainname(real_email+len, sizeof(real_email)-len);
  44        }
  45#endif
  46        /* And set the default date */
  47        datestamp(real_date, sizeof(real_date));
  48        return 0;
  49}
  50
  51static int add_raw(char *buf, int size, int offset, const char *str)
  52{
  53        int len = strlen(str);
  54        if (offset + len > size)
  55                return size;
  56        memcpy(buf + offset, str, len);
  57        return offset + len;
  58}
  59
  60static int crud(unsigned char c)
  61{
  62        static char crud_array[256];
  63        static int crud_array_initialized = 0;
  64
  65        if (!crud_array_initialized) {
  66                int k;
  67
  68                for (k = 0; k <= 31; ++k) crud_array[k] = 1;
  69                crud_array[' '] = 1;
  70                crud_array['.'] = 1;
  71                crud_array[','] = 1;
  72                crud_array[':'] = 1;
  73                crud_array[';'] = 1;
  74                crud_array['<'] = 1;
  75                crud_array['>'] = 1;
  76                crud_array['"'] = 1;
  77                crud_array['\''] = 1;
  78                crud_array_initialized = 1;
  79        }
  80        return crud_array[c];
  81}
  82
  83/*
  84 * Copy over a string to the destination, but avoid special
  85 * characters ('\n', '<' and '>') and remove crud at the end
  86 */
  87static int copy(char *buf, int size, int offset, const char *src)
  88{
  89        int i, len;
  90        unsigned char c;
  91
  92        /* Remove crud from the beginning.. */
  93        while ((c = *src) != 0) {
  94                if (!crud(c))
  95                        break;
  96                src++;
  97        }
  98
  99        /* Remove crud from the end.. */
 100        len = strlen(src);
 101        while (len > 0) {
 102                c = src[len-1];
 103                if (!crud(c))
 104                        break;
 105                --len;
 106        }
 107
 108        /*
 109         * Copy the rest to the buffer, but avoid the special
 110         * characters '\n' '<' and '>' that act as delimeters on
 111         * a identification line
 112         */
 113        for (i = 0; i < len; i++) {
 114                c = *src++;
 115                switch (c) {
 116                case '\n': case '<': case '>':
 117                        continue;
 118                }
 119                if (offset >= size)
 120                        return size;
 121                buf[offset++] = c;
 122        }
 123        return offset;
 124}
 125
 126char *get_ident(const char *name, const char *email, const char *date_str)
 127{
 128        static char buffer[1000];
 129        char date[50];
 130        int i;
 131
 132        if (!name)
 133                name = real_name;
 134        if (!email)
 135                email = real_email;
 136        strcpy(date, real_date);
 137        if (date_str)
 138                parse_date(date_str, date, sizeof(date));
 139
 140        i = copy(buffer, sizeof(buffer), 0, name);
 141        i = add_raw(buffer, sizeof(buffer), i, " <");
 142        i = copy(buffer, sizeof(buffer), i, email);
 143        i = add_raw(buffer, sizeof(buffer), i, "> ");
 144        i = copy(buffer, sizeof(buffer), i, date);
 145        if (i >= sizeof(buffer))
 146                die("Impossibly long personal identifier");
 147        buffer[i] = 0;
 148        return buffer;
 149}
 150
 151char *git_author_info(void)
 152{
 153        return get_ident(getenv("GIT_AUTHOR_NAME"), getenv("GIT_AUTHOR_EMAIL"), getenv("GIT_AUTHOR_DATE"));
 154}
 155
 156char *git_committer_info(void)
 157{
 158        return get_ident(getenv("GIT_COMMITTER_NAME"), getenv("GIT_COMMITTER_EMAIL"), getenv("GIT_COMMITTER_DATE"));
 159}