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 | |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 12 | static void copy_gecos(const struct passwd *w, char *name, size_t sz) |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 13 | { |
| 14 | char *src, *dst; |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 15 | size_t len, nlen; |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 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 | |
Luiz Fernando N. Capitulino | 0b952a9 | 2007-04-15 18:40:31 -0300 | [diff] [blame] | 46 | static void copy_email(const 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 | */ |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 52 | size_t 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]) { |
Matt Kraai | 46f74f0 | 2007-07-05 17:29:41 -0700 | [diff] [blame] | 86 | const char *email = getenv("EMAIL"); |
| 87 | |
Junio C Hamano | 99178c8 | 2010-01-08 08:01:10 -0800 | [diff] [blame] | 88 | if (email && email[0]) { |
Matt Kraai | 46f74f0 | 2007-07-05 17:29:41 -0700 | [diff] [blame] | 89 | strlcpy(git_default_email, email, |
| 90 | sizeof(git_default_email)); |
Junio C Hamano | 99178c8 | 2010-01-08 08:01:10 -0800 | [diff] [blame] | 91 | user_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 92 | } else { |
Matt Kraai | 46f74f0 | 2007-07-05 17:29:41 -0700 | [diff] [blame] | 93 | if (!pw) |
| 94 | pw = getpwuid(getuid()); |
| 95 | if (!pw) |
| 96 | die("You don't exist. Go away!"); |
| 97 | copy_email(pw); |
| 98 | } |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 101 | /* And set the default date */ |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 102 | if (!git_default_date[0]) |
| 103 | datestamp(git_default_date, sizeof(git_default_date)); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 106 | static int add_raw(char *buf, size_t size, int offset, const char *str) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 107 | { |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 108 | size_t len = strlen(str); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 109 | if (offset + len > size) |
| 110 | return size; |
| 111 | memcpy(buf + offset, str, len); |
| 112 | return offset + len; |
| 113 | } |
| 114 | |
| 115 | static int crud(unsigned char c) |
| 116 | { |
Alex Riesen | f64c81d | 2007-12-03 20:11:43 +0100 | [diff] [blame] | 117 | return c <= 32 || |
| 118 | c == '.' || |
| 119 | c == ',' || |
| 120 | c == ':' || |
| 121 | c == ';' || |
| 122 | c == '<' || |
| 123 | c == '>' || |
| 124 | c == '"' || |
Linus Torvalds | d404bf0 | 2008-12-01 08:41:50 -0800 | [diff] [blame] | 125 | c == '\\' || |
Alex Riesen | f64c81d | 2007-12-03 20:11:43 +0100 | [diff] [blame] | 126 | c == '\''; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Copy over a string to the destination, but avoid special |
| 131 | * characters ('\n', '<' and '>') and remove crud at the end |
| 132 | */ |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 133 | static int copy(char *buf, size_t size, int offset, const char *src) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 134 | { |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 135 | size_t i, len; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 136 | unsigned char c; |
| 137 | |
| 138 | /* Remove crud from the beginning.. */ |
| 139 | while ((c = *src) != 0) { |
| 140 | if (!crud(c)) |
| 141 | break; |
| 142 | src++; |
| 143 | } |
| 144 | |
| 145 | /* Remove crud from the end.. */ |
| 146 | len = strlen(src); |
| 147 | while (len > 0) { |
| 148 | c = src[len-1]; |
| 149 | if (!crud(c)) |
| 150 | break; |
| 151 | --len; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Copy the rest to the buffer, but avoid the special |
Junio C Hamano | 82f9d58 | 2005-12-29 01:30:08 -0800 | [diff] [blame] | 156 | * characters '\n' '<' and '>' that act as delimiters on |
Jim Meyering | 790296f | 2008-01-03 15:18:07 +0100 | [diff] [blame] | 157 | * an identification line |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 158 | */ |
| 159 | for (i = 0; i < len; i++) { |
| 160 | c = *src++; |
| 161 | switch (c) { |
| 162 | case '\n': case '<': case '>': |
| 163 | continue; |
| 164 | } |
| 165 | if (offset >= size) |
| 166 | return size; |
| 167 | buf[offset++] = c; |
| 168 | } |
| 169 | return offset; |
| 170 | } |
| 171 | |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 172 | static const char *env_hint = |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 173 | "\n" |
Santi BĂ©jar | 6c293d4 | 2008-03-08 12:30:04 +0100 | [diff] [blame] | 174 | "*** Please tell me who you are.\n" |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 175 | "\n" |
| 176 | "Run\n" |
| 177 | "\n" |
David Symonds | 8e7425d | 2007-12-07 10:36:45 +1100 | [diff] [blame] | 178 | " git config --global user.email \"you@example.com\"\n" |
Steffen Prohaska | 180787c | 2007-08-14 00:05:50 +0200 | [diff] [blame] | 179 | " git config --global user.name \"Your Name\"\n" |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 180 | "\n" |
Steffen Prohaska | 180787c | 2007-08-14 00:05:50 +0200 | [diff] [blame] | 181 | "to set your account\'s default identity.\n" |
| 182 | "Omit --global to set the identity only in this repository.\n" |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 183 | "\n"; |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 184 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 185 | const char *fmt_ident(const char *name, const char *email, |
| 186 | const char *date_str, int flag) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 187 | { |
| 188 | static char buffer[1000]; |
| 189 | char date[50]; |
| 190 | int i; |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 191 | int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME); |
| 192 | int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME); |
| 193 | int name_addr_only = (flag & IDENT_NO_DATE); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 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 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 204 | if ((warn_on_no_name || error_on_no_name) && |
Junio C Hamano | cb280e1 | 2007-01-25 19:05:01 -0800 | [diff] [blame] | 205 | name == git_default_name && env_hint) { |
Tarmigan Casebolt | 8b770a2 | 2010-01-17 00:19:24 -0800 | [diff] [blame] | 206 | fputs(env_hint, stderr); |
Jonathan Nieder | c27b392 | 2009-11-11 17:57:36 -0600 | [diff] [blame] | 207 | env_hint = NULL; /* warn only once */ |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 208 | } |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 209 | if (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); |
Jeff King | 4579bb4 | 2010-12-13 12:02:25 -0500 | [diff] [blame] | 220 | if (!name_addr_only && date_str && date_str[0]) { |
| 221 | if (parse_date(date_str, date, sizeof(date)) < 0) |
| 222 | die("invalid date format: %s", date_str); |
| 223 | } |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 224 | |
| 225 | i = copy(buffer, sizeof(buffer), 0, name); |
| 226 | i = add_raw(buffer, sizeof(buffer), i, " <"); |
| 227 | i = copy(buffer, sizeof(buffer), i, email); |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 228 | if (!name_addr_only) { |
| 229 | i = add_raw(buffer, sizeof(buffer), i, "> "); |
| 230 | i = copy(buffer, sizeof(buffer), i, date); |
| 231 | } else { |
| 232 | i = add_raw(buffer, sizeof(buffer), i, ">"); |
| 233 | } |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 234 | if (i >= sizeof(buffer)) |
| 235 | die("Impossibly long personal identifier"); |
| 236 | buffer[i] = 0; |
| 237 | return buffer; |
| 238 | } |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 239 | |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 240 | const char *fmt_name(const char *name, const char *email) |
| 241 | { |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 242 | return fmt_ident(name, email, NULL, IDENT_ERROR_ON_NO_NAME | IDENT_NO_DATE); |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 245 | const char *git_author_info(int flag) |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 246 | { |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 247 | return fmt_ident(getenv("GIT_AUTHOR_NAME"), |
Junio C Hamano | c7d77da | 2005-11-21 23:44:35 -0800 | [diff] [blame] | 248 | getenv("GIT_AUTHOR_EMAIL"), |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 249 | getenv("GIT_AUTHOR_DATE"), |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 250 | flag); |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 251 | } |
| 252 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 253 | const char *git_committer_info(int flag) |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 254 | { |
Junio C Hamano | 91c38a2 | 2010-01-08 07:39:11 -0800 | [diff] [blame] | 255 | if (getenv("GIT_COMMITTER_NAME")) |
| 256 | user_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 257 | if (getenv("GIT_COMMITTER_EMAIL")) |
| 258 | user_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 259 | return fmt_ident(getenv("GIT_COMMITTER_NAME"), |
Junio C Hamano | c7d77da | 2005-11-21 23:44:35 -0800 | [diff] [blame] | 260 | getenv("GIT_COMMITTER_EMAIL"), |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 261 | getenv("GIT_COMMITTER_DATE"), |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 262 | flag); |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 263 | } |
Junio C Hamano | 5aeb3a3 | 2010-01-17 13:54:28 -0800 | [diff] [blame] | 264 | |
| 265 | int user_ident_sufficiently_given(void) |
| 266 | { |
| 267 | #ifndef WINDOWS |
| 268 | return (user_ident_explicitly_given & IDENT_MAIL_GIVEN); |
| 269 | #else |
| 270 | return (user_ident_explicitly_given == IDENT_ALL_GIVEN); |
| 271 | #endif |
| 272 | } |