bisect: replace "; then" with "\n<tab>*then"
[git/dscho.git] / ident.c
blob35a6f264737e700ddd45714a0e966422a0d756c7
1 /*
2 * ident.c
4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
7 */
8 #include "cache.h"
10 static char git_default_date[50];
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
18 static void copy_gecos(const struct passwd *w, char *name, size_t sz)
20 char *src, *dst;
21 size_t len, nlen;
23 nlen = strlen(w->pw_name);
25 /* Traditionally GECOS field had office phone numbers etc, separated
26 * with commas. Also & stands for capitalized form of the login name.
29 for (len = 0, dst = name, src = get_gecos(w); len < sz; src++) {
30 int ch = *src;
31 if (ch != '&') {
32 *dst++ = ch;
33 if (ch == 0 || ch == ',')
34 break;
35 len++;
36 continue;
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;
43 len += nlen;
46 if (len < sz)
47 name[len] = 0;
48 else
49 die("Your parents must have hated you!");
53 static void copy_email(const struct passwd *pw)
56 * Make up a fake email address
57 * (name + '@' + hostname [+ '.' + domainname])
59 size_t len = strlen(pw->pw_name);
60 if (len > sizeof(git_default_email)/2)
61 die("Your sysadmin must hate you!");
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, '.')) {
66 struct hostent *he = gethostbyname(git_default_email + len);
67 char *domainname;
69 len = strlen(git_default_email);
70 git_default_email[len++] = '.';
71 if (he && (domainname = strchr(he->h_name, '.')))
72 strlcpy(git_default_email + len, domainname + 1,
73 sizeof(git_default_email) - len);
74 else
75 strlcpy(git_default_email + len, "(none)",
76 sizeof(git_default_email) - len);
80 static void setup_ident(void)
82 struct passwd *pw = NULL;
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));
92 if (!git_default_email[0]) {
93 const char *email = getenv("EMAIL");
95 if (email && email[0]) {
96 strlcpy(git_default_email, email,
97 sizeof(git_default_email));
98 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
99 } else {
100 if (!pw)
101 pw = getpwuid(getuid());
102 if (!pw)
103 die("You don't exist. Go away!");
104 copy_email(pw);
108 /* And set the default date */
109 if (!git_default_date[0])
110 datestamp(git_default_date, sizeof(git_default_date));
113 static int add_raw(char *buf, size_t size, int offset, const char *str)
115 size_t len = strlen(str);
116 if (offset + len > size)
117 return size;
118 memcpy(buf + offset, str, len);
119 return offset + len;
122 static int crud(unsigned char c)
124 return c <= 32 ||
125 c == '.' ||
126 c == ',' ||
127 c == ':' ||
128 c == ';' ||
129 c == '<' ||
130 c == '>' ||
131 c == '"' ||
132 c == '\\' ||
133 c == '\'';
137 * Copy over a string to the destination, but avoid special
138 * characters ('\n', '<' and '>') and remove crud at the end
140 static int copy(char *buf, size_t size, int offset, const char *src)
142 size_t i, len;
143 unsigned char c;
145 /* Remove crud from the beginning.. */
146 while ((c = *src) != 0) {
147 if (!crud(c))
148 break;
149 src++;
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;
162 * Copy the rest to the buffer, but avoid the special
163 * characters '\n' '<' and '>' that act as delimiters on
164 * an identification line
166 for (i = 0; i < len; i++) {
167 c = *src++;
168 switch (c) {
169 case '\n': case '<': case '>':
170 continue;
172 if (offset >= size)
173 return size;
174 buf[offset++] = c;
176 return offset;
179 static const char *env_hint =
180 "\n"
181 "*** Please tell me who you are.\n"
182 "\n"
183 "Run\n"
184 "\n"
185 " git config --global user.email \"you@example.com\"\n"
186 " git config --global user.name \"Your Name\"\n"
187 "\n"
188 "to set your account\'s default identity.\n"
189 "Omit --global to set the identity only in this repository.\n"
190 "\n";
192 const char *fmt_ident(const char *name, const char *email,
193 const char *date_str, int flag)
195 static char buffer[1000];
196 char date[50];
197 int i;
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);
202 setup_ident();
203 if (!name)
204 name = git_default_name;
205 if (!email)
206 email = git_default_email;
208 if (!*name) {
209 struct passwd *pw;
211 if ((warn_on_no_name || error_on_no_name) &&
212 name == git_default_name && env_hint) {
213 fputs(env_hint, stderr);
214 env_hint = NULL; /* warn only once */
216 if (error_on_no_name)
217 die("empty ident %s <%s> not allowed", name, email);
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;
226 strcpy(date, git_default_date);
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);
232 i = copy(buffer, sizeof(buffer), 0, name);
233 i = add_raw(buffer, sizeof(buffer), i, " <");
234 i = copy(buffer, sizeof(buffer), i, email);
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, ">");
241 if (i >= sizeof(buffer))
242 die("Impossibly long personal identifier");
243 buffer[i] = 0;
244 return buffer;
247 const char *fmt_name(const char *name, const char *email)
249 return fmt_ident(name, email, NULL, IDENT_ERROR_ON_NO_NAME | IDENT_NO_DATE);
252 const char *git_author_info(int flag)
254 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
255 getenv("GIT_AUTHOR_EMAIL"),
256 getenv("GIT_AUTHOR_DATE"),
257 flag);
260 const char *git_committer_info(int flag)
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;
266 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
267 getenv("GIT_COMMITTER_EMAIL"),
268 getenv("GIT_COMMITTER_DATE"),
269 flag);
272 int user_ident_sufficiently_given(void)
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