gitweb: Change header search action to use parse_commits.
[git/dkf.git] / ident.c
blob6ad8fedd60ba1461d77dc2b1d66e52cf56158c6f
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(struct passwd *w, char *name, int sz)
14 char *src, *dst;
15 int 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 int setup_ident(void)
48 int len;
49 struct passwd *pw = getpwuid(getuid());
51 if (!pw)
52 die("You don't exist. Go away!");
54 /* Get the name ("gecos") */
55 copy_gecos(pw, git_default_name, sizeof(git_default_name));
57 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
58 len = strlen(pw->pw_name);
59 if (len > sizeof(git_default_email)/2)
60 die("Your sysadmin must hate you!");
61 memcpy(git_default_email, pw->pw_name, len);
62 git_default_email[len++] = '@';
63 gethostname(git_default_email + len, sizeof(git_default_email) - len);
64 if (!strchr(git_default_email+len, '.')) {
65 struct hostent *he = gethostbyname(git_default_email + len);
66 char *domainname;
68 len = strlen(git_default_email);
69 git_default_email[len++] = '.';
70 if (he && (domainname = strchr(he->h_name, '.')))
71 strlcpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
72 else
73 strlcpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
75 /* And set the default date */
76 datestamp(git_default_date, sizeof(git_default_date));
77 return 0;
80 static int add_raw(char *buf, int size, int offset, const char *str)
82 int len = strlen(str);
83 if (offset + len > size)
84 return size;
85 memcpy(buf + offset, str, len);
86 return offset + len;
89 static int crud(unsigned char c)
91 static char crud_array[256];
92 static int crud_array_initialized = 0;
94 if (!crud_array_initialized) {
95 int k;
97 for (k = 0; k <= 31; ++k) crud_array[k] = 1;
98 crud_array[' '] = 1;
99 crud_array['.'] = 1;
100 crud_array[','] = 1;
101 crud_array[':'] = 1;
102 crud_array[';'] = 1;
103 crud_array['<'] = 1;
104 crud_array['>'] = 1;
105 crud_array['"'] = 1;
106 crud_array['\''] = 1;
107 crud_array_initialized = 1;
109 return crud_array[c];
113 * Copy over a string to the destination, but avoid special
114 * characters ('\n', '<' and '>') and remove crud at the end
116 static int copy(char *buf, int size, int offset, const char *src)
118 int i, len;
119 unsigned char c;
121 /* Remove crud from the beginning.. */
122 while ((c = *src) != 0) {
123 if (!crud(c))
124 break;
125 src++;
128 /* Remove crud from the end.. */
129 len = strlen(src);
130 while (len > 0) {
131 c = src[len-1];
132 if (!crud(c))
133 break;
134 --len;
138 * Copy the rest to the buffer, but avoid the special
139 * characters '\n' '<' and '>' that act as delimiters on
140 * a identification line
142 for (i = 0; i < len; i++) {
143 c = *src++;
144 switch (c) {
145 case '\n': case '<': case '>':
146 continue;
148 if (offset >= size)
149 return size;
150 buf[offset++] = c;
152 return offset;
155 static const char au_env[] = "GIT_AUTHOR_NAME";
156 static const char co_env[] = "GIT_COMMITTER_NAME";
157 static const char *env_hint =
158 "\n"
159 "*** Your name cannot be determined from your system services (gecos).\n"
160 "\n"
161 "Run\n"
162 "\n"
163 " git repo-config user.email \"you@email.com\"\n"
164 " git repo-config user.name \"Your Name\"\n"
165 "\n"
166 "To set the identity in this repository.\n"
167 "Add --global to set your account\'s default\n"
168 "\n";
170 static const char *get_ident(const char *name, const char *email,
171 const char *date_str, int error_on_no_name)
173 static char buffer[1000];
174 char date[50];
175 int i;
177 if (!name)
178 name = git_default_name;
179 if (!email)
180 email = git_default_email;
182 if (!*name) {
183 if (name == git_default_name && env_hint) {
184 fprintf(stderr, env_hint, au_env, co_env);
185 env_hint = NULL; /* warn only once, for "git-var -l" */
187 if (error_on_no_name)
188 die("empty ident %s <%s> not allowed", name, email);
191 strcpy(date, git_default_date);
192 if (date_str)
193 parse_date(date_str, date, sizeof(date));
195 i = copy(buffer, sizeof(buffer), 0, name);
196 i = add_raw(buffer, sizeof(buffer), i, " <");
197 i = copy(buffer, sizeof(buffer), i, email);
198 i = add_raw(buffer, sizeof(buffer), i, "> ");
199 i = copy(buffer, sizeof(buffer), i, date);
200 if (i >= sizeof(buffer))
201 die("Impossibly long personal identifier");
202 buffer[i] = 0;
203 return buffer;
206 const char *git_author_info(int error_on_no_name)
208 return get_ident(getenv("GIT_AUTHOR_NAME"),
209 getenv("GIT_AUTHOR_EMAIL"),
210 getenv("GIT_AUTHOR_DATE"),
211 error_on_no_name);
214 const char *git_committer_info(int error_on_no_name)
216 return get_ident(getenv("GIT_COMMITTER_NAME"),
217 getenv("GIT_COMMITTER_EMAIL"),
218 getenv("GIT_COMMITTER_DATE"),
219 error_on_no_name);
222 void ignore_missing_committer_name()
224 /* If we did not get a name from the user's gecos entry then
225 * git_default_name is empty; so instead load the username
226 * into it as a 'good enough for now' approximation of who
227 * this user is.
229 if (!*git_default_name) {
230 struct passwd *pw = getpwuid(getuid());
231 if (!pw)
232 die("You don't exist. Go away!");
233 strlcpy(git_default_name, pw->pw_name, sizeof(git_default_name));