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.

574 lines
15KB

  1. /*
  2. * Copyright (C) 2005 Junio C Hamano
  3. */
  4. #include "cache.h"
  5. #include "quote.h"
  6. #include "commit.h"
  7. #include "diff.h"
  8. #include "diffcore.h"
  9. #include "revision.h"
  10. #include "cache-tree.h"
  11. #include "unpack-trees.h"
  12. #include "refs.h"
  13. #include "submodule.h"
  14. #include "dir.h"
  15. #include "fsmonitor.h"
  16. /*
  17. * diff-files
  18. */
  19. /*
  20. * Has the work tree entity been removed?
  21. *
  22. * Return 1 if it was removed from the work tree, 0 if an entity to be
  23. * compared with the cache entry ce still exists (the latter includes
  24. * the case where a directory that is not a submodule repository
  25. * exists for ce that is a submodule -- it is a submodule that is not
  26. * checked out). Return negative for an error.
  27. */
  28. static int check_removed(const struct cache_entry *ce, struct stat *st)
  29. {
  30. if (lstat(ce->name, st) < 0) {
  31. if (!is_missing_file_error(errno))
  32. return -1;
  33. return 1;
  34. }
  35. if (has_symlink_leading_path(ce->name, ce_namelen(ce)))
  36. return 1;
  37. if (S_ISDIR(st->st_mode)) {
  38. struct object_id sub;
  39. /*
  40. * If ce is already a gitlink, we can have a plain
  41. * directory (i.e. the submodule is not checked out),
  42. * or a checked out submodule. Either case this is not
  43. * a case where something was removed from the work tree,
  44. * so we will return 0.
  45. *
  46. * Otherwise, if the directory is not a submodule
  47. * repository, that means ce which was a blob turned into
  48. * a directory --- the blob was removed!
  49. */
  50. if (!S_ISGITLINK(ce->ce_mode) &&
  51. resolve_gitlink_ref(ce->name, "HEAD", &sub))
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. /*
  57. * Has a file changed or has a submodule new commits or a dirty work tree?
  58. *
  59. * Return 1 when changes are detected, 0 otherwise. If the DIRTY_SUBMODULES
  60. * option is set, the caller does not only want to know if a submodule is
  61. * modified at all but wants to know all the conditions that are met (new
  62. * commits, untracked content and/or modified content).
  63. */
  64. static int match_stat_with_submodule(struct diff_options *diffopt,
  65. const struct cache_entry *ce,
  66. struct stat *st, unsigned ce_option,
  67. unsigned *dirty_submodule)
  68. {
  69. int changed = ie_match_stat(diffopt->repo->index, ce, st, ce_option);
  70. if (S_ISGITLINK(ce->ce_mode)) {
  71. struct diff_flags orig_flags = diffopt->flags;
  72. if (!diffopt->flags.override_submodule_config)
  73. set_diffopt_flags_from_submodule_config(diffopt, ce->name);
  74. if (diffopt->flags.ignore_submodules)
  75. changed = 0;
  76. else if (!diffopt->flags.ignore_dirty_submodules &&
  77. (!changed || diffopt->flags.dirty_submodules))
  78. *dirty_submodule = is_submodule_modified(ce->name,
  79. diffopt->flags.ignore_untracked_in_submodules);
  80. diffopt->flags = orig_flags;
  81. }
  82. return changed;
  83. }
  84. int run_diff_files(struct rev_info *revs, unsigned int option)
  85. {
  86. int entries, i;
  87. int diff_unmerged_stage = revs->max_count;
  88. unsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED)
  89. ? CE_MATCH_RACY_IS_DIRTY : 0);
  90. uint64_t start = getnanotime();
  91. struct index_state *istate = revs->diffopt.repo->index;
  92. diff_set_mnemonic_prefix(&revs->diffopt, "i/", "w/");
  93. if (diff_unmerged_stage < 0)
  94. diff_unmerged_stage = 2;
  95. entries = istate->cache_nr;
  96. for (i = 0; i < entries; i++) {
  97. unsigned int oldmode, newmode;
  98. struct cache_entry *ce = istate->cache[i];
  99. int changed;
  100. unsigned dirty_submodule = 0;
  101. const struct object_id *old_oid, *new_oid;
  102. if (diff_can_quit_early(&revs->diffopt))
  103. break;
  104. if (!ce_path_match(istate, ce, &revs->prune_data, NULL))
  105. continue;
  106. if (ce_stage(ce)) {
  107. struct combine_diff_path *dpath;
  108. struct diff_filepair *pair;
  109. unsigned int wt_mode = 0;
  110. int num_compare_stages = 0;
  111. size_t path_len;
  112. struct stat st;
  113. path_len = ce_namelen(ce);
  114. dpath = xmalloc(combine_diff_path_size(5, path_len));
  115. dpath->path = (char *) &(dpath->parent[5]);
  116. dpath->next = NULL;
  117. memcpy(dpath->path, ce->name, path_len);
  118. dpath->path[path_len] = '\0';
  119. oidclr(&dpath->oid);
  120. memset(&(dpath->parent[0]), 0,
  121. sizeof(struct combine_diff_parent)*5);
  122. changed = check_removed(ce, &st);
  123. if (!changed)
  124. wt_mode = ce_mode_from_stat(ce, st.st_mode);
  125. else {
  126. if (changed < 0) {
  127. perror(ce->name);
  128. continue;
  129. }
  130. wt_mode = 0;
  131. }
  132. dpath->mode = wt_mode;
  133. while (i < entries) {
  134. struct cache_entry *nce = istate->cache[i];
  135. int stage;
  136. if (strcmp(ce->name, nce->name))
  137. break;
  138. /* Stage #2 (ours) is the first parent,
  139. * stage #3 (theirs) is the second.
  140. */
  141. stage = ce_stage(nce);
  142. if (2 <= stage) {
  143. int mode = nce->ce_mode;
  144. num_compare_stages++;
  145. oidcpy(&dpath->parent[stage - 2].oid,
  146. &nce->oid);
  147. dpath->parent[stage-2].mode = ce_mode_from_stat(nce, mode);
  148. dpath->parent[stage-2].status =
  149. DIFF_STATUS_MODIFIED;
  150. }
  151. /* diff against the proper unmerged stage */
  152. if (stage == diff_unmerged_stage)
  153. ce = nce;
  154. i++;
  155. }
  156. /*
  157. * Compensate for loop update
  158. */
  159. i--;
  160. if (revs->combine_merges && num_compare_stages == 2) {
  161. show_combined_diff(dpath, 2,
  162. revs->dense_combined_merges,
  163. revs);
  164. free(dpath);
  165. continue;
  166. }
  167. FREE_AND_NULL(dpath);
  168. /*
  169. * Show the diff for the 'ce' if we found the one
  170. * from the desired stage.
  171. */
  172. pair = diff_unmerge(&revs->diffopt, ce->name);
  173. if (wt_mode)
  174. pair->two->mode = wt_mode;
  175. if (ce_stage(ce) != diff_unmerged_stage)
  176. continue;
  177. }
  178. if (ce_uptodate(ce) || ce_skip_worktree(ce))
  179. continue;
  180. /* If CE_VALID is set, don't look at workdir for file removal */
  181. if (ce->ce_flags & CE_VALID) {
  182. changed = 0;
  183. newmode = ce->ce_mode;
  184. } else {
  185. struct stat st;
  186. changed = check_removed(ce, &st);
  187. if (changed) {
  188. if (changed < 0) {
  189. perror(ce->name);
  190. continue;
  191. }
  192. diff_addremove(&revs->diffopt, '-', ce->ce_mode,
  193. &ce->oid,
  194. !is_null_oid(&ce->oid),
  195. ce->name, 0);
  196. continue;
  197. } else if (revs->diffopt.ita_invisible_in_index &&
  198. ce_intent_to_add(ce)) {
  199. diff_addremove(&revs->diffopt, '+', ce->ce_mode,
  200. the_hash_algo->empty_tree, 0,
  201. ce->name, 0);
  202. continue;
  203. }
  204. changed = match_stat_with_submodule(&revs->diffopt, ce, &st,
  205. ce_option, &dirty_submodule);
  206. newmode = ce_mode_from_stat(ce, st.st_mode);
  207. }
  208. if (!changed && !dirty_submodule) {
  209. ce_mark_uptodate(ce);
  210. mark_fsmonitor_valid(istate, ce);
  211. if (!revs->diffopt.flags.find_copies_harder)
  212. continue;
  213. }
  214. oldmode = ce->ce_mode;
  215. old_oid = &ce->oid;
  216. new_oid = changed ? &null_oid : &ce->oid;
  217. diff_change(&revs->diffopt, oldmode, newmode,
  218. old_oid, new_oid,
  219. !is_null_oid(old_oid),
  220. !is_null_oid(new_oid),
  221. ce->name, 0, dirty_submodule);
  222. }
  223. diffcore_std(&revs->diffopt);
  224. diff_flush(&revs->diffopt);
  225. trace_performance_since(start, "diff-files");
  226. return 0;
  227. }
  228. /*
  229. * diff-index
  230. */
  231. /* A file entry went away or appeared */
  232. static void diff_index_show_file(struct rev_info *revs,
  233. const char *prefix,
  234. const struct cache_entry *ce,
  235. const struct object_id *oid, int oid_valid,
  236. unsigned int mode,
  237. unsigned dirty_submodule)
  238. {
  239. diff_addremove(&revs->diffopt, prefix[0], mode,
  240. oid, oid_valid, ce->name, dirty_submodule);
  241. }
  242. static int get_stat_data(const struct cache_entry *ce,
  243. const struct object_id **oidp,
  244. unsigned int *modep,
  245. int cached, int match_missing,
  246. unsigned *dirty_submodule, struct diff_options *diffopt)
  247. {
  248. const struct object_id *oid = &ce->oid;
  249. unsigned int mode = ce->ce_mode;
  250. if (!cached && !ce_uptodate(ce)) {
  251. int changed;
  252. struct stat st;
  253. changed = check_removed(ce, &st);
  254. if (changed < 0)
  255. return -1;
  256. else if (changed) {
  257. if (match_missing) {
  258. *oidp = oid;
  259. *modep = mode;
  260. return 0;
  261. }
  262. return -1;
  263. }
  264. changed = match_stat_with_submodule(diffopt, ce, &st,
  265. 0, dirty_submodule);
  266. if (changed) {
  267. mode = ce_mode_from_stat(ce, st.st_mode);
  268. oid = &null_oid;
  269. }
  270. }
  271. *oidp = oid;
  272. *modep = mode;
  273. return 0;
  274. }
  275. static void show_new_file(struct rev_info *revs,
  276. const struct cache_entry *new_file,
  277. int cached, int match_missing)
  278. {
  279. const struct object_id *oid;
  280. unsigned int mode;
  281. unsigned dirty_submodule = 0;
  282. /*
  283. * New file in the index: it might actually be different in
  284. * the working tree.
  285. */
  286. if (get_stat_data(new_file, &oid, &mode, cached, match_missing,
  287. &dirty_submodule, &revs->diffopt) < 0)
  288. return;
  289. diff_index_show_file(revs, "+", new_file, oid, !is_null_oid(oid), mode, dirty_submodule);
  290. }
  291. static int show_modified(struct rev_info *revs,
  292. const struct cache_entry *old_entry,
  293. const struct cache_entry *new_entry,
  294. int report_missing,
  295. int cached, int match_missing)
  296. {
  297. unsigned int mode, oldmode;
  298. const struct object_id *oid;
  299. unsigned dirty_submodule = 0;
  300. if (get_stat_data(new_entry, &oid, &mode, cached, match_missing,
  301. &dirty_submodule, &revs->diffopt) < 0) {
  302. if (report_missing)
  303. diff_index_show_file(revs, "-", old_entry,
  304. &old_entry->oid, 1, old_entry->ce_mode,
  305. 0);
  306. return -1;
  307. }
  308. if (revs->combine_merges && !cached &&
  309. (!oideq(oid, &old_entry->oid) || !oideq(&old_entry->oid, &new_entry->oid))) {
  310. struct combine_diff_path *p;
  311. int pathlen = ce_namelen(new_entry);
  312. p = xmalloc(combine_diff_path_size(2, pathlen));
  313. p->path = (char *) &p->parent[2];
  314. p->next = NULL;
  315. memcpy(p->path, new_entry->name, pathlen);
  316. p->path[pathlen] = 0;
  317. p->mode = mode;
  318. oidclr(&p->oid);
  319. memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent));
  320. p->parent[0].status = DIFF_STATUS_MODIFIED;
  321. p->parent[0].mode = new_entry->ce_mode;
  322. oidcpy(&p->parent[0].oid, &new_entry->oid);
  323. p->parent[1].status = DIFF_STATUS_MODIFIED;
  324. p->parent[1].mode = old_entry->ce_mode;
  325. oidcpy(&p->parent[1].oid, &old_entry->oid);
  326. show_combined_diff(p, 2, revs->dense_combined_merges, revs);
  327. free(p);
  328. return 0;
  329. }
  330. oldmode = old_entry->ce_mode;
  331. if (mode == oldmode && oideq(oid, &old_entry->oid) && !dirty_submodule &&
  332. !revs->diffopt.flags.find_copies_harder)
  333. return 0;
  334. diff_change(&revs->diffopt, oldmode, mode,
  335. &old_entry->oid, oid, 1, !is_null_oid(oid),
  336. old_entry->name, 0, dirty_submodule);
  337. return 0;
  338. }
  339. /*
  340. * This gets a mix of an existing index and a tree, one pathname entry
  341. * at a time. The index entry may be a single stage-0 one, but it could
  342. * also be multiple unmerged entries (in which case idx_pos/idx_nr will
  343. * give you the position and number of entries in the index).
  344. */
  345. static void do_oneway_diff(struct unpack_trees_options *o,
  346. const struct cache_entry *idx,
  347. const struct cache_entry *tree)
  348. {
  349. struct rev_info *revs = o->unpack_data;
  350. int match_missing, cached;
  351. /*
  352. * i-t-a entries do not actually exist in the index (if we're
  353. * looking at its content)
  354. */
  355. if (o->index_only &&
  356. revs->diffopt.ita_invisible_in_index &&
  357. idx && ce_intent_to_add(idx)) {
  358. idx = NULL;
  359. if (!tree)
  360. return; /* nothing to diff.. */
  361. }
  362. /* if the entry is not checked out, don't examine work tree */
  363. cached = o->index_only ||
  364. (idx && ((idx->ce_flags & CE_VALID) || ce_skip_worktree(idx)));
  365. /*
  366. * Backward compatibility wart - "diff-index -m" does
  367. * not mean "do not ignore merges", but "match_missing".
  368. *
  369. * But with the revision flag parsing, that's found in
  370. * "!revs->ignore_merges".
  371. */
  372. match_missing = !revs->ignore_merges;
  373. if (cached && idx && ce_stage(idx)) {
  374. struct diff_filepair *pair;
  375. pair = diff_unmerge(&revs->diffopt, idx->name);
  376. if (tree)
  377. fill_filespec(pair->one, &tree->oid, 1,
  378. tree->ce_mode);
  379. return;
  380. }
  381. /*
  382. * Something added to the tree?
  383. */
  384. if (!tree) {
  385. show_new_file(revs, idx, cached, match_missing);
  386. return;
  387. }
  388. /*
  389. * Something removed from the tree?
  390. */
  391. if (!idx) {
  392. diff_index_show_file(revs, "-", tree, &tree->oid, 1,
  393. tree->ce_mode, 0);
  394. return;
  395. }
  396. /* Show difference between old and new */
  397. show_modified(revs, tree, idx, 1, cached, match_missing);
  398. }
  399. /*
  400. * The unpack_trees() interface is designed for merging, so
  401. * the different source entries are designed primarily for
  402. * the source trees, with the old index being really mainly
  403. * used for being replaced by the result.
  404. *
  405. * For diffing, the index is more important, and we only have a
  406. * single tree.
  407. *
  408. * We're supposed to advance o->pos to skip what we have already processed.
  409. *
  410. * This wrapper makes it all more readable, and takes care of all
  411. * the fairly complex unpack_trees() semantic requirements, including
  412. * the skipping, the path matching, the type conflict cases etc.
  413. */
  414. static int oneway_diff(const struct cache_entry * const *src,
  415. struct unpack_trees_options *o)
  416. {
  417. const struct cache_entry *idx = src[0];
  418. const struct cache_entry *tree = src[1];
  419. struct rev_info *revs = o->unpack_data;
  420. /*
  421. * Unpack-trees generates a DF/conflict entry if
  422. * there was a directory in the index and a tree
  423. * in the tree. From a diff standpoint, that's a
  424. * delete of the tree and a create of the file.
  425. */
  426. if (tree == o->df_conflict_entry)
  427. tree = NULL;
  428. if (ce_path_match(revs->diffopt.repo->index,
  429. idx ? idx : tree,
  430. &revs->prune_data, NULL)) {
  431. do_oneway_diff(o, idx, tree);
  432. if (diff_can_quit_early(&revs->diffopt)) {
  433. o->exiting_early = 1;
  434. return -1;
  435. }
  436. }
  437. return 0;
  438. }
  439. static int diff_cache(struct rev_info *revs,
  440. const struct object_id *tree_oid,
  441. const char *tree_name,
  442. int cached)
  443. {
  444. struct tree *tree;
  445. struct tree_desc t;
  446. struct unpack_trees_options opts;
  447. tree = parse_tree_indirect(tree_oid);
  448. if (!tree)
  449. return error("bad tree object %s",
  450. tree_name ? tree_name : oid_to_hex(tree_oid));
  451. memset(&opts, 0, sizeof(opts));
  452. opts.head_idx = 1;
  453. opts.index_only = cached;
  454. opts.diff_index_cached = (cached &&
  455. !revs->diffopt.flags.find_copies_harder);
  456. opts.merge = 1;
  457. opts.fn = oneway_diff;
  458. opts.unpack_data = revs;
  459. opts.src_index = revs->diffopt.repo->index;
  460. opts.dst_index = NULL;
  461. opts.pathspec = &revs->diffopt.pathspec;
  462. opts.pathspec->recursive = 1;
  463. init_tree_desc(&t, tree->buffer, tree->size);
  464. return unpack_trees(1, &t, &opts);
  465. }
  466. int run_diff_index(struct rev_info *revs, int cached)
  467. {
  468. struct object_array_entry *ent;
  469. if (revs->pending.nr != 1)
  470. BUG("run_diff_index must be passed exactly one tree");
  471. trace_performance_enter();
  472. ent = revs->pending.objects;
  473. if (diff_cache(revs, &ent->item->oid, ent->name, cached))
  474. exit(128);
  475. diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/");
  476. diffcore_fix_diff_index();
  477. diffcore_std(&revs->diffopt);
  478. diff_flush(&revs->diffopt);
  479. trace_performance_leave("diff-index");
  480. return 0;
  481. }
  482. int do_diff_cache(const struct object_id *tree_oid, struct diff_options *opt)
  483. {
  484. struct rev_info revs;
  485. repo_init_revisions(opt->repo, &revs, NULL);
  486. copy_pathspec(&revs.prune_data, &opt->pathspec);
  487. revs.diffopt = *opt;
  488. if (diff_cache(&revs, tree_oid, NULL, 1))
  489. exit(128);
  490. return 0;
  491. }
  492. int index_differs_from(struct repository *r,
  493. const char *def, const struct diff_flags *flags,
  494. int ita_invisible_in_index)
  495. {
  496. struct rev_info rev;
  497. struct setup_revision_opt opt;
  498. repo_init_revisions(r, &rev, NULL);
  499. memset(&opt, 0, sizeof(opt));
  500. opt.def = def;
  501. setup_revisions(0, NULL, &rev, &opt);
  502. rev.diffopt.flags.quick = 1;
  503. rev.diffopt.flags.exit_with_status = 1;
  504. if (flags)
  505. diff_flags_or(&rev.diffopt.flags, flags);
  506. rev.diffopt.ita_invisible_in_index = ita_invisible_in_index;
  507. run_diff_index(&rev, 1);
  508. object_array_clear(&rev.pending);
  509. return (rev.diffopt.flags.has_changes != 0);
  510. }