PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ident.c

https://gitlab.com/mayakarya/git
C | 517 lines | 478 code | 23 blank | 16 comment | 36 complexity | c66495f50802825a396413c7edac9691 MD5 | raw file
  1. /*
  2. * ident.c
  3. *
  4. * create git identifier lines of the form "name <email> date"
  5. *
  6. * Copyright (C) 2005 Linus Torvalds
  7. */
  8. #include "cache.h"
  9. static struct strbuf git_default_name = STRBUF_INIT;
  10. static struct strbuf git_default_email = STRBUF_INIT;
  11. static struct strbuf git_default_date = STRBUF_INIT;
  12. static int default_email_is_bogus;
  13. static int default_name_is_bogus;
  14. static int ident_use_config_only;
  15. #define IDENT_NAME_GIVEN 01
  16. #define IDENT_MAIL_GIVEN 02
  17. #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
  18. static int committer_ident_explicitly_given;
  19. static int author_ident_explicitly_given;
  20. static int ident_config_given;
  21. #ifdef NO_GECOS_IN_PWENT
  22. #define get_gecos(ignored) "&"
  23. #else
  24. #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
  25. #endif
  26. static struct passwd *xgetpwuid_self(int *is_bogus)
  27. {
  28. struct passwd *pw;
  29. errno = 0;
  30. pw = getpwuid(getuid());
  31. if (!pw) {
  32. static struct passwd fallback;
  33. fallback.pw_name = "unknown";
  34. #ifndef NO_GECOS_IN_PWENT
  35. fallback.pw_gecos = "Unknown";
  36. #endif
  37. pw = &fallback;
  38. if (is_bogus)
  39. *is_bogus = 1;
  40. }
  41. return pw;
  42. }
  43. static void copy_gecos(const struct passwd *w, struct strbuf *name)
  44. {
  45. char *src;
  46. /* Traditionally GECOS field had office phone numbers etc, separated
  47. * with commas. Also & stands for capitalized form of the login name.
  48. */
  49. for (src = get_gecos(w); *src && *src != ','; src++) {
  50. int ch = *src;
  51. if (ch != '&')
  52. strbuf_addch(name, ch);
  53. else {
  54. /* Sorry, Mr. McDonald... */
  55. strbuf_addch(name, toupper(*w->pw_name));
  56. strbuf_addstr(name, w->pw_name + 1);
  57. }
  58. }
  59. }
  60. static int add_mailname_host(struct strbuf *buf)
  61. {
  62. FILE *mailname;
  63. struct strbuf mailnamebuf = STRBUF_INIT;
  64. mailname = fopen("/etc/mailname", "r");
  65. if (!mailname) {
  66. if (errno != ENOENT)
  67. warning_errno("cannot open /etc/mailname");
  68. return -1;
  69. }
  70. if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
  71. if (ferror(mailname))
  72. warning_errno("cannot read /etc/mailname");
  73. strbuf_release(&mailnamebuf);
  74. fclose(mailname);
  75. return -1;
  76. }
  77. /* success! */
  78. strbuf_addbuf(buf, &mailnamebuf);
  79. strbuf_release(&mailnamebuf);
  80. fclose(mailname);
  81. return 0;
  82. }
  83. static int canonical_name(const char *host, struct strbuf *out)
  84. {
  85. int status = -1;
  86. #ifndef NO_IPV6
  87. struct addrinfo hints, *ai;
  88. memset (&hints, '\0', sizeof (hints));
  89. hints.ai_flags = AI_CANONNAME;
  90. if (!getaddrinfo(host, NULL, &hints, &ai)) {
  91. if (ai && strchr(ai->ai_canonname, '.')) {
  92. strbuf_addstr(out, ai->ai_canonname);
  93. status = 0;
  94. }
  95. freeaddrinfo(ai);
  96. }
  97. #else
  98. struct hostent *he = gethostbyname(host);
  99. if (he && strchr(he->h_name, '.')) {
  100. strbuf_addstr(out, he->h_name);
  101. status = 0;
  102. }
  103. #endif /* NO_IPV6 */
  104. return status;
  105. }
  106. static void add_domainname(struct strbuf *out, int *is_bogus)
  107. {
  108. char buf[1024];
  109. if (gethostname(buf, sizeof(buf))) {
  110. warning_errno("cannot get host name");
  111. strbuf_addstr(out, "(none)");
  112. *is_bogus = 1;
  113. return;
  114. }
  115. if (strchr(buf, '.'))
  116. strbuf_addstr(out, buf);
  117. else if (canonical_name(buf, out) < 0) {
  118. strbuf_addf(out, "%s.(none)", buf);
  119. *is_bogus = 1;
  120. }
  121. }
  122. static void copy_email(const struct passwd *pw, struct strbuf *email,
  123. int *is_bogus)
  124. {
  125. /*
  126. * Make up a fake email address
  127. * (name + '@' + hostname [+ '.' + domainname])
  128. */
  129. strbuf_addstr(email, pw->pw_name);
  130. strbuf_addch(email, '@');
  131. if (!add_mailname_host(email))
  132. return; /* read from "/etc/mailname" (Debian) */
  133. add_domainname(email, is_bogus);
  134. }
  135. const char *ident_default_name(void)
  136. {
  137. if (!git_default_name.len) {
  138. copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
  139. strbuf_trim(&git_default_name);
  140. }
  141. return git_default_name.buf;
  142. }
  143. const char *ident_default_email(void)
  144. {
  145. if (!git_default_email.len) {
  146. const char *email = getenv("EMAIL");
  147. if (email && email[0]) {
  148. strbuf_addstr(&git_default_email, email);
  149. committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
  150. author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
  151. } else
  152. copy_email(xgetpwuid_self(&default_email_is_bogus),
  153. &git_default_email, &default_email_is_bogus);
  154. strbuf_trim(&git_default_email);
  155. }
  156. return git_default_email.buf;
  157. }
  158. static const char *ident_default_date(void)
  159. {
  160. if (!git_default_date.len)
  161. datestamp(&git_default_date);
  162. return git_default_date.buf;
  163. }
  164. static int crud(unsigned char c)
  165. {
  166. return c <= 32 ||
  167. c == '.' ||
  168. c == ',' ||
  169. c == ':' ||
  170. c == ';' ||
  171. c == '<' ||
  172. c == '>' ||
  173. c == '"' ||
  174. c == '\\' ||
  175. c == '\'';
  176. }
  177. /*
  178. * Copy over a string to the destination, but avoid special
  179. * characters ('\n', '<' and '>') and remove crud at the end
  180. */
  181. static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
  182. {
  183. size_t i, len;
  184. unsigned char c;
  185. /* Remove crud from the beginning.. */
  186. while ((c = *src) != 0) {
  187. if (!crud(c))
  188. break;
  189. src++;
  190. }
  191. /* Remove crud from the end.. */
  192. len = strlen(src);
  193. while (len > 0) {
  194. c = src[len-1];
  195. if (!crud(c))
  196. break;
  197. --len;
  198. }
  199. /*
  200. * Copy the rest to the buffer, but avoid the special
  201. * characters '\n' '<' and '>' that act as delimiters on
  202. * an identification line. We can only remove crud, never add it,
  203. * so 'len' is our maximum.
  204. */
  205. strbuf_grow(sb, len);
  206. for (i = 0; i < len; i++) {
  207. c = *src++;
  208. switch (c) {
  209. case '\n': case '<': case '>':
  210. continue;
  211. }
  212. sb->buf[sb->len++] = c;
  213. }
  214. sb->buf[sb->len] = '\0';
  215. }
  216. /*
  217. * Reverse of fmt_ident(); given an ident line, split the fields
  218. * to allow the caller to parse it.
  219. * Signal a success by returning 0, but date/tz fields of the result
  220. * can still be NULL if the input line only has the name/email part
  221. * (e.g. reading from a reflog entry).
  222. */
  223. int split_ident_line(struct ident_split *split, const char *line, int len)
  224. {
  225. const char *cp;
  226. size_t span;
  227. int status = -1;
  228. memset(split, 0, sizeof(*split));
  229. split->name_begin = line;
  230. for (cp = line; *cp && cp < line + len; cp++)
  231. if (*cp == '<') {
  232. split->mail_begin = cp + 1;
  233. break;
  234. }
  235. if (!split->mail_begin)
  236. return status;
  237. for (cp = split->mail_begin - 2; line <= cp; cp--)
  238. if (!isspace(*cp)) {
  239. split->name_end = cp + 1;
  240. break;
  241. }
  242. if (!split->name_end) {
  243. /* no human readable name */
  244. split->name_end = split->name_begin;
  245. }
  246. for (cp = split->mail_begin; cp < line + len; cp++)
  247. if (*cp == '>') {
  248. split->mail_end = cp;
  249. break;
  250. }
  251. if (!split->mail_end)
  252. return status;
  253. /*
  254. * Look from the end-of-line to find the trailing ">" of the mail
  255. * address, even though we should already know it as split->mail_end.
  256. * This can help in cases of broken idents with an extra ">" somewhere
  257. * in the email address. Note that we are assuming the timestamp will
  258. * never have a ">" in it.
  259. *
  260. * Note that we will always find some ">" before going off the front of
  261. * the string, because will always hit the split->mail_end closing
  262. * bracket.
  263. */
  264. for (cp = line + len - 1; *cp != '>'; cp--)
  265. ;
  266. for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
  267. ;
  268. if (line + len <= cp)
  269. goto person_only;
  270. split->date_begin = cp;
  271. span = strspn(cp, "0123456789");
  272. if (!span)
  273. goto person_only;
  274. split->date_end = split->date_begin + span;
  275. for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
  276. ;
  277. if (line + len <= cp || (*cp != '+' && *cp != '-'))
  278. goto person_only;
  279. split->tz_begin = cp;
  280. span = strspn(cp + 1, "0123456789");
  281. if (!span)
  282. goto person_only;
  283. split->tz_end = split->tz_begin + 1 + span;
  284. return 0;
  285. person_only:
  286. split->date_begin = NULL;
  287. split->date_end = NULL;
  288. split->tz_begin = NULL;
  289. split->tz_end = NULL;
  290. return 0;
  291. }
  292. static const char *env_hint =
  293. "\n"
  294. "*** Please tell me who you are.\n"
  295. "\n"
  296. "Run\n"
  297. "\n"
  298. " git config --global user.email \"you@example.com\"\n"
  299. " git config --global user.name \"Your Name\"\n"
  300. "\n"
  301. "to set your account\'s default identity.\n"
  302. "Omit --global to set the identity only in this repository.\n"
  303. "\n";
  304. const char *fmt_ident(const char *name, const char *email,
  305. const char *date_str, int flag)
  306. {
  307. static struct strbuf ident = STRBUF_INIT;
  308. int strict = (flag & IDENT_STRICT);
  309. int want_date = !(flag & IDENT_NO_DATE);
  310. int want_name = !(flag & IDENT_NO_NAME);
  311. if (want_name) {
  312. int using_default = 0;
  313. if (!name) {
  314. if (strict && ident_use_config_only
  315. && !(ident_config_given & IDENT_NAME_GIVEN)) {
  316. fputs(env_hint, stderr);
  317. die("no name was given and auto-detection is disabled");
  318. }
  319. name = ident_default_name();
  320. using_default = 1;
  321. if (strict && default_name_is_bogus) {
  322. fputs(env_hint, stderr);
  323. die("unable to auto-detect name (got '%s')", name);
  324. }
  325. }
  326. if (!*name) {
  327. struct passwd *pw;
  328. if (strict) {
  329. if (using_default)
  330. fputs(env_hint, stderr);
  331. die("empty ident name (for <%s>) not allowed", email);
  332. }
  333. pw = xgetpwuid_self(NULL);
  334. name = pw->pw_name;
  335. }
  336. }
  337. if (!email) {
  338. if (strict && ident_use_config_only
  339. && !(ident_config_given & IDENT_MAIL_GIVEN)) {
  340. fputs(env_hint, stderr);
  341. die("no email was given and auto-detection is disabled");
  342. }
  343. email = ident_default_email();
  344. if (strict && default_email_is_bogus) {
  345. fputs(env_hint, stderr);
  346. die("unable to auto-detect email address (got '%s')", email);
  347. }
  348. }
  349. strbuf_reset(&ident);
  350. if (want_name) {
  351. strbuf_addstr_without_crud(&ident, name);
  352. strbuf_addstr(&ident, " <");
  353. }
  354. strbuf_addstr_without_crud(&ident, email);
  355. if (want_name)
  356. strbuf_addch(&ident, '>');
  357. if (want_date) {
  358. strbuf_addch(&ident, ' ');
  359. if (date_str && date_str[0]) {
  360. if (parse_date(date_str, &ident) < 0)
  361. die("invalid date format: %s", date_str);
  362. }
  363. else
  364. strbuf_addstr(&ident, ident_default_date());
  365. }
  366. return ident.buf;
  367. }
  368. const char *fmt_name(const char *name, const char *email)
  369. {
  370. return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
  371. }
  372. const char *git_author_info(int flag)
  373. {
  374. if (getenv("GIT_AUTHOR_NAME"))
  375. author_ident_explicitly_given |= IDENT_NAME_GIVEN;
  376. if (getenv("GIT_AUTHOR_EMAIL"))
  377. author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
  378. return fmt_ident(getenv("GIT_AUTHOR_NAME"),
  379. getenv("GIT_AUTHOR_EMAIL"),
  380. getenv("GIT_AUTHOR_DATE"),
  381. flag);
  382. }
  383. const char *git_committer_info(int flag)
  384. {
  385. if (getenv("GIT_COMMITTER_NAME"))
  386. committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
  387. if (getenv("GIT_COMMITTER_EMAIL"))
  388. committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
  389. return fmt_ident(getenv("GIT_COMMITTER_NAME"),
  390. getenv("GIT_COMMITTER_EMAIL"),
  391. getenv("GIT_COMMITTER_DATE"),
  392. flag);
  393. }
  394. static int ident_is_sufficient(int user_ident_explicitly_given)
  395. {
  396. #ifndef WINDOWS
  397. return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
  398. #else
  399. return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
  400. #endif
  401. }
  402. int committer_ident_sufficiently_given(void)
  403. {
  404. return ident_is_sufficient(committer_ident_explicitly_given);
  405. }
  406. int author_ident_sufficiently_given(void)
  407. {
  408. return ident_is_sufficient(author_ident_explicitly_given);
  409. }
  410. int git_ident_config(const char *var, const char *value, void *data)
  411. {
  412. if (!strcmp(var, "user.useconfigonly")) {
  413. ident_use_config_only = git_config_bool(var, value);
  414. return 0;
  415. }
  416. if (!strcmp(var, "user.name")) {
  417. if (!value)
  418. return config_error_nonbool(var);
  419. strbuf_reset(&git_default_name);
  420. strbuf_addstr(&git_default_name, value);
  421. committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
  422. author_ident_explicitly_given |= IDENT_NAME_GIVEN;
  423. ident_config_given |= IDENT_NAME_GIVEN;
  424. return 0;
  425. }
  426. if (!strcmp(var, "user.email")) {
  427. if (!value)
  428. return config_error_nonbool(var);
  429. strbuf_reset(&git_default_email);
  430. strbuf_addstr(&git_default_email, value);
  431. committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
  432. author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
  433. ident_config_given |= IDENT_MAIL_GIVEN;
  434. return 0;
  435. }
  436. return 0;
  437. }
  438. static int buf_cmp(const char *a_begin, const char *a_end,
  439. const char *b_begin, const char *b_end)
  440. {
  441. int a_len = a_end - a_begin;
  442. int b_len = b_end - b_begin;
  443. int min = a_len < b_len ? a_len : b_len;
  444. int cmp;
  445. cmp = memcmp(a_begin, b_begin, min);
  446. if (cmp)
  447. return cmp;
  448. return a_len - b_len;
  449. }
  450. int ident_cmp(const struct ident_split *a,
  451. const struct ident_split *b)
  452. {
  453. int cmp;
  454. cmp = buf_cmp(a->mail_begin, a->mail_end,
  455. b->mail_begin, b->mail_end);
  456. if (cmp)
  457. return cmp;
  458. return buf_cmp(a->name_begin, a->name_end,
  459. b->name_begin, b->name_end);
  460. }