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 #ifdef NO_GECOS_IN_PWENT
13 #define get_gecos(ignored) "&"
15 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
18 static void copy_gecos(const struct passwd *w, char *name, size_t sz)
23 nlen = strlen(w->pw_name);
25 /* Traditionally GECOS field had office phone numbers etc, separated
26 * with commas. Also & stands for capitalized form of the login name.
29 for (len = 0, dst = name, src = get_gecos(w); len < sz; src++) {
33 if (ch == 0 || ch == ',')
38 if (len + nlen < sz) {
39 /* Sorry, Mr. McDonald... */
40 *dst++ = toupper(*w->pw_name);
41 memcpy(dst, w->pw_name + 1, nlen - 1);
49 die("Your parents must have hated you!");
53 static int add_mailname_host(char *buf, size_t len)
57 mailname = fopen("/etc/mailname", "r");
60 warning("cannot open /etc/mailname: %s",
64 if (!fgets(buf, len, mailname)) {
66 warning("cannot read /etc/mailname: %s",
76 static void add_domainname(char *buf, size_t len)
80 const char *domainname;
82 if (gethostname(buf, len)) {
83 warning("cannot get host name: %s", strerror(errno));
84 strlcpy(buf, "(none)", len);
87 namelen = strlen(buf);
88 if (memchr(buf, '.', namelen))
91 he = gethostbyname(buf);
95 if (he && (domainname = strchr(he->h_name, '.')))
96 strlcpy(buf, domainname + 1, len);
98 strlcpy(buf, "(none)", len);
101 static void copy_email(const struct passwd *pw)
104 * Make up a fake email address
105 * (name + '@' + hostname [+ '.' + domainname])
107 size_t len = strlen(pw->pw_name);
108 if (len > sizeof(git_default_email)/2)
109 die("Your sysadmin must hate you!");
110 memcpy(git_default_email, pw->pw_name, len);
111 git_default_email[len++] = '@';
113 if (!add_mailname_host(git_default_email + len,
114 sizeof(git_default_email) - len))
115 return; /* read from "/etc/mailname" (Debian) */
116 add_domainname(git_default_email + len,
117 sizeof(git_default_email) - len);
120 static void setup_ident(const char **name, const char **emailp)
122 struct passwd *pw = NULL;
124 /* Get the name ("gecos") */
125 if (!*name && !git_default_name[0]) {
126 pw = getpwuid(getuid());
128 die("You don't exist. Go away!");
129 copy_gecos(pw, git_default_name, sizeof(git_default_name));
132 *name = git_default_name;
134 if (!*emailp && !git_default_email[0]) {
135 const char *email = getenv("EMAIL");
137 if (email && email[0]) {
138 strlcpy(git_default_email, email,
139 sizeof(git_default_email));
140 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
143 pw = getpwuid(getuid());
145 die("You don't exist. Go away!");
150 *emailp = git_default_email;
152 /* And set the default date */
153 if (!git_default_date[0])
154 datestamp(git_default_date, sizeof(git_default_date));
157 static int add_raw(char *buf, size_t size, int offset, const char *str)
159 size_t len = strlen(str);
160 if (offset + len > size)
162 memcpy(buf + offset, str, len);
166 static int crud(unsigned char c)
181 * Copy over a string to the destination, but avoid special
182 * characters ('\n', '<' and '>') and remove crud at the end
184 static int copy(char *buf, size_t size, int offset, const char *src)
189 /* Remove crud from the beginning.. */
190 while ((c = *src) != 0) {
196 /* Remove crud from the end.. */
206 * Copy the rest to the buffer, but avoid the special
207 * characters '\n' '<' and '>' that act as delimiters on
208 * an identification line
210 for (i = 0; i < len; i++) {
213 case '\n': case '<': case '>':
224 * Reverse of fmt_ident(); given an ident line, split the fields
225 * to allow the caller to parse it.
226 * Signal a success by returning 0, but date/tz fields of the result
227 * can still be NULL if the input line only has the name/email part
228 * (e.g. reading from a reflog entry).
230 int split_ident_line(struct ident_split *split, const char *line, int len)
236 memset(split, 0, sizeof(*split));
238 split->name_begin = line;
239 for (cp = line; *cp && cp < line + len; cp++)
241 split->mail_begin = cp + 1;
244 if (!split->mail_begin)
247 for (cp = split->mail_begin - 2; line < cp; cp--)
249 split->name_end = cp + 1;
252 if (!split->name_end)
255 for (cp = split->mail_begin; cp < line + len; cp++)
257 split->mail_end = cp;
260 if (!split->mail_end)
263 for (cp = split->mail_end + 1; cp < line + len && isspace(*cp); cp++)
265 if (line + len <= cp)
267 split->date_begin = cp;
268 span = strspn(cp, "0123456789");
271 split->date_end = split->date_begin + span;
272 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
274 if (line + len <= cp || (*cp != '+' && *cp != '-'))
276 split->tz_begin = cp;
277 span = strspn(cp + 1, "0123456789");
280 split->tz_end = split->tz_begin + 1 + span;
284 split->date_begin = NULL;
285 split->date_end = NULL;
286 split->tz_begin = NULL;
287 split->tz_end = NULL;
291 static const char *env_hint =
293 "*** Please tell me who you are.\n"
297 " git config --global user.email \"you@example.com\"\n"
298 " git config --global user.name \"Your Name\"\n"
300 "to set your account\'s default identity.\n"
301 "Omit --global to set the identity only in this repository.\n"
304 const char *fmt_ident(const char *name, const char *email,
305 const char *date_str, int flag)
307 static char buffer[1000];
310 int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME);
311 int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME);
312 int name_addr_only = (flag & IDENT_NO_DATE);
314 setup_ident(&name, &email);
319 if ((warn_on_no_name || error_on_no_name) &&
320 name == git_default_name && env_hint) {
321 fputs(env_hint, stderr);
322 env_hint = NULL; /* warn only once */
324 if (error_on_no_name)
325 die("empty ident %s <%s> not allowed", name, email);
326 pw = getpwuid(getuid());
328 die("You don't exist. Go away!");
329 strlcpy(git_default_name, pw->pw_name,
330 sizeof(git_default_name));
331 name = git_default_name;
334 strcpy(date, git_default_date);
335 if (!name_addr_only && date_str && date_str[0]) {
336 if (parse_date(date_str, date, sizeof(date)) < 0)
337 die("invalid date format: %s", date_str);
340 i = copy(buffer, sizeof(buffer), 0, name);
341 i = add_raw(buffer, sizeof(buffer), i, " <");
342 i = copy(buffer, sizeof(buffer), i, email);
343 if (!name_addr_only) {
344 i = add_raw(buffer, sizeof(buffer), i, "> ");
345 i = copy(buffer, sizeof(buffer), i, date);
347 i = add_raw(buffer, sizeof(buffer), i, ">");
349 if (i >= sizeof(buffer))
350 die("Impossibly long personal identifier");
355 const char *fmt_name(const char *name, const char *email)
357 return fmt_ident(name, email, NULL, IDENT_ERROR_ON_NO_NAME | IDENT_NO_DATE);
360 const char *git_author_info(int flag)
362 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
363 getenv("GIT_AUTHOR_EMAIL"),
364 getenv("GIT_AUTHOR_DATE"),
368 const char *git_committer_info(int flag)
370 if (getenv("GIT_COMMITTER_NAME"))
371 user_ident_explicitly_given |= IDENT_NAME_GIVEN;
372 if (getenv("GIT_COMMITTER_EMAIL"))
373 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
374 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
375 getenv("GIT_COMMITTER_EMAIL"),
376 getenv("GIT_COMMITTER_DATE"),
380 int user_ident_sufficiently_given(void)
383 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
385 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);