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 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 10 | static struct strbuf git_default_name = STRBUF_INIT; |
| 11 | static struct strbuf git_default_email = STRBUF_INIT; |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 12 | static char git_default_date[50]; |
Jeff King | 4528023 | 2012-11-14 16:34:05 -0800 | [diff] [blame] | 13 | |
| 14 | #define IDENT_NAME_GIVEN 01 |
| 15 | #define IDENT_MAIL_GIVEN 02 |
| 16 | #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN) |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 17 | static int committer_ident_explicitly_given; |
| 18 | static int author_ident_explicitly_given; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 19 | |
Rafael Gieschke | 590e081 | 2011-05-19 13:37:55 +0200 | [diff] [blame] | 20 | #ifdef NO_GECOS_IN_PWENT |
| 21 | #define get_gecos(ignored) "&" |
| 22 | #else |
| 23 | #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos) |
| 24 | #endif |
| 25 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 26 | static void copy_gecos(const struct passwd *w, struct strbuf *name) |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 27 | { |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 28 | char *src; |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 29 | |
| 30 | /* Traditionally GECOS field had office phone numbers etc, separated |
| 31 | * with commas. Also & stands for capitalized form of the login name. |
| 32 | */ |
| 33 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 34 | for (src = get_gecos(w); *src && *src != ','; src++) { |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 35 | int ch = *src; |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 36 | if (ch != '&') |
| 37 | strbuf_addch(name, ch); |
| 38 | else { |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 39 | /* Sorry, Mr. McDonald... */ |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 40 | strbuf_addch(name, toupper(*w->pw_name)); |
| 41 | strbuf_addstr(name, w->pw_name + 1); |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 46 | static int add_mailname_host(struct strbuf *buf) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 47 | { |
| 48 | FILE *mailname; |
Jonathan Nieder | dc342a2 | 2013-01-24 15:21:46 -0800 | [diff] [blame] | 49 | struct strbuf mailnamebuf = STRBUF_INIT; |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 50 | |
| 51 | mailname = fopen("/etc/mailname", "r"); |
| 52 | if (!mailname) { |
| 53 | if (errno != ENOENT) |
| 54 | warning("cannot open /etc/mailname: %s", |
| 55 | strerror(errno)); |
| 56 | return -1; |
| 57 | } |
Jonathan Nieder | dc342a2 | 2013-01-24 15:21:46 -0800 | [diff] [blame] | 58 | if (strbuf_getline(&mailnamebuf, mailname, '\n') == EOF) { |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 59 | if (ferror(mailname)) |
| 60 | warning("cannot read /etc/mailname: %s", |
| 61 | strerror(errno)); |
Jonathan Nieder | dc342a2 | 2013-01-24 15:21:46 -0800 | [diff] [blame] | 62 | strbuf_release(&mailnamebuf); |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 63 | fclose(mailname); |
| 64 | return -1; |
| 65 | } |
| 66 | /* success! */ |
Jonathan Nieder | dc342a2 | 2013-01-24 15:21:46 -0800 | [diff] [blame] | 67 | strbuf_addbuf(buf, &mailnamebuf); |
| 68 | strbuf_release(&mailnamebuf); |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 69 | fclose(mailname); |
| 70 | return 0; |
| 71 | } |
| 72 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 73 | static void add_domainname(struct strbuf *out) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 74 | { |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 75 | char buf[1024]; |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 76 | struct hostent *he; |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 77 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 78 | if (gethostname(buf, sizeof(buf))) { |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 79 | warning("cannot get host name: %s", strerror(errno)); |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 80 | strbuf_addstr(out, "(none)"); |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 81 | return; |
| 82 | } |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 83 | if (strchr(buf, '.')) |
Jeff King | f8254d3 | 2012-05-21 19:10:23 -0400 | [diff] [blame] | 84 | strbuf_addstr(out, buf); |
| 85 | else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.')) |
| 86 | strbuf_addstr(out, he->h_name); |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 87 | else |
Jeff King | f8254d3 | 2012-05-21 19:10:23 -0400 | [diff] [blame] | 88 | strbuf_addf(out, "%s.(none)", buf); |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 91 | static void copy_email(const struct passwd *pw, struct strbuf *email) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 92 | { |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 93 | /* |
| 94 | * Make up a fake email address |
| 95 | * (name + '@' + hostname [+ '.' + domainname]) |
| 96 | */ |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 97 | strbuf_addstr(email, pw->pw_name); |
| 98 | strbuf_addch(email, '@'); |
Petr Baudis | adc3dbc | 2005-10-21 03:57:39 +0200 | [diff] [blame] | 99 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 100 | if (!add_mailname_host(email)) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 101 | return; /* read from "/etc/mailname" (Debian) */ |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 102 | add_domainname(email); |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Junio C Hamano | dad148c | 2012-09-15 22:50:09 -0700 | [diff] [blame] | 105 | static const char *ident_default_name(void) |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 106 | { |
Jeff King | be641ab | 2012-05-21 19:10:29 -0400 | [diff] [blame] | 107 | if (!git_default_name.len) { |
Jeff King | 2f70587 | 2012-05-21 19:10:20 -0400 | [diff] [blame] | 108 | copy_gecos(xgetpwuid_self(), &git_default_name); |
Jeff King | be641ab | 2012-05-21 19:10:29 -0400 | [diff] [blame] | 109 | strbuf_trim(&git_default_name); |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 110 | } |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 111 | return git_default_name.buf; |
Jeff King | bcb2b00 | 2012-05-21 19:09:43 -0400 | [diff] [blame] | 112 | } |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 113 | |
Jeff King | bcb2b00 | 2012-05-21 19:09:43 -0400 | [diff] [blame] | 114 | const char *ident_default_email(void) |
| 115 | { |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 116 | if (!git_default_email.len) { |
Matt Kraai | 46f74f0 | 2007-07-05 17:29:41 -0700 | [diff] [blame] | 117 | const char *email = getenv("EMAIL"); |
| 118 | |
Junio C Hamano | 99178c8 | 2010-01-08 08:01:10 -0800 | [diff] [blame] | 119 | if (email && email[0]) { |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 120 | strbuf_addstr(&git_default_email, email); |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 121 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 122 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Jeff King | 2f70587 | 2012-05-21 19:10:20 -0400 | [diff] [blame] | 123 | } else |
| 124 | copy_email(xgetpwuid_self(), &git_default_email); |
Jeff King | be641ab | 2012-05-21 19:10:29 -0400 | [diff] [blame] | 125 | strbuf_trim(&git_default_email); |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 126 | } |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 127 | return git_default_email.buf; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Junio C Hamano | dad148c | 2012-09-15 22:50:09 -0700 | [diff] [blame] | 130 | static const char *ident_default_date(void) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 131 | { |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 132 | if (!git_default_date[0]) |
| 133 | datestamp(git_default_date, sizeof(git_default_date)); |
Jeff King | bcb2b00 | 2012-05-21 19:09:43 -0400 | [diff] [blame] | 134 | return git_default_date; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static int crud(unsigned char c) |
| 138 | { |
Alex Riesen | f64c81d | 2007-12-03 20:11:43 +0100 | [diff] [blame] | 139 | return c <= 32 || |
| 140 | c == '.' || |
| 141 | c == ',' || |
| 142 | c == ':' || |
| 143 | c == ';' || |
| 144 | c == '<' || |
| 145 | c == '>' || |
| 146 | c == '"' || |
Linus Torvalds | d404bf0 | 2008-12-01 08:41:50 -0800 | [diff] [blame] | 147 | c == '\\' || |
Alex Riesen | f64c81d | 2007-12-03 20:11:43 +0100 | [diff] [blame] | 148 | c == '\''; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Copy over a string to the destination, but avoid special |
| 153 | * characters ('\n', '<' and '>') and remove crud at the end |
| 154 | */ |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 155 | static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 156 | { |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 157 | size_t i, len; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 158 | unsigned char c; |
| 159 | |
| 160 | /* Remove crud from the beginning.. */ |
| 161 | while ((c = *src) != 0) { |
| 162 | if (!crud(c)) |
| 163 | break; |
| 164 | src++; |
| 165 | } |
| 166 | |
| 167 | /* Remove crud from the end.. */ |
| 168 | len = strlen(src); |
| 169 | while (len > 0) { |
| 170 | c = src[len-1]; |
| 171 | if (!crud(c)) |
| 172 | break; |
| 173 | --len; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Copy the rest to the buffer, but avoid the special |
Junio C Hamano | 82f9d58 | 2005-12-29 01:30:08 -0800 | [diff] [blame] | 178 | * characters '\n' '<' and '>' that act as delimiters on |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 179 | * an identification line. We can only remove crud, never add it, |
| 180 | * so 'len' is our maximum. |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 181 | */ |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 182 | strbuf_grow(sb, len); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 183 | for (i = 0; i < len; i++) { |
| 184 | c = *src++; |
| 185 | switch (c) { |
| 186 | case '\n': case '<': case '>': |
| 187 | continue; |
| 188 | } |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 189 | sb->buf[sb->len++] = c; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 190 | } |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 191 | sb->buf[sb->len] = '\0'; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 194 | /* |
| 195 | * Reverse of fmt_ident(); given an ident line, split the fields |
| 196 | * to allow the caller to parse it. |
| 197 | * Signal a success by returning 0, but date/tz fields of the result |
| 198 | * can still be NULL if the input line only has the name/email part |
| 199 | * (e.g. reading from a reflog entry). |
| 200 | */ |
| 201 | int split_ident_line(struct ident_split *split, const char *line, int len) |
| 202 | { |
| 203 | const char *cp; |
| 204 | size_t span; |
| 205 | int status = -1; |
| 206 | |
| 207 | memset(split, 0, sizeof(*split)); |
| 208 | |
| 209 | split->name_begin = line; |
| 210 | for (cp = line; *cp && cp < line + len; cp++) |
| 211 | if (*cp == '<') { |
| 212 | split->mail_begin = cp + 1; |
| 213 | break; |
| 214 | } |
| 215 | if (!split->mail_begin) |
| 216 | return status; |
| 217 | |
Jeff King | d9955fd | 2012-05-22 02:12:20 -0400 | [diff] [blame] | 218 | for (cp = split->mail_begin - 2; line <= cp; cp--) |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 219 | if (!isspace(*cp)) { |
| 220 | split->name_end = cp + 1; |
| 221 | break; |
| 222 | } |
Junio C Hamano | e27ddb6 | 2012-08-31 14:54:18 -0700 | [diff] [blame] | 223 | if (!split->name_end) { |
| 224 | /* no human readable name */ |
| 225 | split->name_end = split->name_begin; |
| 226 | } |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 227 | |
| 228 | for (cp = split->mail_begin; cp < line + len; cp++) |
| 229 | if (*cp == '>') { |
| 230 | split->mail_end = cp; |
| 231 | break; |
| 232 | } |
| 233 | if (!split->mail_end) |
| 234 | return status; |
| 235 | |
| 236 | for (cp = split->mail_end + 1; cp < line + len && isspace(*cp); cp++) |
| 237 | ; |
| 238 | if (line + len <= cp) |
| 239 | goto person_only; |
| 240 | split->date_begin = cp; |
| 241 | span = strspn(cp, "0123456789"); |
| 242 | if (!span) |
| 243 | goto person_only; |
| 244 | split->date_end = split->date_begin + span; |
| 245 | for (cp = split->date_end; cp < line + len && isspace(*cp); cp++) |
| 246 | ; |
| 247 | if (line + len <= cp || (*cp != '+' && *cp != '-')) |
| 248 | goto person_only; |
| 249 | split->tz_begin = cp; |
| 250 | span = strspn(cp + 1, "0123456789"); |
| 251 | if (!span) |
| 252 | goto person_only; |
| 253 | split->tz_end = split->tz_begin + 1 + span; |
| 254 | return 0; |
| 255 | |
| 256 | person_only: |
| 257 | split->date_begin = NULL; |
| 258 | split->date_end = NULL; |
| 259 | split->tz_begin = NULL; |
| 260 | split->tz_end = NULL; |
| 261 | return 0; |
| 262 | } |
| 263 | |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 264 | static const char *env_hint = |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 265 | "\n" |
Santi Béjar | 6c293d4 | 2008-03-08 12:30:04 +0100 | [diff] [blame] | 266 | "*** Please tell me who you are.\n" |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 267 | "\n" |
| 268 | "Run\n" |
| 269 | "\n" |
David Symonds | 8e7425d | 2007-12-07 10:36:45 +1100 | [diff] [blame] | 270 | " git config --global user.email \"you@example.com\"\n" |
Steffen Prohaska | 180787c | 2007-08-14 00:05:50 +0200 | [diff] [blame] | 271 | " git config --global user.name \"Your Name\"\n" |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 272 | "\n" |
Steffen Prohaska | 180787c | 2007-08-14 00:05:50 +0200 | [diff] [blame] | 273 | "to set your account\'s default identity.\n" |
| 274 | "Omit --global to set the identity only in this repository.\n" |
Han-Wen Nienhuys | d5cc2de | 2006-11-28 11:27:39 +0100 | [diff] [blame] | 275 | "\n"; |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 276 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 277 | const char *fmt_ident(const char *name, const char *email, |
| 278 | const char *date_str, int flag) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 279 | { |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 280 | static struct strbuf ident = STRBUF_INIT; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 281 | char date[50]; |
Jeff King | f9bc573 | 2012-05-24 19:28:40 -0400 | [diff] [blame] | 282 | int strict = (flag & IDENT_STRICT); |
Jeff King | 359b27a | 2012-05-24 19:26:50 -0400 | [diff] [blame] | 283 | int want_date = !(flag & IDENT_NO_DATE); |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 284 | int want_name = !(flag & IDENT_NO_NAME); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 285 | |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 286 | if (want_name && !name) |
Jeff King | bcb2b00 | 2012-05-21 19:09:43 -0400 | [diff] [blame] | 287 | name = ident_default_name(); |
| 288 | if (!email) |
| 289 | email = ident_default_email(); |
Junio C Hamano | dfdd309 | 2006-02-07 13:19:10 -0800 | [diff] [blame] | 290 | |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 291 | if (want_name && !*name) { |
Junio C Hamano | cb280e1 | 2007-01-25 19:05:01 -0800 | [diff] [blame] | 292 | struct passwd *pw; |
| 293 | |
Jeff King | f9bc573 | 2012-05-24 19:28:40 -0400 | [diff] [blame] | 294 | if (strict) { |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 295 | if (name == git_default_name.buf) |
Jeff King | b9f0ac1 | 2012-05-21 19:10:11 -0400 | [diff] [blame] | 296 | fputs(env_hint, stderr); |
Jeff King | b00f6cf | 2012-05-24 19:26:32 -0400 | [diff] [blame] | 297 | die("empty ident name (for <%s>) not allowed", email); |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 298 | } |
Jeff King | 2f70587 | 2012-05-21 19:10:20 -0400 | [diff] [blame] | 299 | pw = xgetpwuid_self(); |
Jeff King | 060d4bb | 2012-05-21 19:10:14 -0400 | [diff] [blame] | 300 | name = pw->pw_name; |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 301 | } |
Junio C Hamano | dfdd309 | 2006-02-07 13:19:10 -0800 | [diff] [blame] | 302 | |
Jeff King | 8c5b1ae | 2012-05-24 19:32:37 -0400 | [diff] [blame] | 303 | if (strict && email == git_default_email.buf && |
| 304 | strstr(email, "(none)")) { |
| 305 | fputs(env_hint, stderr); |
| 306 | die("unable to auto-detect email address (got '%s')", email); |
Jeff King | 4579bb4 | 2010-12-13 12:02:25 -0500 | [diff] [blame] | 307 | } |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 308 | |
Jeff King | 359b27a | 2012-05-24 19:26:50 -0400 | [diff] [blame] | 309 | if (want_date) { |
| 310 | if (date_str && date_str[0]) { |
| 311 | if (parse_date(date_str, date, sizeof(date)) < 0) |
| 312 | die("invalid date format: %s", date_str); |
| 313 | } |
| 314 | else |
| 315 | strcpy(date, ident_default_date()); |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 316 | } |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 317 | |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 318 | strbuf_reset(&ident); |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 319 | if (want_name) { |
| 320 | strbuf_addstr_without_crud(&ident, name); |
| 321 | strbuf_addstr(&ident, " <"); |
| 322 | } |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 323 | strbuf_addstr_without_crud(&ident, email); |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 324 | if (want_name) |
| 325 | strbuf_addch(&ident, '>'); |
Jeff King | 359b27a | 2012-05-24 19:26:50 -0400 | [diff] [blame] | 326 | if (want_date) { |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 327 | strbuf_addch(&ident, ' '); |
| 328 | strbuf_addstr_without_crud(&ident, date); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 329 | } |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 330 | return ident.buf; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 331 | } |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 332 | |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 333 | const char *fmt_name(const char *name, const char *email) |
| 334 | { |
Jeff King | f9bc573 | 2012-05-24 19:28:40 -0400 | [diff] [blame] | 335 | return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE); |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 338 | const char *git_author_info(int flag) |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 339 | { |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 340 | if (getenv("GIT_AUTHOR_NAME")) |
| 341 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 342 | if (getenv("GIT_AUTHOR_EMAIL")) |
| 343 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 344 | return fmt_ident(getenv("GIT_AUTHOR_NAME"), |
Junio C Hamano | c7d77da | 2005-11-21 23:44:35 -0800 | [diff] [blame] | 345 | getenv("GIT_AUTHOR_EMAIL"), |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 346 | getenv("GIT_AUTHOR_DATE"), |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 347 | flag); |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 348 | } |
| 349 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 350 | const char *git_committer_info(int flag) |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 351 | { |
Junio C Hamano | 91c38a2 | 2010-01-08 07:39:11 -0800 | [diff] [blame] | 352 | if (getenv("GIT_COMMITTER_NAME")) |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 353 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
Junio C Hamano | 91c38a2 | 2010-01-08 07:39:11 -0800 | [diff] [blame] | 354 | if (getenv("GIT_COMMITTER_EMAIL")) |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 355 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 356 | return fmt_ident(getenv("GIT_COMMITTER_NAME"), |
Junio C Hamano | c7d77da | 2005-11-21 23:44:35 -0800 | [diff] [blame] | 357 | getenv("GIT_COMMITTER_EMAIL"), |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 358 | getenv("GIT_COMMITTER_DATE"), |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 359 | flag); |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 360 | } |
Junio C Hamano | 5aeb3a3 | 2010-01-17 13:54:28 -0800 | [diff] [blame] | 361 | |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 362 | static int ident_is_sufficient(int user_ident_explicitly_given) |
Junio C Hamano | 5aeb3a3 | 2010-01-17 13:54:28 -0800 | [diff] [blame] | 363 | { |
| 364 | #ifndef WINDOWS |
| 365 | return (user_ident_explicitly_given & IDENT_MAIL_GIVEN); |
| 366 | #else |
| 367 | return (user_ident_explicitly_given == IDENT_ALL_GIVEN); |
| 368 | #endif |
| 369 | } |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 370 | |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 371 | int committer_ident_sufficiently_given(void) |
| 372 | { |
| 373 | return ident_is_sufficient(committer_ident_explicitly_given); |
| 374 | } |
| 375 | |
| 376 | int author_ident_sufficiently_given(void) |
| 377 | { |
| 378 | return ident_is_sufficient(author_ident_explicitly_given); |
| 379 | } |
| 380 | |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 381 | int git_ident_config(const char *var, const char *value, void *data) |
| 382 | { |
| 383 | if (!strcmp(var, "user.name")) { |
| 384 | if (!value) |
| 385 | return config_error_nonbool(var); |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 386 | strbuf_reset(&git_default_name); |
| 387 | strbuf_addstr(&git_default_name, value); |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 388 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 389 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | if (!strcmp(var, "user.email")) { |
| 394 | if (!value) |
| 395 | return config_error_nonbool(var); |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 396 | strbuf_reset(&git_default_email); |
| 397 | strbuf_addstr(&git_default_email, value); |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 398 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 399 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |