diff options
Diffstat (limited to 'ident.c')
-rw-r--r-- | ident.c | 202 |
1 files changed, 131 insertions, 71 deletions
@@ -7,15 +7,18 @@ */ #include "cache.h" -#include <pwd.h> -#include <netdb.h> - static char git_default_date[50]; -static void copy_gecos(struct passwd *w, char *name, int sz) +#ifdef NO_GECOS_IN_PWENT +#define get_gecos(ignored) "&" +#else +#define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos) +#endif + +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); @@ -23,7 +26,7 @@ static void copy_gecos(struct passwd *w, char *name, int sz) * with commas. Also & stands for capitalized form of the login name. */ - for (len = 0, dst = name, src = w->pw_gecos; len < sz; src++) { + for (len = 0, dst = name, src = get_gecos(w); len < sz; src++) { int ch = *src; if (ch != '&') { *dst++ = ch; @@ -37,6 +40,7 @@ static void copy_gecos(struct passwd *w, char *name, int sz) *dst++ = toupper(*w->pw_name); memcpy(dst, w->pw_name + 1, nlen - 1); dst += nlen - 1; + len += nlen; } } if (len < sz) @@ -46,19 +50,13 @@ static void copy_gecos(struct passwd *w, char *name, int sz) } -int setup_ident(void) +static void copy_email(const struct passwd *pw) { - int len; - struct passwd *pw = getpwuid(getuid()); - - if (!pw) - die("You don't exist. Go away!"); - - /* Get the name ("gecos") */ - copy_gecos(pw, git_default_name, sizeof(git_default_name)); - - /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */ - len = strlen(pw->pw_name); + /* + * Make up a fake email address + * (name + '@' + hostname [+ '.' + domainname]) + */ + 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); @@ -71,19 +69,50 @@ int setup_ident(void) len = strlen(git_default_email); git_default_email[len++] = '.'; if (he && (domainname = strchr(he->h_name, '.'))) - strncpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len); + strlcpy(git_default_email + len, domainname + 1, + sizeof(git_default_email) - len); else - strncpy(git_default_email + len, "(none)", sizeof(git_default_email) - len); - git_default_email[sizeof(git_default_email) - 1] = 0; + strlcpy(git_default_email + len, "(none)", + sizeof(git_default_email) - len); } +} + +static void setup_ident(void) +{ + struct passwd *pw = NULL; + + /* Get the name ("gecos") */ + if (!git_default_name[0]) { + pw = getpwuid(getuid()); + if (!pw) + die("You don't exist. Go away!"); + copy_gecos(pw, git_default_name, sizeof(git_default_name)); + } + + if (!git_default_email[0]) { + const char *email = getenv("EMAIL"); + + if (email && email[0]) { + strlcpy(git_default_email, email, + sizeof(git_default_email)); + user_ident_explicitly_given |= IDENT_MAIL_GIVEN; + } else { + if (!pw) + pw = getpwuid(getuid()); + if (!pw) + die("You don't exist. Go away!"); + copy_email(pw); + } + } + /* And set the default date */ - datestamp(git_default_date, sizeof(git_default_date)); - return 0; + if (!git_default_date[0]) + 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); @@ -92,34 +121,25 @@ 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 == '\\' || + 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.. */ @@ -141,7 +161,7 @@ static int copy(char *buf, int 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++; @@ -156,64 +176,104 @@ static int copy(char *buf, int size, int offset, const char *src) return offset; } -static const char au_env[] = "GIT_AUTHOR_NAME"; -static const char co_env[] = "GIT_COMMITTER_NAME"; static const char *env_hint = -"\n*** Environment problem:\n" -"*** Your name cannot be determined from your system services (gecos).\n" -"*** You would need to set %s and %s\n" -"*** environment variables; otherwise you won't be able to perform\n" -"*** certain operations because of \"empty ident\" errors.\n" -"*** Alternatively, you can use user.name configuration variable.\n\n"; - -static const char *get_ident(const char *name, const char *email, - const char *date_str, int error_on_no_name) +"\n" +"*** Please tell me who you are.\n" +"\n" +"Run\n" +"\n" +" git config --global user.email \"you@example.com\"\n" +" git config --global user.name \"Your Name\"\n" +"\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 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 (!*name) { - if (name == git_default_name && env_hint) { - fprintf(stderr, env_hint, au_env, co_env); - env_hint = NULL; /* warn only once, for "git-var -l" */ + struct passwd *pw; + + if ((warn_on_no_name || error_on_no_name) && + name == git_default_name && env_hint) { + fputs(env_hint, stderr); + env_hint = NULL; /* warn only once */ } if (error_on_no_name) die("empty ident %s <%s> not allowed", name, email); + pw = getpwuid(getuid()); + if (!pw) + die("You don't exist. Go away!"); + strlcpy(git_default_name, pw->pw_name, + sizeof(git_default_name)); + name = git_default_name; } strcpy(date, git_default_date); - if (date_str) - parse_date(date_str, date, sizeof(date)); + if (!name_addr_only && date_str && date_str[0]) { + if (parse_date(date_str, date, sizeof(date)) < 0) + die("invalid date format: %s", date_str); + } 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 get_ident(getenv("GIT_AUTHOR_NAME"), + 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) { - return get_ident(getenv("GIT_COMMITTER_NAME"), + if (getenv("GIT_COMMITTER_NAME")) + user_ident_explicitly_given |= IDENT_NAME_GIVEN; + if (getenv("GIT_COMMITTER_EMAIL")) + user_ident_explicitly_given |= IDENT_MAIL_GIVEN; + return fmt_ident(getenv("GIT_COMMITTER_NAME"), getenv("GIT_COMMITTER_EMAIL"), getenv("GIT_COMMITTER_DATE"), - error_on_no_name); + flag); +} + +int user_ident_sufficiently_given(void) +{ +#ifndef WINDOWS + return (user_ident_explicitly_given & IDENT_MAIL_GIVEN); +#else + return (user_ident_explicitly_given == IDENT_ALL_GIVEN); +#endif } |