MinGW cannot support sideband communication.
[git/mingw.git] / ident.c
blob83ce631a06286ca06b3c2e827b6ca73228b4951e
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 int setup_ident(void)
50 int len;
51 struct passwd *pw = getpwuid(getuid());
53 if (!pw)
54 die("You don't exist. Go away!");
56 /* Get the name ("gecos") */
57 copy_gecos(pw, git_default_name, sizeof(git_default_name));
59 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
60 len = strlen(pw->pw_name);
61 if (len > sizeof(git_default_email)/2)
62 die("Your sysadmin must hate you!");
63 memcpy(git_default_email, pw->pw_name, len);
64 git_default_email[len++] = '@';
65 gethostname(git_default_email + len, sizeof(git_default_email) - len);
66 if (!strchr(git_default_email+len, '.')) {
67 struct hostent *he = gethostbyname(git_default_email + len);
68 char *domainname;
70 len = strlen(git_default_email);
71 git_default_email[len++] = '.';
72 if (he && (domainname = strchr(he->h_name, '.')))
73 strlcpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
74 else
75 strlcpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
77 /* And set the default date */
78 datestamp(git_default_date, sizeof(git_default_date));
79 return 0;
82 #else /* NO_ETC_PASSWD */
84 static int add_raw(char *buf, int size, int offset, const char *str)
86 int len = strlen(str);
87 if (offset + len > size)
88 return size;
89 memcpy(buf + offset, str, len);
90 return offset + len;
93 static int crud(unsigned char c)
95 static char crud_array[256];
96 static int crud_array_initialized = 0;
98 if (!crud_array_initialized) {
99 int k;
101 for (k = 0; k <= 31; ++k) crud_array[k] = 1;
102 crud_array[' '] = 1;
103 crud_array['.'] = 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_initialized = 1;
113 return crud_array[c];
117 * Copy over a string to the destination, but avoid special
118 * characters ('\n', '<' and '>') and remove crud at the end
120 static int copy(char *buf, int size, int offset, const char *src)
122 int i, len;
123 unsigned char c;
125 /* Remove crud from the beginning.. */
126 while ((c = *src) != 0) {
127 if (!crud(c))
128 break;
129 src++;
132 /* Remove crud from the end.. */
133 len = strlen(src);
134 while (len > 0) {
135 c = src[len-1];
136 if (!crud(c))
137 break;
138 --len;
142 * Copy the rest to the buffer, but avoid the special
143 * characters '\n' '<' and '>' that act as delimiters on
144 * a identification line
146 for (i = 0; i < len; i++) {
147 c = *src++;
148 switch (c) {
149 case '\n': case '<': case '>':
150 continue;
152 if (offset >= size)
153 return size;
154 buf[offset++] = c;
156 return offset;
159 int setup_ident(void)
161 return 0;
164 #endif
166 static const char au_env[] = "GIT_AUTHOR_NAME";
167 static const char co_env[] = "GIT_COMMITTER_NAME";
168 static const char *env_hint =
169 "\n"
170 "*** Your name cannot be determined from your system services (gecos).\n"
171 "\n"
172 "Run\n"
173 "\n"
174 " git repo-config user.email \"you@email.com\"\n"
175 " git repo-config user.name \"Your Name\"\n"
176 "\n"
177 "To set the identity in this repository.\n"
178 "Add --global to set your account\'s default\n"
179 "\n";
181 static const char *get_ident(const char *name, const char *email,
182 const char *date_str, int error_on_no_name)
184 static char buffer[1000];
185 char date[50];
186 int i;
188 if (!name)
189 name = git_default_name;
190 if (!email)
191 email = git_default_email;
193 if (!*name) {
194 if (name == git_default_name && env_hint) {
195 fprintf(stderr, env_hint, au_env, co_env);
196 env_hint = NULL; /* warn only once, for "git-var -l" */
198 if (error_on_no_name)
199 die("empty ident %s <%s> not allowed", name, email);
202 strcpy(date, git_default_date);
203 if (date_str)
204 parse_date(date_str, date, sizeof(date));
206 i = copy(buffer, sizeof(buffer), 0, name);
207 i = add_raw(buffer, sizeof(buffer), i, " <");
208 i = copy(buffer, sizeof(buffer), i, email);
209 i = add_raw(buffer, sizeof(buffer), i, "> ");
210 i = copy(buffer, sizeof(buffer), i, date);
211 if (i >= sizeof(buffer))
212 die("Impossibly long personal identifier");
213 buffer[i] = 0;
214 return buffer;
217 const char *git_author_info(int error_on_no_name)
219 return get_ident(getenv("GIT_AUTHOR_NAME"),
220 getenv("GIT_AUTHOR_EMAIL"),
221 getenv("GIT_AUTHOR_DATE"),
222 error_on_no_name);
225 const char *git_committer_info(int error_on_no_name)
227 return get_ident(getenv("GIT_COMMITTER_NAME"),
228 getenv("GIT_COMMITTER_EMAIL"),
229 getenv("GIT_COMMITTER_DATE"),
230 error_on_no_name);
233 #ifndef NO_ETC_PASSWD
235 void ignore_missing_committer_name()
237 /* If we did not get a name from the user's gecos entry then
238 * git_default_name is empty; so instead load the username
239 * into it as a 'good enough for now' approximation of who
240 * this user is.
242 if (!*git_default_name) {
243 struct passwd *pw = getpwuid(getuid());
244 if (!pw)
245 die("You don't exist. Go away!");
246 strlcpy(git_default_name, pw->pw_name, sizeof(git_default_name));
250 #else
252 void ignore_missing_committer_name()
254 strcpy(git_default_name, "unknown");
257 #endif