GIT 1.5.2.3
[git/jnareb-git/bp-gitweb.git] / ident.c
blob3d49608e6f9afe22f24a2faf70f2d6cee50b8919
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 static void copy_gecos(const struct passwd *w, char *name, size_t sz)
14 char *src, *dst;
15 size_t len, nlen;
17 nlen = strlen(w->pw_name);
19 /* Traditionally GECOS field had office phone numbers etc, separated
20 * with commas. Also & stands for capitalized form of the login name.
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;
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;
39 if (len < sz)
40 name[len] = 0;
41 else
42 die("Your parents must have hated you!");
46 static void copy_email(const struct passwd *pw)
49 * Make up a fake email address
50 * (name + '@' + hostname [+ '.' + domainname])
52 size_t len = strlen(pw->pw_name);
53 if (len > sizeof(git_default_email)/2)
54 die("Your sysadmin must hate you!");
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, '.')) {
59 struct hostent *he = gethostbyname(git_default_email + len);
60 char *domainname;
62 len = strlen(git_default_email);
63 git_default_email[len++] = '.';
64 if (he && (domainname = strchr(he->h_name, '.')))
65 strlcpy(git_default_email + len, domainname + 1,
66 sizeof(git_default_email) - len);
67 else
68 strlcpy(git_default_email + len, "(none)",
69 sizeof(git_default_email) - len);
73 static void setup_ident(void)
75 struct passwd *pw = NULL;
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));
85 if (!git_default_email[0]) {
86 if (!pw)
87 pw = getpwuid(getuid());
88 if (!pw)
89 die("You don't exist. Go away!");
90 copy_email(pw);
93 /* And set the default date */
94 if (!git_default_date[0])
95 datestamp(git_default_date, sizeof(git_default_date));
98 static int add_raw(char *buf, size_t size, int offset, const char *str)
100 size_t len = strlen(str);
101 if (offset + len > size)
102 return size;
103 memcpy(buf + offset, str, len);
104 return offset + len;
107 static int crud(unsigned char c)
109 static char crud_array[256];
110 static int crud_array_initialized = 0;
112 if (!crud_array_initialized) {
113 int k;
115 for (k = 0; k <= 31; ++k) crud_array[k] = 1;
116 crud_array[' '] = 1;
117 crud_array['.'] = 1;
118 crud_array[','] = 1;
119 crud_array[':'] = 1;
120 crud_array[';'] = 1;
121 crud_array['<'] = 1;
122 crud_array['>'] = 1;
123 crud_array['"'] = 1;
124 crud_array['\''] = 1;
125 crud_array_initialized = 1;
127 return crud_array[c];
131 * Copy over a string to the destination, but avoid special
132 * characters ('\n', '<' and '>') and remove crud at the end
134 static int copy(char *buf, size_t size, int offset, const char *src)
136 size_t i, len;
137 unsigned char c;
139 /* Remove crud from the beginning.. */
140 while ((c = *src) != 0) {
141 if (!crud(c))
142 break;
143 src++;
146 /* Remove crud from the end.. */
147 len = strlen(src);
148 while (len > 0) {
149 c = src[len-1];
150 if (!crud(c))
151 break;
152 --len;
156 * Copy the rest to the buffer, but avoid the special
157 * characters '\n' '<' and '>' that act as delimiters on
158 * a identification line
160 for (i = 0; i < len; i++) {
161 c = *src++;
162 switch (c) {
163 case '\n': case '<': case '>':
164 continue;
166 if (offset >= size)
167 return size;
168 buf[offset++] = c;
170 return offset;
173 static const char au_env[] = "GIT_AUTHOR_NAME";
174 static const char co_env[] = "GIT_COMMITTER_NAME";
175 static const char *env_hint =
176 "\n"
177 "*** Your name cannot be determined from your system services (gecos).\n"
178 "\n"
179 "Run\n"
180 "\n"
181 " git config user.email \"you@email.com\"\n"
182 " git config user.name \"Your Name\"\n"
183 "\n"
184 "To set the identity in this repository.\n"
185 "Add --global to set your account\'s default\n"
186 "\n";
188 const char *fmt_ident(const char *name, const char *email,
189 const char *date_str, int error_on_no_name)
191 static char buffer[1000];
192 char date[50];
193 int i;
195 setup_ident();
196 if (!name)
197 name = git_default_name;
198 if (!email)
199 email = git_default_email;
200 if (!email)
201 email = getenv("EMAIL");
203 if (!*name) {
204 struct passwd *pw;
206 if (0 <= error_on_no_name &&
207 name == git_default_name && env_hint) {
208 fprintf(stderr, env_hint, au_env, co_env);
209 env_hint = NULL; /* warn only once, for "git-var -l" */
211 if (0 < error_on_no_name)
212 die("empty ident %s <%s> not allowed", name, email);
213 pw = getpwuid(getuid());
214 if (!pw)
215 die("You don't exist. Go away!");
216 strlcpy(git_default_name, pw->pw_name,
217 sizeof(git_default_name));
218 name = git_default_name;
221 strcpy(date, git_default_date);
222 if (date_str)
223 parse_date(date_str, date, sizeof(date));
225 i = copy(buffer, sizeof(buffer), 0, name);
226 i = add_raw(buffer, sizeof(buffer), i, " <");
227 i = copy(buffer, sizeof(buffer), i, email);
228 i = add_raw(buffer, sizeof(buffer), i, "> ");
229 i = copy(buffer, sizeof(buffer), i, date);
230 if (i >= sizeof(buffer))
231 die("Impossibly long personal identifier");
232 buffer[i] = 0;
233 return buffer;
236 const char *git_author_info(int error_on_no_name)
238 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
239 getenv("GIT_AUTHOR_EMAIL"),
240 getenv("GIT_AUTHOR_DATE"),
241 error_on_no_name);
244 const char *git_committer_info(int error_on_no_name)
246 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
247 getenv("GIT_COMMITTER_EMAIL"),
248 getenv("GIT_COMMITTER_DATE"),
249 error_on_no_name);