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];
21 struct passwd *pw = getpwuid(getuid());
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));
50 static int add_raw(char *buf, int size, int offset, const char *str)
52 int len = strlen(str);
53 if (offset + len > size)
55 memcpy(buf + offset, str, 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) {
67 for (k = 0; k <= 31; ++k) crud_array[k] = 1;
77 crud_array_initialized = 1;
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)
91 /* Remove crud from the beginning.. */
92 while ((c = *src) != 0) {
98 /* Remove crud from the end.. */
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++) {
115 case '\n': case '<': case '>':
125 char *get_ident(const char *name, const char *email, const char *date_str)
127 static char buffer[1000];
135 strcpy(date, real_date);
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");
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"));