t: drop useless sane_unset GIT_* calls
[gitweb.git] / ident.c
diff --git a/ident.c b/ident.c
index a4bf206e2f88da3a7ee31a04c5c0dda2b1547baa..1d9b6e770d02d12f77b417e05aca5134eccf1c40 100644 (file)
--- a/ident.c
+++ b/ident.c
 static struct strbuf git_default_name = STRBUF_INIT;
 static struct strbuf git_default_email = STRBUF_INIT;
 static char git_default_date[50];
-int user_ident_explicitly_given;
+
+#define IDENT_NAME_GIVEN 01
+#define IDENT_MAIL_GIVEN 02
+#define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
+static int committer_ident_explicitly_given;
+static int author_ident_explicitly_given;
 
 #ifdef NO_GECOS_IN_PWENT
 #define get_gecos(ignored) "&"
@@ -41,6 +46,7 @@ static void copy_gecos(const struct passwd *w, struct strbuf *name)
 static int add_mailname_host(struct strbuf *buf)
 {
        FILE *mailname;
+       struct strbuf mailnamebuf = STRBUF_INIT;
 
        mailname = fopen("/etc/mailname", "r");
        if (!mailname) {
@@ -49,14 +55,17 @@ static int add_mailname_host(struct strbuf *buf)
                                strerror(errno));
                return -1;
        }
-       if (strbuf_getline(buf, mailname, '\n') == EOF) {
+       if (strbuf_getline(&mailnamebuf, mailname, '\n') == EOF) {
                if (ferror(mailname))
                        warning("cannot read /etc/mailname: %s",
                                strerror(errno));
+               strbuf_release(&mailnamebuf);
                fclose(mailname);
                return -1;
        }
        /* success! */
+       strbuf_addbuf(buf, &mailnamebuf);
+       strbuf_release(&mailnamebuf);
        fclose(mailname);
        return 0;
 }
@@ -109,7 +118,8 @@ const char *ident_default_email(void)
 
                if (email && email[0]) {
                        strbuf_addstr(&git_default_email, email);
-                       user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
+                       committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
+                       author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
                } else
                        copy_email(xgetpwuid_self(), &git_default_email);
                strbuf_trim(&git_default_email);
@@ -223,7 +233,21 @@ int split_ident_line(struct ident_split *split, const char *line, int len)
        if (!split->mail_end)
                return status;
 
-       for (cp = split->mail_end + 1; cp < line + len && isspace(*cp); cp++)
+       /*
+        * Look from the end-of-line to find the trailing ">" of the mail
+        * address, even though we should already know it as split->mail_end.
+        * This can help in cases of broken idents with an extra ">" somewhere
+        * in the email address.  Note that we are assuming the timestamp will
+        * never have a ">" in it.
+        *
+        * Note that we will always find some ">" before going off the front of
+        * the string, because will always hit the split->mail_end closing
+        * bracket.
+        */
+       for (cp = line + len - 1; *cp != '>'; cp--)
+               ;
+
+       for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
                ;
        if (line + len <= cp)
                goto person_only;
@@ -327,6 +351,10 @@ const char *fmt_name(const char *name, const char *email)
 
 const char *git_author_info(int flag)
 {
+       if (getenv("GIT_AUTHOR_NAME"))
+               author_ident_explicitly_given |= IDENT_NAME_GIVEN;
+       if (getenv("GIT_AUTHOR_EMAIL"))
+               author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
        return fmt_ident(getenv("GIT_AUTHOR_NAME"),
                         getenv("GIT_AUTHOR_EMAIL"),
                         getenv("GIT_AUTHOR_DATE"),
@@ -336,16 +364,16 @@ const char *git_author_info(int flag)
 const char *git_committer_info(int flag)
 {
        if (getenv("GIT_COMMITTER_NAME"))
-               user_ident_explicitly_given |= IDENT_NAME_GIVEN;
+               committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
        if (getenv("GIT_COMMITTER_EMAIL"))
-               user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
+               committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
        return fmt_ident(getenv("GIT_COMMITTER_NAME"),
                         getenv("GIT_COMMITTER_EMAIL"),
                         getenv("GIT_COMMITTER_DATE"),
                         flag);
 }
 
-int user_ident_sufficiently_given(void)
+static int ident_is_sufficient(int user_ident_explicitly_given)
 {
 #ifndef WINDOWS
        return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
@@ -354,6 +382,16 @@ int user_ident_sufficiently_given(void)
 #endif
 }
 
+int committer_ident_sufficiently_given(void)
+{
+       return ident_is_sufficient(committer_ident_explicitly_given);
+}
+
+int author_ident_sufficiently_given(void)
+{
+       return ident_is_sufficient(author_ident_explicitly_given);
+}
+
 int git_ident_config(const char *var, const char *value, void *data)
 {
        if (!strcmp(var, "user.name")) {
@@ -361,7 +399,8 @@ int git_ident_config(const char *var, const char *value, void *data)
                        return config_error_nonbool(var);
                strbuf_reset(&git_default_name);
                strbuf_addstr(&git_default_name, value);
-               user_ident_explicitly_given |= IDENT_NAME_GIVEN;
+               committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
+               author_ident_explicitly_given |= IDENT_NAME_GIVEN;
                return 0;
        }
 
@@ -370,9 +409,39 @@ int git_ident_config(const char *var, const char *value, void *data)
                        return config_error_nonbool(var);
                strbuf_reset(&git_default_email);
                strbuf_addstr(&git_default_email, value);
-               user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
+               committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
+               author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
                return 0;
        }
 
        return 0;
 }
+
+static int buf_cmp(const char *a_begin, const char *a_end,
+                  const char *b_begin, const char *b_end)
+{
+       int a_len = a_end - a_begin;
+       int b_len = b_end - b_begin;
+       int min = a_len < b_len ? a_len : b_len;
+       int cmp;
+
+       cmp = memcmp(a_begin, b_begin, min);
+       if (cmp)
+               return cmp;
+
+       return a_len - b_len;
+}
+
+int ident_cmp(const struct ident_split *a,
+             const struct ident_split *b)
+{
+       int cmp;
+
+       cmp = buf_cmp(a->mail_begin, a->mail_end,
+                     b->mail_begin, b->mail_end);
+       if (cmp)
+               return cmp;
+
+       return buf_cmp(a->name_begin, a->name_end,
+                      b->name_begin, b->name_end);
+}