4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
10 static char git_default_date
[50];
14 static void copy_gecos(const struct passwd
*w
, char *name
, size_t sz
)
19 nlen
= strlen(w
->pw_name
);
21 /* Traditionally GECOS field had office phone numbers etc, separated
22 * with commas. Also & stands for capitalized form of the login name.
25 for (len
= 0, dst
= name
, src
= w
->pw_gecos
; len
< sz
; src
++) {
29 if (ch
== 0 || ch
== ',')
34 if (len
+ nlen
< sz
) {
35 /* Sorry, Mr. McDonald... */
36 *dst
++ = toupper(*w
->pw_name
);
37 memcpy(dst
, w
->pw_name
+ 1, nlen
- 1);
44 die("Your parents must have hated you!");
48 static void copy_email(const struct passwd
*pw
)
51 * Make up a fake email address
52 * (name + '@' + hostname [+ '.' + domainname])
54 size_t len
= strlen(pw
->pw_name
);
55 if (len
> sizeof(git_default_email
)/2)
56 die("Your sysadmin must hate you!");
57 memcpy(git_default_email
, pw
->pw_name
, len
);
58 git_default_email
[len
++] = '@';
59 gethostname(git_default_email
+ len
, sizeof(git_default_email
) - len
);
60 if (!strchr(git_default_email
+len
, '.')) {
61 struct hostent
*he
= gethostbyname(git_default_email
+ len
);
64 len
= strlen(git_default_email
);
65 git_default_email
[len
++] = '.';
66 if (he
&& (domainname
= strchr(he
->h_name
, '.')))
67 strlcpy(git_default_email
+ len
, domainname
+ 1,
68 sizeof(git_default_email
) - len
);
70 strlcpy(git_default_email
+ len
, "(none)",
71 sizeof(git_default_email
) - len
);
77 static void setup_ident(void)
80 struct passwd
*pw
= NULL
;
82 /* Get the name ("gecos") */
83 if (!git_default_name
[0]) {
84 pw
= getpwuid(getuid());
86 die("You don't exist. Go away!");
87 copy_gecos(pw
, git_default_name
, sizeof(git_default_name
));
90 if (!git_default_email
[0]) {
92 pw
= getpwuid(getuid());
94 die("You don't exist. Go away!");
99 /* And set the default date */
100 if (!git_default_date
[0])
101 datestamp(git_default_date
, sizeof(git_default_date
));
104 static int add_raw(char *buf
, size_t size
, int offset
, const char *str
)
106 size_t len
= strlen(str
);
107 if (offset
+ len
> size
)
109 memcpy(buf
+ offset
, str
, len
);
113 static int crud(unsigned char c
)
115 static char crud_array
[256];
116 static int crud_array_initialized
= 0;
118 if (!crud_array_initialized
) {
121 for (k
= 0; k
<= 31; ++k
) crud_array
[k
] = 1;
130 crud_array
['\''] = 1;
131 crud_array_initialized
= 1;
133 return crud_array
[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 * a identification line
166 for (i
= 0; i
< len
; i
++) {
169 case '\n': case '<': case '>':
179 static const char au_env
[] = "GIT_AUTHOR_NAME";
180 static const char co_env
[] = "GIT_COMMITTER_NAME";
181 static const char *env_hint
=
183 "*** Your name cannot be determined from your system services (gecos).\n"
187 " git config user.email \"you@email.com\"\n"
188 " git config user.name \"Your Name\"\n"
190 "To set the identity in this repository.\n"
191 "Add --global to set your account\'s default\n"
194 const char *fmt_ident(const char *name
, const char *email
,
195 const char *date_str
, int error_on_no_name
)
197 static char buffer
[1000];
203 name
= git_default_name
;
205 email
= git_default_email
;
207 email
= getenv("EMAIL");
210 #ifndef NO_ETC_PASSWD
214 if (0 <= error_on_no_name
&&
215 name
== git_default_name
&& env_hint
) {
216 fprintf(stderr
, env_hint
, au_env
, co_env
);
217 env_hint
= NULL
; /* warn only once, for "git-var -l" */
219 if (0 < error_on_no_name
)
220 die("empty ident %s <%s> not allowed", name
, email
);
221 #ifndef NO_ETC_PASSWD
222 pw
= getpwuid(getuid());
224 die("You don't exist. Go away!");
225 strlcpy(git_default_name
, pw
->pw_name
,
226 sizeof(git_default_name
));
228 strlcpy(git_default_name
, "unknown",
229 sizeof(git_default_name
));
231 name
= git_default_name
;
234 strcpy(date
, git_default_date
);
236 parse_date(date_str
, date
, sizeof(date
));
238 i
= copy(buffer
, sizeof(buffer
), 0, name
);
239 i
= add_raw(buffer
, sizeof(buffer
), i
, " <");
240 i
= copy(buffer
, sizeof(buffer
), i
, email
);
241 i
= add_raw(buffer
, sizeof(buffer
), i
, "> ");
242 i
= copy(buffer
, sizeof(buffer
), i
, date
);
243 if (i
>= sizeof(buffer
))
244 die("Impossibly long personal identifier");
249 const char *git_author_info(int error_on_no_name
)
251 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
252 getenv("GIT_AUTHOR_EMAIL"),
253 getenv("GIT_AUTHOR_DATE"),
257 const char *git_committer_info(int error_on_no_name
)
259 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
260 getenv("GIT_COMMITTER_EMAIL"),
261 getenv("GIT_COMMITTER_DATE"),