]>
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 | |
b0732115 | 12 | static void copy_gecos(const struct passwd *w, char *name, size_t sz) |
e9bacb4f JH |
13 | { |
14 | char *src, *dst; | |
b0732115 | 15 | size_t len, nlen; |
e9bacb4f JH |
16 | |
17 | nlen = strlen(w->pw_name); | |
18 | ||
19 | /* Traditionally GECOS field had office phone numbers etc, separated | |
20 | * with commas. Also & stands for capitalized form of the login name. | |
21 | */ | |
22 | ||
23 | for (len = 0, dst = name, src = w->pw_gecos; len < sz; src++) { | |
24 | int ch = *src; | |
25 | if (ch != '&') { | |
26 | *dst++ = ch; | |
27 | if (ch == 0 || ch == ',') | |
28 | break; | |
29 | len++; | |
30 | continue; | |
31 | } | |
32 | if (len + nlen < sz) { | |
33 | /* Sorry, Mr. McDonald... */ | |
34 | *dst++ = toupper(*w->pw_name); | |
35 | memcpy(dst, w->pw_name + 1, nlen - 1); | |
36 | dst += nlen - 1; | |
37 | } | |
38 | } | |
39 | if (len < sz) | |
40 | name[len] = 0; | |
41 | else | |
42 | die("Your parents must have hated you!"); | |
43 | ||
44 | } | |
45 | ||
0b952a98 | 46 | static void copy_email(const struct passwd *pw) |
6aa33f40 | 47 | { |
01754769 JH |
48 | /* |
49 | * Make up a fake email address | |
50 | * (name + '@' + hostname [+ '.' + domainname]) | |
51 | */ | |
b0732115 | 52 | size_t len = strlen(pw->pw_name); |
e1b10391 | 53 | if (len > sizeof(git_default_email)/2) |
7a868a84 | 54 | die("Your sysadmin must hate you!"); |
e1b10391 LT |
55 | memcpy(git_default_email, pw->pw_name, len); |
56 | git_default_email[len++] = '@'; | |
57 | gethostname(git_default_email + len, sizeof(git_default_email) - len); | |
58 | if (!strchr(git_default_email+len, '.')) { | |
adc3dbca PB |
59 | struct hostent *he = gethostbyname(git_default_email + len); |
60 | char *domainname; | |
61 | ||
e1b10391 LT |
62 | len = strlen(git_default_email); |
63 | git_default_email[len++] = '.'; | |
adc3dbca | 64 | if (he && (domainname = strchr(he->h_name, '.'))) |
01754769 JH |
65 | strlcpy(git_default_email + len, domainname + 1, |
66 | sizeof(git_default_email) - len); | |
adc3dbca | 67 | else |
01754769 JH |
68 | strlcpy(git_default_email + len, "(none)", |
69 | sizeof(git_default_email) - len); | |
6aa33f40 | 70 | } |
01754769 JH |
71 | } |
72 | ||
73 | static void setup_ident(void) | |
74 | { | |
75 | struct passwd *pw = NULL; | |
76 | ||
77 | /* Get the name ("gecos") */ | |
78 | if (!git_default_name[0]) { | |
79 | pw = getpwuid(getuid()); | |
80 | if (!pw) | |
81 | die("You don't exist. Go away!"); | |
82 | copy_gecos(pw, git_default_name, sizeof(git_default_name)); | |
83 | } | |
84 | ||
85 | if (!git_default_email[0]) { | |
46f74f00 MK |
86 | const char *email = getenv("EMAIL"); |
87 | ||
99178c83 | 88 | if (email && email[0]) { |
46f74f00 MK |
89 | strlcpy(git_default_email, email, |
90 | sizeof(git_default_email)); | |
99178c83 JH |
91 | user_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
92 | } else { | |
46f74f00 MK |
93 | if (!pw) |
94 | pw = getpwuid(getuid()); | |
95 | if (!pw) | |
96 | die("You don't exist. Go away!"); | |
97 | copy_email(pw); | |
98 | } | |
01754769 JH |
99 | } |
100 | ||
6aa33f40 | 101 | /* And set the default date */ |
01754769 JH |
102 | if (!git_default_date[0]) |
103 | datestamp(git_default_date, sizeof(git_default_date)); | |
6aa33f40 LT |
104 | } |
105 | ||
b0732115 | 106 | static int add_raw(char *buf, size_t size, int offset, const char *str) |
6aa33f40 | 107 | { |
b0732115 | 108 | size_t len = strlen(str); |
6aa33f40 LT |
109 | if (offset + len > size) |
110 | return size; | |
111 | memcpy(buf + offset, str, len); | |
112 | return offset + len; | |
113 | } | |
114 | ||
115 | static int crud(unsigned char c) | |
116 | { | |
f64c81d4 AR |
117 | return c <= 32 || |
118 | c == '.' || | |
119 | c == ',' || | |
120 | c == ':' || | |
121 | c == ';' || | |
122 | c == '<' || | |
123 | c == '>' || | |
124 | c == '"' || | |
d404bf02 | 125 | c == '\\' || |
f64c81d4 | 126 | c == '\''; |
6aa33f40 LT |
127 | } |
128 | ||
129 | /* | |
130 | * Copy over a string to the destination, but avoid special | |
131 | * characters ('\n', '<' and '>') and remove crud at the end | |
132 | */ | |
b0732115 | 133 | static int copy(char *buf, size_t size, int offset, const char *src) |
6aa33f40 | 134 | { |
b0732115 | 135 | size_t i, len; |
6aa33f40 LT |
136 | unsigned char c; |
137 | ||
138 | /* Remove crud from the beginning.. */ | |
139 | while ((c = *src) != 0) { | |
140 | if (!crud(c)) | |
141 | break; | |
142 | src++; | |
143 | } | |
144 | ||
145 | /* Remove crud from the end.. */ | |
146 | len = strlen(src); | |
147 | while (len > 0) { | |
148 | c = src[len-1]; | |
149 | if (!crud(c)) | |
150 | break; | |
151 | --len; | |
152 | } | |
153 | ||
154 | /* | |
155 | * Copy the rest to the buffer, but avoid the special | |
82f9d58a | 156 | * characters '\n' '<' and '>' that act as delimiters on |
790296fd | 157 | * an identification line |
6aa33f40 LT |
158 | */ |
159 | for (i = 0; i < len; i++) { | |
160 | c = *src++; | |
161 | switch (c) { | |
162 | case '\n': case '<': case '>': | |
163 | continue; | |
164 | } | |
165 | if (offset >= size) | |
166 | return size; | |
167 | buf[offset++] = c; | |
168 | } | |
169 | return offset; | |
170 | } | |
171 | ||
749be728 | 172 | static const char *env_hint = |
d5cc2de9 | 173 | "\n" |
6c293d40 | 174 | "*** Please tell me who you are.\n" |
d5cc2de9 HWN |
175 | "\n" |
176 | "Run\n" | |
177 | "\n" | |
8e7425da | 178 | " git config --global user.email \"you@example.com\"\n" |
180787c4 | 179 | " git config --global user.name \"Your Name\"\n" |
d5cc2de9 | 180 | "\n" |
180787c4 SP |
181 | "to set your account\'s default identity.\n" |
182 | "Omit --global to set the identity only in this repository.\n" | |
d5cc2de9 | 183 | "\n"; |
749be728 | 184 | |
774751a8 JH |
185 | const char *fmt_ident(const char *name, const char *email, |
186 | const char *date_str, int flag) | |
6aa33f40 LT |
187 | { |
188 | static char buffer[1000]; | |
189 | char date[50]; | |
190 | int i; | |
774751a8 JH |
191 | int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME); |
192 | int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME); | |
193 | int name_addr_only = (flag & IDENT_NO_DATE); | |
6aa33f40 | 194 | |
01754769 | 195 | setup_ident(); |
6aa33f40 | 196 | if (!name) |
e1b10391 | 197 | name = git_default_name; |
6aa33f40 | 198 | if (!email) |
e1b10391 | 199 | email = git_default_email; |
dfdd309e | 200 | |
749be728 | 201 | if (!*name) { |
cb280e10 JH |
202 | struct passwd *pw; |
203 | ||
774751a8 | 204 | if ((warn_on_no_name || error_on_no_name) && |
cb280e10 | 205 | name == git_default_name && env_hint) { |
8b770a2a | 206 | fputs(env_hint, stderr); |
c27b3925 | 207 | env_hint = NULL; /* warn only once */ |
749be728 | 208 | } |
774751a8 | 209 | if (error_on_no_name) |
749be728 | 210 | die("empty ident %s <%s> not allowed", name, email); |
cb280e10 JH |
211 | pw = getpwuid(getuid()); |
212 | if (!pw) | |
213 | die("You don't exist. Go away!"); | |
214 | strlcpy(git_default_name, pw->pw_name, | |
215 | sizeof(git_default_name)); | |
216 | name = git_default_name; | |
749be728 | 217 | } |
dfdd309e | 218 | |
e1b10391 | 219 | strcpy(date, git_default_date); |
d9ccfe77 | 220 | if (!name_addr_only && date_str) |
6aa33f40 LT |
221 | parse_date(date_str, date, sizeof(date)); |
222 | ||
223 | i = copy(buffer, sizeof(buffer), 0, name); | |
224 | i = add_raw(buffer, sizeof(buffer), i, " <"); | |
225 | i = copy(buffer, sizeof(buffer), i, email); | |
d9ccfe77 JH |
226 | if (!name_addr_only) { |
227 | i = add_raw(buffer, sizeof(buffer), i, "> "); | |
228 | i = copy(buffer, sizeof(buffer), i, date); | |
229 | } else { | |
230 | i = add_raw(buffer, sizeof(buffer), i, ">"); | |
231 | } | |
6aa33f40 LT |
232 | if (i >= sizeof(buffer)) |
233 | die("Impossibly long personal identifier"); | |
234 | buffer[i] = 0; | |
235 | return buffer; | |
236 | } | |
d289d136 | 237 | |
d9ccfe77 JH |
238 | const char *fmt_name(const char *name, const char *email) |
239 | { | |
774751a8 | 240 | return fmt_ident(name, email, NULL, IDENT_ERROR_ON_NO_NAME | IDENT_NO_DATE); |
d9ccfe77 JH |
241 | } |
242 | ||
774751a8 | 243 | const char *git_author_info(int flag) |
d289d136 | 244 | { |
798123af | 245 | return fmt_ident(getenv("GIT_AUTHOR_NAME"), |
c7d77dab | 246 | getenv("GIT_AUTHOR_EMAIL"), |
749be728 | 247 | getenv("GIT_AUTHOR_DATE"), |
774751a8 | 248 | flag); |
d289d136 EB |
249 | } |
250 | ||
774751a8 | 251 | const char *git_committer_info(int flag) |
d289d136 | 252 | { |
91c38a21 JH |
253 | if (getenv("GIT_COMMITTER_NAME")) |
254 | user_ident_explicitly_given |= IDENT_NAME_GIVEN; | |
255 | if (getenv("GIT_COMMITTER_EMAIL")) | |
256 | user_ident_explicitly_given |= IDENT_MAIL_GIVEN; | |
798123af | 257 | return fmt_ident(getenv("GIT_COMMITTER_NAME"), |
c7d77dab | 258 | getenv("GIT_COMMITTER_EMAIL"), |
749be728 | 259 | getenv("GIT_COMMITTER_DATE"), |
774751a8 | 260 | flag); |
d289d136 | 261 | } |
5aeb3a3a JH |
262 | |
263 | int user_ident_sufficiently_given(void) | |
264 | { | |
265 | #ifndef WINDOWS | |
266 | return (user_ident_explicitly_given & IDENT_MAIL_GIVEN); | |
267 | #else | |
268 | return (user_ident_explicitly_given == IDENT_ALL_GIVEN); | |
269 | #endif | |
270 | } |