THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

636 lines
15KB

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