4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
10 static char git_default_date[50];
12 static void copy_gecos(struct passwd *w, char *name, int sz)
17 nlen = strlen(w->pw_name);
19 /* Traditionally GECOS field had office phone numbers etc, separated
20 * with commas. Also & stands for capitalized form of the login name.
23 for (len = 0, dst = name, src = w->pw_gecos; len < sz; src++) {
27 if (ch == 0 || ch == ',')
32 if (len + nlen < sz) {
33 /* Sorry, Mr. McDonald... */
34 *dst++ = toupper(*w->pw_name);
35 memcpy(dst, w->pw_name + 1, nlen - 1);
42 die("Your parents must have hated you!");
49 struct passwd *pw = getpwuid(getuid());
52 die("You don't exist. Go away!");
54 /* Get the name ("gecos") */
55 copy_gecos(pw, git_default_name, sizeof(git_default_name));
57 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
58 len = strlen(pw->pw_name);
59 if (len > sizeof(git_default_email)/2)
60 die("Your sysadmin must hate you!");
61 memcpy(git_default_email, pw->pw_name, len);
62 git_default_email[len++] = '@';
63 gethostname(git_default_email + len, sizeof(git_default_email) - len);
64 if (!strchr(git_default_email+len, '.')) {
65 struct hostent *he = gethostbyname(git_default_email + len);
68 len = strlen(git_default_email);
69 git_default_email[len++] = '.';
70 if (he && (domainname = strchr(he->h_name, '.')))
71 strlcpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
73 strlcpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
75 /* And set the default date */
76 datestamp(git_default_date, sizeof(git_default_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 delimiters on
140 * a identification line
142 for (i = 0; i < len; i++) {
145 case '\n': case '<': case '>':
155 static const char au_env[] = "GIT_AUTHOR_NAME";
156 static const char co_env[] = "GIT_COMMITTER_NAME";
157 static const char *env_hint =
159 "*** Your name cannot be determined from your system services (gecos).\n"
163 " git repo-config user.email \"you@email.com\"\n"
164 " git repo-config user.name \"Your Name\"\n"
166 "To set the identity in this repository.\n"
167 "Add --global to set your account\'s default\n"
170 static const char *get_ident(const char *name, const char *email,
171 const char *date_str, int error_on_no_name)
173 static char buffer[1000];
178 name = git_default_name;
180 email = git_default_email;
183 if (name == git_default_name && env_hint) {
184 fprintf(stderr, env_hint, au_env, co_env);
185 env_hint = NULL; /* warn only once, for "git-var -l" */
187 if (error_on_no_name)
188 die("empty ident %s <%s> not allowed", name, email);
191 strcpy(date, git_default_date);
193 parse_date(date_str, date, sizeof(date));
195 i = copy(buffer, sizeof(buffer), 0, name);
196 i = add_raw(buffer, sizeof(buffer), i, " <");
197 i = copy(buffer, sizeof(buffer), i, email);
198 i = add_raw(buffer, sizeof(buffer), i, "> ");
199 i = copy(buffer, sizeof(buffer), i, date);
200 if (i >= sizeof(buffer))
201 die("Impossibly long personal identifier");
206 const char *git_author_info(int error_on_no_name)
208 return get_ident(getenv("GIT_AUTHOR_NAME"),
209 getenv("GIT_AUTHOR_EMAIL"),
210 getenv("GIT_AUTHOR_DATE"),
214 const char *git_committer_info(int error_on_no_name)
216 return get_ident(getenv("GIT_COMMITTER_NAME"),
217 getenv("GIT_COMMITTER_EMAIL"),
218 getenv("GIT_COMMITTER_DATE"),
222 void ignore_missing_committer_name()
224 /* If we did not get a name from the user's gecos entry then
225 * git_default_name is empty; so instead load the username
226 * into it as a 'good enough for now' approximation of who
229 if (!*git_default_name) {
230 struct passwd *pw = getpwuid(getuid());
232 die("You don't exist. Go away!");
233 strlcpy(git_default_name, pw->pw_name, sizeof(git_default_name));