Use spawn*_pipe() instead of fork()/exec() in send-pack and receive-pack.
[git/mingw.git] / ident.c
blob145f54a5056a64ca5ba1864fa629c666694a11cc
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 #ifndef NO_ETC_PASSWD
14 static void copy_gecos(struct passwd *w, char *name, int sz)
16 char *src, *dst;
17 int len, nlen;
19 nlen = strlen(w->pw_name);
21 /* Traditionally GECOS field had office phone numbers etc, separated
22 * with commas. Also & stands for capitalized form of the login name.
25 for (len = 0, dst = name, src = w->pw_gecos; len < sz; src++) {
26 int ch = *src;
27 if (ch != '&') {
28 *dst++ = ch;
29 if (ch == 0 || ch == ',')
30 break;
31 len++;
32 continue;
34 if (len + nlen < sz) {
35 /* Sorry, Mr. McDonald... */
36 *dst++ = toupper(*w->pw_name);
37 memcpy(dst, w->pw_name + 1, nlen - 1);
38 dst += nlen - 1;
41 if (len < sz)
42 name[len] = 0;
43 else
44 die("Your parents must have hated you!");
48 #endif
50 int setup_ident(void)
52 #ifndef NO_ETC_PASSWD
53 int len;
54 struct passwd *pw = getpwuid(getuid());
56 if (!pw)
57 die("You don't exist. Go away!");
59 /* Get the name ("gecos") */
60 copy_gecos(pw, git_default_name, sizeof(git_default_name));
62 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
63 len = strlen(pw->pw_name);
64 if (len > sizeof(git_default_email)/2)
65 die("Your sysadmin must hate you!");
66 memcpy(git_default_email, pw->pw_name, len);
67 git_default_email[len++] = '@';
68 gethostname(git_default_email + len, sizeof(git_default_email) - len);
69 if (!strchr(git_default_email+len, '.')) {
70 struct hostent *he = gethostbyname(git_default_email + len);
71 char *domainname;
73 len = strlen(git_default_email);
74 git_default_email[len++] = '.';
75 if (he && (domainname = strchr(he->h_name, '.')))
76 strlcpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
77 else
78 strlcpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
80 #endif
81 /* And set the default date */
82 datestamp(git_default_date, sizeof(git_default_date));
83 return 0;
86 static int add_raw(char *buf, int size, int offset, const char *str)
88 int len = strlen(str);
89 if (offset + len > size)
90 return size;
91 memcpy(buf + offset, str, len);
92 return offset + len;
95 static int crud(unsigned char c)
97 static char crud_array[256];
98 static int crud_array_initialized = 0;
100 if (!crud_array_initialized) {
101 int k;
103 for (k = 0; k <= 31; ++k) crud_array[k] = 1;
104 crud_array[' '] = 1;
105 crud_array['.'] = 1;
106 crud_array[','] = 1;
107 crud_array[':'] = 1;
108 crud_array[';'] = 1;
109 crud_array['<'] = 1;
110 crud_array['>'] = 1;
111 crud_array['"'] = 1;
112 crud_array['\''] = 1;
113 crud_array_initialized = 1;
115 return crud_array[c];
119 * Copy over a string to the destination, but avoid special
120 * characters ('\n', '<' and '>') and remove crud at the end
122 static int copy(char *buf, int size, int offset, const char *src)
124 int i, len;
125 unsigned char c;
127 /* Remove crud from the beginning.. */
128 while ((c = *src) != 0) {
129 if (!crud(c))
130 break;
131 src++;
134 /* Remove crud from the end.. */
135 len = strlen(src);
136 while (len > 0) {
137 c = src[len-1];
138 if (!crud(c))
139 break;
140 --len;
144 * Copy the rest to the buffer, but avoid the special
145 * characters '\n' '<' and '>' that act as delimiters on
146 * a identification line
148 for (i = 0; i < len; i++) {
149 c = *src++;
150 switch (c) {
151 case '\n': case '<': case '>':
152 continue;
154 if (offset >= size)
155 return size;
156 buf[offset++] = c;
158 return offset;
161 static const char au_env[] = "GIT_AUTHOR_NAME";
162 static const char co_env[] = "GIT_COMMITTER_NAME";
163 static const char *env_hint =
164 "\n"
165 "*** Your name cannot be determined from your system services (gecos).\n"
166 "\n"
167 "Run\n"
168 "\n"
169 " git repo-config user.email \"you@email.com\"\n"
170 " git repo-config user.name \"Your Name\"\n"
171 "\n"
172 "To set the identity in this repository.\n"
173 "Add --global to set your account\'s default\n"
174 "\n";
176 static const char *get_ident(const char *name, const char *email,
177 const char *date_str, int error_on_no_name)
179 static char buffer[1000];
180 char date[50];
181 int i;
183 if (!name)
184 name = git_default_name;
185 if (!email)
186 email = git_default_email;
188 if (!*name) {
189 if (name == git_default_name && env_hint) {
190 fprintf(stderr, env_hint, au_env, co_env);
191 env_hint = NULL; /* warn only once, for "git-var -l" */
193 if (error_on_no_name)
194 die("empty ident %s <%s> not allowed", name, email);
197 strcpy(date, git_default_date);
198 if (date_str)
199 parse_date(date_str, date, sizeof(date));
201 i = copy(buffer, sizeof(buffer), 0, name);
202 i = add_raw(buffer, sizeof(buffer), i, " <");
203 i = copy(buffer, sizeof(buffer), i, email);
204 i = add_raw(buffer, sizeof(buffer), i, "> ");
205 i = copy(buffer, sizeof(buffer), i, date);
206 if (i >= sizeof(buffer))
207 die("Impossibly long personal identifier");
208 buffer[i] = 0;
209 return buffer;
212 const char *git_author_info(int error_on_no_name)
214 return get_ident(getenv("GIT_AUTHOR_NAME"),
215 getenv("GIT_AUTHOR_EMAIL"),
216 getenv("GIT_AUTHOR_DATE"),
217 error_on_no_name);
220 const char *git_committer_info(int error_on_no_name)
222 return get_ident(getenv("GIT_COMMITTER_NAME"),
223 getenv("GIT_COMMITTER_EMAIL"),
224 getenv("GIT_COMMITTER_DATE"),
225 error_on_no_name);
228 #ifndef NO_ETC_PASSWD
230 void ignore_missing_committer_name()
232 /* If we did not get a name from the user's gecos entry then
233 * git_default_name is empty; so instead load the username
234 * into it as a 'good enough for now' approximation of who
235 * this user is.
237 if (!*git_default_name) {
238 struct passwd *pw = getpwuid(getuid());
239 if (!pw)
240 die("You don't exist. Go away!");
241 strlcpy(git_default_name, pw->pw_name, sizeof(git_default_name));
245 #else
247 void ignore_missing_committer_name()
249 strcpy(git_default_name, "unknown");
252 #endif