]>
Commit | Line | Data |
---|---|---|
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 | ||
10 | #include <pwd.h> | |
adc3dbca | 11 | #include <netdb.h> |
6aa33f40 | 12 | |
e1b10391 | 13 | static char git_default_date[50]; |
6aa33f40 | 14 | |
e9bacb4f JH |
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 | ||
6aa33f40 LT |
49 | int setup_ident(void) |
50 | { | |
51 | int len; | |
52 | struct passwd *pw = getpwuid(getuid()); | |
53 | ||
54 | if (!pw) | |
55 | die("You don't exist. Go away!"); | |
56 | ||
57 | /* Get the name ("gecos") */ | |
e1b10391 | 58 | copy_gecos(pw, git_default_name, sizeof(git_default_name)); |
6aa33f40 LT |
59 | |
60 | /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */ | |
61 | len = strlen(pw->pw_name); | |
e1b10391 | 62 | if (len > sizeof(git_default_email)/2) |
7a868a84 | 63 | die("Your sysadmin must hate you!"); |
e1b10391 LT |
64 | memcpy(git_default_email, pw->pw_name, len); |
65 | git_default_email[len++] = '@'; | |
66 | gethostname(git_default_email + len, sizeof(git_default_email) - len); | |
67 | if (!strchr(git_default_email+len, '.')) { | |
adc3dbca PB |
68 | struct hostent *he = gethostbyname(git_default_email + len); |
69 | char *domainname; | |
70 | ||
e1b10391 LT |
71 | len = strlen(git_default_email); |
72 | git_default_email[len++] = '.'; | |
adc3dbca PB |
73 | if (he && (domainname = strchr(he->h_name, '.'))) |
74 | strncpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len); | |
75 | else | |
76 | strncpy(git_default_email + len, "(none)", sizeof(git_default_email) - len); | |
77 | git_default_email[sizeof(git_default_email) - 1] = 0; | |
6aa33f40 | 78 | } |
6aa33f40 | 79 | /* And set the default date */ |
e1b10391 | 80 | datestamp(git_default_date, sizeof(git_default_date)); |
6aa33f40 LT |
81 | return 0; |
82 | } | |
83 | ||
84 | static int add_raw(char *buf, int size, int offset, const char *str) | |
85 | { | |
86 | int len = strlen(str); | |
87 | if (offset + len > size) | |
88 | return size; | |
89 | memcpy(buf + offset, str, len); | |
90 | return offset + len; | |
91 | } | |
92 | ||
93 | static int crud(unsigned char c) | |
94 | { | |
fb2af037 JR |
95 | static char crud_array[256]; |
96 | static int crud_array_initialized = 0; | |
97 | ||
98 | if (!crud_array_initialized) { | |
99 | int k; | |
100 | ||
101 | for (k = 0; k <= 31; ++k) crud_array[k] = 1; | |
102 | crud_array[' '] = 1; | |
103 | crud_array['.'] = 1; | |
104 | crud_array[','] = 1; | |
105 | crud_array[':'] = 1; | |
106 | crud_array[';'] = 1; | |
107 | crud_array['<'] = 1; | |
108 | crud_array['>'] = 1; | |
109 | crud_array['"'] = 1; | |
110 | crud_array['\''] = 1; | |
111 | crud_array_initialized = 1; | |
112 | } | |
6aa33f40 LT |
113 | return crud_array[c]; |
114 | } | |
115 | ||
116 | /* | |
117 | * Copy over a string to the destination, but avoid special | |
118 | * characters ('\n', '<' and '>') and remove crud at the end | |
119 | */ | |
120 | static int copy(char *buf, int size, int offset, const char *src) | |
121 | { | |
122 | int i, len; | |
123 | unsigned char c; | |
124 | ||
125 | /* Remove crud from the beginning.. */ | |
126 | while ((c = *src) != 0) { | |
127 | if (!crud(c)) | |
128 | break; | |
129 | src++; | |
130 | } | |
131 | ||
132 | /* Remove crud from the end.. */ | |
133 | len = strlen(src); | |
134 | while (len > 0) { | |
135 | c = src[len-1]; | |
136 | if (!crud(c)) | |
137 | break; | |
138 | --len; | |
139 | } | |
140 | ||
141 | /* | |
142 | * Copy the rest to the buffer, but avoid the special | |
82f9d58a | 143 | * characters '\n' '<' and '>' that act as delimiters on |
6aa33f40 LT |
144 | * a identification line |
145 | */ | |
146 | for (i = 0; i < len; i++) { | |
147 | c = *src++; | |
148 | switch (c) { | |
149 | case '\n': case '<': case '>': | |
150 | continue; | |
151 | } | |
152 | if (offset >= size) | |
153 | return size; | |
154 | buf[offset++] = c; | |
155 | } | |
156 | return offset; | |
157 | } | |
158 | ||
749be728 JH |
159 | static const char au_env[] = "GIT_AUTHOR_NAME"; |
160 | static const char co_env[] = "GIT_COMMITTER_NAME"; | |
161 | static const char *env_hint = | |
162 | "\n*** Environment problem:\n" | |
163 | "*** Your name cannot be determined from your system services (gecos).\n" | |
164 | "*** You would need to set %s and %s\n" | |
165 | "*** environment variables; otherwise you won't be able to perform\n" | |
166 | "*** certain operations because of \"empty ident\" errors.\n" | |
167 | "*** Alternatively, you can use user.name configuration variable.\n\n"; | |
168 | ||
c7d77dab | 169 | static const char *get_ident(const char *name, const char *email, |
749be728 | 170 | const char *date_str, int error_on_no_name) |
6aa33f40 LT |
171 | { |
172 | static char buffer[1000]; | |
173 | char date[50]; | |
174 | int i; | |
175 | ||
176 | if (!name) | |
e1b10391 | 177 | name = git_default_name; |
6aa33f40 | 178 | if (!email) |
e1b10391 | 179 | email = git_default_email; |
dfdd309e | 180 | |
749be728 JH |
181 | if (!*name) { |
182 | if (name == git_default_name && env_hint) { | |
183 | fprintf(stderr, env_hint, au_env, co_env); | |
184 | env_hint = NULL; /* warn only once, for "git-var -l" */ | |
185 | } | |
186 | if (error_on_no_name) | |
187 | die("empty ident %s <%s> not allowed", name, email); | |
188 | } | |
dfdd309e | 189 | |
e1b10391 | 190 | strcpy(date, git_default_date); |
6aa33f40 LT |
191 | if (date_str) |
192 | parse_date(date_str, date, sizeof(date)); | |
193 | ||
194 | i = copy(buffer, sizeof(buffer), 0, name); | |
195 | i = add_raw(buffer, sizeof(buffer), i, " <"); | |
196 | i = copy(buffer, sizeof(buffer), i, email); | |
197 | i = add_raw(buffer, sizeof(buffer), i, "> "); | |
198 | i = copy(buffer, sizeof(buffer), i, date); | |
199 | if (i >= sizeof(buffer)) | |
200 | die("Impossibly long personal identifier"); | |
201 | buffer[i] = 0; | |
202 | return buffer; | |
203 | } | |
d289d136 | 204 | |
749be728 | 205 | const char *git_author_info(int error_on_no_name) |
d289d136 | 206 | { |
c7d77dab JH |
207 | return get_ident(getenv("GIT_AUTHOR_NAME"), |
208 | getenv("GIT_AUTHOR_EMAIL"), | |
749be728 JH |
209 | getenv("GIT_AUTHOR_DATE"), |
210 | error_on_no_name); | |
d289d136 EB |
211 | } |
212 | ||
749be728 | 213 | const char *git_committer_info(int error_on_no_name) |
d289d136 | 214 | { |
c7d77dab JH |
215 | return get_ident(getenv("GIT_COMMITTER_NAME"), |
216 | getenv("GIT_COMMITTER_EMAIL"), | |
749be728 JH |
217 | getenv("GIT_COMMITTER_DATE"), |
218 | error_on_no_name); | |
d289d136 | 219 | } |