4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
12 static char git_default_date[50];
14 static void copy_gecos(struct passwd *w, char *name, int sz)
19 nlen = strlen(w->pw_name);
21 /* Traditionally GECOS field had office phone numbers etc, separated
22 * with commas. Also & stands for capitalized form of the login name.
25 for (len = 0, dst = name, src = w->pw_gecos; len < sz; src++) {
29 if (ch == 0 || ch == ',')
34 if (len + nlen < sz) {
35 /* Sorry, Mr. McDonald... */
36 *dst++ = toupper(*w->pw_name);
37 memcpy(dst, w->pw_name + 1, nlen - 1);
44 die("Your parents must have hated you!");
51 struct passwd *pw = getpwuid(getuid());
54 die("You don't exist. Go away!");
56 /* Get the name ("gecos") */
57 copy_gecos(pw, git_default_name, sizeof(git_default_name));
59 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
60 len = strlen(pw->pw_name);
61 if (len > sizeof(git_default_email)/2)
62 die("Your sysadmin must hate you!");
63 memcpy(git_default_email, pw->pw_name, len);
64 git_default_email[len++] = '@';
65 gethostname(git_default_email + len, sizeof(git_default_email) - len);
66 if (!strchr(git_default_email+len, '.')) {
67 len = strlen(git_default_email);
68 git_default_email[len++] = '.';
69 getdomainname(git_default_email+len, sizeof(git_default_email)-len);
71 /* And set the default date */
72 datestamp(git_default_date, sizeof(git_default_date));
76 static int add_raw(char *buf, int size, int offset, const char *str)
78 int len = strlen(str);
79 if (offset + len > size)
81 memcpy(buf + offset, str, len);
85 static int crud(unsigned char c)
87 static char crud_array[256];
88 static int crud_array_initialized = 0;
90 if (!crud_array_initialized) {
93 for (k = 0; k <= 31; ++k) crud_array[k] = 1;
102 crud_array['\''] = 1;
103 crud_array_initialized = 1;
105 return crud_array[c];
109 * Copy over a string to the destination, but avoid special
110 * characters ('\n', '<' and '>') and remove crud at the end
112 static int copy(char *buf, int size, int offset, const char *src)
117 /* Remove crud from the beginning.. */
118 while ((c = *src) != 0) {
124 /* Remove crud from the end.. */
134 * Copy the rest to the buffer, but avoid the special
135 * characters '\n' '<' and '>' that act as delimeters on
136 * a identification line
138 for (i = 0; i < len; i++) {
141 case '\n': case '<': case '>':
151 char *get_ident(const char *name, const char *email, const char *date_str)
153 static char buffer[1000];
158 name = git_default_name;
160 email = git_default_email;
161 strcpy(date, git_default_date);
163 parse_date(date_str, date, sizeof(date));
165 i = copy(buffer, sizeof(buffer), 0, name);
166 i = add_raw(buffer, sizeof(buffer), i, " <");
167 i = copy(buffer, sizeof(buffer), i, email);
168 i = add_raw(buffer, sizeof(buffer), i, "> ");
169 i = copy(buffer, sizeof(buffer), i, date);
170 if (i >= sizeof(buffer))
171 die("Impossibly long personal identifier");
176 char *git_author_info(void)
178 return get_ident(getenv("GIT_AUTHOR_NAME"), getenv("GIT_AUTHOR_EMAIL"), getenv("GIT_AUTHOR_DATE"));
181 char *git_committer_info(void)
183 return get_ident(getenv("GIT_COMMITTER_NAME"), getenv("GIT_COMMITTER_EMAIL"), getenv("GIT_COMMITTER_DATE"));