Disable the new test, which covers only symbolic links.
[git/mingw/4msysgit/wingit-dll.git] / ident.c
blobf62d1ec4700451373ee4a817e495f1950c95d44c
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(const struct passwd *w, char *name, size_t sz)
16 char *src, *dst;
17 size_t 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 static void copy_email(const struct passwd *pw)
51 * Make up a fake email address
52 * (name + '@' + hostname [+ '.' + domainname])
54 size_t len = strlen(pw->pw_name);
55 if (len > sizeof(git_default_email)/2)
56 die("Your sysadmin must hate you!");
57 memcpy(git_default_email, pw->pw_name, len);
58 git_default_email[len++] = '@';
59 gethostname(git_default_email + len, sizeof(git_default_email) - len);
60 if (!strchr(git_default_email+len, '.')) {
61 struct hostent *he = gethostbyname(git_default_email + len);
62 char *domainname;
64 len = strlen(git_default_email);
65 git_default_email[len++] = '.';
66 if (he && (domainname = strchr(he->h_name, '.')))
67 strlcpy(git_default_email + len, domainname + 1,
68 sizeof(git_default_email) - len);
69 else
70 strlcpy(git_default_email + len, "(none)",
71 sizeof(git_default_email) - len);
75 #endif
77 static void setup_ident(void)
79 #ifndef NO_ETC_PASSWD
80 struct passwd *pw = NULL;
82 /* Get the name ("gecos") */
83 if (!git_default_name[0]) {
84 pw = getpwuid(getuid());
85 if (!pw)
86 die("You don't exist. Go away!");
87 copy_gecos(pw, git_default_name, sizeof(git_default_name));
90 if (!git_default_email[0]) {
91 if (!pw)
92 pw = getpwuid(getuid());
93 if (!pw)
94 die("You don't exist. Go away!");
95 copy_email(pw);
97 #endif
99 /* And set the default date */
100 if (!git_default_date[0])
101 datestamp(git_default_date, sizeof(git_default_date));
104 static int add_raw(char *buf, size_t size, int offset, const char *str)
106 size_t len = strlen(str);
107 if (offset + len > size)
108 return size;
109 memcpy(buf + offset, str, len);
110 return offset + len;
113 static int crud(unsigned char c)
115 static char crud_array[256];
116 static int crud_array_initialized = 0;
118 if (!crud_array_initialized) {
119 int k;
121 for (k = 0; k <= 31; ++k) crud_array[k] = 1;
122 crud_array[' '] = 1;
123 crud_array['.'] = 1;
124 crud_array[','] = 1;
125 crud_array[':'] = 1;
126 crud_array[';'] = 1;
127 crud_array['<'] = 1;
128 crud_array['>'] = 1;
129 crud_array['"'] = 1;
130 crud_array['\''] = 1;
131 crud_array_initialized = 1;
133 return crud_array[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 * a 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 au_env[] = "GIT_AUTHOR_NAME";
180 static const char co_env[] = "GIT_COMMITTER_NAME";
181 static const char *env_hint =
182 "\n"
183 "*** Your name cannot be determined from your system services (gecos).\n"
184 "\n"
185 "Run\n"
186 "\n"
187 " git config user.email \"you@email.com\"\n"
188 " git config user.name \"Your Name\"\n"
189 "\n"
190 "To set the identity in this repository.\n"
191 "Add --global to set your account\'s default\n"
192 "\n";
194 const char *fmt_ident(const char *name, const char *email,
195 const char *date_str, int error_on_no_name)
197 static char buffer[1000];
198 char date[50];
199 int i;
201 setup_ident();
202 if (!name)
203 name = git_default_name;
204 if (!email)
205 email = git_default_email;
206 if (!email)
207 email = getenv("EMAIL");
209 if (!*name) {
210 #ifndef NO_ETC_PASSWD
211 struct passwd *pw;
212 #endif
214 if (0 <= error_on_no_name &&
215 name == git_default_name && env_hint) {
216 fprintf(stderr, env_hint, au_env, co_env);
217 env_hint = NULL; /* warn only once, for "git-var -l" */
219 if (0 < error_on_no_name)
220 die("empty ident %s <%s> not allowed", name, email);
221 #ifndef NO_ETC_PASSWD
222 pw = getpwuid(getuid());
223 if (!pw)
224 die("You don't exist. Go away!");
225 strlcpy(git_default_name, pw->pw_name,
226 sizeof(git_default_name));
227 #else
228 strlcpy(git_default_name, "unknown",
229 sizeof(git_default_name));
230 #endif
231 name = git_default_name;
234 strcpy(date, git_default_date);
235 if (date_str)
236 parse_date(date_str, date, sizeof(date));
238 i = copy(buffer, sizeof(buffer), 0, name);
239 i = add_raw(buffer, sizeof(buffer), i, " <");
240 i = copy(buffer, sizeof(buffer), i, email);
241 i = add_raw(buffer, sizeof(buffer), i, "> ");
242 i = copy(buffer, sizeof(buffer), i, date);
243 if (i >= sizeof(buffer))
244 die("Impossibly long personal identifier");
245 buffer[i] = 0;
246 return buffer;
249 const char *git_author_info(int error_on_no_name)
251 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
252 getenv("GIT_AUTHOR_EMAIL"),
253 getenv("GIT_AUTHOR_DATE"),
254 error_on_no_name);
257 const char *git_committer_info(int error_on_no_name)
259 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
260 getenv("GIT_COMMITTER_EMAIL"),
261 getenv("GIT_COMMITTER_DATE"),
262 error_on_no_name);