]> asedeno.scripts.mit.edu Git - git.git/blobdiff - ident.c
Simplify crud() in ident.c
[git.git] / ident.c
diff --git a/ident.c b/ident.c
index bb03bddd34f2471ed130c499affc369758d4bfd9..dbd0f527b27e1fd4139862a452d901d851367fb0 100644 (file)
--- a/ident.c
+++ b/ident.c
@@ -9,10 +9,10 @@
 
 static char git_default_date[50];
 
-static void copy_gecos(struct passwd *w, char *name, int sz)
+static void copy_gecos(const struct passwd *w, char *name, size_t sz)
 {
        char *src, *dst;
-       int len, nlen;
+       size_t len, nlen;
 
        nlen = strlen(w->pw_name);
 
@@ -43,13 +43,13 @@ static void copy_gecos(struct passwd *w, char *name, int sz)
 
 }
 
-static void copy_email(struct passwd *pw)
+static void copy_email(const struct passwd *pw)
 {
        /*
         * Make up a fake email address
         * (name + '@' + hostname [+ '.' + domainname])
         */
-       int len = strlen(pw->pw_name);
+       size_t len = strlen(pw->pw_name);
        if (len > sizeof(git_default_email)/2)
                die("Your sysadmin must hate you!");
        memcpy(git_default_email, pw->pw_name, len);
@@ -83,11 +83,18 @@ static void setup_ident(void)
        }
 
        if (!git_default_email[0]) {
-               if (!pw)
-                       pw = getpwuid(getuid());
-               if (!pw)
-                       die("You don't exist. Go away!");
-               copy_email(pw);
+               const char *email = getenv("EMAIL");
+
+               if (email && email[0])
+                       strlcpy(git_default_email, email,
+                               sizeof(git_default_email));
+               else {
+                       if (!pw)
+                               pw = getpwuid(getuid());
+                       if (!pw)
+                               die("You don't exist. Go away!");
+                       copy_email(pw);
+               }
        }
 
        /* And set the default date */
@@ -95,9 +102,9 @@ static void setup_ident(void)
                datestamp(git_default_date, sizeof(git_default_date));
 }
 
-static int add_raw(char *buf, int size, int offset, const char *str)
+static int add_raw(char *buf, size_t size, int offset, const char *str)
 {
-       int len = strlen(str);
+       size_t len = strlen(str);
        if (offset + len > size)
                return size;
        memcpy(buf + offset, str, len);
@@ -106,34 +113,24 @@ static int add_raw(char *buf, int size, int offset, const char *str)
 
 static int crud(unsigned char c)
 {
-       static char crud_array[256];
-       static int crud_array_initialized = 0;
-
-       if (!crud_array_initialized) {
-               int k;
-
-               for (k = 0; k <= 31; ++k) crud_array[k] = 1;
-               crud_array[' '] = 1;
-               crud_array['.'] = 1;
-               crud_array[','] = 1;
-               crud_array[':'] = 1;
-               crud_array[';'] = 1;
-               crud_array['<'] = 1;
-               crud_array['>'] = 1;
-               crud_array['"'] = 1;
-               crud_array['\''] = 1;
-               crud_array_initialized = 1;
-       }
-       return crud_array[c];
+       return  c <= 32  ||
+               c == '.' ||
+               c == ',' ||
+               c == ':' ||
+               c == ';' ||
+               c == '<' ||
+               c == '>' ||
+               c == '"' ||
+               c == '\'';
 }
 
 /*
  * Copy over a string to the destination, but avoid special
  * characters ('\n', '<' and '>') and remove crud at the end
  */
-static int copy(char *buf, int size, int offset, const char *src)
+static int copy(char *buf, size_t size, int offset, const char *src)
 {
-       int i, len;
+       size_t i, len;
        unsigned char c;
 
        /* Remove crud from the beginning.. */
@@ -178,11 +175,11 @@ static const char *env_hint =
 "\n"
 "Run\n"
 "\n"
-"  git config user.email \"you@email.com\"\n"
-"  git config user.name \"Your Name\"\n"
+"  git config --global user.email \"you@email.com\"\n"
+"  git config --global user.name \"Your Name\"\n"
 "\n"
-"To set the identity in this repository.\n"
-"Add --global to set your account\'s default\n"
+"to set your account\'s default identity.\n"
+"Omit --global to set the identity only in this repository.\n"
 "\n";
 
 const char *fmt_ident(const char *name, const char *email,