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