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 <time.h>
12#include <ctype.h>
13
14static char real_email[1000];
15static char real_name[1000];
16static char real_date[50];
17
18static void copy_gecos(struct passwd *w, char *name, int sz)
19{
20 char *src, *dst;
21 int len, nlen;
22
23 nlen = strlen(w->pw_name);
24
25 /* Traditionally GECOS field had office phone numbers etc, separated
26 * with commas. Also & stands for capitalized form of the login name.
27 */
28
29 for (len = 0, dst = name, src = w->pw_gecos; 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 }
44 }
45 if (len < sz)
46 name[len] = 0;
47 else
48 die("Your parents must have hated you!");
49
50}
51
52int setup_ident(void)
53{
54 int len;
55 struct passwd *pw = getpwuid(getuid());
56
57 if (!pw)
58 die("You don't exist. Go away!");
59
60 /* Get the name ("gecos") */
61 copy_gecos(pw, real_name, sizeof(real_name));
62
63 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
64 len = strlen(pw->pw_name);
65 if (len > sizeof(real_email)/2)
66 die("Your sysadmin must hate you!");
67 memcpy(real_email, pw->pw_name, len);
68 real_email[len++] = '@';
69 gethostname(real_email + len, sizeof(real_email) - len);
70 if (!strchr(real_email+len, '.')) {
71 len = strlen(real_email);
72 real_email[len++] = '.';
73 getdomainname(real_email+len, sizeof(real_email)-len);
74 }
75 /* And set the default date */
76 datestamp(real_date, sizeof(real_date));
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{
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 }
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
139 * characters '\n' '<' and '>' that act as delimeters on
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
155char *get_ident(const char *name, const char *email, const char *date_str)
156{
157 static char buffer[1000];
158 char date[50];
159 int i;
160
161 if (!name)
162 name = real_name;
163 if (!email)
164 email = real_email;
165 strcpy(date, real_date);
166 if (date_str)
167 parse_date(date_str, date, sizeof(date));
168
169 i = copy(buffer, sizeof(buffer), 0, name);
170 i = add_raw(buffer, sizeof(buffer), i, " <");
171 i = copy(buffer, sizeof(buffer), i, email);
172 i = add_raw(buffer, sizeof(buffer), i, "> ");
173 i = copy(buffer, sizeof(buffer), i, date);
174 if (i >= sizeof(buffer))
175 die("Impossibly long personal identifier");
176 buffer[i] = 0;
177 return buffer;
178}
179
180char *git_author_info(void)
181{
182 return get_ident(getenv("GIT_AUTHOR_NAME"), getenv("GIT_AUTHOR_EMAIL"), getenv("GIT_AUTHOR_DATE"));
183}
184
185char *git_committer_info(void)
186{
187 return get_ident(getenv("GIT_COMMITTER_NAME"), getenv("GIT_COMMITTER_EMAIL"), getenv("GIT_COMMITTER_DATE"));
188}