Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 10 | static char git_default_date[50]; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 11 | |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 12 | static void copy_gecos(struct passwd *w, char *name, int sz) |
| 13 | { |
| 14 | char *src, *dst; |
| 15 | int len, nlen; |
| 16 | |
| 17 | nlen = strlen(w->pw_name); |
| 18 | |
| 19 | /* Traditionally GECOS field had office phone numbers etc, separated |
| 20 | * with commas. Also & stands for capitalized form of the login name. |
| 21 | */ |
| 22 | |
| 23 | for (len = 0, dst = name, src = w->pw_gecos; len < sz; src++) { |
| 24 | int ch = *src; |
| 25 | if (ch != '&') { |
| 26 | *dst++ = ch; |
| 27 | if (ch == 0 || ch == ',') |
| 28 | break; |
| 29 | len++; |
| 30 | continue; |
| 31 | } |
| 32 | if (len + nlen < sz) { |
| 33 | /* Sorry, Mr. McDonald... */ |
| 34 | *dst++ = toupper(*w->pw_name); |
| 35 | memcpy(dst, w->pw_name + 1, nlen - 1); |
| 36 | dst += nlen - 1; |
| 37 | } |
| 38 | } |
| 39 | if (len < sz) |
| 40 | name[len] = 0; |
| 41 | else |
| 42 | die("Your parents must have hated you!"); |
| 43 | |
| 44 | } |
| 45 | |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 46 | static void copy_email(struct passwd *pw) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 47 | { |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 48 | /* |
| 49 | * Make up a fake email address |
| 50 | * (name + '@' + hostname [+ '.' + domainname]) |
| 51 | */ |
| 52 | int len = strlen(pw->pw_name); |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 53 | if (len > sizeof(git_default_email)/2) |
Eric W. Biederman | 7a868a8 | 2005-07-14 18:52:31 -0600 | [diff] [blame] | 54 | die("Your sysadmin must hate you!"); |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 55 | memcpy(git_default_email, pw->pw_name, len); |
| 56 | git_default_email[len++] = '@'; |
| 57 | gethostname(git_default_email + len, sizeof(git_default_email) - len); |
| 58 | if (!strchr(git_default_email+len, '.')) { |
Petr Baudis | adc3dbc | 2005-10-21 03:57:39 +0200 | [diff] [blame] | 59 | struct hostent *he = gethostbyname(git_default_email + len); |
| 60 | char *domainname; |
| 61 | |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 62 | len = strlen(git_default_email); |
| 63 | git_default_email[len++] = '.'; |
Petr Baudis | adc3dbc | 2005-10-21 03:57:39 +0200 | [diff] [blame] | 64 | if (he && (domainname = strchr(he->h_name, '.'))) |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 65 | strlcpy(git_default_email + len, domainname + 1, |
| 66 | sizeof(git_default_email) - len); |
Petr Baudis | adc3dbc | 2005-10-21 03:57:39 +0200 | [diff] [blame] | 67 | else |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 68 | strlcpy(git_default_email + len, "(none)", |
| 69 | sizeof(git_default_email) - len); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 70 | } |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void setup_ident(void) |
| 74 | { |
| 75 | struct passwd *pw = NULL; |
| 76 | |
| 77 | /* Get the name ("gecos") */ |
| 78 | if (!git_default_name[0]) { |
| 79 | pw = getpwuid(getuid()); |
| 80 | if (!pw) |
| 81 | die("You don't exist. Go away!"); |
| 82 | copy_gecos(pw, git_default_name, sizeof(git_default_name)); |
| 83 | } |
| 84 | |
| 85 | if (!git_default_email[0]) { |
| 86 | if (!pw) |
| 87 | pw = getpwuid(getuid()); |
| 88 | if (!pw) |
| 89 | die("You don't exist. Go away!"); |
| 90 | copy_email(pw); |
| 91 | } |
| 92 | |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 93 | /* And set the default date */ |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 94 | if (!git_default_date[0]) |
| 95 | datestamp(git_default_date, sizeof(git_default_date)); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static int add_raw(char *buf, int size, int offset, const char *str) |
| 99 | { |
| 100 | int len = strlen(str); |
| 101 | if (offset + len > size) |
| 102 | return size; |
| 103 | memcpy(buf + offset, str, len); |
| 104 | return offset + len; |
| 105 | } |
| 106 | |
| 107 | static int crud(unsigned char c) |
| 108 | { |
Jason Riedy | fb2af03 | 2005-08-23 13:52:52 -0700 | [diff] [blame] | 109 | static char crud_array[256]; |
| 110 | static int crud_array_initialized = 0; |
| 111 | |
| 112 | if (!crud_array_initialized) { |
| 113 | int k; |
| 114 | |
| 115 | for (k = 0; k <= 31; ++k) crud_array[k] = 1; |
| 116 | crud_array[' '] = 1; |
| 117 | crud_array['.'] = 1; |
| 118 | crud_array[','] = 1; |
| 119 | crud_array[':'] = 1; |
| 120 | crud_array[';'] = 1; |
| 121 | crud_array['<'] = 1; |
| 122 | crud_array['>'] = 1; |
| 123 | crud_array['"'] = 1; |
| 124 | crud_array['\''] = 1; |
| 125 | crud_array_initialized = 1; |
| 126 | } |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 127 | return crud_array[c]; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Copy over a string to the destination, but avoid special |
| 132 | * characters ('\n', '<' and '>') and remove crud at the end |
| 133 | */ |
| 134 | static int copy(char *buf, int size, int offset, const char *src) |
| 135 | { |
| 136 | int i, len; |
| 137 | unsigned char c; |
| 138 | |
| 139 | /* Remove crud from the beginning.. */ |
| 140 | while ((c = *src) != 0) { |
| 141 | if (!crud(c)) |
| 142 | break; |
| 143 | src++; |
| 144 | } |
| 145 | |
| 146 | /* Remove crud from the end.. */ |
| 147 | len = strlen(src); |
| 148 | while (len > 0) { |
| 149 | c = src[len-1]; |
| 150 | if (!crud(c)) |
| 151 | break; |
| 152 | --len; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Copy the rest to the buffer, but avoid the special |
Junio C Hamano | 82f9d58 | 2005-12-29 01:30:08 -0800 | [diff] [blame] | 157 | * characters '\n' '<' and '>' that act as delimiters on |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 158 | * a identification line |
| 159 | */ |
| 160 | for (i = 0; i < len; i++) { |
| 161 | c = *src++; |
| 162 | switch (c) { |
| 163 | case '\n': case '<': case '>': |
| 164 | continue; |
| 165 | } |
| 166 | if (offset >= size) |
| 167 | return size; |
| 168 | buf[offset++] = c; |
| 169 | } |
| 170 | return offset; |
| 171 | } |
| 172 | |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 173 | static const char au_env[] = "GIT_AUTHOR_NAME"; |
| 174 | static const char co_env[] = "GIT_COMMITTER_NAME"; |
| 175 | static const char *env_hint = |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 176 | "\n" |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 177 | "*** Your name cannot be determined from your system services (gecos).\n" |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 178 | "\n" |
| 179 | "Run\n" |
| 180 | "\n" |
Tom Prince | e0d10e1 | 2007-01-28 16:16:53 -0800 | [diff] [blame] | 181 | " git config user.email \"you@email.com\"\n" |
| 182 | " git config user.name \"Your Name\"\n" |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 183 | "\n" |
| 184 | "To set the identity in this repository.\n" |
| 185 | "Add --global to set your account\'s default\n" |
| 186 | "\n"; |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 187 | |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 188 | const char *fmt_ident(const char *name, const char *email, |
| 189 | const char *date_str, int error_on_no_name) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 190 | { |
| 191 | static char buffer[1000]; |
| 192 | char date[50]; |
| 193 | int i; |
| 194 | |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 195 | setup_ident(); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 196 | if (!name) |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 197 | name = git_default_name; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 198 | if (!email) |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 199 | email = git_default_email; |
Junio C Hamano | dfdd309 | 2006-02-07 13:19:10 -0800 | [diff] [blame] | 200 | |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 201 | if (!*name) { |
Junio C Hamano | cb280e1 | 2007-01-25 19:05:01 -0800 | [diff] [blame] | 202 | struct passwd *pw; |
| 203 | |
| 204 | if (0 <= error_on_no_name && |
| 205 | name == git_default_name && env_hint) { |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 206 | fprintf(stderr, env_hint, au_env, co_env); |
| 207 | env_hint = NULL; /* warn only once, for "git-var -l" */ |
| 208 | } |
Junio C Hamano | cb280e1 | 2007-01-25 19:05:01 -0800 | [diff] [blame] | 209 | if (0 < error_on_no_name) |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 210 | die("empty ident %s <%s> not allowed", name, email); |
Junio C Hamano | cb280e1 | 2007-01-25 19:05:01 -0800 | [diff] [blame] | 211 | pw = getpwuid(getuid()); |
| 212 | if (!pw) |
| 213 | die("You don't exist. Go away!"); |
| 214 | strlcpy(git_default_name, pw->pw_name, |
| 215 | sizeof(git_default_name)); |
| 216 | name = git_default_name; |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 217 | } |
Junio C Hamano | dfdd309 | 2006-02-07 13:19:10 -0800 | [diff] [blame] | 218 | |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 219 | strcpy(date, git_default_date); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 220 | if (date_str) |
| 221 | parse_date(date_str, date, sizeof(date)); |
| 222 | |
| 223 | i = copy(buffer, sizeof(buffer), 0, name); |
| 224 | i = add_raw(buffer, sizeof(buffer), i, " <"); |
| 225 | i = copy(buffer, sizeof(buffer), i, email); |
| 226 | i = add_raw(buffer, sizeof(buffer), i, "> "); |
| 227 | i = copy(buffer, sizeof(buffer), i, date); |
| 228 | if (i >= sizeof(buffer)) |
| 229 | die("Impossibly long personal identifier"); |
| 230 | buffer[i] = 0; |
| 231 | return buffer; |
| 232 | } |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 233 | |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 234 | const char *git_author_info(int error_on_no_name) |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 235 | { |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 236 | return fmt_ident(getenv("GIT_AUTHOR_NAME"), |
Junio C Hamano | c7d77da | 2005-11-21 23:44:35 -0800 | [diff] [blame] | 237 | getenv("GIT_AUTHOR_EMAIL"), |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 238 | getenv("GIT_AUTHOR_DATE"), |
| 239 | error_on_no_name); |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 240 | } |
| 241 | |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 242 | const char *git_committer_info(int error_on_no_name) |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 243 | { |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 244 | return fmt_ident(getenv("GIT_COMMITTER_NAME"), |
Junio C Hamano | c7d77da | 2005-11-21 23:44:35 -0800 | [diff] [blame] | 245 | getenv("GIT_COMMITTER_EMAIL"), |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 246 | getenv("GIT_COMMITTER_DATE"), |
| 247 | error_on_no_name); |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 248 | } |