The synopsis of the manpages should use the hyphenated version
[git/dscho.git] / ident.c
blob562f5f18160166506e99aa4f6b5b417cbdc847d8
1 /*
2 * ident.c
4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
7 */
8 #include "cache.h"
10 #include <pwd.h>
11 #include <time.h>
12 #include <ctype.h>
14 static char real_email[1000];
15 static char real_name[1000];
16 static char real_date[50];
18 static void copy_gecos(struct passwd *w, char *name, int sz)
20 char *src, *dst;
21 int len, nlen;
23 nlen = strlen(w->pw_name);
25 /* Traditionally GECOS field had office phone numbers etc, separated
26 * with commas. Also & stands for capitalized form of the login name.
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;
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;
45 if (len < sz)
46 name[len] = 0;
47 else
48 die("Your parents must have hated you!");
52 int setup_ident(void)
54 int len;
55 struct passwd *pw = getpwuid(getuid());
57 if (!pw)
58 die("You don't exist. Go away!");
60 /* Get the name ("gecos") */
61 copy_gecos(pw, real_name, sizeof(real_name));
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);
75 /* And set the default date */
76 datestamp(real_date, sizeof(real_date));
77 return 0;
80 static int add_raw(char *buf, int size, int offset, const char *str)
82 int len = strlen(str);
83 if (offset + len > size)
84 return size;
85 memcpy(buf + offset, str, len);
86 return offset + len;
89 static int crud(unsigned char c)
91 static char crud_array[256];
92 static int crud_array_initialized = 0;
94 if (!crud_array_initialized) {
95 int k;
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;
109 return crud_array[c];
113 * Copy over a string to the destination, but avoid special
114 * characters ('\n', '<' and '>') and remove crud at the end
116 static int copy(char *buf, int size, int offset, const char *src)
118 int i, len;
119 unsigned char c;
121 /* Remove crud from the beginning.. */
122 while ((c = *src) != 0) {
123 if (!crud(c))
124 break;
125 src++;
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;
138 * Copy the rest to the buffer, but avoid the special
139 * characters '\n' '<' and '>' that act as delimeters on
140 * a identification line
142 for (i = 0; i < len; i++) {
143 c = *src++;
144 switch (c) {
145 case '\n': case '<': case '>':
146 continue;
148 if (offset >= size)
149 return size;
150 buf[offset++] = c;
152 return offset;
155 char *get_ident(const char *name, const char *email, const char *date_str)
157 static char buffer[1000];
158 char date[50];
159 int i;
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));
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;
180 char *git_author_info(void)
182 return get_ident(getenv("GIT_AUTHOR_NAME"), getenv("GIT_AUTHOR_EMAIL"), getenv("GIT_AUTHOR_DATE"));
185 char *git_committer_info(void)
187 return get_ident(getenv("GIT_COMMITTER_NAME"), getenv("GIT_COMMITTER_EMAIL"), getenv("GIT_COMMITTER_DATE"));