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 void copy_email(const struct passwd
*pw
)
56 * Make up a fake email address
57 * (name + '@' + hostname [+ '.' + domainname])
59 size_t len
= strlen(pw
->pw_name
);
60 if (len
> sizeof(git_default_email
)/2)
61 die("Your sysadmin must hate you!");
62 memcpy(git_default_email
, pw
->pw_name
, len
);
63 git_default_email
[len
++] = '@';
64 gethostname(git_default_email
+ len
, sizeof(git_default_email
) - len
);
65 if (!strchr(git_default_email
+len
, '.')) {
66 struct hostent
*he
= gethostbyname(git_default_email
+ len
);
69 len
= strlen(git_default_email
);
70 git_default_email
[len
++] = '.';
71 if (he
&& (domainname
= strchr(he
->h_name
, '.')))
72 strlcpy(git_default_email
+ len
, domainname
+ 1,
73 sizeof(git_default_email
) - len
);
75 strlcpy(git_default_email
+ len
, "(none)",
76 sizeof(git_default_email
) - len
);
80 static void setup_ident(void)
82 struct passwd
*pw
= NULL
;
84 /* Get the name ("gecos") */
85 if (!git_default_name
[0]) {
86 pw
= getpwuid(getuid());
88 die("You don't exist. Go away!");
89 copy_gecos(pw
, git_default_name
, sizeof(git_default_name
));
92 if (!git_default_email
[0]) {
93 const char *email
= getenv("EMAIL");
95 if (email
&& email
[0]) {
96 strlcpy(git_default_email
, email
,
97 sizeof(git_default_email
));
98 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
101 pw
= getpwuid(getuid());
103 die("You don't exist. Go away!");
108 /* And set the default date */
109 if (!git_default_date
[0])
110 datestamp(git_default_date
, sizeof(git_default_date
));
113 static int add_raw(char *buf
, size_t size
, int offset
, const char *str
)
115 size_t len
= strlen(str
);
116 if (offset
+ len
> size
)
118 memcpy(buf
+ offset
, str
, len
);
122 static int crud(unsigned char c
)
137 * Copy over a string to the destination, but avoid special
138 * characters ('\n', '<' and '>') and remove crud at the end
140 static int copy(char *buf
, size_t size
, int offset
, const char *src
)
145 /* Remove crud from the beginning.. */
146 while ((c
= *src
) != 0) {
152 /* Remove crud from the end.. */
162 * Copy the rest to the buffer, but avoid the special
163 * characters '\n' '<' and '>' that act as delimiters on
164 * an identification line
166 for (i
= 0; i
< len
; i
++) {
169 case '\n': case '<': case '>':
179 static const char *env_hint
=
181 "*** Please tell me who you are.\n"
185 " git config --global user.email \"you@example.com\"\n"
186 " git config --global user.name \"Your Name\"\n"
188 "to set your account\'s default identity.\n"
189 "Omit --global to set the identity only in this repository.\n"
192 const char *fmt_ident(const char *name
, const char *email
,
193 const char *date_str
, int flag
)
195 static char buffer
[1000];
198 int error_on_no_name
= (flag
& IDENT_ERROR_ON_NO_NAME
);
199 int warn_on_no_name
= (flag
& IDENT_WARN_ON_NO_NAME
);
200 int name_addr_only
= (flag
& IDENT_NO_DATE
);
204 name
= git_default_name
;
206 email
= git_default_email
;
211 if ((warn_on_no_name
|| error_on_no_name
) &&
212 name
== git_default_name
&& env_hint
) {
213 fputs(env_hint
, stderr
);
214 env_hint
= NULL
; /* warn only once */
216 if (error_on_no_name
)
217 die("empty ident %s <%s> not allowed", name
, email
);
218 pw
= getpwuid(getuid());
220 die("You don't exist. Go away!");
221 strlcpy(git_default_name
, pw
->pw_name
,
222 sizeof(git_default_name
));
223 name
= git_default_name
;
226 strcpy(date
, git_default_date
);
227 if (!name_addr_only
&& date_str
&& date_str
[0]) {
228 if (parse_date(date_str
, date
, sizeof(date
)) < 0)
229 die("invalid date format: %s", date_str
);
232 i
= copy(buffer
, sizeof(buffer
), 0, name
);
233 i
= add_raw(buffer
, sizeof(buffer
), i
, " <");
234 i
= copy(buffer
, sizeof(buffer
), i
, email
);
235 if (!name_addr_only
) {
236 i
= add_raw(buffer
, sizeof(buffer
), i
, "> ");
237 i
= copy(buffer
, sizeof(buffer
), i
, date
);
239 i
= add_raw(buffer
, sizeof(buffer
), i
, ">");
241 if (i
>= sizeof(buffer
))
242 die("Impossibly long personal identifier");
247 const char *fmt_name(const char *name
, const char *email
)
249 return fmt_ident(name
, email
, NULL
, IDENT_ERROR_ON_NO_NAME
| IDENT_NO_DATE
);
252 const char *git_author_info(int flag
)
254 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
255 getenv("GIT_AUTHOR_EMAIL"),
256 getenv("GIT_AUTHOR_DATE"),
260 const char *git_committer_info(int flag
)
262 if (getenv("GIT_COMMITTER_NAME"))
263 user_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
264 if (getenv("GIT_COMMITTER_EMAIL"))
265 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
266 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
267 getenv("GIT_COMMITTER_EMAIL"),
268 getenv("GIT_COMMITTER_DATE"),
272 int user_ident_sufficiently_given(void)
275 return (user_ident_explicitly_given
& IDENT_MAIL_GIVEN
);
277 return (user_ident_explicitly_given
== IDENT_ALL_GIVEN
);