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