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(struct passwd
*w
, char *name
, int 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!");
51 struct passwd
*pw
= getpwuid(getuid());
54 die("You don't exist. Go away!");
56 /* Get the name ("gecos") */
57 copy_gecos(pw
, git_default_name
, sizeof(git_default_name
));
59 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
60 len
= strlen(pw
->pw_name
);
61 if (len
> sizeof(git_default_email
)/2)
62 die("Your sysadmin must hate you!");
63 memcpy(git_default_email
, pw
->pw_name
, len
);
64 git_default_email
[len
++] = '@';
65 gethostname(git_default_email
+ len
, sizeof(git_default_email
) - len
);
66 if (!strchr(git_default_email
+len
, '.')) {
67 struct hostent
*he
= gethostbyname(git_default_email
+ len
);
70 len
= strlen(git_default_email
);
71 git_default_email
[len
++] = '.';
72 if (he
&& (domainname
= strchr(he
->h_name
, '.')))
73 strlcpy(git_default_email
+ len
, domainname
+ 1, sizeof(git_default_email
) - len
);
75 strlcpy(git_default_email
+ len
, "(none)", sizeof(git_default_email
) - len
);
77 /* And set the default date */
78 datestamp(git_default_date
, sizeof(git_default_date
));
82 #else /* NO_ETC_PASSWD */
84 static int add_raw(char *buf
, int size
, int offset
, const char *str
)
86 int len
= strlen(str
);
87 if (offset
+ len
> size
)
89 memcpy(buf
+ offset
, str
, len
);
93 static int crud(unsigned char c
)
95 static char crud_array
[256];
96 static int crud_array_initialized
= 0;
98 if (!crud_array_initialized
) {
101 for (k
= 0; k
<= 31; ++k
) crud_array
[k
] = 1;
110 crud_array
['\''] = 1;
111 crud_array_initialized
= 1;
113 return crud_array
[c
];
117 * Copy over a string to the destination, but avoid special
118 * characters ('\n', '<' and '>') and remove crud at the end
120 static int copy(char *buf
, int size
, int offset
, const char *src
)
125 /* Remove crud from the beginning.. */
126 while ((c
= *src
) != 0) {
132 /* Remove crud from the end.. */
142 * Copy the rest to the buffer, but avoid the special
143 * characters '\n' '<' and '>' that act as delimiters on
144 * a identification line
146 for (i
= 0; i
< len
; i
++) {
149 case '\n': case '<': case '>':
159 int setup_ident(void)
166 static const char au_env
[] = "GIT_AUTHOR_NAME";
167 static const char co_env
[] = "GIT_COMMITTER_NAME";
168 static const char *env_hint
=
170 "*** Your name cannot be determined from your system services (gecos).\n"
174 " git repo-config user.email \"you@email.com\"\n"
175 " git repo-config user.name \"Your Name\"\n"
177 "To set the identity in this repository.\n"
178 "Add --global to set your account\'s default\n"
181 static const char *get_ident(const char *name
, const char *email
,
182 const char *date_str
, int error_on_no_name
)
184 static char buffer
[1000];
189 name
= git_default_name
;
191 email
= git_default_email
;
194 if (name
== git_default_name
&& env_hint
) {
195 fprintf(stderr
, env_hint
, au_env
, co_env
);
196 env_hint
= NULL
; /* warn only once, for "git-var -l" */
198 if (error_on_no_name
)
199 die("empty ident %s <%s> not allowed", name
, email
);
202 strcpy(date
, git_default_date
);
204 parse_date(date_str
, date
, sizeof(date
));
206 i
= copy(buffer
, sizeof(buffer
), 0, name
);
207 i
= add_raw(buffer
, sizeof(buffer
), i
, " <");
208 i
= copy(buffer
, sizeof(buffer
), i
, email
);
209 i
= add_raw(buffer
, sizeof(buffer
), i
, "> ");
210 i
= copy(buffer
, sizeof(buffer
), i
, date
);
211 if (i
>= sizeof(buffer
))
212 die("Impossibly long personal identifier");
217 const char *git_author_info(int error_on_no_name
)
219 return get_ident(getenv("GIT_AUTHOR_NAME"),
220 getenv("GIT_AUTHOR_EMAIL"),
221 getenv("GIT_AUTHOR_DATE"),
225 const char *git_committer_info(int error_on_no_name
)
227 return get_ident(getenv("GIT_COMMITTER_NAME"),
228 getenv("GIT_COMMITTER_EMAIL"),
229 getenv("GIT_COMMITTER_DATE"),
233 #ifndef NO_ETC_PASSWD
235 void ignore_missing_committer_name()
237 /* If we did not get a name from the user's gecos entry then
238 * git_default_name is empty; so instead load the username
239 * into it as a 'good enough for now' approximation of who
242 if (!*git_default_name
) {
243 struct passwd
*pw
= getpwuid(getuid());
245 die("You don't exist. Go away!");
246 strlcpy(git_default_name
, pw
->pw_name
, sizeof(git_default_name
));
252 void ignore_missing_committer_name()
254 strcpy(git_default_name
, "unknown");