ident.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 void ident_env_hint(enum want_ident whose_ident)
  310. {
  311. switch (whose_ident) {
  312. case WANT_AUTHOR_IDENT:
  313. fputs(_("Author identity unknown\n"), stderr);
  314. break;
  315. case WANT_COMMITTER_IDENT:
  316. fputs(_("Committer identity unknown\n"), stderr);
  317. break;
  318. default:
  319. break;
  320. }
  321. fputs(_("\n"
  322. "*** Please tell me who you are.\n"
  323. "\n"
  324. "Run\n"
  325. "\n"
  326. " git config --global user.email \"you@example.com\"\n"
  327. " git config --global user.name \"Your Name\"\n"
  328. "\n"
  329. "to set your account\'s default identity.\n"
  330. "Omit --global to set the identity only in this repository.\n"
  331. "\n"), stderr);
  332. }
  333. const char *fmt_ident(const char *name, const char *email,
  334. enum want_ident whose_ident, const char *date_str, int flag)
  335. {
  336. static int index;
  337. static struct strbuf ident_pool[2] = { STRBUF_INIT, STRBUF_INIT };
  338. int strict = (flag & IDENT_STRICT);
  339. int want_date = !(flag & IDENT_NO_DATE);
  340. int want_name = !(flag & IDENT_NO_NAME);
  341. struct strbuf *ident = &ident_pool[index];
  342. index = (index + 1) % ARRAY_SIZE(ident_pool);
  343. if (!email) {
  344. if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len)
  345. email = git_author_email.buf;
  346. else if (whose_ident == WANT_COMMITTER_IDENT && git_committer_email.len)
  347. email = git_committer_email.buf;
  348. }
  349. if (!email) {
  350. if (strict && ident_use_config_only
  351. && !(ident_config_given & IDENT_MAIL_GIVEN)) {
  352. ident_env_hint(whose_ident);
  353. die(_("no email was given and auto-detection is disabled"));
  354. }
  355. email = ident_default_email();
  356. if (strict && default_email_is_bogus) {
  357. ident_env_hint(whose_ident);
  358. die(_("unable to auto-detect email address (got '%s')"), email);
  359. }
  360. }
  361. if (want_name) {
  362. int using_default = 0;
  363. if (!name) {
  364. if (whose_ident == WANT_AUTHOR_IDENT && git_author_name.len)
  365. name = git_author_name.buf;
  366. else if (whose_ident == WANT_COMMITTER_IDENT &&
  367. git_committer_name.len)
  368. name = git_committer_name.buf;
  369. }
  370. if (!name) {
  371. if (strict && ident_use_config_only
  372. && !(ident_config_given & IDENT_NAME_GIVEN)) {
  373. ident_env_hint(whose_ident);
  374. die(_("no name was given and auto-detection is disabled"));
  375. }
  376. name = ident_default_name();
  377. using_default = 1;
  378. if (strict && default_name_is_bogus) {
  379. ident_env_hint(whose_ident);
  380. die(_("unable to auto-detect name (got '%s')"), name);
  381. }
  382. }
  383. if (!*name) {
  384. struct passwd *pw;
  385. if (strict) {
  386. if (using_default)
  387. ident_env_hint(whose_ident);
  388. die(_("empty ident name (for <%s>) not allowed"), email);
  389. }
  390. pw = xgetpwuid_self(NULL);
  391. name = pw->pw_name;
  392. }
  393. if (strict && !has_non_crud(name))
  394. die(_("name consists only of disallowed characters: %s"), name);
  395. }
  396. strbuf_reset(ident);
  397. if (want_name) {
  398. strbuf_addstr_without_crud(ident, name);
  399. strbuf_addstr(ident, " <");
  400. }
  401. strbuf_addstr_without_crud(ident, email);
  402. if (want_name)
  403. strbuf_addch(ident, '>');
  404. if (want_date) {
  405. strbuf_addch(ident, ' ');
  406. if (date_str && date_str[0]) {
  407. if (parse_date(date_str, ident) < 0)
  408. die(_("invalid date format: %s"), date_str);
  409. }
  410. else
  411. strbuf_addstr(ident, ident_default_date());
  412. }
  413. return ident->buf;
  414. }
  415. const char *fmt_name(enum want_ident whose_ident)
  416. {
  417. char *name = NULL;
  418. char *email = NULL;
  419. switch (whose_ident) {
  420. case WANT_BLANK_IDENT:
  421. break;
  422. case WANT_AUTHOR_IDENT:
  423. name = getenv("GIT_AUTHOR_NAME");
  424. email = getenv("GIT_AUTHOR_EMAIL");
  425. break;
  426. case WANT_COMMITTER_IDENT:
  427. name = getenv("GIT_COMMITTER_NAME");
  428. email = getenv("GIT_COMMITTER_EMAIL");
  429. break;
  430. }
  431. return fmt_ident(name, email, whose_ident, NULL,
  432. IDENT_STRICT | IDENT_NO_DATE);
  433. }
  434. const char *git_author_info(int flag)
  435. {
  436. if (getenv("GIT_AUTHOR_NAME"))
  437. author_ident_explicitly_given |= IDENT_NAME_GIVEN;
  438. if (getenv("GIT_AUTHOR_EMAIL"))
  439. author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
  440. return fmt_ident(getenv("GIT_AUTHOR_NAME"),
  441. getenv("GIT_AUTHOR_EMAIL"),
  442. WANT_AUTHOR_IDENT,
  443. getenv("GIT_AUTHOR_DATE"),
  444. flag);
  445. }
  446. const char *git_committer_info(int flag)
  447. {
  448. if (getenv("GIT_COMMITTER_NAME"))
  449. committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
  450. if (getenv("GIT_COMMITTER_EMAIL"))
  451. committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
  452. return fmt_ident(getenv("GIT_COMMITTER_NAME"),
  453. getenv("GIT_COMMITTER_EMAIL"),
  454. WANT_COMMITTER_IDENT,
  455. getenv("GIT_COMMITTER_DATE"),
  456. flag);
  457. }
  458. static int ident_is_sufficient(int user_ident_explicitly_given)
  459. {
  460. #ifndef WINDOWS
  461. return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
  462. #else
  463. return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
  464. #endif
  465. }
  466. int committer_ident_sufficiently_given(void)
  467. {
  468. return ident_is_sufficient(committer_ident_explicitly_given);
  469. }
  470. int author_ident_sufficiently_given(void)
  471. {
  472. return ident_is_sufficient(author_ident_explicitly_given);
  473. }
  474. static int set_ident(const char *var, const char *value)
  475. {
  476. if (!strcmp(var, "author.name")) {
  477. if (!value)
  478. return config_error_nonbool(var);
  479. strbuf_reset(&git_author_name);
  480. strbuf_addstr(&git_author_name, value);
  481. author_ident_explicitly_given |= IDENT_NAME_GIVEN;
  482. ident_config_given |= IDENT_NAME_GIVEN;
  483. return 0;
  484. }
  485. if (!strcmp(var, "author.email")) {
  486. if (!value)
  487. return config_error_nonbool(var);
  488. strbuf_reset(&git_author_email);
  489. strbuf_addstr(&git_author_email, value);
  490. author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
  491. ident_config_given |= IDENT_MAIL_GIVEN;
  492. return 0;
  493. }
  494. if (!strcmp(var, "committer.name")) {
  495. if (!value)
  496. return config_error_nonbool(var);
  497. strbuf_reset(&git_committer_name);
  498. strbuf_addstr(&git_committer_name, value);
  499. committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
  500. ident_config_given |= IDENT_NAME_GIVEN;
  501. return 0;
  502. }
  503. if (!strcmp(var, "committer.email")) {
  504. if (!value)
  505. return config_error_nonbool(var);
  506. strbuf_reset(&git_committer_email);
  507. strbuf_addstr(&git_committer_email, value);
  508. committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
  509. ident_config_given |= IDENT_MAIL_GIVEN;
  510. return 0;
  511. }
  512. if (!strcmp(var, "user.name")) {
  513. if (!value)
  514. return config_error_nonbool(var);
  515. strbuf_reset(&git_default_name);
  516. strbuf_addstr(&git_default_name, value);
  517. committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
  518. author_ident_explicitly_given |= IDENT_NAME_GIVEN;
  519. ident_config_given |= IDENT_NAME_GIVEN;
  520. return 0;
  521. }
  522. if (!strcmp(var, "user.email")) {
  523. if (!value)
  524. return config_error_nonbool(var);
  525. strbuf_reset(&git_default_email);
  526. strbuf_addstr(&git_default_email, value);
  527. committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
  528. author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
  529. ident_config_given |= IDENT_MAIL_GIVEN;
  530. return 0;
  531. }
  532. return 0;
  533. }
  534. int git_ident_config(const char *var, const char *value, void *data)
  535. {
  536. if (!strcmp(var, "user.useconfigonly")) {
  537. ident_use_config_only = git_config_bool(var, value);
  538. return 0;
  539. }
  540. return set_ident(var, value);
  541. }
  542. static void set_env_if(const char *key, const char *value, int *given, int bit)
  543. {
  544. if ((*given & bit) || getenv(key))
  545. return; /* nothing to do */
  546. setenv(key, value, 0);
  547. *given |= bit;
  548. }
  549. void prepare_fallback_ident(const char *name, const char *email)
  550. {
  551. set_env_if("GIT_AUTHOR_NAME", name,
  552. &author_ident_explicitly_given, IDENT_NAME_GIVEN);
  553. set_env_if("GIT_AUTHOR_EMAIL", email,
  554. &author_ident_explicitly_given, IDENT_MAIL_GIVEN);
  555. set_env_if("GIT_COMMITTER_NAME", name,
  556. &committer_ident_explicitly_given, IDENT_NAME_GIVEN);
  557. set_env_if("GIT_COMMITTER_EMAIL", email,
  558. &committer_ident_explicitly_given, IDENT_MAIL_GIVEN);
  559. }
  560. static int buf_cmp(const char *a_begin, const char *a_end,
  561. const char *b_begin, const char *b_end)
  562. {
  563. int a_len = a_end - a_begin;
  564. int b_len = b_end - b_begin;
  565. int min = a_len < b_len ? a_len : b_len;
  566. int cmp;
  567. cmp = memcmp(a_begin, b_begin, min);
  568. if (cmp)
  569. return cmp;
  570. return a_len - b_len;
  571. }
  572. int ident_cmp(const struct ident_split *a,
  573. const struct ident_split *b)
  574. {
  575. int cmp;
  576. cmp = buf_cmp(a->mail_begin, a->mail_end,
  577. b->mail_begin, b->mail_end);
  578. if (cmp)
  579. return cmp;
  580. return buf_cmp(a->name_begin, a->name_end,
  581. b->name_begin, b->name_end);
  582. }