Merge branch 'jc/ident'
[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"
9
10 #include <pwd.h>
11 #include <netdb.h>
12
13 static char git_default_date[50];
14
15 static void copy_gecos(struct passwd *w, char *name, int sz)
16 {
17         char *src, *dst;
18         int len, nlen;
19
20         nlen = strlen(w->pw_name);
21
22         /* Traditionally GECOS field had office phone numbers etc, separated
23          * with commas.  Also & stands for capitalized form of the login name.
24          */
25
26         for (len = 0, dst = name, src = w->pw_gecos; len < sz; src++) {
27                 int ch = *src;
28                 if (ch != '&') {
29                         *dst++ = ch;
30                         if (ch == 0 || ch == ',')
31                                 break;
32                         len++;
33                         continue;
34                 }
35                 if (len + nlen < sz) {
36                         /* Sorry, Mr. McDonald... */
37                         *dst++ = toupper(*w->pw_name);
38                         memcpy(dst, w->pw_name + 1, nlen - 1);
39                         dst += nlen - 1;
40                 }
41         }
42         if (len < sz)
43                 name[len] = 0;
44         else
45                 die("Your parents must have hated you!");
46
47 }
48
49 static const char au_env[] = "GIT_AUTHOR_NAME";
50 static const char co_env[] = "GIT_COMMITTER_NAME";
51 static const char env_hint[] =
52 "\n*** Environment problem:\n"
53 "*** Your name cannot be determined from your system services (gecos).\n"
54 "*** You would need to set %s and %s\n"
55 "*** environment variables; otherwise you won't be able to perform\n"
56 "*** certain operations because of \"empty ident\" errors.\n\n";
57
58 int setup_ident(void)
59 {
60         int len;
61         struct passwd *pw = getpwuid(getuid());
62
63         if (!pw)
64                 die("You don't exist. Go away!");
65
66         /* Get the name ("gecos") */
67         copy_gecos(pw, git_default_name, sizeof(git_default_name));
68
69         if (!*git_default_name) {
70                 if (!getenv(au_env) || !getenv(co_env))
71                         fprintf(stderr, env_hint, au_env, co_env);
72         }
73
74         /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
75         len = strlen(pw->pw_name);
76         if (len > sizeof(git_default_email)/2)
77                 die("Your sysadmin must hate you!");
78         memcpy(git_default_email, pw->pw_name, len);
79         git_default_email[len++] = '@';
80         gethostname(git_default_email + len, sizeof(git_default_email) - len);
81         if (!strchr(git_default_email+len, '.')) {
82                 struct hostent *he = gethostbyname(git_default_email + len);
83                 char *domainname;
84
85                 len = strlen(git_default_email);
86                 git_default_email[len++] = '.';
87                 if (he && (domainname = strchr(he->h_name, '.')))
88                         strncpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
89                 else
90                         strncpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
91                 git_default_email[sizeof(git_default_email) - 1] = 0;
92         }
93         /* And set the default date */
94         datestamp(git_default_date, sizeof(git_default_date));
95         return 0;
96 }
97
98 static int add_raw(char *buf, int size, int offset, const char *str)
99 {
100         int len = strlen(str);
101         if (offset + len > size)
102                 return size;
103         memcpy(buf + offset, str, len);
104         return offset + len;
105 }
106
107 static int crud(unsigned char c)
108 {
109         static char crud_array[256];
110         static int crud_array_initialized = 0;
111
112         if (!crud_array_initialized) {
113                 int k;
114
115                 for (k = 0; k <= 31; ++k) crud_array[k] = 1;
116                 crud_array[' '] = 1;
117                 crud_array['.'] = 1;
118                 crud_array[','] = 1;
119                 crud_array[':'] = 1;
120                 crud_array[';'] = 1;
121                 crud_array['<'] = 1;
122                 crud_array['>'] = 1;
123                 crud_array['"'] = 1;
124                 crud_array['\''] = 1;
125                 crud_array_initialized = 1;
126         }
127         return crud_array[c];
128 }
129
130 /*
131  * Copy over a string to the destination, but avoid special
132  * characters ('\n', '<' and '>') and remove crud at the end
133  */
134 static int copy(char *buf, int size, int offset, const char *src)
135 {
136         int i, len;
137         unsigned char c;
138
139         /* Remove crud from the beginning.. */
140         while ((c = *src) != 0) {
141                 if (!crud(c))
142                         break;
143                 src++;
144         }
145
146         /* Remove crud from the end.. */
147         len = strlen(src);
148         while (len > 0) {
149                 c = src[len-1];
150                 if (!crud(c))
151                         break;
152                 --len;
153         }
154
155         /*
156          * Copy the rest to the buffer, but avoid the special
157          * characters '\n' '<' and '>' that act as delimiters on
158          * a identification line
159          */
160         for (i = 0; i < len; i++) {
161                 c = *src++;
162                 switch (c) {
163                 case '\n': case '<': case '>':
164                         continue;
165                 }
166                 if (offset >= size)
167                         return size;
168                 buf[offset++] = c;
169         }
170         return offset;
171 }
172
173 static const char *get_ident(const char *name, const char *email,
174                              const char *date_str)
175 {
176         static char buffer[1000];
177         char date[50];
178         int i;
179
180         if (!name)
181                 name = git_default_name;
182         if (!email)
183                 email = git_default_email;
184
185         if (!*name || !*email)
186                 die("empty ident %s <%s> not allowed",
187                     name, email);
188
189         strcpy(date, git_default_date);
190         if (date_str)
191                 parse_date(date_str, date, sizeof(date));
192
193         i = copy(buffer, sizeof(buffer), 0, name);
194         i = add_raw(buffer, sizeof(buffer), i, " <");
195         i = copy(buffer, sizeof(buffer), i, email);
196         i = add_raw(buffer, sizeof(buffer), i, "> ");
197         i = copy(buffer, sizeof(buffer), i, date);
198         if (i >= sizeof(buffer))
199                 die("Impossibly long personal identifier");
200         buffer[i] = 0;
201         return buffer;
202 }
203
204 const char *git_author_info(void)
205 {
206         return get_ident(getenv("GIT_AUTHOR_NAME"),
207                          getenv("GIT_AUTHOR_EMAIL"),
208                          getenv("GIT_AUTHOR_DATE"));
209 }
210
211 const char *git_committer_info(void)
212 {
213         return get_ident(getenv("GIT_COMMITTER_NAME"),
214                          getenv("GIT_COMMITTER_EMAIL"),
215                          getenv("GIT_COMMITTER_DATE"));
216 }