Merge refs/heads/master from .
[git/git-svn.git] / ident.c
blob0fe81f6900dcb6951539c050f1398b1d4b208ea6
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 int setup_ident(void)
20 int len;
21 struct passwd *pw = getpwuid(getuid());
23 if (!pw)
24 die("You don't exist. Go away!");
26 /* Get the name ("gecos") */
27 len = strlen(pw->pw_gecos);
28 if (len >= sizeof(real_name))
29 die("Your parents must have hated you!");
30 memcpy(real_name, pw->pw_gecos, len+1);
32 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
33 len = strlen(pw->pw_name);
34 if (len > sizeof(real_email)/2)
35 die("Your sysadmin must hate you!");
36 memcpy(real_email, pw->pw_name, len);
37 real_email[len++] = '@';
38 gethostname(real_email + len, sizeof(real_email) - len);
39 if (!strchr(real_email+len, '.')) {
40 len = strlen(real_email);
41 real_email[len++] = '.';
42 getdomainname(real_email+len, sizeof(real_email)-len);
45 /* And set the default date */
46 datestamp(real_date, sizeof(real_date));
47 return 0;
50 static int add_raw(char *buf, int size, int offset, const char *str)
52 int len = strlen(str);
53 if (offset + len > size)
54 return size;
55 memcpy(buf + offset, str, len);
56 return offset + len;
59 static int crud(unsigned char c)
61 static char crud_array[256];
62 static int crud_array_initialized = 0;
64 if (!crud_array_initialized) {
65 int k;
67 for (k = 0; k <= 31; ++k) crud_array[k] = 1;
68 crud_array[' '] = 1;
69 crud_array['.'] = 1;
70 crud_array[','] = 1;
71 crud_array[':'] = 1;
72 crud_array[';'] = 1;
73 crud_array['<'] = 1;
74 crud_array['>'] = 1;
75 crud_array['"'] = 1;
76 crud_array['\''] = 1;
77 crud_array_initialized = 1;
79 return crud_array[c];
83 * Copy over a string to the destination, but avoid special
84 * characters ('\n', '<' and '>') and remove crud at the end
86 static int copy(char *buf, int size, int offset, const char *src)
88 int i, len;
89 unsigned char c;
91 /* Remove crud from the beginning.. */
92 while ((c = *src) != 0) {
93 if (!crud(c))
94 break;
95 src++;
98 /* Remove crud from the end.. */
99 len = strlen(src);
100 while (len > 0) {
101 c = src[len-1];
102 if (!crud(c))
103 break;
104 --len;
108 * Copy the rest to the buffer, but avoid the special
109 * characters '\n' '<' and '>' that act as delimeters on
110 * a identification line
112 for (i = 0; i < len; i++) {
113 c = *src++;
114 switch (c) {
115 case '\n': case '<': case '>':
116 continue;
118 if (offset >= size)
119 return size;
120 buf[offset++] = c;
122 return offset;
125 char *get_ident(const char *name, const char *email, const char *date_str)
127 static char buffer[1000];
128 char date[50];
129 int i;
131 if (!name)
132 name = real_name;
133 if (!email)
134 email = real_email;
135 strcpy(date, real_date);
136 if (date_str)
137 parse_date(date_str, date, sizeof(date));
139 i = copy(buffer, sizeof(buffer), 0, name);
140 i = add_raw(buffer, sizeof(buffer), i, " <");
141 i = copy(buffer, sizeof(buffer), i, email);
142 i = add_raw(buffer, sizeof(buffer), i, "> ");
143 i = copy(buffer, sizeof(buffer), i, date);
144 if (i >= sizeof(buffer))
145 die("Impossibly long personal identifier");
146 buffer[i] = 0;
147 return buffer;
150 char *git_author_info(void)
152 return get_ident(gitenv("GIT_AUTHOR_NAME"), gitenv("GIT_AUTHOR_EMAIL"), gitenv("GIT_AUTHOR_DATE"));
155 char *git_committer_info(void)
157 return get_ident(gitenv("GIT_COMMITTER_NAME"), gitenv("GIT_COMMITTER_EMAIL"), gitenv("GIT_COMMITTER_DATE"));