4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
13 static char git_default_date[50];
15 static void copy_gecos(struct passwd *w, char *name, int sz)
20 nlen = strlen(w->pw_name);
22 /* Traditionally GECOS field had office phone numbers etc, separated
23 * with commas. Also & stands for capitalized form of the login name.
26 for (len = 0, dst = name, src = w->pw_gecos; len < sz; src++) {
30 if (ch == 0 || ch == ',')
35 if (len + nlen < sz) {
36 /* Sorry, Mr. McDonald... */
37 *dst++ = toupper(*w->pw_name);
38 memcpy(dst, w->pw_name + 1, nlen - 1);
45 die("Your parents must have hated you!");
52 struct passwd *pw = getpwuid(getuid());
55 die("You don't exist. Go away!");
57 /* Get the name ("gecos") */
58 copy_gecos(pw, git_default_name, sizeof(git_default_name));
60 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
61 len = strlen(pw->pw_name);
62 if (len > sizeof(git_default_email)/2)
63 die("Your sysadmin must hate you!");
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, '.')) {
68 len = strlen(git_default_email);
69 git_default_email[len++] = '.';
70 getdomainname(git_default_email+len, sizeof(git_default_email)-len);
72 /* And set the default date */
73 datestamp(git_default_date, sizeof(git_default_date));
77 static int add_raw(char *buf, int size, int offset, const char *str)
79 int len = strlen(str);
80 if (offset + len > size)
82 memcpy(buf + offset, str, len);
86 static int crud(unsigned char c)
88 static char crud_array[256];
89 static int crud_array_initialized = 0;
91 if (!crud_array_initialized) {
94 for (k = 0; k <= 31; ++k) crud_array[k] = 1;
103 crud_array['\''] = 1;
104 crud_array_initialized = 1;
106 return crud_array[c];
110 * Copy over a string to the destination, but avoid special
111 * characters ('\n', '<' and '>') and remove crud at the end
113 static int copy(char *buf, int size, int offset, const char *src)
118 /* Remove crud from the beginning.. */
119 while ((c = *src) != 0) {
125 /* Remove crud from the end.. */
135 * Copy the rest to the buffer, but avoid the special
136 * characters '\n' '<' and '>' that act as delimeters on
137 * a identification line
139 for (i = 0; i < len; i++) {
142 case '\n': case '<': case '>':
152 char *get_ident(const char *name, const char *email, const char *date_str)
154 static char buffer[1000];
159 name = git_default_name;
161 email = git_default_email;
162 strcpy(date, git_default_date);
164 parse_date(date_str, date, sizeof(date));
166 i = copy(buffer, sizeof(buffer), 0, name);
167 i = add_raw(buffer, sizeof(buffer), i, " <");
168 i = copy(buffer, sizeof(buffer), i, email);
169 i = add_raw(buffer, sizeof(buffer), i, "> ");
170 i = copy(buffer, sizeof(buffer), i, date);
171 if (i >= sizeof(buffer))
172 die("Impossibly long personal identifier");
177 char *git_author_info(void)
179 return get_ident(getenv("GIT_AUTHOR_NAME"), getenv("GIT_AUTHOR_EMAIL"), getenv("GIT_AUTHOR_DATE"));
182 char *git_committer_info(void)
184 return get_ident(getenv("GIT_COMMITTER_NAME"), getenv("GIT_COMMITTER_EMAIL"), getenv("GIT_COMMITTER_DATE"));