4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
14 static char git_default_date[50];
16 static void copy_gecos(struct passwd *w, char *name, int sz)
21 nlen = strlen(w->pw_name);
23 /* Traditionally GECOS field had office phone numbers etc, separated
24 * with commas. Also & stands for capitalized form of the login name.
27 for (len = 0, dst = name, src = w->pw_gecos; len < sz; src++) {
31 if (ch == 0 || ch == ',')
36 if (len + nlen < sz) {
37 /* Sorry, Mr. McDonald... */
38 *dst++ = toupper(*w->pw_name);
39 memcpy(dst, w->pw_name + 1, nlen - 1);
46 die("Your parents must have hated you!");
53 struct passwd *pw = getpwuid(getuid());
56 die("You don't exist. Go away!");
58 /* Get the name ("gecos") */
59 copy_gecos(pw, git_default_name, sizeof(git_default_name));
61 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
62 len = strlen(pw->pw_name);
63 if (len > sizeof(git_default_email)/2)
64 die("Your sysadmin must hate you!");
65 memcpy(git_default_email, pw->pw_name, len);
66 git_default_email[len++] = '@';
67 gethostname(git_default_email + len, sizeof(git_default_email) - len);
68 if (!strchr(git_default_email+len, '.')) {
69 len = strlen(git_default_email);
70 git_default_email[len++] = '.';
71 getdomainname(git_default_email+len, sizeof(git_default_email)-len);
73 /* And set the default date */
74 datestamp(git_default_date, sizeof(git_default_date));
78 static int add_raw(char *buf, int size, int offset, const char *str)
80 int len = strlen(str);
81 if (offset + len > size)
83 memcpy(buf + offset, str, len);
87 static int crud(unsigned char c)
89 static char crud_array[256];
90 static int crud_array_initialized = 0;
92 if (!crud_array_initialized) {
95 for (k = 0; k <= 31; ++k) crud_array[k] = 1;
104 crud_array['\''] = 1;
105 crud_array_initialized = 1;
107 return crud_array[c];
111 * Copy over a string to the destination, but avoid special
112 * characters ('\n', '<' and '>') and remove crud at the end
114 static int copy(char *buf, int size, int offset, const char *src)
119 /* Remove crud from the beginning.. */
120 while ((c = *src) != 0) {
126 /* Remove crud from the end.. */
136 * Copy the rest to the buffer, but avoid the special
137 * characters '\n' '<' and '>' that act as delimeters on
138 * a identification line
140 for (i = 0; i < len; i++) {
143 case '\n': case '<': case '>':
153 char *get_ident(const char *name, const char *email, const char *date_str)
155 static char buffer[1000];
160 name = git_default_name;
162 email = git_default_email;
163 strcpy(date, git_default_date);
165 parse_date(date_str, date, sizeof(date));
167 i = copy(buffer, sizeof(buffer), 0, name);
168 i = add_raw(buffer, sizeof(buffer), i, " <");
169 i = copy(buffer, sizeof(buffer), i, email);
170 i = add_raw(buffer, sizeof(buffer), i, "> ");
171 i = copy(buffer, sizeof(buffer), i, date);
172 if (i >= sizeof(buffer))
173 die("Impossibly long personal identifier");
178 char *git_author_info(void)
180 return get_ident(getenv("GIT_AUTHOR_NAME"), getenv("GIT_AUTHOR_EMAIL"), getenv("GIT_AUTHOR_DATE"));
183 char *git_committer_info(void)
185 return get_ident(getenv("GIT_COMMITTER_NAME"), getenv("GIT_COMMITTER_EMAIL"), getenv("GIT_COMMITTER_DATE"));