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(const struct passwd *w, char *name, size_t 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);
43 die("Your parents must have hated you!");
47 static void copy_email(const struct passwd *pw)
50 * Make up a fake email address
51 * (name + '@' + hostname [+ '.' + domainname])
53 size_t len = strlen(pw->pw_name);
54 if (len > sizeof(git_default_email)/2)
55 die("Your sysadmin must hate you!");
56 memcpy(git_default_email, pw->pw_name, len);
57 git_default_email[len++] = '@';
58 gethostname(git_default_email + len, sizeof(git_default_email) - len);
59 if (!strchr(git_default_email+len, '.')) {
60 struct hostent *he = gethostbyname(git_default_email + len);
63 len = strlen(git_default_email);
64 git_default_email[len++] = '.';
65 if (he && (domainname = strchr(he->h_name, '.')))
66 strlcpy(git_default_email + len, domainname + 1,
67 sizeof(git_default_email) - len);
69 strlcpy(git_default_email + len, "(none)",
70 sizeof(git_default_email) - len);
74 static void setup_ident(void)
76 struct passwd *pw = NULL;
78 /* Get the name ("gecos") */
79 if (!git_default_name[0]) {
80 pw = getpwuid(getuid());
82 die("You don't exist. Go away!");
83 copy_gecos(pw, git_default_name, sizeof(git_default_name));
86 if (!git_default_email[0]) {
87 const char *email = getenv("EMAIL");
89 if (email && email[0]) {
90 strlcpy(git_default_email, email,
91 sizeof(git_default_email));
92 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
95 pw = getpwuid(getuid());
97 die("You don't exist. Go away!");
102 /* And set the default date */
103 if (!git_default_date[0])
104 datestamp(git_default_date, sizeof(git_default_date));
107 static int add_raw(char *buf, size_t size, int offset, const char *str)
109 size_t len = strlen(str);
110 if (offset + len > size)
112 memcpy(buf + offset, str, len);
116 static int crud(unsigned char c)
131 * Copy over a string to the destination, but avoid special
132 * characters ('\n', '<' and '>') and remove crud at the end
134 static int copy(char *buf, size_t size, int offset, const char *src)
139 /* Remove crud from the beginning.. */
140 while ((c = *src) != 0) {
146 /* Remove crud from the end.. */
156 * Copy the rest to the buffer, but avoid the special
157 * characters '\n' '<' and '>' that act as delimiters on
158 * an identification line
160 for (i = 0; i < len; i++) {
163 case '\n': case '<': case '>':
173 static const char *env_hint =
175 "*** Please tell me who you are.\n"
179 " git config --global user.email \"you@example.com\"\n"
180 " git config --global user.name \"Your Name\"\n"
182 "to set your account\'s default identity.\n"
183 "Omit --global to set the identity only in this repository.\n"
186 const char *fmt_ident(const char *name, const char *email,
187 const char *date_str, int flag)
189 static char buffer[1000];
192 int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME);
193 int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME);
194 int name_addr_only = (flag & IDENT_NO_DATE);
198 name = git_default_name;
200 email = git_default_email;
205 if ((warn_on_no_name || error_on_no_name) &&
206 name == git_default_name && env_hint) {
207 fputs(env_hint, stderr);
208 env_hint = NULL; /* warn only once */
210 if (error_on_no_name)
211 die("empty ident %s <%s> not allowed", name, email);
212 pw = getpwuid(getuid());
214 die("You don't exist. Go away!");
215 strlcpy(git_default_name, pw->pw_name,
216 sizeof(git_default_name));
217 name = git_default_name;
220 strcpy(date, git_default_date);
221 if (!name_addr_only && date_str && date_str[0]) {
222 if (parse_date(date_str, date, sizeof(date)) < 0)
223 die("invalid date format: %s", date_str);
226 i = copy(buffer, sizeof(buffer), 0, name);
227 i = add_raw(buffer, sizeof(buffer), i, " <");
228 i = copy(buffer, sizeof(buffer), i, email);
229 if (!name_addr_only) {
230 i = add_raw(buffer, sizeof(buffer), i, "> ");
231 i = copy(buffer, sizeof(buffer), i, date);
233 i = add_raw(buffer, sizeof(buffer), i, ">");
235 if (i >= sizeof(buffer))
236 die("Impossibly long personal identifier");
241 const char *fmt_name(const char *name, const char *email)
243 return fmt_ident(name, email, NULL, IDENT_ERROR_ON_NO_NAME | IDENT_NO_DATE);
246 const char *git_author_info(int flag)
248 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
249 getenv("GIT_AUTHOR_EMAIL"),
250 getenv("GIT_AUTHOR_DATE"),
254 const char *git_committer_info(int flag)
256 if (getenv("GIT_COMMITTER_NAME"))
257 user_ident_explicitly_given |= IDENT_NAME_GIVEN;
258 if (getenv("GIT_COMMITTER_EMAIL"))
259 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
260 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
261 getenv("GIT_COMMITTER_EMAIL"),
262 getenv("GIT_COMMITTER_DATE"),
266 int user_ident_sufficiently_given(void)
269 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
271 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);