4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
14 static char real_email
[1000];
15 static char real_name
[1000];
16 static char real_date
[50];
21 struct passwd
*pw
= getpwuid(getuid());
24 die("You don't exist. Go away!");
26 /* Get the name ("gecos") */
27 len
= strlen(pw
->pw_gecos
);
28 if (len
>= sizeof(real_name
))
29 die("Your parents must have hated you!");
30 memcpy(real_name
, pw
->pw_gecos
, len
+1);
32 /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */
33 len
= strlen(pw
->pw_name
);
34 if (len
> sizeof(real_email
)/2)
35 die("Your sysadmin must hate you!");
36 memcpy(real_email
, pw
->pw_name
, len
);
37 real_email
[len
++] = '@';
38 gethostname(real_email
+ len
, sizeof(real_email
) - len
);
39 if (!strchr(real_email
+len
, '.')) {
40 len
= strlen(real_email
);
41 real_email
[len
++] = '.';
42 getdomainname(real_email
+len
, sizeof(real_email
)-len
);
45 /* And set the default date */
46 datestamp(real_date
, sizeof(real_date
));
50 static int add_raw(char *buf
, int size
, int offset
, const char *str
)
52 int len
= strlen(str
);
53 if (offset
+ len
> size
)
55 memcpy(buf
+ offset
, str
, len
);
59 static int crud(unsigned char c
)
61 static const char crud_array
[256] = {
67 ['"'] = 1, ['\''] = 1,
73 * Copy over a string to the destination, but avoid special
74 * characters ('\n', '<' and '>') and remove crud at the end
76 static int copy(char *buf
, int size
, int offset
, const char *src
)
81 /* Remove crud from the beginning.. */
82 while ((c
= *src
) != 0) {
88 /* Remove crud from the end.. */
98 * Copy the rest to the buffer, but avoid the special
99 * characters '\n' '<' and '>' that act as delimeters on
100 * a identification line
102 for (i
= 0; i
< len
; i
++) {
105 case '\n': case '<': case '>':
115 char *get_ident(const char *name
, const char *email
, const char *date_str
)
117 static char buffer
[1000];
125 strcpy(date
, real_date
);
127 parse_date(date_str
, date
, sizeof(date
));
129 i
= copy(buffer
, sizeof(buffer
), 0, name
);
130 i
= add_raw(buffer
, sizeof(buffer
), i
, " <");
131 i
= copy(buffer
, sizeof(buffer
), i
, email
);
132 i
= add_raw(buffer
, sizeof(buffer
), i
, "> ");
133 i
= copy(buffer
, sizeof(buffer
), i
, date
);
134 if (i
>= sizeof(buffer
))
135 die("Impossibly long personal identifier");
140 char *git_author_info(void)
142 return get_ident(gitenv("GIT_AUTHOR_NAME"), gitenv("GIT_AUTHOR_EMAIL"), gitenv("GIT_AUTHOR_DATE"));
145 char *git_committer_info(void)
147 return get_ident(gitenv("GIT_COMMITTER_NAME"), gitenv("GIT_COMMITTER_EMAIL"), gitenv("GIT_COMMITTER_DATE"));