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 | |
Rafael Gieschke | 590e081 | 2011-05-19 13:37:55 +0200 | [diff] [blame] | 12 | #ifdef NO_GECOS_IN_PWENT |
| 13 | #define get_gecos(ignored) "&" |
| 14 | #else |
| 15 | #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos) |
| 16 | #endif |
| 17 | |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 18 | 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] | 19 | { |
| 20 | char *src, *dst; |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 21 | size_t len, nlen; |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 22 | |
| 23 | nlen = strlen(w->pw_name); |
| 24 | |
| 25 | /* Traditionally GECOS field had office phone numbers etc, separated |
| 26 | * with commas. Also & stands for capitalized form of the login name. |
| 27 | */ |
| 28 | |
Rafael Gieschke | 590e081 | 2011-05-19 13:37:55 +0200 | [diff] [blame] | 29 | for (len = 0, dst = name, src = get_gecos(w); len < sz; src++) { |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 30 | int ch = *src; |
| 31 | if (ch != '&') { |
| 32 | *dst++ = ch; |
| 33 | if (ch == 0 || ch == ',') |
| 34 | break; |
| 35 | len++; |
| 36 | continue; |
| 37 | } |
| 38 | if (len + nlen < sz) { |
| 39 | /* Sorry, Mr. McDonald... */ |
| 40 | *dst++ = toupper(*w->pw_name); |
| 41 | memcpy(dst, w->pw_name + 1, nlen - 1); |
| 42 | dst += nlen - 1; |
Rafael Gieschke | c0336ff | 2011-05-16 22:08:21 +0200 | [diff] [blame] | 43 | len += nlen; |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | if (len < sz) |
| 47 | name[len] = 0; |
| 48 | else |
| 49 | die("Your parents must have hated you!"); |
| 50 | |
| 51 | } |
| 52 | |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 53 | static int add_mailname_host(char *buf, size_t len) |
| 54 | { |
| 55 | FILE *mailname; |
| 56 | |
| 57 | mailname = fopen("/etc/mailname", "r"); |
| 58 | if (!mailname) { |
| 59 | if (errno != ENOENT) |
| 60 | warning("cannot open /etc/mailname: %s", |
| 61 | strerror(errno)); |
| 62 | return -1; |
| 63 | } |
| 64 | if (!fgets(buf, len, mailname)) { |
| 65 | if (ferror(mailname)) |
| 66 | warning("cannot read /etc/mailname: %s", |
| 67 | strerror(errno)); |
| 68 | fclose(mailname); |
| 69 | return -1; |
| 70 | } |
| 71 | /* success! */ |
| 72 | fclose(mailname); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static void add_domainname(char *buf, size_t len) |
| 77 | { |
| 78 | struct hostent *he; |
| 79 | size_t namelen; |
| 80 | const char *domainname; |
| 81 | |
| 82 | if (gethostname(buf, len)) { |
| 83 | warning("cannot get host name: %s", strerror(errno)); |
| 84 | strlcpy(buf, "(none)", len); |
| 85 | return; |
| 86 | } |
| 87 | namelen = strlen(buf); |
| 88 | if (memchr(buf, '.', namelen)) |
| 89 | return; |
| 90 | |
| 91 | he = gethostbyname(buf); |
| 92 | buf[namelen++] = '.'; |
| 93 | buf += namelen; |
| 94 | len -= namelen; |
| 95 | if (he && (domainname = strchr(he->h_name, '.'))) |
| 96 | strlcpy(buf, domainname + 1, len); |
| 97 | else |
| 98 | strlcpy(buf, "(none)", len); |
| 99 | } |
| 100 | |
Luiz Fernando N. Capitulino | 0b952a9 | 2007-04-15 18:40:31 -0300 | [diff] [blame] | 101 | static void copy_email(const struct passwd *pw) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 102 | { |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 103 | /* |
| 104 | * Make up a fake email address |
| 105 | * (name + '@' + hostname [+ '.' + domainname]) |
| 106 | */ |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 107 | size_t len = strlen(pw->pw_name); |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 108 | if (len > sizeof(git_default_email)/2) |
Eric W. Biederman | 7a868a8 | 2005-07-14 18:52:31 -0600 | [diff] [blame] | 109 | die("Your sysadmin must hate you!"); |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 110 | memcpy(git_default_email, pw->pw_name, len); |
| 111 | git_default_email[len++] = '@'; |
Petr Baudis | adc3dbc | 2005-10-21 03:57:39 +0200 | [diff] [blame] | 112 | |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 113 | if (!add_mailname_host(git_default_email + len, |
| 114 | sizeof(git_default_email) - len)) |
| 115 | return; /* read from "/etc/mailname" (Debian) */ |
| 116 | add_domainname(git_default_email + len, |
| 117 | sizeof(git_default_email) - len); |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Jonathan Nieder | d855e4d | 2011-10-06 12:17:19 -0500 | [diff] [blame] | 120 | static void setup_ident(const char **name, const char **emailp) |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 121 | { |
| 122 | struct passwd *pw = NULL; |
| 123 | |
| 124 | /* Get the name ("gecos") */ |
Jonathan Nieder | d855e4d | 2011-10-06 12:17:19 -0500 | [diff] [blame] | 125 | if (!*name && !git_default_name[0]) { |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 126 | pw = getpwuid(getuid()); |
| 127 | if (!pw) |
| 128 | die("You don't exist. Go away!"); |
| 129 | copy_gecos(pw, git_default_name, sizeof(git_default_name)); |
| 130 | } |
Jonathan Nieder | d855e4d | 2011-10-06 12:17:19 -0500 | [diff] [blame] | 131 | if (!*name) |
| 132 | *name = git_default_name; |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 133 | |
Jonathan Nieder | d855e4d | 2011-10-06 12:17:19 -0500 | [diff] [blame] | 134 | if (!*emailp && !git_default_email[0]) { |
Matt Kraai | 46f74f0 | 2007-07-05 17:29:41 -0700 | [diff] [blame] | 135 | const char *email = getenv("EMAIL"); |
| 136 | |
Junio C Hamano | 99178c8 | 2010-01-08 08:01:10 -0800 | [diff] [blame] | 137 | if (email && email[0]) { |
Matt Kraai | 46f74f0 | 2007-07-05 17:29:41 -0700 | [diff] [blame] | 138 | strlcpy(git_default_email, email, |
| 139 | sizeof(git_default_email)); |
Junio C Hamano | 99178c8 | 2010-01-08 08:01:10 -0800 | [diff] [blame] | 140 | user_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 141 | } else { |
Matt Kraai | 46f74f0 | 2007-07-05 17:29:41 -0700 | [diff] [blame] | 142 | if (!pw) |
| 143 | pw = getpwuid(getuid()); |
| 144 | if (!pw) |
| 145 | die("You don't exist. Go away!"); |
| 146 | copy_email(pw); |
| 147 | } |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 148 | } |
Jonathan Nieder | d855e4d | 2011-10-06 12:17:19 -0500 | [diff] [blame] | 149 | if (!*emailp) |
| 150 | *emailp = git_default_email; |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 151 | |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 152 | /* And set the default date */ |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 153 | if (!git_default_date[0]) |
| 154 | datestamp(git_default_date, sizeof(git_default_date)); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 157 | 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] | 158 | { |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 159 | size_t len = strlen(str); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 160 | if (offset + len > size) |
| 161 | return size; |
| 162 | memcpy(buf + offset, str, len); |
| 163 | return offset + len; |
| 164 | } |
| 165 | |
| 166 | static int crud(unsigned char c) |
| 167 | { |
Alex Riesen | f64c81d | 2007-12-03 20:11:43 +0100 | [diff] [blame] | 168 | return c <= 32 || |
| 169 | c == '.' || |
| 170 | c == ',' || |
| 171 | c == ':' || |
| 172 | c == ';' || |
| 173 | c == '<' || |
| 174 | c == '>' || |
| 175 | c == '"' || |
Linus Torvalds | d404bf0 | 2008-12-01 08:41:50 -0800 | [diff] [blame] | 176 | c == '\\' || |
Alex Riesen | f64c81d | 2007-12-03 20:11:43 +0100 | [diff] [blame] | 177 | c == '\''; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Copy over a string to the destination, but avoid special |
| 182 | * characters ('\n', '<' and '>') and remove crud at the end |
| 183 | */ |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 184 | 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] | 185 | { |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 186 | size_t i, len; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 187 | unsigned char c; |
| 188 | |
| 189 | /* Remove crud from the beginning.. */ |
| 190 | while ((c = *src) != 0) { |
| 191 | if (!crud(c)) |
| 192 | break; |
| 193 | src++; |
| 194 | } |
| 195 | |
| 196 | /* Remove crud from the end.. */ |
| 197 | len = strlen(src); |
| 198 | while (len > 0) { |
| 199 | c = src[len-1]; |
| 200 | if (!crud(c)) |
| 201 | break; |
| 202 | --len; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Copy the rest to the buffer, but avoid the special |
Junio C Hamano | 82f9d58 | 2005-12-29 01:30:08 -0800 | [diff] [blame] | 207 | * characters '\n' '<' and '>' that act as delimiters on |
Jim Meyering | 790296f | 2008-01-03 15:18:07 +0100 | [diff] [blame] | 208 | * an identification line |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 209 | */ |
| 210 | for (i = 0; i < len; i++) { |
| 211 | c = *src++; |
| 212 | switch (c) { |
| 213 | case '\n': case '<': case '>': |
| 214 | continue; |
| 215 | } |
| 216 | if (offset >= size) |
| 217 | return size; |
| 218 | buf[offset++] = c; |
| 219 | } |
| 220 | return offset; |
| 221 | } |
| 222 | |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 223 | static const char *env_hint = |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 224 | "\n" |
Santi Béjar | 6c293d4 | 2008-03-08 12:30:04 +0100 | [diff] [blame] | 225 | "*** Please tell me who you are.\n" |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 226 | "\n" |
| 227 | "Run\n" |
| 228 | "\n" |
David Symonds | 8e7425d | 2007-12-07 10:36:45 +1100 | [diff] [blame] | 229 | " git config --global user.email \"you@example.com\"\n" |
Steffen Prohaska | 180787c | 2007-08-14 00:05:50 +0200 | [diff] [blame] | 230 | " git config --global user.name \"Your Name\"\n" |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 231 | "\n" |
Steffen Prohaska | 180787c | 2007-08-14 00:05:50 +0200 | [diff] [blame] | 232 | "to set your account\'s default identity.\n" |
| 233 | "Omit --global to set the identity only in this repository.\n" |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 234 | "\n"; |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 235 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 236 | const char *fmt_ident(const char *name, const char *email, |
| 237 | const char *date_str, int flag) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 238 | { |
| 239 | static char buffer[1000]; |
| 240 | char date[50]; |
| 241 | int i; |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 242 | int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME); |
| 243 | int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME); |
| 244 | int name_addr_only = (flag & IDENT_NO_DATE); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 245 | |
Jonathan Nieder | d855e4d | 2011-10-06 12:17:19 -0500 | [diff] [blame] | 246 | setup_ident(&name, &email); |
Junio C Hamano | dfdd309 | 2006-02-07 13:19:10 -0800 | [diff] [blame] | 247 | |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 248 | if (!*name) { |
Junio C Hamano | cb280e1 | 2007-01-25 19:05:01 -0800 | [diff] [blame] | 249 | struct passwd *pw; |
| 250 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 251 | if ((warn_on_no_name || error_on_no_name) && |
Junio C Hamano | cb280e1 | 2007-01-25 19:05:01 -0800 | [diff] [blame] | 252 | name == git_default_name && env_hint) { |
Tarmigan Casebolt | 8b770a2 | 2010-01-17 00:19:24 -0800 | [diff] [blame] | 253 | fputs(env_hint, stderr); |
Jonathan Nieder | c27b392 | 2009-11-11 17:57:36 -0600 | [diff] [blame] | 254 | env_hint = NULL; /* warn only once */ |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 255 | } |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 256 | if (error_on_no_name) |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 257 | die("empty ident %s <%s> not allowed", name, email); |
Junio C Hamano | cb280e1 | 2007-01-25 19:05:01 -0800 | [diff] [blame] | 258 | pw = getpwuid(getuid()); |
| 259 | if (!pw) |
| 260 | die("You don't exist. Go away!"); |
| 261 | strlcpy(git_default_name, pw->pw_name, |
| 262 | sizeof(git_default_name)); |
| 263 | name = git_default_name; |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 264 | } |
Junio C Hamano | dfdd309 | 2006-02-07 13:19:10 -0800 | [diff] [blame] | 265 | |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 266 | strcpy(date, git_default_date); |
Jeff King | 4579bb4 | 2010-12-13 12:02:25 -0500 | [diff] [blame] | 267 | if (!name_addr_only && date_str && date_str[0]) { |
| 268 | if (parse_date(date_str, date, sizeof(date)) < 0) |
| 269 | die("invalid date format: %s", date_str); |
| 270 | } |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 271 | |
| 272 | i = copy(buffer, sizeof(buffer), 0, name); |
| 273 | i = add_raw(buffer, sizeof(buffer), i, " <"); |
| 274 | i = copy(buffer, sizeof(buffer), i, email); |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 275 | if (!name_addr_only) { |
| 276 | i = add_raw(buffer, sizeof(buffer), i, "> "); |
| 277 | i = copy(buffer, sizeof(buffer), i, date); |
| 278 | } else { |
| 279 | i = add_raw(buffer, sizeof(buffer), i, ">"); |
| 280 | } |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 281 | if (i >= sizeof(buffer)) |
| 282 | die("Impossibly long personal identifier"); |
| 283 | buffer[i] = 0; |
| 284 | return buffer; |
| 285 | } |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 286 | |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 287 | const char *fmt_name(const char *name, const char *email) |
| 288 | { |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 289 | 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] | 290 | } |
| 291 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 292 | const char *git_author_info(int flag) |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 293 | { |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 294 | return fmt_ident(getenv("GIT_AUTHOR_NAME"), |
Junio C Hamano | c7d77da | 2005-11-21 23:44:35 -0800 | [diff] [blame] | 295 | getenv("GIT_AUTHOR_EMAIL"), |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 296 | getenv("GIT_AUTHOR_DATE"), |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 297 | flag); |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 298 | } |
| 299 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 300 | const char *git_committer_info(int flag) |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 301 | { |
Junio C Hamano | 91c38a2 | 2010-01-08 07:39:11 -0800 | [diff] [blame] | 302 | if (getenv("GIT_COMMITTER_NAME")) |
| 303 | user_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 304 | if (getenv("GIT_COMMITTER_EMAIL")) |
| 305 | user_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 306 | return fmt_ident(getenv("GIT_COMMITTER_NAME"), |
Junio C Hamano | c7d77da | 2005-11-21 23:44:35 -0800 | [diff] [blame] | 307 | getenv("GIT_COMMITTER_EMAIL"), |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 308 | getenv("GIT_COMMITTER_DATE"), |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 309 | flag); |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 310 | } |
Junio C Hamano | 5aeb3a3 | 2010-01-17 13:54:28 -0800 | [diff] [blame] | 311 | |
| 312 | int user_ident_sufficiently_given(void) |
| 313 | { |
| 314 | #ifndef WINDOWS |
| 315 | return (user_ident_explicitly_given & IDENT_MAIL_GIVEN); |
| 316 | #else |
| 317 | return (user_ident_explicitly_given == IDENT_ALL_GIVEN); |
| 318 | #endif |
| 319 | } |