Code

edb4314494c421278f5d69bf69df5f7efde8e68a
[git.git] / ident.c
1 /*
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"
10 static char git_default_date[50];
12 #ifdef NO_GECOS_IN_PWENT
13 #define get_gecos(ignored) "&"
14 #else
15 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
16 #endif
18 static void copy_gecos(const struct passwd *w, char *name, size_t sz)
19 {
20         char *src, *dst;
21         size_t len, nlen;
23         nlen = strlen(w->pw_name);
25         /* Traditionally GECOS field had office phone numbers etc, separated
26          * with commas.  Also & stands for capitalized form of the login name.
27          */
29         for (len = 0, dst = name, src = get_gecos(w); len < sz; src++) {
30                 int ch = *src;
31                 if (ch != '&') {
32                         *dst++ = ch;
33                         if (ch == 0 || ch == ',')
34                                 break;
35                         len++;
36                         continue;
37                 }
38                 if (len + nlen < sz) {
39                         /* Sorry, Mr. McDonald... */
40                         *dst++ = toupper(*w->pw_name);
41                         memcpy(dst, w->pw_name + 1, nlen - 1);
42                         dst += nlen - 1;
43                         len += nlen;
44                 }
45         }
46         if (len < sz)
47                 name[len] = 0;
48         else
49                 die("Your parents must have hated you!");
51 }
53 static int add_mailname_host(char *buf, size_t len)
54 {
55         FILE *mailname;
57         mailname = fopen("/etc/mailname", "r");
58         if (!mailname) {
59                 if (errno != ENOENT)
60                         warning("cannot open /etc/mailname: %s",
61                                 strerror(errno));
62                 return -1;
63         }
64         if (!fgets(buf, len, mailname)) {
65                 if (ferror(mailname))
66                         warning("cannot read /etc/mailname: %s",
67                                 strerror(errno));
68                 fclose(mailname);
69                 return -1;
70         }
71         /* success! */
72         fclose(mailname);
73         return 0;
74 }
76 static void add_domainname(char *buf, size_t len)
77 {
78         struct hostent *he;
79         size_t namelen;
80         const char *domainname;
82         if (gethostname(buf, len)) {
83                 warning("cannot get host name: %s", strerror(errno));
84                 strlcpy(buf, "(none)", len);
85                 return;
86         }
87         namelen = strlen(buf);
88         if (memchr(buf, '.', namelen))
89                 return;
91         he = gethostbyname(buf);
92         buf[namelen++] = '.';
93         buf += namelen;
94         len -= namelen;
95         if (he && (domainname = strchr(he->h_name, '.')))
96                 strlcpy(buf, domainname + 1, len);
97         else
98                 strlcpy(buf, "(none)", len);
99 }
101 static void copy_email(const struct passwd *pw)
103         /*
104          * Make up a fake email address
105          * (name + '@' + hostname [+ '.' + domainname])
106          */
107         size_t len = strlen(pw->pw_name);
108         if (len > sizeof(git_default_email)/2)
109                 die("Your sysadmin must hate you!");
110         memcpy(git_default_email, pw->pw_name, len);
111         git_default_email[len++] = '@';
113         if (!add_mailname_host(git_default_email + len,
114                                 sizeof(git_default_email) - len))
115                 return; /* read from "/etc/mailname" (Debian) */
116         add_domainname(git_default_email + len,
117                         sizeof(git_default_email) - len);
120 static void setup_ident(void)
122         struct passwd *pw = NULL;
124         /* Get the name ("gecos") */
125         if (!git_default_name[0]) {
126                 pw = getpwuid(getuid());
127                 if (!pw)
128                         die("You don't exist. Go away!");
129                 copy_gecos(pw, git_default_name, sizeof(git_default_name));
130         }
132         if (!git_default_email[0]) {
133                 const char *email = getenv("EMAIL");
135                 if (email && email[0]) {
136                         strlcpy(git_default_email, email,
137                                 sizeof(git_default_email));
138                         user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
139                 } else {
140                         if (!pw)
141                                 pw = getpwuid(getuid());
142                         if (!pw)
143                                 die("You don't exist. Go away!");
144                         copy_email(pw);
145                 }
146         }
148         /* And set the default date */
149         if (!git_default_date[0])
150                 datestamp(git_default_date, sizeof(git_default_date));
153 static int add_raw(char *buf, size_t size, int offset, const char *str)
155         size_t len = strlen(str);
156         if (offset + len > size)
157                 return size;
158         memcpy(buf + offset, str, len);
159         return offset + len;
162 static int crud(unsigned char c)
164         return  c <= 32  ||
165                 c == '.' ||
166                 c == ',' ||
167                 c == ':' ||
168                 c == ';' ||
169                 c == '<' ||
170                 c == '>' ||
171                 c == '"' ||
172                 c == '\\' ||
173                 c == '\'';
176 /*
177  * Copy over a string to the destination, but avoid special
178  * characters ('\n', '<' and '>') and remove crud at the end
179  */
180 static int copy(char *buf, size_t size, int offset, const char *src)
182         size_t i, len;
183         unsigned char c;
185         /* Remove crud from the beginning.. */
186         while ((c = *src) != 0) {
187                 if (!crud(c))
188                         break;
189                 src++;
190         }
192         /* Remove crud from the end.. */
193         len = strlen(src);
194         while (len > 0) {
195                 c = src[len-1];
196                 if (!crud(c))
197                         break;
198                 --len;
199         }
201         /*
202          * Copy the rest to the buffer, but avoid the special
203          * characters '\n' '<' and '>' that act as delimiters on
204          * an identification line
205          */
206         for (i = 0; i < len; i++) {
207                 c = *src++;
208                 switch (c) {
209                 case '\n': case '<': case '>':
210                         continue;
211                 }
212                 if (offset >= size)
213                         return size;
214                 buf[offset++] = c;
215         }
216         return offset;
219 static const char *env_hint =
220 "\n"
221 "*** Please tell me who you are.\n"
222 "\n"
223 "Run\n"
224 "\n"
225 "  git config --global user.email \"you@example.com\"\n"
226 "  git config --global user.name \"Your Name\"\n"
227 "\n"
228 "to set your account\'s default identity.\n"
229 "Omit --global to set the identity only in this repository.\n"
230 "\n";
232 const char *fmt_ident(const char *name, const char *email,
233                       const char *date_str, int flag)
235         static char buffer[1000];
236         char date[50];
237         int i;
238         int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME);
239         int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME);
240         int name_addr_only = (flag & IDENT_NO_DATE);
242         setup_ident();
243         if (!name)
244                 name = git_default_name;
245         if (!email)
246                 email = git_default_email;
248         if (!*name) {
249                 struct passwd *pw;
251                 if ((warn_on_no_name || error_on_no_name) &&
252                     name == git_default_name && env_hint) {
253                         fputs(env_hint, stderr);
254                         env_hint = NULL; /* warn only once */
255                 }
256                 if (error_on_no_name)
257                         die("empty ident %s <%s> not allowed", name, email);
258                 pw = getpwuid(getuid());
259                 if (!pw)
260                         die("You don't exist. Go away!");
261                 strlcpy(git_default_name, pw->pw_name,
262                         sizeof(git_default_name));
263                 name = git_default_name;
264         }
266         strcpy(date, git_default_date);
267         if (!name_addr_only && date_str && date_str[0]) {
268                 if (parse_date(date_str, date, sizeof(date)) < 0)
269                         die("invalid date format: %s", date_str);
270         }
272         i = copy(buffer, sizeof(buffer), 0, name);
273         i = add_raw(buffer, sizeof(buffer), i, " <");
274         i = copy(buffer, sizeof(buffer), i, email);
275         if (!name_addr_only) {
276                 i = add_raw(buffer, sizeof(buffer), i,  "> ");
277                 i = copy(buffer, sizeof(buffer), i, date);
278         } else {
279                 i = add_raw(buffer, sizeof(buffer), i, ">");
280         }
281         if (i >= sizeof(buffer))
282                 die("Impossibly long personal identifier");
283         buffer[i] = 0;
284         return buffer;
287 const char *fmt_name(const char *name, const char *email)
289         return fmt_ident(name, email, NULL, IDENT_ERROR_ON_NO_NAME | IDENT_NO_DATE);
292 const char *git_author_info(int flag)
294         return fmt_ident(getenv("GIT_AUTHOR_NAME"),
295                          getenv("GIT_AUTHOR_EMAIL"),
296                          getenv("GIT_AUTHOR_DATE"),
297                          flag);
300 const char *git_committer_info(int flag)
302         if (getenv("GIT_COMMITTER_NAME"))
303                 user_ident_explicitly_given |= IDENT_NAME_GIVEN;
304         if (getenv("GIT_COMMITTER_EMAIL"))
305                 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
306         return fmt_ident(getenv("GIT_COMMITTER_NAME"),
307                          getenv("GIT_COMMITTER_EMAIL"),
308                          getenv("GIT_COMMITTER_DATE"),
309                          flag);
312 int user_ident_sufficiently_given(void)
314 #ifndef WINDOWS
315         return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
316 #else
317         return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
318 #endif