blob: 3f6c29b74ddab694337c4bf6b9c35cafbbd0a601 [file] [log] [blame]
Linus Torvalds6aa33f42005-07-12 11:49:27 -07001/*
2 * ident.c
3 *
4 * create git identifier lines of the form "name <email> date"
5 *
6 * Copyright (C) 2005 Linus Torvalds
7 */
8#include "cache.h"
9
Linus Torvaldse1b10392005-10-11 18:47:34 -070010static char git_default_date[50];
Linus Torvalds6aa33f42005-07-12 11:49:27 -070011
Luiz Fernando N. Capitulinob0732112007-04-15 15:51:29 -030012static void copy_gecos(const struct passwd *w, char *name, size_t sz)
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070013{
14 char *src, *dst;
Luiz Fernando N. Capitulinob0732112007-04-15 15:51:29 -030015 size_t len, nlen;
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070016
17 nlen = strlen(w->pw_name);
18
19 /* Traditionally GECOS field had office phone numbers etc, separated
20 * with commas. Also & stands for capitalized form of the login name.
21 */
22
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;
31 }
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;
37 }
38 }
39 if (len < sz)
40 name[len] = 0;
41 else
42 die("Your parents must have hated you!");
43
44}
45
Luiz Fernando N. Capitulino0b952a92007-04-15 18:40:31 -030046static void copy_email(const struct passwd *pw)
Linus Torvalds6aa33f42005-07-12 11:49:27 -070047{
Junio C Hamano01754762007-01-28 00:50:53 -080048 /*
49 * Make up a fake email address
50 * (name + '@' + hostname [+ '.' + domainname])
51 */
Luiz Fernando N. Capitulinob0732112007-04-15 15:51:29 -030052 size_t len = strlen(pw->pw_name);
Linus Torvaldse1b10392005-10-11 18:47:34 -070053 if (len > sizeof(git_default_email)/2)
Eric W. Biederman7a868a82005-07-14 18:52:31 -060054 die("Your sysadmin must hate you!");
Linus Torvaldse1b10392005-10-11 18:47:34 -070055 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, '.')) {
Petr Baudisadc3dbc2005-10-21 03:57:39 +020059 struct hostent *he = gethostbyname(git_default_email + len);
60 char *domainname;
61
Linus Torvaldse1b10392005-10-11 18:47:34 -070062 len = strlen(git_default_email);
63 git_default_email[len++] = '.';
Petr Baudisadc3dbc2005-10-21 03:57:39 +020064 if (he && (domainname = strchr(he->h_name, '.')))
Junio C Hamano01754762007-01-28 00:50:53 -080065 strlcpy(git_default_email + len, domainname + 1,
66 sizeof(git_default_email) - len);
Petr Baudisadc3dbc2005-10-21 03:57:39 +020067 else
Junio C Hamano01754762007-01-28 00:50:53 -080068 strlcpy(git_default_email + len, "(none)",
69 sizeof(git_default_email) - len);
Linus Torvalds6aa33f42005-07-12 11:49:27 -070070 }
Junio C Hamano01754762007-01-28 00:50:53 -080071}
72
73static void setup_ident(void)
74{
75 struct passwd *pw = NULL;
76
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));
83 }
84
85 if (!git_default_email[0]) {
Matt Kraai46f74f02007-07-05 17:29:41 -070086 const char *email = getenv("EMAIL");
87
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);
97 }
Junio C Hamano01754762007-01-28 00:50:53 -080098 }
99
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700100 /* And set the default date */
Junio C Hamano01754762007-01-28 00:50:53 -0800101 if (!git_default_date[0])
102 datestamp(git_default_date, sizeof(git_default_date));
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700103}
104
Luiz Fernando N. Capitulinob0732112007-04-15 15:51:29 -0300105static int add_raw(char *buf, size_t size, int offset, const char *str)
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700106{
Luiz Fernando N. Capitulinob0732112007-04-15 15:51:29 -0300107 size_t len = strlen(str);
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700108 if (offset + len > size)
109 return size;
110 memcpy(buf + offset, str, len);
111 return offset + len;
112}
113
114static int crud(unsigned char c)
115{
Jason Riedyfb2af032005-08-23 13:52:52 -0700116 static char crud_array[256];
117 static int crud_array_initialized = 0;
118
119 if (!crud_array_initialized) {
120 int k;
121
122 for (k = 0; k <= 31; ++k) crud_array[k] = 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['\''] = 1;
132 crud_array_initialized = 1;
133 }
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700134 return crud_array[c];
135}
136
137/*
138 * Copy over a string to the destination, but avoid special
139 * characters ('\n', '<' and '>') and remove crud at the end
140 */
Luiz Fernando N. Capitulinob0732112007-04-15 15:51:29 -0300141static int copy(char *buf, size_t size, int offset, const char *src)
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700142{
Luiz Fernando N. Capitulinob0732112007-04-15 15:51:29 -0300143 size_t i, len;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700144 unsigned char c;
145
146 /* Remove crud from the beginning.. */
147 while ((c = *src) != 0) {
148 if (!crud(c))
149 break;
150 src++;
151 }
152
153 /* Remove crud from the end.. */
154 len = strlen(src);
155 while (len > 0) {
156 c = src[len-1];
157 if (!crud(c))
158 break;
159 --len;
160 }
161
162 /*
163 * Copy the rest to the buffer, but avoid the special
Junio C Hamano82f9d582005-12-29 01:30:08 -0800164 * characters '\n' '<' and '>' that act as delimiters on
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700165 * a identification line
166 */
167 for (i = 0; i < len; i++) {
168 c = *src++;
169 switch (c) {
170 case '\n': case '<': case '>':
171 continue;
172 }
173 if (offset >= size)
174 return size;
175 buf[offset++] = c;
176 }
177 return offset;
178}
179
Junio C Hamano749be722006-02-18 20:31:05 -0800180static const char au_env[] = "GIT_AUTHOR_NAME";
181static const char co_env[] = "GIT_COMMITTER_NAME";
182static const char *env_hint =
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100183"\n"
Junio C Hamano749be722006-02-18 20:31:05 -0800184"*** Your name cannot be determined from your system services (gecos).\n"
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100185"\n"
186"Run\n"
187"\n"
David Symonds8e7425d2007-12-07 10:36:45 +1100188" git config --global user.email \"you@example.com\"\n"
Steffen Prohaska180787c2007-08-14 00:05:50 +0200189" git config --global user.name \"Your Name\"\n"
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100190"\n"
Steffen Prohaska180787c2007-08-14 00:05:50 +0200191"to set your account\'s default identity.\n"
192"Omit --global to set the identity only in this repository.\n"
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100193"\n";
Junio C Hamano749be722006-02-18 20:31:05 -0800194
Junio C Hamano798123a2007-02-04 17:50:14 -0800195const char *fmt_ident(const char *name, const char *email,
196 const char *date_str, int error_on_no_name)
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700197{
198 static char buffer[1000];
199 char date[50];
200 int i;
201
Junio C Hamano01754762007-01-28 00:50:53 -0800202 setup_ident();
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700203 if (!name)
Linus Torvaldse1b10392005-10-11 18:47:34 -0700204 name = git_default_name;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700205 if (!email)
Linus Torvaldse1b10392005-10-11 18:47:34 -0700206 email = git_default_email;
Junio C Hamanodfdd3092006-02-07 13:19:10 -0800207
Junio C Hamano749be722006-02-18 20:31:05 -0800208 if (!*name) {
Junio C Hamanocb280e12007-01-25 19:05:01 -0800209 struct passwd *pw;
210
211 if (0 <= error_on_no_name &&
212 name == git_default_name && env_hint) {
Junio C Hamano749be722006-02-18 20:31:05 -0800213 fprintf(stderr, env_hint, au_env, co_env);
214 env_hint = NULL; /* warn only once, for "git-var -l" */
215 }
Junio C Hamanocb280e12007-01-25 19:05:01 -0800216 if (0 < error_on_no_name)
Junio C Hamano749be722006-02-18 20:31:05 -0800217 die("empty ident %s <%s> not allowed", name, email);
Junio C Hamanocb280e12007-01-25 19:05:01 -0800218 pw = getpwuid(getuid());
219 if (!pw)
220 die("You don't exist. Go away!");
221 strlcpy(git_default_name, pw->pw_name,
222 sizeof(git_default_name));
223 name = git_default_name;
Junio C Hamano749be722006-02-18 20:31:05 -0800224 }
Junio C Hamanodfdd3092006-02-07 13:19:10 -0800225
Linus Torvaldse1b10392005-10-11 18:47:34 -0700226 strcpy(date, git_default_date);
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700227 if (date_str)
228 parse_date(date_str, date, sizeof(date));
229
230 i = copy(buffer, sizeof(buffer), 0, name);
231 i = add_raw(buffer, sizeof(buffer), i, " <");
232 i = copy(buffer, sizeof(buffer), i, email);
233 i = add_raw(buffer, sizeof(buffer), i, "> ");
234 i = copy(buffer, sizeof(buffer), i, date);
235 if (i >= sizeof(buffer))
236 die("Impossibly long personal identifier");
237 buffer[i] = 0;
238 return buffer;
239}
Eric W. Biedermand289d132005-07-14 18:50:33 -0600240
Junio C Hamano749be722006-02-18 20:31:05 -0800241const char *git_author_info(int error_on_no_name)
Eric W. Biedermand289d132005-07-14 18:50:33 -0600242{
Junio C Hamano798123a2007-02-04 17:50:14 -0800243 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
Junio C Hamanoc7d77da2005-11-21 23:44:35 -0800244 getenv("GIT_AUTHOR_EMAIL"),
Junio C Hamano749be722006-02-18 20:31:05 -0800245 getenv("GIT_AUTHOR_DATE"),
246 error_on_no_name);
Eric W. Biedermand289d132005-07-14 18:50:33 -0600247}
248
Junio C Hamano749be722006-02-18 20:31:05 -0800249const char *git_committer_info(int error_on_no_name)
Eric W. Biedermand289d132005-07-14 18:50:33 -0600250{
Junio C Hamano798123a2007-02-04 17:50:14 -0800251 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
Junio C Hamanoc7d77da2005-11-21 23:44:35 -0800252 getenv("GIT_COMMITTER_EMAIL"),
Junio C Hamano749be722006-02-18 20:31:05 -0800253 getenv("GIT_COMMITTER_DATE"),
254 error_on_no_name);
Eric W. Biedermand289d132005-07-14 18:50:33 -0600255}