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);
42 die("Your parents must have hated you!");
46 static void copy_email(const struct passwd
*pw
)
49 * Make up a fake email address
50 * (name + '@' + hostname [+ '.' + domainname])
52 size_t len
= strlen(pw
->pw_name
);
53 if (len
> sizeof(git_default_email
)/2)
54 die("Your sysadmin must hate you!");
55 memcpy(git_default_email
, pw
->pw_name
, len
);
56 git_default_email
[len
++] = '@';
57 gethostname(git_default_email
+ len
, sizeof(git_default_email
) - len
);
58 if (!strchr(git_default_email
+len
, '.')) {
59 struct hostent
*he
= gethostbyname(git_default_email
+ len
);
62 len
= strlen(git_default_email
);
63 git_default_email
[len
++] = '.';
64 if (he
&& (domainname
= strchr(he
->h_name
, '.')))
65 strlcpy(git_default_email
+ len
, domainname
+ 1,
66 sizeof(git_default_email
) - len
);
68 strlcpy(git_default_email
+ len
, "(none)",
69 sizeof(git_default_email
) - len
);
73 static void setup_ident(void)
75 struct passwd
*pw
= NULL
;
77 /* Get the name ("gecos") */
78 if (!git_default_name
[0]) {
79 pw
= getpwuid(getuid());
81 die("You don't exist. Go away!");
82 copy_gecos(pw
, git_default_name
, sizeof(git_default_name
));
85 if (!git_default_email
[0]) {
86 const char *email
= getenv("EMAIL");
88 if (email
&& email
[0])
89 strlcpy(git_default_email
, email
,
90 sizeof(git_default_email
));
93 pw
= getpwuid(getuid());
95 die("You don't exist. Go away!");
100 /* And set the default date */
101 if (!git_default_date
[0])
102 datestamp(git_default_date
, sizeof(git_default_date
));
105 static int add_raw(char *buf
, size_t size
, int offset
, const char *str
)
107 size_t len
= strlen(str
);
108 if (offset
+ len
> size
)
110 memcpy(buf
+ offset
, str
, len
);
114 static int crud(unsigned char c
)
129 * Copy over a string to the destination, but avoid special
130 * characters ('\n', '<' and '>') and remove crud at the end
132 static int copy(char *buf
, size_t size
, int offset
, const char *src
)
137 /* Remove crud from the beginning.. */
138 while ((c
= *src
) != 0) {
144 /* Remove crud from the end.. */
154 * Copy the rest to the buffer, but avoid the special
155 * characters '\n' '<' and '>' that act as delimiters on
156 * an identification line
158 for (i
= 0; i
< len
; i
++) {
161 case '\n': case '<': case '>':
171 static const char *env_hint
=
173 "*** Please tell me who you are.\n"
177 " git config --global user.email \"you@example.com\"\n"
178 " git config --global user.name \"Your Name\"\n"
180 "to set your account\'s default identity.\n"
181 "Omit --global to set the identity only in this repository.\n"
184 const char *fmt_ident(const char *name
, const char *email
,
185 const char *date_str
, int flag
)
187 static char buffer
[1000];
190 int error_on_no_name
= (flag
& IDENT_ERROR_ON_NO_NAME
);
191 int warn_on_no_name
= (flag
& IDENT_WARN_ON_NO_NAME
);
192 int name_addr_only
= (flag
& IDENT_NO_DATE
);
196 name
= git_default_name
;
198 email
= git_default_email
;
203 if ((warn_on_no_name
|| error_on_no_name
) &&
204 name
== git_default_name
&& env_hint
) {
205 fprintf(stderr
, env_hint
);
206 env_hint
= NULL
; /* warn only once */
208 if (error_on_no_name
)
209 die("empty ident %s <%s> not allowed", name
, email
);
210 pw
= getpwuid(getuid());
212 die("You don't exist. Go away!");
213 strlcpy(git_default_name
, pw
->pw_name
,
214 sizeof(git_default_name
));
215 name
= git_default_name
;
218 strcpy(date
, git_default_date
);
219 if (!name_addr_only
&& date_str
)
220 parse_date(date_str
, date
, sizeof(date
));
222 i
= copy(buffer
, sizeof(buffer
), 0, name
);
223 i
= add_raw(buffer
, sizeof(buffer
), i
, " <");
224 i
= copy(buffer
, sizeof(buffer
), i
, email
);
225 if (!name_addr_only
) {
226 i
= add_raw(buffer
, sizeof(buffer
), i
, "> ");
227 i
= copy(buffer
, sizeof(buffer
), i
, date
);
229 i
= add_raw(buffer
, sizeof(buffer
), i
, ">");
231 if (i
>= sizeof(buffer
))
232 die("Impossibly long personal identifier");
237 const char *fmt_name(const char *name
, const char *email
)
239 return fmt_ident(name
, email
, NULL
, IDENT_ERROR_ON_NO_NAME
| IDENT_NO_DATE
);
242 const char *git_author_info(int flag
)
244 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
245 getenv("GIT_AUTHOR_EMAIL"),
246 getenv("GIT_AUTHOR_DATE"),
250 const char *git_committer_info(int flag
)
252 if (getenv("GIT_COMMITTER_NAME") &&
253 getenv("GIT_COMMITTER_EMAIL"))
254 user_ident_explicitly_given
= 1;
255 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
256 getenv("GIT_COMMITTER_EMAIL"),
257 getenv("GIT_COMMITTER_DATE"),