]> git.ipfire.org Git - thirdparty/git.git/blame - ident.c
git-log -g --pretty=oneline should display the reflog message
[thirdparty/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 182 if (!*name) {
cb280e10
JH
183 struct passwd *pw;
184
185 if (0 <= error_on_no_name &&
186 name == git_default_name && env_hint) {
749be728
JH
187 fprintf(stderr, env_hint, au_env, co_env);
188 env_hint = NULL; /* warn only once, for "git-var -l" */
189 }
cb280e10 190 if (0 < error_on_no_name)
749be728 191 die("empty ident %s <%s> not allowed", name, email);
cb280e10
JH
192 pw = getpwuid(getuid());
193 if (!pw)
194 die("You don't exist. Go away!");
195 strlcpy(git_default_name, pw->pw_name,
196 sizeof(git_default_name));
197 name = git_default_name;
749be728 198 }
dfdd309e 199
e1b10391 200 strcpy(date, git_default_date);
6aa33f40
LT
201 if (date_str)
202 parse_date(date_str, date, sizeof(date));
203
204 i = copy(buffer, sizeof(buffer), 0, name);
205 i = add_raw(buffer, sizeof(buffer), i, " <");
206 i = copy(buffer, sizeof(buffer), i, email);
207 i = add_raw(buffer, sizeof(buffer), i, "> ");
208 i = copy(buffer, sizeof(buffer), i, date);
209 if (i >= sizeof(buffer))
210 die("Impossibly long personal identifier");
211 buffer[i] = 0;
212 return buffer;
213}
d289d136 214
749be728 215const char *git_author_info(int error_on_no_name)
d289d136 216{
c7d77dab
JH
217 return get_ident(getenv("GIT_AUTHOR_NAME"),
218 getenv("GIT_AUTHOR_EMAIL"),
749be728
JH
219 getenv("GIT_AUTHOR_DATE"),
220 error_on_no_name);
d289d136
EB
221}
222
749be728 223const char *git_committer_info(int error_on_no_name)
d289d136 224{
c7d77dab
JH
225 return get_ident(getenv("GIT_COMMITTER_NAME"),
226 getenv("GIT_COMMITTER_EMAIL"),
749be728
JH
227 getenv("GIT_COMMITTER_DATE"),
228 error_on_no_name);
d289d136 229}