contrib/emacs/vc-git.el: support vc-version-other-window
[git/git.git] / ident.c
CommitLineData
6aa33f40
LT
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
e1b10391 10static char git_default_date[50];
6aa33f40 11
e9bacb4f
JH
12static void copy_gecos(struct passwd *w, char *name, int sz)
13{
14 char *src, *dst;
15 int len, nlen;
16
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
6aa33f40
LT
46int setup_ident(void)
47{
48 int len;
49 struct passwd *pw = getpwuid(getuid());
50
51 if (!pw)
52 die("You don't exist. Go away!");
53
54 /* Get the name ("gecos") */
e1b10391 55 copy_gecos(pw, git_default_name, sizeof(git_default_name));
6aa33f40
LT
56
57 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
58 len = strlen(pw->pw_name);
e1b10391 59 if (len > sizeof(git_default_email)/2)
7a868a84 60 die("Your sysadmin must hate you!");
e1b10391
LT
61 memcpy(git_default_email, pw->pw_name, len);
62 git_default_email[len++] = '@';
63 gethostname(git_default_email + len, sizeof(git_default_email) - len);
64 if (!strchr(git_default_email+len, '.')) {
adc3dbca
PB
65 struct hostent *he = gethostbyname(git_default_email + len);
66 char *domainname;
67
e1b10391
LT
68 len = strlen(git_default_email);
69 git_default_email[len++] = '.';
adc3dbca 70 if (he && (domainname = strchr(he->h_name, '.')))
817151e6 71 strlcpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
adc3dbca 72 else
817151e6 73 strlcpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
6aa33f40 74 }
6aa33f40 75 /* And set the default date */
e1b10391 76 datestamp(git_default_date, sizeof(git_default_date));
6aa33f40
LT
77 return 0;
78}
79
80static int add_raw(char *buf, int size, int offset, const char *str)
81{
82 int len = strlen(str);
83 if (offset + len > size)
84 return size;
85 memcpy(buf + offset, str, len);
86 return offset + len;
87}
88
89static int crud(unsigned char c)
90{
fb2af037
JR
91 static char crud_array[256];
92 static int crud_array_initialized = 0;
93
94 if (!crud_array_initialized) {
95 int k;
96
97 for (k = 0; k <= 31; ++k) crud_array[k] = 1;
98 crud_array[' '] = 1;
99 crud_array['.'] = 1;
100 crud_array[','] = 1;
101 crud_array[':'] = 1;
102 crud_array[';'] = 1;
103 crud_array['<'] = 1;
104 crud_array['>'] = 1;
105 crud_array['"'] = 1;
106 crud_array['\''] = 1;
107 crud_array_initialized = 1;
108 }
6aa33f40
LT
109 return crud_array[c];
110}
111
112/*
113 * Copy over a string to the destination, but avoid special
114 * characters ('\n', '<' and '>') and remove crud at the end
115 */
116static int copy(char *buf, int size, int offset, const char *src)
117{
118 int i, len;
119 unsigned char c;
120
121 /* Remove crud from the beginning.. */
122 while ((c = *src) != 0) {
123 if (!crud(c))
124 break;
125 src++;
126 }
127
128 /* Remove crud from the end.. */
129 len = strlen(src);
130 while (len > 0) {
131 c = src[len-1];
132 if (!crud(c))
133 break;
134 --len;
135 }
136
137 /*
138 * Copy the rest to the buffer, but avoid the special
82f9d58a 139 * characters '\n' '<' and '>' that act as delimiters on
6aa33f40
LT
140 * a identification line
141 */
142 for (i = 0; i < len; i++) {
143 c = *src++;
144 switch (c) {
145 case '\n': case '<': case '>':
146 continue;
147 }
148 if (offset >= size)
149 return size;
150 buf[offset++] = c;
151 }
152 return offset;
153}
154
749be728
JH
155static const char au_env[] = "GIT_AUTHOR_NAME";
156static const char co_env[] = "GIT_COMMITTER_NAME";
157static const char *env_hint =
d5cc2de9 158"\n"
749be728 159"*** Your name cannot be determined from your system services (gecos).\n"
d5cc2de9
HWN
160"\n"
161"Run\n"
162"\n"
163" git repo-config user.email \"you@email.com\"\n"
164" git repo-config user.name \"Your Name\"\n"
165"\n"
166"To set the identity in this repository.\n"
167"Add --global to set your account\'s default\n"
168"\n";
749be728 169
c7d77dab 170static const char *get_ident(const char *name, const char *email,
749be728 171 const char *date_str, int error_on_no_name)
6aa33f40
LT
172{
173 static char buffer[1000];
174 char date[50];
175 int i;
176
177 if (!name)
e1b10391 178 name = git_default_name;
6aa33f40 179 if (!email)
e1b10391 180 email = git_default_email;
dfdd309e 181
749be728
JH
182 if (!*name) {
183 if (name == git_default_name && env_hint) {
184 fprintf(stderr, env_hint, au_env, co_env);
185 env_hint = NULL; /* warn only once, for "git-var -l" */
186 }
187 if (error_on_no_name)
188 die("empty ident %s <%s> not allowed", name, email);
189 }
dfdd309e 190
e1b10391 191 strcpy(date, git_default_date);
6aa33f40
LT
192 if (date_str)
193 parse_date(date_str, date, sizeof(date));
194
195 i = copy(buffer, sizeof(buffer), 0, name);
196 i = add_raw(buffer, sizeof(buffer), i, " <");
197 i = copy(buffer, sizeof(buffer), i, email);
198 i = add_raw(buffer, sizeof(buffer), i, "> ");
199 i = copy(buffer, sizeof(buffer), i, date);
200 if (i >= sizeof(buffer))
201 die("Impossibly long personal identifier");
202 buffer[i] = 0;
203 return buffer;
204}
d289d136 205
749be728 206const char *git_author_info(int error_on_no_name)
d289d136 207{
c7d77dab
JH
208 return get_ident(getenv("GIT_AUTHOR_NAME"),
209 getenv("GIT_AUTHOR_EMAIL"),
749be728
JH
210 getenv("GIT_AUTHOR_DATE"),
211 error_on_no_name);
d289d136
EB
212}
213
749be728 214const char *git_committer_info(int error_on_no_name)
d289d136 215{
c7d77dab
JH
216 return get_ident(getenv("GIT_COMMITTER_NAME"),
217 getenv("GIT_COMMITTER_EMAIL"),
749be728
JH
218 getenv("GIT_COMMITTER_DATE"),
219 error_on_no_name);
d289d136 220}
a7f196a7
SP
221
222void ignore_missing_committer_name()
223{
224 /* If we did not get a name from the user's gecos entry then
225 * git_default_name is empty; so instead load the username
226 * into it as a 'good enough for now' approximation of who
227 * this user is.
228 */
229 if (!*git_default_name) {
230 struct passwd *pw = getpwuid(getuid());
231 if (!pw)
232 die("You don't exist. Go away!");
233 strlcpy(git_default_name, pw->pw_name, sizeof(git_default_name));
234 }
235}