4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
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)
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++) {
33 if (ch == 0 || ch == ',')
38 if (len + nlen < sz) {
39 /* Sorry, Mr. McDonald... */
40 *dst++ = toupper(*w->pw_name);
41 memcpy(dst, w->pw_name + 1, nlen - 1);
48 die("Your parents must have hated you!");
55 struct passwd *pw = getpwuid(getuid());
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));
80 static int add_raw(char *buf, int size, int offset, const char *str)
82 int len = strlen(str);
83 if (offset + len > size)
85 memcpy(buf + offset, str, 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) {
97 for (k = 0; k <= 31; ++k) crud_array[k] = 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)
121 /* Remove crud from the beginning.. */
122 while ((c = *src) != 0) {
128 /* Remove crud from the end.. */
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++) {
145 case '\n': case '<': case '>':
155 char *get_ident(const char *name, const char *email, const char *date_str)
157 static char buffer[1000];
165 strcpy(date, real_date);
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");
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"));