]>
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 | ||
e1b10391 | 10 | static char git_default_date[50]; |
6aa33f40 | 11 | |
590e081d RG |
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 | ||
b0732115 | 18 | static void copy_gecos(const struct passwd *w, char *name, size_t sz) |
e9bacb4f JH |
19 | { |
20 | char *src, *dst; | |
b0732115 | 21 | size_t len, nlen; |
e9bacb4f JH |
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 | ||
590e081d | 29 | for (len = 0, dst = name, src = get_gecos(w); len < sz; src++) { |
e9bacb4f JH |
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; | |
c0336ff2 | 43 | len += nlen; |
e9bacb4f JH |
44 | } |
45 | } | |
46 | if (len < sz) | |
47 | name[len] = 0; | |
48 | else | |
49 | die("Your parents must have hated you!"); | |
50 | ||
51 | } | |
52 | ||
8a55caa8 JN |
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 | ||
0b952a98 | 101 | static void copy_email(const struct passwd *pw) |
6aa33f40 | 102 | { |
01754769 JH |
103 | /* |
104 | * Make up a fake email address | |
105 | * (name + '@' + hostname [+ '.' + domainname]) | |
106 | */ | |
b0732115 | 107 | size_t len = strlen(pw->pw_name); |
e1b10391 | 108 | if (len > sizeof(git_default_email)/2) |
7a868a84 | 109 | die("Your sysadmin must hate you!"); |
e1b10391 LT |
110 | memcpy(git_default_email, pw->pw_name, len); |
111 | git_default_email[len++] = '@'; | |
8a55caa8 JN |
112 | |
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); | |
01754769 JH |
118 | } |
119 | ||
d855e4d3 | 120 | static void setup_ident(const char **name, const char **emailp) |
01754769 JH |
121 | { |
122 | struct passwd *pw = NULL; | |
123 | ||
124 | /* Get the name ("gecos") */ | |
d855e4d3 | 125 | if (!*name && !git_default_name[0]) { |
01754769 JH |
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 | } | |
d855e4d3 JN |
131 | if (!*name) |
132 | *name = git_default_name; | |
01754769 | 133 | |
d855e4d3 | 134 | if (!*emailp && !git_default_email[0]) { |
46f74f00 MK |
135 | const char *email = getenv("EMAIL"); |
136 | ||
99178c83 | 137 | if (email && email[0]) { |
46f74f00 MK |
138 | strlcpy(git_default_email, email, |
139 | sizeof(git_default_email)); | |
99178c83 JH |
140 | user_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
141 | } else { | |
46f74f00 MK |
142 | if (!pw) |
143 | pw = getpwuid(getuid()); | |
144 | if (!pw) | |
145 | die("You don't exist. Go away!"); | |
146 | copy_email(pw); | |
147 | } | |
01754769 | 148 | } |
d855e4d3 JN |
149 | if (!*emailp) |
150 | *emailp = git_default_email; | |
01754769 | 151 | |
6aa33f40 | 152 | /* And set the default date */ |
01754769 JH |
153 | if (!git_default_date[0]) |
154 | datestamp(git_default_date, sizeof(git_default_date)); | |
6aa33f40 LT |
155 | } |
156 | ||
b0732115 | 157 | static int add_raw(char *buf, size_t size, int offset, const char *str) |
6aa33f40 | 158 | { |
b0732115 | 159 | size_t len = strlen(str); |
6aa33f40 LT |
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 | { | |
f64c81d4 AR |
168 | return c <= 32 || |
169 | c == '.' || | |
170 | c == ',' || | |
171 | c == ':' || | |
172 | c == ';' || | |
173 | c == '<' || | |
174 | c == '>' || | |
175 | c == '"' || | |
d404bf02 | 176 | c == '\\' || |
f64c81d4 | 177 | c == '\''; |
6aa33f40 LT |
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 | */ | |
b0732115 | 184 | static int copy(char *buf, size_t size, int offset, const char *src) |
6aa33f40 | 185 | { |
b0732115 | 186 | size_t i, len; |
6aa33f40 LT |
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 | |
82f9d58a | 207 | * characters '\n' '<' and '>' that act as delimiters on |
790296fd | 208 | * an identification line |
6aa33f40 LT |
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 | ||
4b340cfa JH |
223 | /* |
224 | * Reverse of fmt_ident(); given an ident line, split the fields | |
225 | * to allow the caller to parse it. | |
226 | * Signal a success by returning 0, but date/tz fields of the result | |
227 | * can still be NULL if the input line only has the name/email part | |
228 | * (e.g. reading from a reflog entry). | |
229 | */ | |
230 | int split_ident_line(struct ident_split *split, const char *line, int len) | |
231 | { | |
232 | const char *cp; | |
233 | size_t span; | |
234 | int status = -1; | |
235 | ||
236 | memset(split, 0, sizeof(*split)); | |
237 | ||
238 | split->name_begin = line; | |
239 | for (cp = line; *cp && cp < line + len; cp++) | |
240 | if (*cp == '<') { | |
241 | split->mail_begin = cp + 1; | |
242 | break; | |
243 | } | |
244 | if (!split->mail_begin) | |
245 | return status; | |
246 | ||
247 | for (cp = split->mail_begin - 2; line < cp; cp--) | |
248 | if (!isspace(*cp)) { | |
249 | split->name_end = cp + 1; | |
250 | break; | |
251 | } | |
252 | if (!split->name_end) | |
253 | return status; | |
254 | ||
255 | for (cp = split->mail_begin; cp < line + len; cp++) | |
256 | if (*cp == '>') { | |
257 | split->mail_end = cp; | |
258 | break; | |
259 | } | |
260 | if (!split->mail_end) | |
261 | return status; | |
262 | ||
263 | for (cp = split->mail_end + 1; cp < line + len && isspace(*cp); cp++) | |
264 | ; | |
265 | if (line + len <= cp) | |
266 | goto person_only; | |
267 | split->date_begin = cp; | |
268 | span = strspn(cp, "0123456789"); | |
269 | if (!span) | |
270 | goto person_only; | |
271 | split->date_end = split->date_begin + span; | |
272 | for (cp = split->date_end; cp < line + len && isspace(*cp); cp++) | |
273 | ; | |
274 | if (line + len <= cp || (*cp != '+' && *cp != '-')) | |
275 | goto person_only; | |
276 | split->tz_begin = cp; | |
277 | span = strspn(cp + 1, "0123456789"); | |
278 | if (!span) | |
279 | goto person_only; | |
280 | split->tz_end = split->tz_begin + 1 + span; | |
281 | return 0; | |
282 | ||
283 | person_only: | |
284 | split->date_begin = NULL; | |
285 | split->date_end = NULL; | |
286 | split->tz_begin = NULL; | |
287 | split->tz_end = NULL; | |
288 | return 0; | |
289 | } | |
290 | ||
749be728 | 291 | static const char *env_hint = |
d5cc2de9 | 292 | "\n" |
6c293d40 | 293 | "*** Please tell me who you are.\n" |
d5cc2de9 HWN |
294 | "\n" |
295 | "Run\n" | |
296 | "\n" | |
8e7425da | 297 | " git config --global user.email \"you@example.com\"\n" |
180787c4 | 298 | " git config --global user.name \"Your Name\"\n" |
d5cc2de9 | 299 | "\n" |
180787c4 SP |
300 | "to set your account\'s default identity.\n" |
301 | "Omit --global to set the identity only in this repository.\n" | |
d5cc2de9 | 302 | "\n"; |
749be728 | 303 | |
774751a8 JH |
304 | const char *fmt_ident(const char *name, const char *email, |
305 | const char *date_str, int flag) | |
6aa33f40 LT |
306 | { |
307 | static char buffer[1000]; | |
308 | char date[50]; | |
309 | int i; | |
774751a8 JH |
310 | int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME); |
311 | int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME); | |
312 | int name_addr_only = (flag & IDENT_NO_DATE); | |
6aa33f40 | 313 | |
d855e4d3 | 314 | setup_ident(&name, &email); |
dfdd309e | 315 | |
749be728 | 316 | if (!*name) { |
cb280e10 JH |
317 | struct passwd *pw; |
318 | ||
774751a8 | 319 | if ((warn_on_no_name || error_on_no_name) && |
cb280e10 | 320 | name == git_default_name && env_hint) { |
8b770a2a | 321 | fputs(env_hint, stderr); |
c27b3925 | 322 | env_hint = NULL; /* warn only once */ |
749be728 | 323 | } |
774751a8 | 324 | if (error_on_no_name) |
749be728 | 325 | die("empty ident %s <%s> not allowed", name, email); |
cb280e10 JH |
326 | pw = getpwuid(getuid()); |
327 | if (!pw) | |
328 | die("You don't exist. Go away!"); | |
329 | strlcpy(git_default_name, pw->pw_name, | |
330 | sizeof(git_default_name)); | |
331 | name = git_default_name; | |
749be728 | 332 | } |
dfdd309e | 333 | |
e1b10391 | 334 | strcpy(date, git_default_date); |
4579bb41 JK |
335 | if (!name_addr_only && date_str && date_str[0]) { |
336 | if (parse_date(date_str, date, sizeof(date)) < 0) | |
337 | die("invalid date format: %s", date_str); | |
338 | } | |
6aa33f40 LT |
339 | |
340 | i = copy(buffer, sizeof(buffer), 0, name); | |
341 | i = add_raw(buffer, sizeof(buffer), i, " <"); | |
342 | i = copy(buffer, sizeof(buffer), i, email); | |
d9ccfe77 JH |
343 | if (!name_addr_only) { |
344 | i = add_raw(buffer, sizeof(buffer), i, "> "); | |
345 | i = copy(buffer, sizeof(buffer), i, date); | |
346 | } else { | |
347 | i = add_raw(buffer, sizeof(buffer), i, ">"); | |
348 | } | |
6aa33f40 LT |
349 | if (i >= sizeof(buffer)) |
350 | die("Impossibly long personal identifier"); | |
351 | buffer[i] = 0; | |
352 | return buffer; | |
353 | } | |
d289d136 | 354 | |
d9ccfe77 JH |
355 | const char *fmt_name(const char *name, const char *email) |
356 | { | |
774751a8 | 357 | return fmt_ident(name, email, NULL, IDENT_ERROR_ON_NO_NAME | IDENT_NO_DATE); |
d9ccfe77 JH |
358 | } |
359 | ||
774751a8 | 360 | const char *git_author_info(int flag) |
d289d136 | 361 | { |
798123af | 362 | return fmt_ident(getenv("GIT_AUTHOR_NAME"), |
c7d77dab | 363 | getenv("GIT_AUTHOR_EMAIL"), |
749be728 | 364 | getenv("GIT_AUTHOR_DATE"), |
774751a8 | 365 | flag); |
d289d136 EB |
366 | } |
367 | ||
774751a8 | 368 | const char *git_committer_info(int flag) |
d289d136 | 369 | { |
91c38a21 JH |
370 | if (getenv("GIT_COMMITTER_NAME")) |
371 | user_ident_explicitly_given |= IDENT_NAME_GIVEN; | |
372 | if (getenv("GIT_COMMITTER_EMAIL")) | |
373 | user_ident_explicitly_given |= IDENT_MAIL_GIVEN; | |
798123af | 374 | return fmt_ident(getenv("GIT_COMMITTER_NAME"), |
c7d77dab | 375 | getenv("GIT_COMMITTER_EMAIL"), |
749be728 | 376 | getenv("GIT_COMMITTER_DATE"), |
774751a8 | 377 | flag); |
d289d136 | 378 | } |
5aeb3a3a JH |
379 | |
380 | int user_ident_sufficiently_given(void) | |
381 | { | |
382 | #ifndef WINDOWS | |
383 | return (user_ident_explicitly_given & IDENT_MAIL_GIVEN); | |
384 | #else | |
385 | return (user_ident_explicitly_given == IDENT_ALL_GIVEN); | |
386 | #endif | |
387 | } |