PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/builtin/diff.c

https://bitbucket.org/grubix/git
C | 601 lines | 424 code | 63 blank | 114 comment | 117 complexity | daf3caadc1f9707f668353e5538c85f6 MD5 | raw file
Possible License(s): Apache-2.0, BSD-2-Clause, GPL-2.0, LGPL-2.1
  1. /*
  2. * Builtin "git diff"
  3. *
  4. * Copyright (c) 2006 Junio C Hamano
  5. */
  6. #define USE_THE_INDEX_COMPATIBILITY_MACROS
  7. #include "cache.h"
  8. #include "config.h"
  9. #include "ewah/ewok.h"
  10. #include "lockfile.h"
  11. #include "color.h"
  12. #include "commit.h"
  13. #include "blob.h"
  14. #include "tag.h"
  15. #include "diff.h"
  16. #include "diff-merges.h"
  17. #include "diffcore.h"
  18. #include "revision.h"
  19. #include "log-tree.h"
  20. #include "builtin.h"
  21. #include "submodule.h"
  22. #include "oid-array.h"
  23. #define DIFF_NO_INDEX_EXPLICIT 1
  24. #define DIFF_NO_INDEX_IMPLICIT 2
  25. static const char builtin_diff_usage[] =
  26. "git diff [<options>] [<commit>] [--] [<path>...]\n"
  27. " or: git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>...]\n"
  28. " or: git diff [<options>] [--merge-base] <commit> [<commit>...] <commit> [--] [<path>...]\n"
  29. " or: git diff [<options>] <commit>...<commit> [--] [<path>...]\n"
  30. " or: git diff [<options>] <blob> <blob>\n"
  31. " or: git diff [<options>] --no-index [--] <path> <path>\n"
  32. COMMON_DIFF_OPTIONS_HELP;
  33. static const char *blob_path(struct object_array_entry *entry)
  34. {
  35. return entry->path ? entry->path : entry->name;
  36. }
  37. static void stuff_change(struct diff_options *opt,
  38. unsigned old_mode, unsigned new_mode,
  39. const struct object_id *old_oid,
  40. const struct object_id *new_oid,
  41. int old_oid_valid,
  42. int new_oid_valid,
  43. const char *old_path,
  44. const char *new_path)
  45. {
  46. struct diff_filespec *one, *two;
  47. if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
  48. oideq(old_oid, new_oid) && (old_mode == new_mode))
  49. return;
  50. if (opt->flags.reverse_diff) {
  51. SWAP(old_mode, new_mode);
  52. SWAP(old_oid, new_oid);
  53. SWAP(old_path, new_path);
  54. }
  55. if (opt->prefix &&
  56. (strncmp(old_path, opt->prefix, opt->prefix_length) ||
  57. strncmp(new_path, opt->prefix, opt->prefix_length)))
  58. return;
  59. one = alloc_filespec(old_path);
  60. two = alloc_filespec(new_path);
  61. fill_filespec(one, old_oid, old_oid_valid, old_mode);
  62. fill_filespec(two, new_oid, new_oid_valid, new_mode);
  63. diff_queue(&diff_queued_diff, one, two);
  64. }
  65. static int builtin_diff_b_f(struct rev_info *revs,
  66. int argc, const char **argv,
  67. struct object_array_entry **blob)
  68. {
  69. /* Blob vs file in the working tree*/
  70. struct stat st;
  71. const char *path;
  72. if (argc > 1)
  73. usage(builtin_diff_usage);
  74. GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
  75. path = revs->prune_data.items[0].match;
  76. if (lstat(path, &st))
  77. die_errno(_("failed to stat '%s'"), path);
  78. if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
  79. die(_("'%s': not a regular file or symlink"), path);
  80. diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
  81. if (blob[0]->mode == S_IFINVALID)
  82. blob[0]->mode = canon_mode(st.st_mode);
  83. stuff_change(&revs->diffopt,
  84. blob[0]->mode, canon_mode(st.st_mode),
  85. &blob[0]->item->oid, null_oid(),
  86. 1, 0,
  87. blob[0]->path ? blob[0]->path : path,
  88. path);
  89. diffcore_std(&revs->diffopt);
  90. diff_flush(&revs->diffopt);
  91. return 0;
  92. }
  93. static int builtin_diff_blobs(struct rev_info *revs,
  94. int argc, const char **argv,
  95. struct object_array_entry **blob)
  96. {
  97. const unsigned mode = canon_mode(S_IFREG | 0644);
  98. if (argc > 1)
  99. usage(builtin_diff_usage);
  100. if (blob[0]->mode == S_IFINVALID)
  101. blob[0]->mode = mode;
  102. if (blob[1]->mode == S_IFINVALID)
  103. blob[1]->mode = mode;
  104. stuff_change(&revs->diffopt,
  105. blob[0]->mode, blob[1]->mode,
  106. &blob[0]->item->oid, &blob[1]->item->oid,
  107. 1, 1,
  108. blob_path(blob[0]), blob_path(blob[1]));
  109. diffcore_std(&revs->diffopt);
  110. diff_flush(&revs->diffopt);
  111. return 0;
  112. }
  113. static int builtin_diff_index(struct rev_info *revs,
  114. int argc, const char **argv)
  115. {
  116. unsigned int option = 0;
  117. while (1 < argc) {
  118. const char *arg = argv[1];
  119. if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
  120. option |= DIFF_INDEX_CACHED;
  121. else if (!strcmp(arg, "--merge-base"))
  122. option |= DIFF_INDEX_MERGE_BASE;
  123. else
  124. usage(builtin_diff_usage);
  125. argv++; argc--;
  126. }
  127. /*
  128. * Make sure there is one revision (i.e. pending object),
  129. * and there is no revision filtering parameters.
  130. */
  131. if (revs->pending.nr != 1 ||
  132. revs->max_count != -1 || revs->min_age != -1 ||
  133. revs->max_age != -1)
  134. usage(builtin_diff_usage);
  135. if (!(option & DIFF_INDEX_CACHED)) {
  136. setup_work_tree();
  137. if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
  138. perror("read_cache_preload");
  139. return -1;
  140. }
  141. } else if (read_cache() < 0) {
  142. perror("read_cache");
  143. return -1;
  144. }
  145. return run_diff_index(revs, option);
  146. }
  147. static int builtin_diff_tree(struct rev_info *revs,
  148. int argc, const char **argv,
  149. struct object_array_entry *ent0,
  150. struct object_array_entry *ent1)
  151. {
  152. const struct object_id *(oid[2]);
  153. struct object_id mb_oid;
  154. int merge_base = 0;
  155. while (1 < argc) {
  156. const char *arg = argv[1];
  157. if (!strcmp(arg, "--merge-base"))
  158. merge_base = 1;
  159. else
  160. usage(builtin_diff_usage);
  161. argv++; argc--;
  162. }
  163. if (merge_base) {
  164. diff_get_merge_base(revs, &mb_oid);
  165. oid[0] = &mb_oid;
  166. oid[1] = &revs->pending.objects[1].item->oid;
  167. } else {
  168. int swap = 0;
  169. /*
  170. * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
  171. * swap them.
  172. */
  173. if (ent1->item->flags & UNINTERESTING)
  174. swap = 1;
  175. oid[swap] = &ent0->item->oid;
  176. oid[1 - swap] = &ent1->item->oid;
  177. }
  178. diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
  179. log_tree_diff_flush(revs);
  180. return 0;
  181. }
  182. static int builtin_diff_combined(struct rev_info *revs,
  183. int argc, const char **argv,
  184. struct object_array_entry *ent,
  185. int ents)
  186. {
  187. struct oid_array parents = OID_ARRAY_INIT;
  188. int i;
  189. if (argc > 1)
  190. usage(builtin_diff_usage);
  191. diff_merges_set_dense_combined_if_unset(revs);
  192. for (i = 1; i < ents; i++)
  193. oid_array_append(&parents, &ent[i].item->oid);
  194. diff_tree_combined(&ent[0].item->oid, &parents, revs);
  195. oid_array_clear(&parents);
  196. return 0;
  197. }
  198. static void refresh_index_quietly(void)
  199. {
  200. struct lock_file lock_file = LOCK_INIT;
  201. int fd;
  202. fd = hold_locked_index(&lock_file, 0);
  203. if (fd < 0)
  204. return;
  205. discard_cache();
  206. read_cache();
  207. refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
  208. repo_update_index_if_able(the_repository, &lock_file);
  209. }
  210. static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
  211. {
  212. unsigned int options = 0;
  213. while (1 < argc && argv[1][0] == '-') {
  214. if (!strcmp(argv[1], "--base"))
  215. revs->max_count = 1;
  216. else if (!strcmp(argv[1], "--ours"))
  217. revs->max_count = 2;
  218. else if (!strcmp(argv[1], "--theirs"))
  219. revs->max_count = 3;
  220. else if (!strcmp(argv[1], "-q"))
  221. options |= DIFF_SILENT_ON_REMOVED;
  222. else if (!strcmp(argv[1], "-h"))
  223. usage(builtin_diff_usage);
  224. else
  225. return error(_("invalid option: %s"), argv[1]);
  226. argv++; argc--;
  227. }
  228. /*
  229. * "diff --base" should not combine merges because it was not
  230. * asked to. "diff -c" should not densify (if the user wants
  231. * dense one, --cc can be explicitly asked for, or just rely
  232. * on the default).
  233. */
  234. if (revs->max_count == -1 &&
  235. (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
  236. diff_merges_set_dense_combined_if_unset(revs);
  237. setup_work_tree();
  238. if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
  239. perror("read_cache_preload");
  240. return -1;
  241. }
  242. return run_diff_files(revs, options);
  243. }
  244. struct symdiff {
  245. struct bitmap *skip;
  246. int warn;
  247. const char *base, *left, *right;
  248. };
  249. /*
  250. * Check for symmetric-difference arguments, and if present, arrange
  251. * everything we need to know to handle them correctly. As a bonus,
  252. * weed out all bogus range-based revision specifications, e.g.,
  253. * "git diff A..B C..D" or "git diff A..B C" get rejected.
  254. *
  255. * For an actual symmetric diff, *symdiff is set this way:
  256. *
  257. * - its skip is non-NULL and marks *all* rev->pending.objects[i]
  258. * indices that the caller should ignore (extra merge bases, of
  259. * which there might be many, and A in A...B). Note that the
  260. * chosen merge base and right side are NOT marked.
  261. * - warn is set if there are multiple merge bases.
  262. * - base, left, and right point to the names to use in a
  263. * warning about multiple merge bases.
  264. *
  265. * If there is no symmetric diff argument, sym->skip is NULL and
  266. * sym->warn is cleared. The remaining fields are not set.
  267. */
  268. static void symdiff_prepare(struct rev_info *rev, struct symdiff *sym)
  269. {
  270. int i, is_symdiff = 0, basecount = 0, othercount = 0;
  271. int lpos = -1, rpos = -1, basepos = -1;
  272. struct bitmap *map = NULL;
  273. /*
  274. * Use the whence fields to find merge bases and left and
  275. * right parts of symmetric difference, so that we do not
  276. * depend on the order that revisions are parsed. If there
  277. * are any revs that aren't from these sources, we have a
  278. * "git diff C A...B" or "git diff A...B C" case. Or we
  279. * could even get "git diff A...B C...E", for instance.
  280. *
  281. * If we don't have just one merge base, we pick one
  282. * at random.
  283. *
  284. * NB: REV_CMD_LEFT, REV_CMD_RIGHT are also used for A..B,
  285. * so we must check for SYMMETRIC_LEFT too. The two arrays
  286. * rev->pending.objects and rev->cmdline.rev are parallel.
  287. */
  288. for (i = 0; i < rev->cmdline.nr; i++) {
  289. struct object *obj = rev->pending.objects[i].item;
  290. switch (rev->cmdline.rev[i].whence) {
  291. case REV_CMD_MERGE_BASE:
  292. if (basepos < 0)
  293. basepos = i;
  294. basecount++;
  295. break; /* do mark all bases */
  296. case REV_CMD_LEFT:
  297. if (lpos >= 0)
  298. usage(builtin_diff_usage);
  299. lpos = i;
  300. if (obj->flags & SYMMETRIC_LEFT) {
  301. is_symdiff = 1;
  302. break; /* do mark A */
  303. }
  304. continue;
  305. case REV_CMD_RIGHT:
  306. if (rpos >= 0)
  307. usage(builtin_diff_usage);
  308. rpos = i;
  309. continue; /* don't mark B */
  310. case REV_CMD_PARENTS_ONLY:
  311. case REV_CMD_REF:
  312. case REV_CMD_REV:
  313. othercount++;
  314. continue;
  315. }
  316. if (!map)
  317. map = bitmap_new();
  318. bitmap_set(map, i);
  319. }
  320. /*
  321. * Forbid any additional revs for both A...B and A..B.
  322. */
  323. if (lpos >= 0 && othercount > 0)
  324. usage(builtin_diff_usage);
  325. if (!is_symdiff) {
  326. bitmap_free(map);
  327. sym->warn = 0;
  328. sym->skip = NULL;
  329. return;
  330. }
  331. sym->left = rev->pending.objects[lpos].name;
  332. sym->right = rev->pending.objects[rpos].name;
  333. if (basecount == 0)
  334. die(_("%s...%s: no merge base"), sym->left, sym->right);
  335. sym->base = rev->pending.objects[basepos].name;
  336. bitmap_unset(map, basepos); /* unmark the base we want */
  337. sym->warn = basecount > 1;
  338. sym->skip = map;
  339. }
  340. int cmd_diff(int argc, const char **argv, const char *prefix)
  341. {
  342. int i;
  343. struct rev_info rev;
  344. struct object_array ent = OBJECT_ARRAY_INIT;
  345. int blobs = 0, paths = 0;
  346. struct object_array_entry *blob[2];
  347. int nongit = 0, no_index = 0;
  348. int result = 0;
  349. struct symdiff sdiff;
  350. /*
  351. * We could get N tree-ish in the rev.pending_objects list.
  352. * Also there could be M blobs there, and P pathspecs. --cached may
  353. * also be present.
  354. *
  355. * N=0, M=0:
  356. * cache vs files (diff-files)
  357. *
  358. * N=0, M=0, --cached:
  359. * HEAD vs cache (diff-index --cached)
  360. *
  361. * N=0, M=2:
  362. * compare two random blobs. P must be zero.
  363. *
  364. * N=0, M=1, P=1:
  365. * compare a blob with a working tree file.
  366. *
  367. * N=1, M=0:
  368. * tree vs files (diff-index)
  369. *
  370. * N=1, M=0, --cached:
  371. * tree vs cache (diff-index --cached)
  372. *
  373. * N=2, M=0:
  374. * tree vs tree (diff-tree)
  375. *
  376. * N=0, M=0, P=2:
  377. * compare two filesystem entities (aka --no-index).
  378. *
  379. * Other cases are errors.
  380. */
  381. /* Were we asked to do --no-index explicitly? */
  382. for (i = 1; i < argc; i++) {
  383. if (!strcmp(argv[i], "--")) {
  384. i++;
  385. break;
  386. }
  387. if (!strcmp(argv[i], "--no-index"))
  388. no_index = DIFF_NO_INDEX_EXPLICIT;
  389. if (argv[i][0] != '-')
  390. break;
  391. }
  392. prefix = setup_git_directory_gently(&nongit);
  393. if (!nongit) {
  394. prepare_repo_settings(the_repository);
  395. the_repository->settings.command_requires_full_index = 0;
  396. }
  397. if (!no_index) {
  398. /*
  399. * Treat git diff with at least one path outside of the
  400. * repo the same as if the command would have been executed
  401. * outside of a git repository. In this case it behaves
  402. * the same way as "git diff --no-index <a> <b>", which acts
  403. * as a colourful "diff" replacement.
  404. */
  405. if (nongit || ((argc == i + 2) &&
  406. (!path_inside_repo(prefix, argv[i]) ||
  407. !path_inside_repo(prefix, argv[i + 1]))))
  408. no_index = DIFF_NO_INDEX_IMPLICIT;
  409. }
  410. init_diff_ui_defaults();
  411. git_config(git_diff_ui_config, NULL);
  412. prefix = precompose_argv_prefix(argc, argv, prefix);
  413. repo_init_revisions(the_repository, &rev, prefix);
  414. /* Set up defaults that will apply to both no-index and regular diffs. */
  415. rev.diffopt.stat_width = -1;
  416. rev.diffopt.stat_graph_width = -1;
  417. rev.diffopt.flags.allow_external = 1;
  418. rev.diffopt.flags.allow_textconv = 1;
  419. /* If this is a no-index diff, just run it and exit there. */
  420. if (no_index)
  421. exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
  422. argc, argv));
  423. /*
  424. * Otherwise, we are doing the usual "git" diff; set up any
  425. * further defaults that apply to regular diffs.
  426. */
  427. rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
  428. /*
  429. * Default to intent-to-add entries invisible in the
  430. * index. This makes them show up as new files in diff-files
  431. * and not at all in diff-cached.
  432. */
  433. rev.diffopt.ita_invisible_in_index = 1;
  434. if (nongit)
  435. die(_("Not a git repository"));
  436. argc = setup_revisions(argc, argv, &rev, NULL);
  437. if (!rev.diffopt.output_format) {
  438. rev.diffopt.output_format = DIFF_FORMAT_PATCH;
  439. diff_setup_done(&rev.diffopt);
  440. }
  441. rev.diffopt.flags.recursive = 1;
  442. rev.diffopt.rotate_to_strict = 1;
  443. setup_diff_pager(&rev.diffopt);
  444. /*
  445. * Do we have --cached and not have a pending object, then
  446. * default to HEAD by hand. Eek.
  447. */
  448. if (!rev.pending.nr) {
  449. int i;
  450. for (i = 1; i < argc; i++) {
  451. const char *arg = argv[i];
  452. if (!strcmp(arg, "--"))
  453. break;
  454. else if (!strcmp(arg, "--cached") ||
  455. !strcmp(arg, "--staged")) {
  456. add_head_to_pending(&rev);
  457. if (!rev.pending.nr) {
  458. struct tree *tree;
  459. tree = lookup_tree(the_repository,
  460. the_repository->hash_algo->empty_tree);
  461. add_pending_object(&rev, &tree->object, "HEAD");
  462. }
  463. break;
  464. }
  465. }
  466. }
  467. symdiff_prepare(&rev, &sdiff);
  468. for (i = 0; i < rev.pending.nr; i++) {
  469. struct object_array_entry *entry = &rev.pending.objects[i];
  470. struct object *obj = entry->item;
  471. const char *name = entry->name;
  472. int flags = (obj->flags & UNINTERESTING);
  473. if (!obj->parsed)
  474. obj = parse_object(the_repository, &obj->oid);
  475. obj = deref_tag(the_repository, obj, NULL, 0);
  476. if (!obj)
  477. die(_("invalid object '%s' given."), name);
  478. if (obj->type == OBJ_COMMIT)
  479. obj = &get_commit_tree(((struct commit *)obj))->object;
  480. if (obj->type == OBJ_TREE) {
  481. if (sdiff.skip && bitmap_get(sdiff.skip, i))
  482. continue;
  483. obj->flags |= flags;
  484. add_object_array(obj, name, &ent);
  485. } else if (obj->type == OBJ_BLOB) {
  486. if (2 <= blobs)
  487. die(_("more than two blobs given: '%s'"), name);
  488. blob[blobs] = entry;
  489. blobs++;
  490. } else {
  491. die(_("unhandled object '%s' given."), name);
  492. }
  493. }
  494. if (rev.prune_data.nr)
  495. paths += rev.prune_data.nr;
  496. /*
  497. * Now, do the arguments look reasonable?
  498. */
  499. if (!ent.nr) {
  500. switch (blobs) {
  501. case 0:
  502. result = builtin_diff_files(&rev, argc, argv);
  503. break;
  504. case 1:
  505. if (paths != 1)
  506. usage(builtin_diff_usage);
  507. result = builtin_diff_b_f(&rev, argc, argv, blob);
  508. break;
  509. case 2:
  510. if (paths)
  511. usage(builtin_diff_usage);
  512. result = builtin_diff_blobs(&rev, argc, argv, blob);
  513. break;
  514. default:
  515. usage(builtin_diff_usage);
  516. }
  517. }
  518. else if (blobs)
  519. usage(builtin_diff_usage);
  520. else if (ent.nr == 1)
  521. result = builtin_diff_index(&rev, argc, argv);
  522. else if (ent.nr == 2) {
  523. if (sdiff.warn)
  524. warning(_("%s...%s: multiple merge bases, using %s"),
  525. sdiff.left, sdiff.right, sdiff.base);
  526. result = builtin_diff_tree(&rev, argc, argv,
  527. &ent.objects[0], &ent.objects[1]);
  528. } else
  529. result = builtin_diff_combined(&rev, argc, argv,
  530. ent.objects, ent.nr);
  531. result = diff_result_code(&rev.diffopt, result);
  532. if (1 < rev.diffopt.skip_stat_unmatch)
  533. refresh_index_quietly();
  534. UNLEAK(rev);
  535. UNLEAK(ent);
  536. UNLEAK(blob);
  537. return result;
  538. }