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