update-index.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. /*
  2. * GIT - The information manager from hell
  3. *
  4. * Copyright (C) Linus Torvalds, 2005
  5. */
  6. #define USE_THE_INDEX_COMPATIBILITY_MACROS
  7. #include "cache.h"
  8. #include "config.h"
  9. #include "lockfile.h"
  10. #include "quote.h"
  11. #include "cache-tree.h"
  12. #include "tree-walk.h"
  13. #include "builtin.h"
  14. #include "refs.h"
  15. #include "resolve-undo.h"
  16. #include "parse-options.h"
  17. #include "pathspec.h"
  18. #include "dir.h"
  19. #include "split-index.h"
  20. #include "fsmonitor.h"
  21. /*
  22. * Default to not allowing changes to the list of files. The
  23. * tool doesn't actually care, but this makes it harder to add
  24. * files to the revision control by mistake by doing something
  25. * like "git update-index *" and suddenly having all the object
  26. * files be revision controlled.
  27. */
  28. static int allow_add;
  29. static int allow_remove;
  30. static int allow_replace;
  31. static int info_only;
  32. static int force_remove;
  33. static int verbose;
  34. static int mark_valid_only;
  35. static int mark_skip_worktree_only;
  36. static int mark_fsmonitor_only;
  37. static int ignore_skip_worktree_entries;
  38. #define MARK_FLAG 1
  39. #define UNMARK_FLAG 2
  40. static struct strbuf mtime_dir = STRBUF_INIT;
  41. /* Untracked cache mode */
  42. enum uc_mode {
  43. UC_UNSPECIFIED = -1,
  44. UC_DISABLE = 0,
  45. UC_ENABLE,
  46. UC_TEST,
  47. UC_FORCE
  48. };
  49. __attribute__((format (printf, 1, 2)))
  50. static void report(const char *fmt, ...)
  51. {
  52. va_list vp;
  53. if (!verbose)
  54. return;
  55. va_start(vp, fmt);
  56. vprintf(fmt, vp);
  57. putchar('\n');
  58. va_end(vp);
  59. }
  60. static void remove_test_directory(void)
  61. {
  62. if (mtime_dir.len)
  63. remove_dir_recursively(&mtime_dir, 0);
  64. }
  65. static const char *get_mtime_path(const char *path)
  66. {
  67. static struct strbuf sb = STRBUF_INIT;
  68. strbuf_reset(&sb);
  69. strbuf_addf(&sb, "%s/%s", mtime_dir.buf, path);
  70. return sb.buf;
  71. }
  72. static void xmkdir(const char *path)
  73. {
  74. path = get_mtime_path(path);
  75. if (mkdir(path, 0700))
  76. die_errno(_("failed to create directory %s"), path);
  77. }
  78. static int xstat_mtime_dir(struct stat *st)
  79. {
  80. if (stat(mtime_dir.buf, st))
  81. die_errno(_("failed to stat %s"), mtime_dir.buf);
  82. return 0;
  83. }
  84. static int create_file(const char *path)
  85. {
  86. int fd;
  87. path = get_mtime_path(path);
  88. fd = open(path, O_CREAT | O_RDWR, 0644);
  89. if (fd < 0)
  90. die_errno(_("failed to create file %s"), path);
  91. return fd;
  92. }
  93. static void xunlink(const char *path)
  94. {
  95. path = get_mtime_path(path);
  96. if (unlink(path))
  97. die_errno(_("failed to delete file %s"), path);
  98. }
  99. static void xrmdir(const char *path)
  100. {
  101. path = get_mtime_path(path);
  102. if (rmdir(path))
  103. die_errno(_("failed to delete directory %s"), path);
  104. }
  105. static void avoid_racy(void)
  106. {
  107. /*
  108. * not use if we could usleep(10) if USE_NSEC is defined. The
  109. * field nsec could be there, but the OS could choose to
  110. * ignore it?
  111. */
  112. sleep(1);
  113. }
  114. static int test_if_untracked_cache_is_supported(void)
  115. {
  116. struct stat st;
  117. struct stat_data base;
  118. int fd, ret = 0;
  119. char *cwd;
  120. strbuf_addstr(&mtime_dir, "mtime-test-XXXXXX");
  121. if (!mkdtemp(mtime_dir.buf))
  122. die_errno("Could not make temporary directory");
  123. cwd = xgetcwd();
  124. fprintf(stderr, _("Testing mtime in '%s' "), cwd);
  125. free(cwd);
  126. atexit(remove_test_directory);
  127. xstat_mtime_dir(&st);
  128. fill_stat_data(&base, &st);
  129. fputc('.', stderr);
  130. avoid_racy();
  131. fd = create_file("newfile");
  132. xstat_mtime_dir(&st);
  133. if (!match_stat_data(&base, &st)) {
  134. close(fd);
  135. fputc('\n', stderr);
  136. fprintf_ln(stderr,_("directory stat info does not "
  137. "change after adding a new file"));
  138. goto done;
  139. }
  140. fill_stat_data(&base, &st);
  141. fputc('.', stderr);
  142. avoid_racy();
  143. xmkdir("new-dir");
  144. xstat_mtime_dir(&st);
  145. if (!match_stat_data(&base, &st)) {
  146. close(fd);
  147. fputc('\n', stderr);
  148. fprintf_ln(stderr, _("directory stat info does not change "
  149. "after adding a new directory"));
  150. goto done;
  151. }
  152. fill_stat_data(&base, &st);
  153. fputc('.', stderr);
  154. avoid_racy();
  155. write_or_die(fd, "data", 4);
  156. close(fd);
  157. xstat_mtime_dir(&st);
  158. if (match_stat_data(&base, &st)) {
  159. fputc('\n', stderr);
  160. fprintf_ln(stderr, _("directory stat info changes "
  161. "after updating a file"));
  162. goto done;
  163. }
  164. fputc('.', stderr);
  165. avoid_racy();
  166. close(create_file("new-dir/new"));
  167. xstat_mtime_dir(&st);
  168. if (match_stat_data(&base, &st)) {
  169. fputc('\n', stderr);
  170. fprintf_ln(stderr, _("directory stat info changes after "
  171. "adding a file inside subdirectory"));
  172. goto done;
  173. }
  174. fputc('.', stderr);
  175. avoid_racy();
  176. xunlink("newfile");
  177. xstat_mtime_dir(&st);
  178. if (!match_stat_data(&base, &st)) {
  179. fputc('\n', stderr);
  180. fprintf_ln(stderr, _("directory stat info does not "
  181. "change after deleting a file"));
  182. goto done;
  183. }
  184. fill_stat_data(&base, &st);
  185. fputc('.', stderr);
  186. avoid_racy();
  187. xunlink("new-dir/new");
  188. xrmdir("new-dir");
  189. xstat_mtime_dir(&st);
  190. if (!match_stat_data(&base, &st)) {
  191. fputc('\n', stderr);
  192. fprintf_ln(stderr, _("directory stat info does not "
  193. "change after deleting a directory"));
  194. goto done;
  195. }
  196. if (rmdir(mtime_dir.buf))
  197. die_errno(_("failed to delete directory %s"), mtime_dir.buf);
  198. fprintf_ln(stderr, _(" OK"));
  199. ret = 1;
  200. done:
  201. strbuf_release(&mtime_dir);
  202. return ret;
  203. }
  204. static int mark_ce_flags(const char *path, int flag, int mark)
  205. {
  206. int namelen = strlen(path);
  207. int pos = cache_name_pos(path, namelen);
  208. if (0 <= pos) {
  209. mark_fsmonitor_invalid(&the_index, active_cache[pos]);
  210. if (mark)
  211. active_cache[pos]->ce_flags |= flag;
  212. else
  213. active_cache[pos]->ce_flags &= ~flag;
  214. active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
  215. cache_tree_invalidate_path(&the_index, path);
  216. active_cache_changed |= CE_ENTRY_CHANGED;
  217. return 0;
  218. }
  219. return -1;
  220. }
  221. static int remove_one_path(const char *path)
  222. {
  223. if (!allow_remove)
  224. return error("%s: does not exist and --remove not passed", path);
  225. if (remove_file_from_cache(path))
  226. return error("%s: cannot remove from the index", path);
  227. return 0;
  228. }
  229. /*
  230. * Handle a path that couldn't be lstat'ed. It's either:
  231. * - missing file (ENOENT or ENOTDIR). That's ok if we're
  232. * supposed to be removing it and the removal actually
  233. * succeeds.
  234. * - permission error. That's never ok.
  235. */
  236. static int process_lstat_error(const char *path, int err)
  237. {
  238. if (is_missing_file_error(err))
  239. return remove_one_path(path);
  240. return error("lstat(\"%s\"): %s", path, strerror(err));
  241. }
  242. static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
  243. {
  244. int option;
  245. struct cache_entry *ce;
  246. /* Was the old index entry already up-to-date? */
  247. if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
  248. return 0;
  249. ce = make_empty_cache_entry(&the_index, len);
  250. memcpy(ce->name, path, len);
  251. ce->ce_flags = create_ce_flags(0);
  252. ce->ce_namelen = len;
  253. fill_stat_cache_info(&the_index, ce, st);
  254. ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
  255. if (index_path(&the_index, &ce->oid, path, st,
  256. info_only ? 0 : HASH_WRITE_OBJECT)) {
  257. discard_cache_entry(ce);
  258. return -1;
  259. }
  260. option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
  261. option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
  262. if (add_cache_entry(ce, option)) {
  263. discard_cache_entry(ce);
  264. return error("%s: cannot add to the index - missing --add option?", path);
  265. }
  266. return 0;
  267. }
  268. /*
  269. * Handle a path that was a directory. Four cases:
  270. *
  271. * - it's already a gitlink in the index, and we keep it that
  272. * way, and update it if we can (if we cannot find the HEAD,
  273. * we're going to keep it unchanged in the index!)
  274. *
  275. * - it's a *file* in the index, in which case it should be
  276. * removed as a file if removal is allowed, since it doesn't
  277. * exist as such any more. If removal isn't allowed, it's
  278. * an error.
  279. *
  280. * (NOTE! This is old and arguably fairly strange behaviour.
  281. * We might want to make this an error unconditionally, and
  282. * use "--force-remove" if you actually want to force removal).
  283. *
  284. * - it used to exist as a subdirectory (ie multiple files with
  285. * this particular prefix) in the index, in which case it's wrong
  286. * to try to update it as a directory.
  287. *
  288. * - it doesn't exist at all in the index, but it is a valid
  289. * git directory, and it should be *added* as a gitlink.
  290. */
  291. static int process_directory(const char *path, int len, struct stat *st)
  292. {
  293. struct object_id oid;
  294. int pos = cache_name_pos(path, len);
  295. /* Exact match: file or existing gitlink */
  296. if (pos >= 0) {
  297. const struct cache_entry *ce = active_cache[pos];
  298. if (S_ISGITLINK(ce->ce_mode)) {
  299. /* Do nothing to the index if there is no HEAD! */
  300. if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
  301. return 0;
  302. return add_one_path(ce, path, len, st);
  303. }
  304. /* Should this be an unconditional error? */
  305. return remove_one_path(path);
  306. }
  307. /* Inexact match: is there perhaps a subdirectory match? */
  308. pos = -pos-1;
  309. while (pos < active_nr) {
  310. const struct cache_entry *ce = active_cache[pos++];
  311. if (strncmp(ce->name, path, len))
  312. break;
  313. if (ce->name[len] > '/')
  314. break;
  315. if (ce->name[len] < '/')
  316. continue;
  317. /* Subdirectory match - error out */
  318. return error("%s: is a directory - add individual files instead", path);
  319. }
  320. /* No match - should we add it as a gitlink? */
  321. if (!resolve_gitlink_ref(path, "HEAD", &oid))
  322. return add_one_path(NULL, path, len, st);
  323. /* Error out. */
  324. return error("%s: is a directory - add files inside instead", path);
  325. }
  326. static int process_path(const char *path, struct stat *st, int stat_errno)
  327. {
  328. int pos, len;
  329. const struct cache_entry *ce;
  330. len = strlen(path);
  331. if (has_symlink_leading_path(path, len))
  332. return error("'%s' is beyond a symbolic link", path);
  333. pos = cache_name_pos(path, len);
  334. ce = pos < 0 ? NULL : active_cache[pos];
  335. if (ce && ce_skip_worktree(ce)) {
  336. /*
  337. * working directory version is assumed "good"
  338. * so updating it does not make sense.
  339. * On the other hand, removing it from index should work
  340. */
  341. if (!ignore_skip_worktree_entries && allow_remove &&
  342. remove_file_from_cache(path))
  343. return error("%s: cannot remove from the index", path);
  344. return 0;
  345. }
  346. /*
  347. * First things first: get the stat information, to decide
  348. * what to do about the pathname!
  349. */
  350. if (stat_errno)
  351. return process_lstat_error(path, stat_errno);
  352. if (S_ISDIR(st->st_mode))
  353. return process_directory(path, len, st);
  354. return add_one_path(ce, path, len, st);
  355. }
  356. static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
  357. const char *path, int stage)
  358. {
  359. int len, option;
  360. struct cache_entry *ce;
  361. if (!verify_path(path, mode))
  362. return error("Invalid path '%s'", path);
  363. len = strlen(path);
  364. ce = make_empty_cache_entry(&the_index, len);
  365. oidcpy(&ce->oid, oid);
  366. memcpy(ce->name, path, len);
  367. ce->ce_flags = create_ce_flags(stage);
  368. ce->ce_namelen = len;
  369. ce->ce_mode = create_ce_mode(mode);
  370. if (assume_unchanged)
  371. ce->ce_flags |= CE_VALID;
  372. option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
  373. option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
  374. if (add_cache_entry(ce, option))
  375. return error("%s: cannot add to the index - missing --add option?",
  376. path);
  377. report("add '%s'", path);
  378. return 0;
  379. }
  380. static void chmod_path(char flip, const char *path)
  381. {
  382. int pos;
  383. struct cache_entry *ce;
  384. pos = cache_name_pos(path, strlen(path));
  385. if (pos < 0)
  386. goto fail;
  387. ce = active_cache[pos];
  388. if (chmod_cache_entry(ce, flip) < 0)
  389. goto fail;
  390. report("chmod %cx '%s'", flip, path);
  391. return;
  392. fail:
  393. die("git update-index: cannot chmod %cx '%s'", flip, path);
  394. }
  395. static void update_one(const char *path)
  396. {
  397. int stat_errno = 0;
  398. struct stat st;
  399. if (mark_valid_only || mark_skip_worktree_only || force_remove ||
  400. mark_fsmonitor_only)
  401. st.st_mode = 0;
  402. else if (lstat(path, &st) < 0) {
  403. st.st_mode = 0;
  404. stat_errno = errno;
  405. } /* else stat is valid */
  406. if (!verify_path(path, st.st_mode)) {
  407. fprintf(stderr, "Ignoring path %s\n", path);
  408. return;
  409. }
  410. if (mark_valid_only) {
  411. if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG))
  412. die("Unable to mark file %s", path);
  413. return;
  414. }
  415. if (mark_skip_worktree_only) {
  416. if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
  417. die("Unable to mark file %s", path);
  418. return;
  419. }
  420. if (mark_fsmonitor_only) {
  421. if (mark_ce_flags(path, CE_FSMONITOR_VALID, mark_fsmonitor_only == MARK_FLAG))
  422. die("Unable to mark file %s", path);
  423. return;
  424. }
  425. if (force_remove) {
  426. if (remove_file_from_cache(path))
  427. die("git update-index: unable to remove %s", path);
  428. report("remove '%s'", path);
  429. return;
  430. }
  431. if (process_path(path, &st, stat_errno))
  432. die("Unable to process path %s", path);
  433. report("add '%s'", path);
  434. }
  435. static void read_index_info(int nul_term_line)
  436. {
  437. const int hexsz = the_hash_algo->hexsz;
  438. struct strbuf buf = STRBUF_INIT;
  439. struct strbuf uq = STRBUF_INIT;
  440. strbuf_getline_fn getline_fn;
  441. getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
  442. while (getline_fn(&buf, stdin) != EOF) {
  443. char *ptr, *tab;
  444. char *path_name;
  445. struct object_id oid;
  446. unsigned int mode;
  447. unsigned long ul;
  448. int stage;
  449. /* This reads lines formatted in one of three formats:
  450. *
  451. * (1) mode SP sha1 TAB path
  452. * The first format is what "git apply --index-info"
  453. * reports, and used to reconstruct a partial tree
  454. * that is used for phony merge base tree when falling
  455. * back on 3-way merge.
  456. *
  457. * (2) mode SP type SP sha1 TAB path
  458. * The second format is to stuff "git ls-tree" output
  459. * into the index file.
  460. *
  461. * (3) mode SP sha1 SP stage TAB path
  462. * This format is to put higher order stages into the
  463. * index file and matches "git ls-files --stage" output.
  464. */
  465. errno = 0;
  466. ul = strtoul(buf.buf, &ptr, 8);
  467. if (ptr == buf.buf || *ptr != ' '
  468. || errno || (unsigned int) ul != ul)
  469. goto bad_line;
  470. mode = ul;
  471. tab = strchr(ptr, '\t');
  472. if (!tab || tab - ptr < hexsz + 1)
  473. goto bad_line;
  474. if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
  475. stage = tab[-1] - '0';
  476. ptr = tab + 1; /* point at the head of path */
  477. tab = tab - 2; /* point at tail of sha1 */
  478. }
  479. else {
  480. stage = 0;
  481. ptr = tab + 1; /* point at the head of path */
  482. }
  483. if (get_oid_hex(tab - hexsz, &oid) ||
  484. tab[-(hexsz + 1)] != ' ')
  485. goto bad_line;
  486. path_name = ptr;
  487. if (!nul_term_line && path_name[0] == '"') {
  488. strbuf_reset(&uq);
  489. if (unquote_c_style(&uq, path_name, NULL)) {
  490. die("git update-index: bad quoting of path name");
  491. }
  492. path_name = uq.buf;
  493. }
  494. if (!verify_path(path_name, mode)) {
  495. fprintf(stderr, "Ignoring path %s\n", path_name);
  496. continue;
  497. }
  498. if (!mode) {
  499. /* mode == 0 means there is no such path -- remove */
  500. if (remove_file_from_cache(path_name))
  501. die("git update-index: unable to remove %s",
  502. ptr);
  503. }
  504. else {
  505. /* mode ' ' sha1 '\t' name
  506. * ptr[-1] points at tab,
  507. * ptr[-41] is at the beginning of sha1
  508. */
  509. ptr[-(hexsz + 2)] = ptr[-1] = 0;
  510. if (add_cacheinfo(mode, &oid, path_name, stage))
  511. die("git update-index: unable to update %s",
  512. path_name);
  513. }
  514. continue;
  515. bad_line:
  516. die("malformed index info %s", buf.buf);
  517. }
  518. strbuf_release(&buf);
  519. strbuf_release(&uq);
  520. }
  521. static const char * const update_index_usage[] = {
  522. N_("git update-index [<options>] [--] [<file>...]"),
  523. NULL
  524. };
  525. static struct object_id head_oid;
  526. static struct object_id merge_head_oid;
  527. static struct cache_entry *read_one_ent(const char *which,
  528. struct object_id *ent, const char *path,
  529. int namelen, int stage)
  530. {
  531. unsigned short mode;
  532. struct object_id oid;
  533. struct cache_entry *ce;
  534. if (get_tree_entry(the_repository, ent, path, &oid, &mode)) {
  535. if (which)
  536. error("%s: not in %s branch.", path, which);
  537. return NULL;
  538. }
  539. if (mode == S_IFDIR) {
  540. if (which)
  541. error("%s: not a blob in %s branch.", path, which);
  542. return NULL;
  543. }
  544. ce = make_empty_cache_entry(&the_index, namelen);
  545. oidcpy(&ce->oid, &oid);
  546. memcpy(ce->name, path, namelen);
  547. ce->ce_flags = create_ce_flags(stage);
  548. ce->ce_namelen = namelen;
  549. ce->ce_mode = create_ce_mode(mode);
  550. return ce;
  551. }
  552. static int unresolve_one(const char *path)
  553. {
  554. int namelen = strlen(path);
  555. int pos;
  556. int ret = 0;
  557. struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
  558. /* See if there is such entry in the index. */
  559. pos = cache_name_pos(path, namelen);
  560. if (0 <= pos) {
  561. /* already merged */
  562. pos = unmerge_cache_entry_at(pos);
  563. if (pos < active_nr) {
  564. const struct cache_entry *ce = active_cache[pos];
  565. if (ce_stage(ce) &&
  566. ce_namelen(ce) == namelen &&
  567. !memcmp(ce->name, path, namelen))
  568. return 0;
  569. }
  570. /* no resolve-undo information; fall back */
  571. } else {
  572. /* If there isn't, either it is unmerged, or
  573. * resolved as "removed" by mistake. We do not
  574. * want to do anything in the former case.
  575. */
  576. pos = -pos-1;
  577. if (pos < active_nr) {
  578. const struct cache_entry *ce = active_cache[pos];
  579. if (ce_namelen(ce) == namelen &&
  580. !memcmp(ce->name, path, namelen)) {
  581. fprintf(stderr,
  582. "%s: skipping still unmerged path.\n",
  583. path);
  584. goto free_return;
  585. }
  586. }
  587. }
  588. /* Grab blobs from given path from HEAD and MERGE_HEAD,
  589. * stuff HEAD version in stage #2,
  590. * stuff MERGE_HEAD version in stage #3.
  591. */
  592. ce_2 = read_one_ent("our", &head_oid, path, namelen, 2);
  593. ce_3 = read_one_ent("their", &merge_head_oid, path, namelen, 3);
  594. if (!ce_2 || !ce_3) {
  595. ret = -1;
  596. goto free_return;
  597. }
  598. if (oideq(&ce_2->oid, &ce_3->oid) &&
  599. ce_2->ce_mode == ce_3->ce_mode) {
  600. fprintf(stderr, "%s: identical in both, skipping.\n",
  601. path);
  602. goto free_return;
  603. }
  604. remove_file_from_cache(path);
  605. if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
  606. error("%s: cannot add our version to the index.", path);
  607. ret = -1;
  608. goto free_return;
  609. }
  610. if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
  611. return 0;
  612. error("%s: cannot add their version to the index.", path);
  613. ret = -1;
  614. free_return:
  615. discard_cache_entry(ce_2);
  616. discard_cache_entry(ce_3);
  617. return ret;
  618. }
  619. static void read_head_pointers(void)
  620. {
  621. if (read_ref("HEAD", &head_oid))
  622. die("No HEAD -- no initial commit yet?");
  623. if (read_ref("MERGE_HEAD", &merge_head_oid)) {
  624. fprintf(stderr, "Not in the middle of a merge.\n");
  625. exit(0);
  626. }
  627. }
  628. static int do_unresolve(int ac, const char **av,
  629. const char *prefix, int prefix_length)
  630. {
  631. int i;
  632. int err = 0;
  633. /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
  634. * are not doing a merge, so exit with success status.
  635. */
  636. read_head_pointers();
  637. for (i = 1; i < ac; i++) {
  638. const char *arg = av[i];
  639. char *p = prefix_path(prefix, prefix_length, arg);
  640. err |= unresolve_one(p);
  641. free(p);
  642. }
  643. return err;
  644. }
  645. static int do_reupdate(int ac, const char **av,
  646. const char *prefix)
  647. {
  648. /* Read HEAD and run update-index on paths that are
  649. * merged and already different between index and HEAD.
  650. */
  651. int pos;
  652. int has_head = 1;
  653. struct pathspec pathspec;
  654. parse_pathspec(&pathspec, 0,
  655. PATHSPEC_PREFER_CWD,
  656. prefix, av + 1);
  657. if (read_ref("HEAD", &head_oid))
  658. /* If there is no HEAD, that means it is an initial
  659. * commit. Update everything in the index.
  660. */
  661. has_head = 0;
  662. redo:
  663. for (pos = 0; pos < active_nr; pos++) {
  664. const struct cache_entry *ce = active_cache[pos];
  665. struct cache_entry *old = NULL;
  666. int save_nr;
  667. char *path;
  668. if (ce_stage(ce) || !ce_path_match(&the_index, ce, &pathspec, NULL))
  669. continue;
  670. if (has_head)
  671. old = read_one_ent(NULL, &head_oid,
  672. ce->name, ce_namelen(ce), 0);
  673. if (old && ce->ce_mode == old->ce_mode &&
  674. oideq(&ce->oid, &old->oid)) {
  675. discard_cache_entry(old);
  676. continue; /* unchanged */
  677. }
  678. /* Be careful. The working tree may not have the
  679. * path anymore, in which case, under 'allow_remove',
  680. * or worse yet 'allow_replace', active_nr may decrease.
  681. */
  682. save_nr = active_nr;
  683. path = xstrdup(ce->name);
  684. update_one(path);
  685. free(path);
  686. discard_cache_entry(old);
  687. if (save_nr != active_nr)
  688. goto redo;
  689. }
  690. clear_pathspec(&pathspec);
  691. return 0;
  692. }
  693. struct refresh_params {
  694. unsigned int flags;
  695. int *has_errors;
  696. };
  697. static int refresh(struct refresh_params *o, unsigned int flag)
  698. {
  699. setup_work_tree();
  700. read_cache();
  701. *o->has_errors |= refresh_cache(o->flags | flag);
  702. return 0;
  703. }
  704. static int refresh_callback(const struct option *opt,
  705. const char *arg, int unset)
  706. {
  707. BUG_ON_OPT_NEG(unset);
  708. BUG_ON_OPT_ARG(arg);
  709. return refresh(opt->value, 0);
  710. }
  711. static int really_refresh_callback(const struct option *opt,
  712. const char *arg, int unset)
  713. {
  714. BUG_ON_OPT_NEG(unset);
  715. BUG_ON_OPT_ARG(arg);
  716. return refresh(opt->value, REFRESH_REALLY);
  717. }
  718. static int chmod_callback(const struct option *opt,
  719. const char *arg, int unset)
  720. {
  721. char *flip = opt->value;
  722. BUG_ON_OPT_NEG(unset);
  723. if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2])
  724. return error("option 'chmod' expects \"+x\" or \"-x\"");
  725. *flip = arg[0];
  726. return 0;
  727. }
  728. static int resolve_undo_clear_callback(const struct option *opt,
  729. const char *arg, int unset)
  730. {
  731. BUG_ON_OPT_NEG(unset);
  732. BUG_ON_OPT_ARG(arg);
  733. resolve_undo_clear();
  734. return 0;
  735. }
  736. static int parse_new_style_cacheinfo(const char *arg,
  737. unsigned int *mode,
  738. struct object_id *oid,
  739. const char **path)
  740. {
  741. unsigned long ul;
  742. char *endp;
  743. const char *p;
  744. if (!arg)
  745. return -1;
  746. errno = 0;
  747. ul = strtoul(arg, &endp, 8);
  748. if (errno || endp == arg || *endp != ',' || (unsigned int) ul != ul)
  749. return -1; /* not a new-style cacheinfo */
  750. *mode = ul;
  751. endp++;
  752. if (parse_oid_hex(endp, oid, &p) || *p != ',')
  753. return -1;
  754. *path = p + 1;
  755. return 0;
  756. }
  757. static enum parse_opt_result cacheinfo_callback(
  758. struct parse_opt_ctx_t *ctx, const struct option *opt,
  759. const char *arg, int unset)
  760. {
  761. struct object_id oid;
  762. unsigned int mode;
  763. const char *path;
  764. BUG_ON_OPT_NEG(unset);
  765. BUG_ON_OPT_ARG(arg);
  766. if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
  767. if (add_cacheinfo(mode, &oid, path, 0))
  768. die("git update-index: --cacheinfo cannot add %s", path);
  769. ctx->argv++;
  770. ctx->argc--;
  771. return 0;
  772. }
  773. if (ctx->argc <= 3)
  774. return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
  775. if (strtoul_ui(*++ctx->argv, 8, &mode) ||
  776. get_oid_hex(*++ctx->argv, &oid) ||
  777. add_cacheinfo(mode, &oid, *++ctx->argv, 0))
  778. die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
  779. ctx->argc -= 3;
  780. return 0;
  781. }
  782. static enum parse_opt_result stdin_cacheinfo_callback(
  783. struct parse_opt_ctx_t *ctx, const struct option *opt,
  784. const char *arg, int unset)
  785. {
  786. int *nul_term_line = opt->value;
  787. BUG_ON_OPT_NEG(unset);
  788. BUG_ON_OPT_ARG(arg);
  789. if (ctx->argc != 1)
  790. return error("option '%s' must be the last argument", opt->long_name);
  791. allow_add = allow_replace = allow_remove = 1;
  792. read_index_info(*nul_term_line);
  793. return 0;
  794. }
  795. static enum parse_opt_result stdin_callback(
  796. struct parse_opt_ctx_t *ctx, const struct option *opt,
  797. const char *arg, int unset)
  798. {
  799. int *read_from_stdin = opt->value;
  800. BUG_ON_OPT_NEG(unset);
  801. BUG_ON_OPT_ARG(arg);
  802. if (ctx->argc != 1)
  803. return error("option '%s' must be the last argument", opt->long_name);
  804. *read_from_stdin = 1;
  805. return 0;
  806. }
  807. static enum parse_opt_result unresolve_callback(
  808. struct parse_opt_ctx_t *ctx, const struct option *opt,
  809. const char *arg, int unset)
  810. {
  811. int *has_errors = opt->value;
  812. const char *prefix = startup_info->prefix;
  813. BUG_ON_OPT_NEG(unset);
  814. BUG_ON_OPT_ARG(arg);
  815. /* consume remaining arguments. */
  816. *has_errors = do_unresolve(ctx->argc, ctx->argv,
  817. prefix, prefix ? strlen(prefix) : 0);
  818. if (*has_errors)
  819. active_cache_changed = 0;
  820. ctx->argv += ctx->argc - 1;
  821. ctx->argc = 1;
  822. return 0;
  823. }
  824. static enum parse_opt_result reupdate_callback(
  825. struct parse_opt_ctx_t *ctx, const struct option *opt,
  826. const char *arg, int unset)
  827. {
  828. int *has_errors = opt->value;
  829. const char *prefix = startup_info->prefix;
  830. BUG_ON_OPT_NEG(unset);
  831. BUG_ON_OPT_ARG(arg);
  832. /* consume remaining arguments. */
  833. setup_work_tree();
  834. *has_errors = do_reupdate(ctx->argc, ctx->argv, prefix);
  835. if (*has_errors)
  836. active_cache_changed = 0;
  837. ctx->argv += ctx->argc - 1;
  838. ctx->argc = 1;
  839. return 0;
  840. }
  841. int cmd_update_index(int argc, const char **argv, const char *prefix)
  842. {
  843. int newfd, entries, has_errors = 0, nul_term_line = 0;
  844. enum uc_mode untracked_cache = UC_UNSPECIFIED;
  845. int read_from_stdin = 0;
  846. int prefix_length = prefix ? strlen(prefix) : 0;
  847. int preferred_index_format = 0;
  848. char set_executable_bit = 0;
  849. struct refresh_params refresh_args = {0, &has_errors};
  850. int lock_error = 0;
  851. int split_index = -1;
  852. int force_write = 0;
  853. int fsmonitor = -1;
  854. struct lock_file lock_file = LOCK_INIT;
  855. struct parse_opt_ctx_t ctx;
  856. strbuf_getline_fn getline_fn;
  857. int parseopt_state = PARSE_OPT_UNKNOWN;
  858. struct repository *r = the_repository;
  859. struct option options[] = {
  860. OPT_BIT('q', NULL, &refresh_args.flags,
  861. N_("continue refresh even when index needs update"),
  862. REFRESH_QUIET),
  863. OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
  864. N_("refresh: ignore submodules"),
  865. REFRESH_IGNORE_SUBMODULES),
  866. OPT_SET_INT(0, "add", &allow_add,
  867. N_("do not ignore new files"), 1),
  868. OPT_SET_INT(0, "replace", &allow_replace,
  869. N_("let files replace directories and vice-versa"), 1),
  870. OPT_SET_INT(0, "remove", &allow_remove,
  871. N_("notice files missing from worktree"), 1),
  872. OPT_BIT(0, "unmerged", &refresh_args.flags,
  873. N_("refresh even if index contains unmerged entries"),
  874. REFRESH_UNMERGED),
  875. OPT_CALLBACK_F(0, "refresh", &refresh_args, NULL,
  876. N_("refresh stat information"),
  877. PARSE_OPT_NOARG | PARSE_OPT_NONEG,
  878. refresh_callback),
  879. OPT_CALLBACK_F(0, "really-refresh", &refresh_args, NULL,
  880. N_("like --refresh, but ignore assume-unchanged setting"),
  881. PARSE_OPT_NOARG | PARSE_OPT_NONEG,
  882. really_refresh_callback),
  883. {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
  884. N_("<mode>,<object>,<path>"),
  885. N_("add the specified entry to the index"),
  886. PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
  887. PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
  888. NULL, 0,
  889. cacheinfo_callback},
  890. OPT_CALLBACK_F(0, "chmod", &set_executable_bit, "(+|-)x",
  891. N_("override the executable bit of the listed files"),
  892. PARSE_OPT_NONEG,
  893. chmod_callback),
  894. {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL,
  895. N_("mark files as \"not changing\""),
  896. PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
  897. {OPTION_SET_INT, 0, "no-assume-unchanged", &mark_valid_only, NULL,
  898. N_("clear assumed-unchanged bit"),
  899. PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
  900. {OPTION_SET_INT, 0, "skip-worktree", &mark_skip_worktree_only, NULL,
  901. N_("mark files as \"index-only\""),
  902. PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
  903. {OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL,
  904. N_("clear skip-worktree bit"),
  905. PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
  906. OPT_BOOL(0, "ignore-skip-worktree-entries", &ignore_skip_worktree_entries,
  907. N_("do not touch index-only entries")),
  908. OPT_SET_INT(0, "info-only", &info_only,
  909. N_("add to index only; do not add content to object database"), 1),
  910. OPT_SET_INT(0, "force-remove", &force_remove,
  911. N_("remove named paths even if present in worktree"), 1),
  912. OPT_BOOL('z', NULL, &nul_term_line,
  913. N_("with --stdin: input lines are terminated by null bytes")),
  914. {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
  915. N_("read list of paths to be updated from standard input"),
  916. PARSE_OPT_NONEG | PARSE_OPT_NOARG,
  917. NULL, 0, stdin_callback},
  918. {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &nul_term_line, NULL,
  919. N_("add entries from standard input to the index"),
  920. PARSE_OPT_NONEG | PARSE_OPT_NOARG,
  921. NULL, 0, stdin_cacheinfo_callback},
  922. {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
  923. N_("repopulate stages #2 and #3 for the listed paths"),
  924. PARSE_OPT_NONEG | PARSE_OPT_NOARG,
  925. NULL, 0, unresolve_callback},
  926. {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
  927. N_("only update entries that differ from HEAD"),
  928. PARSE_OPT_NONEG | PARSE_OPT_NOARG,
  929. NULL, 0, reupdate_callback},
  930. OPT_BIT(0, "ignore-missing", &refresh_args.flags,
  931. N_("ignore files missing from worktree"),
  932. REFRESH_IGNORE_MISSING),
  933. OPT_SET_INT(0, "verbose", &verbose,
  934. N_("report actions to standard output"), 1),
  935. OPT_CALLBACK_F(0, "clear-resolve-undo", NULL, NULL,
  936. N_("(for porcelains) forget saved unresolved conflicts"),
  937. PARSE_OPT_NOARG | PARSE_OPT_NONEG,
  938. resolve_undo_clear_callback),
  939. OPT_INTEGER(0, "index-version", &preferred_index_format,
  940. N_("write index in this format")),
  941. OPT_BOOL(0, "split-index", &split_index,
  942. N_("enable or disable split index")),
  943. OPT_BOOL(0, "untracked-cache", &untracked_cache,
  944. N_("enable/disable untracked cache")),
  945. OPT_SET_INT(0, "test-untracked-cache", &untracked_cache,
  946. N_("test if the filesystem supports untracked cache"), UC_TEST),
  947. OPT_SET_INT(0, "force-untracked-cache", &untracked_cache,
  948. N_("enable untracked cache without testing the filesystem"), UC_FORCE),
  949. OPT_SET_INT(0, "force-write-index", &force_write,
  950. N_("write out the index even if is not flagged as changed"), 1),
  951. OPT_BOOL(0, "fsmonitor", &fsmonitor,
  952. N_("enable or disable file system monitor")),
  953. {OPTION_SET_INT, 0, "fsmonitor-valid", &mark_fsmonitor_only, NULL,
  954. N_("mark files as fsmonitor valid"),
  955. PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
  956. {OPTION_SET_INT, 0, "no-fsmonitor-valid", &mark_fsmonitor_only, NULL,
  957. N_("clear fsmonitor valid bit"),
  958. PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
  959. OPT_END()
  960. };
  961. if (argc == 2 && !strcmp(argv[1], "-h"))
  962. usage_with_options(update_index_usage, options);
  963. git_config(git_default_config, NULL);
  964. /* we will diagnose later if it turns out that we need to update it */
  965. newfd = hold_locked_index(&lock_file, 0);
  966. if (newfd < 0)
  967. lock_error = errno;
  968. entries = read_cache();
  969. if (entries < 0)
  970. die("cache corrupted");
  971. the_index.updated_skipworktree = 1;
  972. /*
  973. * Custom copy of parse_options() because we want to handle
  974. * filename arguments as they come.
  975. */
  976. parse_options_start(&ctx, argc, argv, prefix,
  977. options, PARSE_OPT_STOP_AT_NON_OPTION);
  978. while (ctx.argc) {
  979. if (parseopt_state != PARSE_OPT_DONE)
  980. parseopt_state = parse_options_step(&ctx, options,
  981. update_index_usage);
  982. if (!ctx.argc)
  983. break;
  984. switch (parseopt_state) {
  985. case PARSE_OPT_HELP:
  986. case PARSE_OPT_ERROR:
  987. exit(129);
  988. case PARSE_OPT_COMPLETE:
  989. exit(0);
  990. case PARSE_OPT_NON_OPTION:
  991. case PARSE_OPT_DONE:
  992. {
  993. const char *path = ctx.argv[0];
  994. char *p;
  995. setup_work_tree();
  996. p = prefix_path(prefix, prefix_length, path);
  997. update_one(p);
  998. if (set_executable_bit)
  999. chmod_path(set_executable_bit, p);
  1000. free(p);
  1001. ctx.argc--;
  1002. ctx.argv++;
  1003. break;
  1004. }
  1005. case PARSE_OPT_UNKNOWN:
  1006. if (ctx.argv[0][1] == '-')
  1007. error("unknown option '%s'", ctx.argv[0] + 2);
  1008. else
  1009. error("unknown switch '%c'", *ctx.opt);
  1010. usage_with_options(update_index_usage, options);
  1011. }
  1012. }
  1013. argc = parse_options_end(&ctx);
  1014. getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
  1015. if (preferred_index_format) {
  1016. if (preferred_index_format < INDEX_FORMAT_LB ||
  1017. INDEX_FORMAT_UB < preferred_index_format)
  1018. die("index-version %d not in range: %d..%d",
  1019. preferred_index_format,
  1020. INDEX_FORMAT_LB, INDEX_FORMAT_UB);
  1021. if (the_index.version != preferred_index_format)
  1022. active_cache_changed |= SOMETHING_CHANGED;
  1023. the_index.version = preferred_index_format;
  1024. }
  1025. if (read_from_stdin) {
  1026. struct strbuf buf = STRBUF_INIT;
  1027. struct strbuf unquoted = STRBUF_INIT;
  1028. setup_work_tree();
  1029. while (getline_fn(&buf, stdin) != EOF) {
  1030. char *p;
  1031. if (!nul_term_line && buf.buf[0] == '"') {
  1032. strbuf_reset(&unquoted);
  1033. if (unquote_c_style(&unquoted, buf.buf, NULL))
  1034. die("line is badly quoted");
  1035. strbuf_swap(&buf, &unquoted);
  1036. }
  1037. p = prefix_path(prefix, prefix_length, buf.buf);
  1038. update_one(p);
  1039. if (set_executable_bit)
  1040. chmod_path(set_executable_bit, p);
  1041. free(p);
  1042. }
  1043. strbuf_release(&unquoted);
  1044. strbuf_release(&buf);
  1045. }
  1046. if (split_index > 0) {
  1047. if (git_config_get_split_index() == 0)
  1048. warning(_("core.splitIndex is set to false; "
  1049. "remove or change it, if you really want to "
  1050. "enable split index"));
  1051. if (the_index.split_index)
  1052. the_index.cache_changed |= SPLIT_INDEX_ORDERED;
  1053. else
  1054. add_split_index(&the_index);
  1055. } else if (!split_index) {
  1056. if (git_config_get_split_index() == 1)
  1057. warning(_("core.splitIndex is set to true; "
  1058. "remove or change it, if you really want to "
  1059. "disable split index"));
  1060. remove_split_index(&the_index);
  1061. }
  1062. prepare_repo_settings(r);
  1063. switch (untracked_cache) {
  1064. case UC_UNSPECIFIED:
  1065. break;
  1066. case UC_DISABLE:
  1067. if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
  1068. warning(_("core.untrackedCache is set to true; "
  1069. "remove or change it, if you really want to "
  1070. "disable the untracked cache"));
  1071. remove_untracked_cache(&the_index);
  1072. report(_("Untracked cache disabled"));
  1073. break;
  1074. case UC_TEST:
  1075. setup_work_tree();
  1076. return !test_if_untracked_cache_is_supported();
  1077. case UC_ENABLE:
  1078. case UC_FORCE:
  1079. if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
  1080. warning(_("core.untrackedCache is set to false; "
  1081. "remove or change it, if you really want to "
  1082. "enable the untracked cache"));
  1083. add_untracked_cache(&the_index);
  1084. report(_("Untracked cache enabled for '%s'"), get_git_work_tree());
  1085. break;
  1086. default:
  1087. BUG("bad untracked_cache value: %d", untracked_cache);
  1088. }
  1089. if (fsmonitor > 0) {
  1090. if (git_config_get_fsmonitor() == 0)
  1091. warning(_("core.fsmonitor is unset; "
  1092. "set it if you really want to "
  1093. "enable fsmonitor"));
  1094. add_fsmonitor(&the_index);
  1095. report(_("fsmonitor enabled"));
  1096. } else if (!fsmonitor) {
  1097. if (git_config_get_fsmonitor() == 1)
  1098. warning(_("core.fsmonitor is set; "
  1099. "remove it if you really want to "
  1100. "disable fsmonitor"));
  1101. remove_fsmonitor(&the_index);
  1102. report(_("fsmonitor disabled"));
  1103. }
  1104. if (active_cache_changed || force_write) {
  1105. if (newfd < 0) {
  1106. if (refresh_args.flags & REFRESH_QUIET)
  1107. exit(128);
  1108. unable_to_lock_die(get_index_file(), lock_error);
  1109. }
  1110. if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
  1111. die("Unable to write new index file");
  1112. }
  1113. rollback_lock_file(&lock_file);
  1114. return has_errors ? 1 : 0;
  1115. }