Simplify crud() in ident.c
[git/raj.git] / ident.c
blobdbd0f527b27e1fd4139862a452d901d851367fb0
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 const char *email = getenv("EMAIL");
88 if (email && email[0])
89 strlcpy(git_default_email, email,
90 sizeof(git_default_email));
91 else {
92 if (!pw)
93 pw = getpwuid(getuid());
94 if (!pw)
95 die("You don't exist. Go away!");
96 copy_email(pw);
100 /* And set the default date */
101 if (!git_default_date[0])
102 datestamp(git_default_date, sizeof(git_default_date));
105 static int add_raw(char *buf, size_t size, int offset, const char *str)
107 size_t len = strlen(str);
108 if (offset + len > size)
109 return size;
110 memcpy(buf + offset, str, len);
111 return offset + len;
114 static int crud(unsigned char c)
116 return c <= 32 ||
117 c == '.' ||
118 c == ',' ||
119 c == ':' ||
120 c == ';' ||
121 c == '<' ||
122 c == '>' ||
123 c == '"' ||
124 c == '\'';
128 * Copy over a string to the destination, but avoid special
129 * characters ('\n', '<' and '>') and remove crud at the end
131 static int copy(char *buf, size_t size, int offset, const char *src)
133 size_t i, len;
134 unsigned char c;
136 /* Remove crud from the beginning.. */
137 while ((c = *src) != 0) {
138 if (!crud(c))
139 break;
140 src++;
143 /* Remove crud from the end.. */
144 len = strlen(src);
145 while (len > 0) {
146 c = src[len-1];
147 if (!crud(c))
148 break;
149 --len;
153 * Copy the rest to the buffer, but avoid the special
154 * characters '\n' '<' and '>' that act as delimiters on
155 * a identification line
157 for (i = 0; i < len; i++) {
158 c = *src++;
159 switch (c) {
160 case '\n': case '<': case '>':
161 continue;
163 if (offset >= size)
164 return size;
165 buf[offset++] = c;
167 return offset;
170 static const char au_env[] = "GIT_AUTHOR_NAME";
171 static const char co_env[] = "GIT_COMMITTER_NAME";
172 static const char *env_hint =
173 "\n"
174 "*** Your name cannot be determined from your system services (gecos).\n"
175 "\n"
176 "Run\n"
177 "\n"
178 " git config --global user.email \"you@email.com\"\n"
179 " git config --global user.name \"Your Name\"\n"
180 "\n"
181 "to set your account\'s default identity.\n"
182 "Omit --global to set the identity only in this repository.\n"
183 "\n";
185 const char *fmt_ident(const char *name, const char *email,
186 const char *date_str, int error_on_no_name)
188 static char buffer[1000];
189 char date[50];
190 int i;
192 setup_ident();
193 if (!name)
194 name = git_default_name;
195 if (!email)
196 email = git_default_email;
198 if (!*name) {
199 struct passwd *pw;
201 if (0 <= error_on_no_name &&
202 name == git_default_name && env_hint) {
203 fprintf(stderr, env_hint, au_env, co_env);
204 env_hint = NULL; /* warn only once, for "git-var -l" */
206 if (0 < error_on_no_name)
207 die("empty ident %s <%s> not allowed", name, email);
208 pw = getpwuid(getuid());
209 if (!pw)
210 die("You don't exist. Go away!");
211 strlcpy(git_default_name, pw->pw_name,
212 sizeof(git_default_name));
213 name = git_default_name;
216 strcpy(date, git_default_date);
217 if (date_str)
218 parse_date(date_str, date, sizeof(date));
220 i = copy(buffer, sizeof(buffer), 0, name);
221 i = add_raw(buffer, sizeof(buffer), i, " <");
222 i = copy(buffer, sizeof(buffer), i, email);
223 i = add_raw(buffer, sizeof(buffer), i, "> ");
224 i = copy(buffer, sizeof(buffer), i, date);
225 if (i >= sizeof(buffer))
226 die("Impossibly long personal identifier");
227 buffer[i] = 0;
228 return buffer;
231 const char *git_author_info(int error_on_no_name)
233 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
234 getenv("GIT_AUTHOR_EMAIL"),
235 getenv("GIT_AUTHOR_DATE"),
236 error_on_no_name);
239 const char *git_committer_info(int error_on_no_name)
241 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
242 getenv("GIT_COMMITTER_EMAIL"),
243 getenv("GIT_COMMITTER_DATE"),
244 error_on_no_name);