4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
13 static char git_default_date
[50];
15 static void copy_gecos(struct passwd
*w
, char *name
, int sz
)
20 nlen
= strlen(w
->pw_name
);
22 /* Traditionally GECOS field had office phone numbers etc, separated
23 * with commas. Also & stands for capitalized form of the login name.
26 for (len
= 0, dst
= name
, src
= w
->pw_gecos
; len
< sz
; src
++) {
30 if (ch
== 0 || ch
== ',')
35 if (len
+ nlen
< sz
) {
36 /* Sorry, Mr. McDonald... */
37 *dst
++ = toupper(*w
->pw_name
);
38 memcpy(dst
, w
->pw_name
+ 1, nlen
- 1);
45 die("Your parents must have hated you!");
52 struct passwd
*pw
= getpwuid(getuid());
55 die("You don't exist. Go away!");
57 /* Get the name ("gecos") */
58 copy_gecos(pw
, git_default_name
, sizeof(git_default_name
));
60 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
61 len
= strlen(pw
->pw_name
);
62 if (len
> sizeof(git_default_email
)/2)
63 die("Your sysadmin must hate you!");
64 memcpy(git_default_email
, pw
->pw_name
, len
);
65 git_default_email
[len
++] = '@';
66 gethostname(git_default_email
+ len
, sizeof(git_default_email
) - len
);
67 if (!strchr(git_default_email
+len
, '.')) {
68 struct hostent
*he
= gethostbyname(git_default_email
+ len
);
71 len
= strlen(git_default_email
);
72 git_default_email
[len
++] = '.';
73 if (he
&& (domainname
= strchr(he
->h_name
, '.')))
74 strlcpy(git_default_email
+ len
, domainname
+ 1, sizeof(git_default_email
) - len
);
76 strlcpy(git_default_email
+ len
, "(none)", sizeof(git_default_email
) - len
);
78 /* And set the default date */
79 datestamp(git_default_date
, sizeof(git_default_date
));
83 static int add_raw(char *buf
, int size
, int offset
, const char *str
)
85 int len
= strlen(str
);
86 if (offset
+ len
> size
)
88 memcpy(buf
+ offset
, str
, len
);
92 static int crud(unsigned char c
)
94 static char crud_array
[256];
95 static int crud_array_initialized
= 0;
97 if (!crud_array_initialized
) {
100 for (k
= 0; k
<= 31; ++k
) crud_array
[k
] = 1;
109 crud_array
['\''] = 1;
110 crud_array_initialized
= 1;
112 return crud_array
[c
];
116 * Copy over a string to the destination, but avoid special
117 * characters ('\n', '<' and '>') and remove crud at the end
119 static int copy(char *buf
, int size
, int offset
, const char *src
)
124 /* Remove crud from the beginning.. */
125 while ((c
= *src
) != 0) {
131 /* Remove crud from the end.. */
141 * Copy the rest to the buffer, but avoid the special
142 * characters '\n' '<' and '>' that act as delimiters on
143 * a identification line
145 for (i
= 0; i
< len
; i
++) {
148 case '\n': case '<': case '>':
158 static const char au_env
[] = "GIT_AUTHOR_NAME";
159 static const char co_env
[] = "GIT_COMMITTER_NAME";
160 static const char *env_hint
=
162 "*** Your name cannot be determined from your system services (gecos).\n"
166 " git repo-config user.email \"you@email.com\"\n"
167 " git repo-config user.name \"Your Name\"\n"
169 "To set the identity in this repository.\n"
170 "Add --global to set your account\'s default\n"
173 static const char *get_ident(const char *name
, const char *email
,
174 const char *date_str
, int error_on_no_name
)
176 static char buffer
[1000];
181 name
= git_default_name
;
183 email
= git_default_email
;
186 if (name
== git_default_name
&& env_hint
) {
187 fprintf(stderr
, env_hint
, au_env
, co_env
);
188 env_hint
= NULL
; /* warn only once, for "git-var -l" */
190 if (error_on_no_name
)
191 die("empty ident %s <%s> not allowed", name
, email
);
194 strcpy(date
, git_default_date
);
196 parse_date(date_str
, date
, sizeof(date
));
198 i
= copy(buffer
, sizeof(buffer
), 0, name
);
199 i
= add_raw(buffer
, sizeof(buffer
), i
, " <");
200 i
= copy(buffer
, sizeof(buffer
), i
, email
);
201 i
= add_raw(buffer
, sizeof(buffer
), i
, "> ");
202 i
= copy(buffer
, sizeof(buffer
), i
, date
);
203 if (i
>= sizeof(buffer
))
204 die("Impossibly long personal identifier");
209 const char *git_author_info(int error_on_no_name
)
211 return get_ident(getenv("GIT_AUTHOR_NAME"),
212 getenv("GIT_AUTHOR_EMAIL"),
213 getenv("GIT_AUTHOR_DATE"),
217 const char *git_committer_info(int error_on_no_name
)
219 return get_ident(getenv("GIT_COMMITTER_NAME"),
220 getenv("GIT_COMMITTER_EMAIL"),
221 getenv("GIT_COMMITTER_DATE"),
225 void ignore_missing_committer_name()
227 /* If we did not get a name from the user's gecos entry then
228 * git_default_name is empty; so instead load the username
229 * into it as a 'good enough for now' approximation of who
232 if (!*git_default_name
) {
233 struct passwd
*pw
= getpwuid(getuid());
235 die("You don't exist. Go away!");
236 strlcpy(git_default_name
, pw
->pw_name
, sizeof(git_default_name
));