Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 274 lines (241 sloc) 6.38 kb
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
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
e1b1039 Use git config file for committer name and email info
Linus Torvalds authored
10 static char git_default_date[50];
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
11
b073211 ident.c: Use size_t (instead of int) to store sizes
Luiz Fernando N. Capitulino authored
12 static void copy_gecos(const struct passwd *w, char *name, size_t sz)
e9bacb4 Use GECOS field a bit better to produce default human readable name.
Junio C Hamano authored
13 {
14 char *src, *dst;
b073211 ident.c: Use size_t (instead of int) to store sizes
Luiz Fernando N. Capitulino authored
15 size_t len, nlen;
e9bacb4 Use GECOS field a bit better to produce default human readable name.
Junio C Hamano authored
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
0b952a9 ident.c: Use const qualifier for 'struct passwd' parameters
Luiz Fernando N. Capitulino authored
46 static void copy_email(const struct passwd *pw)
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
47 {
0175476 Don't force everybody to call setup_ident().
Junio C Hamano authored
48 /*
49 * Make up a fake email address
50 * (name + '@' + hostname [+ '.' + domainname])
51 */
b073211 ident.c: Use size_t (instead of int) to store sizes
Luiz Fernando N. Capitulino authored
52 size_t len = strlen(pw->pw_name);
e1b1039 Use git config file for committer name and email info
Linus Torvalds authored
53 if (len > sizeof(git_default_email)/2)
7a868a8 @ebiederm [PATCH] ident.c: Disambiguate the error messages in setup_ident
ebiederm authored
54 die("Your sysadmin must hate you!");
e1b1039 Use git config file for committer name and email info
Linus Torvalds authored
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, '.')) {
adc3dbc Use sensible domain name (the DNS one) when guessing ident information
Petr Baudis authored
59 struct hostent *he = gethostbyname(git_default_email + len);
60 char *domainname;
61
e1b1039 Use git config file for committer name and email info
Linus Torvalds authored
62 len = strlen(git_default_email);
63 git_default_email[len++] = '.';
adc3dbc Use sensible domain name (the DNS one) when guessing ident information
Petr Baudis authored
64 if (he && (domainname = strchr(he->h_name, '.')))
0175476 Don't force everybody to call setup_ident().
Junio C Hamano authored
65 strlcpy(git_default_email + len, domainname + 1,
66 sizeof(git_default_email) - len);
adc3dbc Use sensible domain name (the DNS one) when guessing ident information
Petr Baudis authored
67 else
0175476 Don't force everybody to call setup_ident().
Junio C Hamano authored
68 strlcpy(git_default_email + len, "(none)",
69 sizeof(git_default_email) - len);
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
70 }
0175476 Don't force everybody to call setup_ident().
Junio C Hamano authored
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]) {
46f74f0 @kraai Prefer EMAIL to username@hostname.
kraai authored
86 const char *email = getenv("EMAIL");
87
88 if (email && email[0])
89 strlcpy(git_default_email, email,
90 sizeof(git_default_email));
91 else {
92 if (!pw)
93 pw = getpwuid(getuid());
94 if (!pw)
95 die("You don't exist. Go away!");
96 copy_email(pw);
97 }
0175476 Don't force everybody to call setup_ident().
Junio C Hamano authored
98 }
99
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
100 /* And set the default date */
0175476 Don't force everybody to call setup_ident().
Junio C Hamano authored
101 if (!git_default_date[0])
102 datestamp(git_default_date, sizeof(git_default_date));
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
103 }
104
b073211 ident.c: Use size_t (instead of int) to store sizes
Luiz Fernando N. Capitulino authored
105 static int add_raw(char *buf, size_t size, int offset, const char *str)
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
106 {
b073211 ident.c: Use size_t (instead of int) to store sizes
Luiz Fernando N. Capitulino authored
107 size_t len = strlen(str);
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
108 if (offset + len > size)
109 return size;
110 memcpy(buf + offset, str, len);
111 return offset + len;
112 }
113
114 static int crud(unsigned char c)
115 {
fb2af03 Replace C99 array initializers with code.
Jason Riedy authored
116 static char crud_array[256];
117 static int crud_array_initialized = 0;
118
119 if (!crud_array_initialized) {
120 int k;
121
122 for (k = 0; k <= 31; ++k) crud_array[k] = 1;
123 crud_array[' '] = 1;
124 crud_array['.'] = 1;
125 crud_array[','] = 1;
126 crud_array[':'] = 1;
127 crud_array[';'] = 1;
128 crud_array['<'] = 1;
129 crud_array['>'] = 1;
130 crud_array['"'] = 1;
131 crud_array['\''] = 1;
132 crud_array_initialized = 1;
133 }
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
134 return crud_array[c];
135 }
136
137 /*
138 * Copy over a string to the destination, but avoid special
139 * characters ('\n', '<' and '>') and remove crud at the end
140 */
b073211 ident.c: Use size_t (instead of int) to store sizes
Luiz Fernando N. Capitulino authored
141 static int copy(char *buf, size_t size, int offset, const char *src)
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
142 {
b073211 ident.c: Use size_t (instead of int) to store sizes
Luiz Fernando N. Capitulino authored
143 size_t i, len;
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
144 unsigned char c;
145
146 /* Remove crud from the beginning.. */
147 while ((c = *src) != 0) {
148 if (!crud(c))
149 break;
150 src++;
151 }
152
153 /* Remove crud from the end.. */
154 len = strlen(src);
155 while (len > 0) {
156 c = src[len-1];
157 if (!crud(c))
158 break;
159 --len;
160 }
161
162 /*
163 * Copy the rest to the buffer, but avoid the special
82f9d58 code comments: spell
Junio C Hamano authored
164 * characters '\n' '<' and '>' that act as delimiters on
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
165 * a identification line
166 */
167 for (i = 0; i < len; i++) {
168 c = *src++;
169 switch (c) {
170 case '\n': case '<': case '>':
171 continue;
172 }
173 if (offset >= size)
174 return size;
175 buf[offset++] = c;
176 }
177 return offset;
178 }
179
749be72 Delay "empty ident" errors until they really matter.
Junio C Hamano authored
180 static const char au_env[] = "GIT_AUTHOR_NAME";
181 static const char co_env[] = "GIT_COMMITTER_NAME";
182 static const char *env_hint =
d5cc2de @hanwen ident.c: Trim hint printed when gecos is empty.
hanwen authored
183 "\n"
749be72 Delay "empty ident" errors until they really matter.
Junio C Hamano authored
184 "*** Your name cannot be determined from your system services (gecos).\n"
d5cc2de @hanwen ident.c: Trim hint printed when gecos is empty.
hanwen authored
185 "\n"
186 "Run\n"
187 "\n"
180787c @sprohaska Improved hint on how to set identity
sprohaska authored
188 " git config --global user.email \"you@email.com\"\n"
189 " git config --global user.name \"Your Name\"\n"
d5cc2de @hanwen ident.c: Trim hint printed when gecos is empty.
hanwen authored
190 "\n"
180787c @sprohaska Improved hint on how to set identity
sprohaska authored
191 "to set your account\'s default identity.\n"
192 "Omit --global to set the identity only in this repository.\n"
d5cc2de @hanwen ident.c: Trim hint printed when gecos is empty.
hanwen authored
193 "\n";
749be72 Delay "empty ident" errors until they really matter.
Junio C Hamano authored
194
d9ccfe7 @gitster Fix --signoff in builtin-commit differently.
gitster authored
195 static const char *fmt_ident_1(const char *name, const char *email,
196 const char *date_str, int flag)
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
197 {
198 static char buffer[1000];
199 char date[50];
200 int i;
d9ccfe7 @gitster Fix --signoff in builtin-commit differently.
gitster authored
201 int error_on_no_name = !!(flag & 01);
202 int name_addr_only = !!(flag & 02);
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
203
0175476 Don't force everybody to call setup_ident().
Junio C Hamano authored
204 setup_ident();
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
205 if (!name)
e1b1039 Use git config file for committer name and email info
Linus Torvalds authored
206 name = git_default_name;
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
207 if (!email)
e1b1039 Use git config file for committer name and email info
Linus Torvalds authored
208 email = git_default_email;
dfdd309 Do not allow empty name or email.
Junio C Hamano authored
209
749be72 Delay "empty ident" errors until they really matter.
Junio C Hamano authored
210 if (!*name) {
cb280e1 Allow non-developer to clone, checkout and fetch more easily.
Junio C Hamano authored
211 struct passwd *pw;
212
213 if (0 <= error_on_no_name &&
214 name == git_default_name && env_hint) {
749be72 Delay "empty ident" errors until they really matter.
Junio C Hamano authored
215 fprintf(stderr, env_hint, au_env, co_env);
216 env_hint = NULL; /* warn only once, for "git-var -l" */
217 }
cb280e1 Allow non-developer to clone, checkout and fetch more easily.
Junio C Hamano authored
218 if (0 < error_on_no_name)
749be72 Delay "empty ident" errors until they really matter.
Junio C Hamano authored
219 die("empty ident %s <%s> not allowed", name, email);
cb280e1 Allow non-developer to clone, checkout and fetch more easily.
Junio C Hamano authored
220 pw = getpwuid(getuid());
221 if (!pw)
222 die("You don't exist. Go away!");
223 strlcpy(git_default_name, pw->pw_name,
224 sizeof(git_default_name));
225 name = git_default_name;
749be72 Delay "empty ident" errors until they really matter.
Junio C Hamano authored
226 }
dfdd309 Do not allow empty name or email.
Junio C Hamano authored
227
e1b1039 Use git config file for committer name and email info
Linus Torvalds authored
228 strcpy(date, git_default_date);
d9ccfe7 @gitster Fix --signoff in builtin-commit differently.
gitster authored
229 if (!name_addr_only && date_str)
230 parse_date(date_str, date, sizeof(date));
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
231
232 i = copy(buffer, sizeof(buffer), 0, name);
233 i = add_raw(buffer, sizeof(buffer), i, " <");
234 i = copy(buffer, sizeof(buffer), i, email);
d9ccfe7 @gitster Fix --signoff in builtin-commit differently.
gitster authored
235 if (!name_addr_only) {
236 i = add_raw(buffer, sizeof(buffer), i, "> ");
237 i = copy(buffer, sizeof(buffer), i, date);
238 } else {
239 i = add_raw(buffer, sizeof(buffer), i, ">");
240 }
6aa33f4 Abstract out the "name <email> date" handling of commit-tree.c
Linus Torvalds authored
241 if (i >= sizeof(buffer))
242 die("Impossibly long personal identifier");
243 buffer[i] = 0;
244 return buffer;
245 }
d289d13 @ebiederm [PATCH] Move git_author_info and git_commiter_info to ident.c
ebiederm authored
246
d9ccfe7 @gitster Fix --signoff in builtin-commit differently.
gitster authored
247 const char *fmt_ident(const char *name, const char *email,
248 const char *date_str, int error_on_no_name)
249 {
250 int flag = (error_on_no_name ? 01 : 0);
251 return fmt_ident_1(name, email, date_str, flag);
252 }
253
254 const char *fmt_name(const char *name, const char *email)
255 {
256 return fmt_ident_1(name, email, NULL, 03);
257 }
258
749be72 Delay "empty ident" errors until they really matter.
Junio C Hamano authored
259 const char *git_author_info(int error_on_no_name)
d289d13 @ebiederm [PATCH] Move git_author_info and git_commiter_info to ident.c
ebiederm authored
260 {
798123a Rename get_ident() to fmt_ident() and make it available to outside
Junio C Hamano authored
261 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
c7d77da git-var: constness and globalness cleanup.
Junio C Hamano authored
262 getenv("GIT_AUTHOR_EMAIL"),
749be72 Delay "empty ident" errors until they really matter.
Junio C Hamano authored
263 getenv("GIT_AUTHOR_DATE"),
264 error_on_no_name);
d289d13 @ebiederm [PATCH] Move git_author_info and git_commiter_info to ident.c
ebiederm authored
265 }
266
749be72 Delay "empty ident" errors until they really matter.
Junio C Hamano authored
267 const char *git_committer_info(int error_on_no_name)
d289d13 @ebiederm [PATCH] Move git_author_info and git_commiter_info to ident.c
ebiederm authored
268 {
798123a Rename get_ident() to fmt_ident() and make it available to outside
Junio C Hamano authored
269 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
c7d77da git-var: constness and globalness cleanup.
Junio C Hamano authored
270 getenv("GIT_COMMITTER_EMAIL"),
749be72 Delay "empty ident" errors until they really matter.
Junio C Hamano authored
271 getenv("GIT_COMMITTER_DATE"),
272 error_on_no_name);
d289d13 @ebiederm [PATCH] Move git_author_info and git_commiter_info to ident.c
ebiederm authored
273 }
Something went wrong with that request. Please try again.