Test git-patch-id
[gitweb.git] / ident.c
diff --git a/ident.c b/ident.c
index 3d49608e6f9afe22f24a2faf70f2d6cee50b8919..99f1c85ea5f5c83247f7affd3d801ff5c14393a6 100644 (file)
--- a/ident.c
+++ b/ident.c
@@ -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 */
@@ -106,25 +113,16 @@ static int add_raw(char *buf, size_t 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 == '\\' ||
+               c == '\'';
 }
 
 /*
@@ -155,7 +153,7 @@ static int copy(char *buf, size_t size, int offset, const char *src)
        /*
         * Copy the rest to the buffer, but avoid the special
         * characters '\n' '<' and '>' that act as delimiters on
-        * a identification line
+        * an identification line
         */
        for (i = 0; i < len; i++) {
                c = *src++;
@@ -174,41 +172,42 @@ static const char au_env[] = "GIT_AUTHOR_NAME";
 static const char co_env[] = "GIT_COMMITTER_NAME";
 static const char *env_hint =
 "\n"
-"*** Your name cannot be determined from your system services (gecos).\n"
+"*** Please tell me who you are.\n"
 "\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@example.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,
-                     const char *date_str, int error_on_no_name)
+                     const char *date_str, int flag)
 {
        static char buffer[1000];
        char date[50];
        int i;
+       int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME);
+       int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME);
+       int name_addr_only = (flag & IDENT_NO_DATE);
 
        setup_ident();
        if (!name)
                name = git_default_name;
        if (!email)
                email = git_default_email;
-       if (!email)
-               email = getenv("EMAIL");
 
        if (!*name) {
                struct passwd *pw;
 
-               if (0 <= error_on_no_name &&
+               if ((warn_on_no_name || error_on_no_name) &&
                    name == git_default_name && env_hint) {
                        fprintf(stderr, env_hint, au_env, co_env);
-                       env_hint = NULL; /* warn only once, for "git-var -l" */
+                       env_hint = NULL; /* warn only once, for "git var -l" */
                }
-               if (0 < error_on_no_name)
+               if (error_on_no_name)
                        die("empty ident %s <%s> not allowed", name, email);
                pw = getpwuid(getuid());
                if (!pw)
@@ -219,32 +218,44 @@ const char *fmt_ident(const char *name, const char *email,
        }
 
        strcpy(date, git_default_date);
-       if (date_str)
+       if (!name_addr_only && date_str)
                parse_date(date_str, date, sizeof(date));
 
        i = copy(buffer, sizeof(buffer), 0, name);
        i = add_raw(buffer, sizeof(buffer), i, " <");
        i = copy(buffer, sizeof(buffer), i, email);
-       i = add_raw(buffer, sizeof(buffer), i, "> ");
-       i = copy(buffer, sizeof(buffer), i, date);
+       if (!name_addr_only) {
+               i = add_raw(buffer, sizeof(buffer), i,  "> ");
+               i = copy(buffer, sizeof(buffer), i, date);
+       } else {
+               i = add_raw(buffer, sizeof(buffer), i, ">");
+       }
        if (i >= sizeof(buffer))
                die("Impossibly long personal identifier");
        buffer[i] = 0;
        return buffer;
 }
 
-const char *git_author_info(int error_on_no_name)
+const char *fmt_name(const char *name, const char *email)
+{
+       return fmt_ident(name, email, NULL, IDENT_ERROR_ON_NO_NAME | IDENT_NO_DATE);
+}
+
+const char *git_author_info(int flag)
 {
        return fmt_ident(getenv("GIT_AUTHOR_NAME"),
                         getenv("GIT_AUTHOR_EMAIL"),
                         getenv("GIT_AUTHOR_DATE"),
-                        error_on_no_name);
+                        flag);
 }
 
-const char *git_committer_info(int error_on_no_name)
+const char *git_committer_info(int flag)
 {
+       if (getenv("GIT_COMMITTER_NAME") &&
+           getenv("GIT_COMMITTER_EMAIL"))
+               user_ident_explicitly_given = 1;
        return fmt_ident(getenv("GIT_COMMITTER_NAME"),
                         getenv("GIT_COMMITTER_EMAIL"),
                         getenv("GIT_COMMITTER_DATE"),
-                        error_on_no_name);
+                        flag);
 }