Really run scripts under the interpreter specified in the first line.
[git/mingw.git] / ident.c
blobcb1a756acf4f60e300d9a035cd57e1feb54a8e62
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 static void copy_email(struct passwd *pw)
51 * Make up a fake email address
52 * (name + '@' + hostname [+ '.' + domainname])
54 int 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, int size, int offset, const char *str)
106 int 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, int size, int offset, const char *src)
142 int 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;
207 if (!*name) {
208 #ifndef NO_ETC_PASSWD
209 struct passwd *pw;
210 #endif
212 if (0 <= error_on_no_name &&
213 name == git_default_name && env_hint) {
214 fprintf(stderr, env_hint, au_env, co_env);
215 env_hint = NULL; /* warn only once, for "git-var -l" */
217 if (0 < error_on_no_name)
218 die("empty ident %s <%s> not allowed", name, email);
219 #ifndef NO_ETC_PASSWD
220 pw = getpwuid(getuid());
221 if (!pw)
222 die("You don't exist. Go away!");
223 strlcpy(git_default_name, pw->pw_name,
224 sizeof(git_default_name));
225 #else
226 strlcpy(git_default_name, "unknown",
227 sizeof(git_default_name));
228 #endif
229 name = git_default_name;
232 strcpy(date, git_default_date);
233 if (date_str)
234 parse_date(date_str, date, sizeof(date));
236 i = copy(buffer, sizeof(buffer), 0, name);
237 i = add_raw(buffer, sizeof(buffer), i, " <");
238 i = copy(buffer, sizeof(buffer), i, email);
239 i = add_raw(buffer, sizeof(buffer), i, "> ");
240 i = copy(buffer, sizeof(buffer), i, date);
241 if (i >= sizeof(buffer))
242 die("Impossibly long personal identifier");
243 buffer[i] = 0;
244 return buffer;
247 const char *git_author_info(int error_on_no_name)
249 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
250 getenv("GIT_AUTHOR_EMAIL"),
251 getenv("GIT_AUTHOR_DATE"),
252 error_on_no_name);
255 const char *git_committer_info(int error_on_no_name)
257 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
258 getenv("GIT_COMMITTER_EMAIL"),
259 getenv("GIT_COMMITTER_DATE"),
260 error_on_no_name);