]>
Commit | Line | Data |
---|---|---|
6aa33f40 LT |
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 | ||
8587ead7 JK |
10 | static struct strbuf git_default_name = STRBUF_INIT; |
11 | static struct strbuf git_default_email = STRBUF_INIT; | |
e1b10391 | 12 | static char git_default_date[50]; |
45280230 JK |
13 | |
14 | #define IDENT_NAME_GIVEN 01 | |
15 | #define IDENT_MAIL_GIVEN 02 | |
16 | #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN) | |
d6991cee JK |
17 | static int committer_ident_explicitly_given; |
18 | static int author_ident_explicitly_given; | |
6aa33f40 | 19 | |
590e081d RG |
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 | ||
8587ead7 | 26 | static void copy_gecos(const struct passwd *w, struct strbuf *name) |
e9bacb4f | 27 | { |
8587ead7 | 28 | char *src; |
e9bacb4f JH |
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 | ||
8587ead7 | 34 | for (src = get_gecos(w); *src && *src != ','; src++) { |
e9bacb4f | 35 | int ch = *src; |
8587ead7 JK |
36 | if (ch != '&') |
37 | strbuf_addch(name, ch); | |
38 | else { | |
e9bacb4f | 39 | /* Sorry, Mr. McDonald... */ |
8587ead7 JK |
40 | strbuf_addch(name, toupper(*w->pw_name)); |
41 | strbuf_addstr(name, w->pw_name + 1); | |
e9bacb4f JH |
42 | } |
43 | } | |
e9bacb4f JH |
44 | } |
45 | ||
8587ead7 | 46 | static int add_mailname_host(struct strbuf *buf) |
8a55caa8 JN |
47 | { |
48 | FILE *mailname; | |
49 | ||
50 | mailname = fopen("/etc/mailname", "r"); | |
51 | if (!mailname) { | |
52 | if (errno != ENOENT) | |
53 | warning("cannot open /etc/mailname: %s", | |
54 | strerror(errno)); | |
55 | return -1; | |
56 | } | |
8587ead7 | 57 | if (strbuf_getline(buf, mailname, '\n') == EOF) { |
8a55caa8 JN |
58 | if (ferror(mailname)) |
59 | warning("cannot read /etc/mailname: %s", | |
60 | strerror(errno)); | |
61 | fclose(mailname); | |
62 | return -1; | |
63 | } | |
64 | /* success! */ | |
65 | fclose(mailname); | |
66 | return 0; | |
67 | } | |
68 | ||
8587ead7 | 69 | static void add_domainname(struct strbuf *out) |
8a55caa8 | 70 | { |
8587ead7 | 71 | char buf[1024]; |
8a55caa8 | 72 | struct hostent *he; |
8a55caa8 | 73 | |
8587ead7 | 74 | if (gethostname(buf, sizeof(buf))) { |
8a55caa8 | 75 | warning("cannot get host name: %s", strerror(errno)); |
8587ead7 | 76 | strbuf_addstr(out, "(none)"); |
8a55caa8 JN |
77 | return; |
78 | } | |
8587ead7 | 79 | if (strchr(buf, '.')) |
f8254d32 JK |
80 | strbuf_addstr(out, buf); |
81 | else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.')) | |
82 | strbuf_addstr(out, he->h_name); | |
8a55caa8 | 83 | else |
f8254d32 | 84 | strbuf_addf(out, "%s.(none)", buf); |
8a55caa8 JN |
85 | } |
86 | ||
8587ead7 | 87 | static void copy_email(const struct passwd *pw, struct strbuf *email) |
6aa33f40 | 88 | { |
01754769 JH |
89 | /* |
90 | * Make up a fake email address | |
91 | * (name + '@' + hostname [+ '.' + domainname]) | |
92 | */ | |
8587ead7 JK |
93 | strbuf_addstr(email, pw->pw_name); |
94 | strbuf_addch(email, '@'); | |
95 | ||
96 | if (!add_mailname_host(email)) | |
8a55caa8 | 97 | return; /* read from "/etc/mailname" (Debian) */ |
8587ead7 | 98 | add_domainname(email); |
01754769 JH |
99 | } |
100 | ||
dad148c3 | 101 | static const char *ident_default_name(void) |
01754769 | 102 | { |
be641abd | 103 | if (!git_default_name.len) { |
2f705875 | 104 | copy_gecos(xgetpwuid_self(), &git_default_name); |
be641abd | 105 | strbuf_trim(&git_default_name); |
01754769 | 106 | } |
8587ead7 | 107 | return git_default_name.buf; |
bcb2b004 | 108 | } |
01754769 | 109 | |
bcb2b004 JK |
110 | const char *ident_default_email(void) |
111 | { | |
8587ead7 | 112 | if (!git_default_email.len) { |
46f74f00 MK |
113 | const char *email = getenv("EMAIL"); |
114 | ||
99178c83 | 115 | if (email && email[0]) { |
8587ead7 | 116 | strbuf_addstr(&git_default_email, email); |
d6991cee JK |
117 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
118 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; | |
2f705875 JK |
119 | } else |
120 | copy_email(xgetpwuid_self(), &git_default_email); | |
be641abd | 121 | strbuf_trim(&git_default_email); |
01754769 | 122 | } |
8587ead7 | 123 | return git_default_email.buf; |
6aa33f40 LT |
124 | } |
125 | ||
dad148c3 | 126 | static const char *ident_default_date(void) |
6aa33f40 | 127 | { |
01754769 JH |
128 | if (!git_default_date[0]) |
129 | datestamp(git_default_date, sizeof(git_default_date)); | |
bcb2b004 | 130 | return git_default_date; |
6aa33f40 LT |
131 | } |
132 | ||
133 | static int crud(unsigned char c) | |
134 | { | |
f64c81d4 AR |
135 | return c <= 32 || |
136 | c == '.' || | |
137 | c == ',' || | |
138 | c == ':' || | |
139 | c == ';' || | |
140 | c == '<' || | |
141 | c == '>' || | |
142 | c == '"' || | |
d404bf02 | 143 | c == '\\' || |
f64c81d4 | 144 | c == '\''; |
6aa33f40 LT |
145 | } |
146 | ||
147 | /* | |
148 | * Copy over a string to the destination, but avoid special | |
149 | * characters ('\n', '<' and '>') and remove crud at the end | |
150 | */ | |
c96f0c8d | 151 | static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src) |
6aa33f40 | 152 | { |
b0732115 | 153 | size_t i, len; |
6aa33f40 LT |
154 | unsigned char c; |
155 | ||
156 | /* Remove crud from the beginning.. */ | |
157 | while ((c = *src) != 0) { | |
158 | if (!crud(c)) | |
159 | break; | |
160 | src++; | |
161 | } | |
162 | ||
163 | /* Remove crud from the end.. */ | |
164 | len = strlen(src); | |
165 | while (len > 0) { | |
166 | c = src[len-1]; | |
167 | if (!crud(c)) | |
168 | break; | |
169 | --len; | |
170 | } | |
171 | ||
172 | /* | |
173 | * Copy the rest to the buffer, but avoid the special | |
82f9d58a | 174 | * characters '\n' '<' and '>' that act as delimiters on |
c96f0c8d JK |
175 | * an identification line. We can only remove crud, never add it, |
176 | * so 'len' is our maximum. | |
6aa33f40 | 177 | */ |
c96f0c8d | 178 | strbuf_grow(sb, len); |
6aa33f40 LT |
179 | for (i = 0; i < len; i++) { |
180 | c = *src++; | |
181 | switch (c) { | |
182 | case '\n': case '<': case '>': | |
183 | continue; | |
184 | } | |
c96f0c8d | 185 | sb->buf[sb->len++] = c; |
6aa33f40 | 186 | } |
c96f0c8d | 187 | sb->buf[sb->len] = '\0'; |
6aa33f40 LT |
188 | } |
189 | ||
4b340cfa JH |
190 | /* |
191 | * Reverse of fmt_ident(); given an ident line, split the fields | |
192 | * to allow the caller to parse it. | |
193 | * Signal a success by returning 0, but date/tz fields of the result | |
194 | * can still be NULL if the input line only has the name/email part | |
195 | * (e.g. reading from a reflog entry). | |
196 | */ | |
197 | int split_ident_line(struct ident_split *split, const char *line, int len) | |
198 | { | |
199 | const char *cp; | |
200 | size_t span; | |
201 | int status = -1; | |
202 | ||
203 | memset(split, 0, sizeof(*split)); | |
204 | ||
205 | split->name_begin = line; | |
206 | for (cp = line; *cp && cp < line + len; cp++) | |
207 | if (*cp == '<') { | |
208 | split->mail_begin = cp + 1; | |
209 | break; | |
210 | } | |
211 | if (!split->mail_begin) | |
212 | return status; | |
213 | ||
d9955fd6 | 214 | for (cp = split->mail_begin - 2; line <= cp; cp--) |
4b340cfa JH |
215 | if (!isspace(*cp)) { |
216 | split->name_end = cp + 1; | |
217 | break; | |
218 | } | |
e27ddb64 JH |
219 | if (!split->name_end) { |
220 | /* no human readable name */ | |
221 | split->name_end = split->name_begin; | |
222 | } | |
4b340cfa JH |
223 | |
224 | for (cp = split->mail_begin; cp < line + len; cp++) | |
225 | if (*cp == '>') { | |
226 | split->mail_end = cp; | |
227 | break; | |
228 | } | |
229 | if (!split->mail_end) | |
230 | return status; | |
231 | ||
232 | for (cp = split->mail_end + 1; cp < line + len && isspace(*cp); cp++) | |
233 | ; | |
234 | if (line + len <= cp) | |
235 | goto person_only; | |
236 | split->date_begin = cp; | |
237 | span = strspn(cp, "0123456789"); | |
238 | if (!span) | |
239 | goto person_only; | |
240 | split->date_end = split->date_begin + span; | |
241 | for (cp = split->date_end; cp < line + len && isspace(*cp); cp++) | |
242 | ; | |
243 | if (line + len <= cp || (*cp != '+' && *cp != '-')) | |
244 | goto person_only; | |
245 | split->tz_begin = cp; | |
246 | span = strspn(cp + 1, "0123456789"); | |
247 | if (!span) | |
248 | goto person_only; | |
249 | split->tz_end = split->tz_begin + 1 + span; | |
250 | return 0; | |
251 | ||
252 | person_only: | |
253 | split->date_begin = NULL; | |
254 | split->date_end = NULL; | |
255 | split->tz_begin = NULL; | |
256 | split->tz_end = NULL; | |
257 | return 0; | |
258 | } | |
259 | ||
749be728 | 260 | static const char *env_hint = |
d5cc2de9 | 261 | "\n" |
6c293d40 | 262 | "*** Please tell me who you are.\n" |
d5cc2de9 HWN |
263 | "\n" |
264 | "Run\n" | |
265 | "\n" | |
8e7425da | 266 | " git config --global user.email \"you@example.com\"\n" |
180787c4 | 267 | " git config --global user.name \"Your Name\"\n" |
d5cc2de9 | 268 | "\n" |
180787c4 SP |
269 | "to set your account\'s default identity.\n" |
270 | "Omit --global to set the identity only in this repository.\n" | |
d5cc2de9 | 271 | "\n"; |
749be728 | 272 | |
774751a8 JH |
273 | const char *fmt_ident(const char *name, const char *email, |
274 | const char *date_str, int flag) | |
6aa33f40 | 275 | { |
c96f0c8d | 276 | static struct strbuf ident = STRBUF_INIT; |
6aa33f40 | 277 | char date[50]; |
f9bc573f | 278 | int strict = (flag & IDENT_STRICT); |
359b27ad | 279 | int want_date = !(flag & IDENT_NO_DATE); |
c15e1987 | 280 | int want_name = !(flag & IDENT_NO_NAME); |
6aa33f40 | 281 | |
c15e1987 | 282 | if (want_name && !name) |
bcb2b004 JK |
283 | name = ident_default_name(); |
284 | if (!email) | |
285 | email = ident_default_email(); | |
dfdd309e | 286 | |
c15e1987 | 287 | if (want_name && !*name) { |
cb280e10 JH |
288 | struct passwd *pw; |
289 | ||
f9bc573f | 290 | if (strict) { |
8587ead7 | 291 | if (name == git_default_name.buf) |
b9f0ac17 | 292 | fputs(env_hint, stderr); |
b00f6cfc | 293 | die("empty ident name (for <%s>) not allowed", email); |
749be728 | 294 | } |
2f705875 | 295 | pw = xgetpwuid_self(); |
060d4bb3 | 296 | name = pw->pw_name; |
749be728 | 297 | } |
dfdd309e | 298 | |
8c5b1ae1 JK |
299 | if (strict && email == git_default_email.buf && |
300 | strstr(email, "(none)")) { | |
301 | fputs(env_hint, stderr); | |
302 | die("unable to auto-detect email address (got '%s')", email); | |
749be728 | 303 | } |
dfdd309e | 304 | |
359b27ad JK |
305 | if (want_date) { |
306 | if (date_str && date_str[0]) { | |
307 | if (parse_date(date_str, date, sizeof(date)) < 0) | |
308 | die("invalid date format: %s", date_str); | |
309 | } | |
310 | else | |
311 | strcpy(date, ident_default_date()); | |
4579bb41 | 312 | } |
6aa33f40 | 313 | |
c96f0c8d | 314 | strbuf_reset(&ident); |
c15e1987 JK |
315 | if (want_name) { |
316 | strbuf_addstr_without_crud(&ident, name); | |
317 | strbuf_addstr(&ident, " <"); | |
d9ccfe77 | 318 | } |
c96f0c8d | 319 | strbuf_addstr_without_crud(&ident, email); |
c15e1987 JK |
320 | if (want_name) |
321 | strbuf_addch(&ident, '>'); | |
359b27ad | 322 | if (want_date) { |
c96f0c8d JK |
323 | strbuf_addch(&ident, ' '); |
324 | strbuf_addstr_without_crud(&ident, date); | |
d9ccfe77 | 325 | } |
c96f0c8d | 326 | return ident.buf; |
6aa33f40 | 327 | } |
d289d136 | 328 | |
d9ccfe77 JH |
329 | const char *fmt_name(const char *name, const char *email) |
330 | { | |
f9bc573f | 331 | return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE); |
d9ccfe77 JH |
332 | } |
333 | ||
774751a8 | 334 | const char *git_author_info(int flag) |
d289d136 | 335 | { |
d6991cee JK |
336 | if (getenv("GIT_AUTHOR_NAME")) |
337 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; | |
338 | if (getenv("GIT_AUTHOR_EMAIL")) | |
339 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; | |
798123af | 340 | return fmt_ident(getenv("GIT_AUTHOR_NAME"), |
c7d77dab | 341 | getenv("GIT_AUTHOR_EMAIL"), |
749be728 | 342 | getenv("GIT_AUTHOR_DATE"), |
774751a8 | 343 | flag); |
d289d136 EB |
344 | } |
345 | ||
774751a8 | 346 | const char *git_committer_info(int flag) |
d289d136 | 347 | { |
91c38a21 | 348 | if (getenv("GIT_COMMITTER_NAME")) |
d6991cee | 349 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
91c38a21 | 350 | if (getenv("GIT_COMMITTER_EMAIL")) |
d6991cee | 351 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
798123af | 352 | return fmt_ident(getenv("GIT_COMMITTER_NAME"), |
c7d77dab | 353 | getenv("GIT_COMMITTER_EMAIL"), |
749be728 | 354 | getenv("GIT_COMMITTER_DATE"), |
774751a8 | 355 | flag); |
d289d136 | 356 | } |
5aeb3a3a | 357 | |
d6991cee | 358 | static int ident_is_sufficient(int user_ident_explicitly_given) |
5aeb3a3a JH |
359 | { |
360 | #ifndef WINDOWS | |
361 | return (user_ident_explicitly_given & IDENT_MAIL_GIVEN); | |
362 | #else | |
363 | return (user_ident_explicitly_given == IDENT_ALL_GIVEN); | |
364 | #endif | |
365 | } | |
9597921b | 366 | |
d6991cee JK |
367 | int committer_ident_sufficiently_given(void) |
368 | { | |
369 | return ident_is_sufficient(committer_ident_explicitly_given); | |
370 | } | |
371 | ||
372 | int author_ident_sufficiently_given(void) | |
373 | { | |
374 | return ident_is_sufficient(author_ident_explicitly_given); | |
375 | } | |
376 | ||
9597921b JK |
377 | int git_ident_config(const char *var, const char *value, void *data) |
378 | { | |
379 | if (!strcmp(var, "user.name")) { | |
380 | if (!value) | |
381 | return config_error_nonbool(var); | |
8587ead7 JK |
382 | strbuf_reset(&git_default_name); |
383 | strbuf_addstr(&git_default_name, value); | |
d6991cee JK |
384 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
385 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; | |
9597921b JK |
386 | return 0; |
387 | } | |
388 | ||
389 | if (!strcmp(var, "user.email")) { | |
390 | if (!value) | |
391 | return config_error_nonbool(var); | |
8587ead7 JK |
392 | strbuf_reset(&git_default_email); |
393 | strbuf_addstr(&git_default_email, value); | |
d6991cee JK |
394 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
395 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; | |
9597921b JK |
396 | return 0; |
397 | } | |
398 | ||
399 | return 0; | |
400 | } |