PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/builtin/diff.c

https://gitlab.com/oyvholm/git
C | 474 lines | 345 code | 54 blank | 75 comment | 107 complexity | 77a737ef9101ae130b76aaae3f128548 MD5 | raw file
  1. /*
  2. * Builtin "git diff"
  3. *
  4. * Copyright (c) 2006 Junio C Hamano
  5. */
  6. #include "cache.h"
  7. #include "lockfile.h"
  8. #include "color.h"
  9. #include "commit.h"
  10. #include "blob.h"
  11. #include "tag.h"
  12. #include "diff.h"
  13. #include "diffcore.h"
  14. #include "revision.h"
  15. #include "log-tree.h"
  16. #include "builtin.h"
  17. #include "submodule.h"
  18. #include "sha1-array.h"
  19. #define DIFF_NO_INDEX_EXPLICIT 1
  20. #define DIFF_NO_INDEX_IMPLICIT 2
  21. struct blobinfo {
  22. unsigned char sha1[20];
  23. const char *name;
  24. unsigned mode;
  25. };
  26. static const char builtin_diff_usage[] =
  27. "git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
  28. static void stuff_change(struct diff_options *opt,
  29. unsigned old_mode, unsigned new_mode,
  30. const unsigned char *old_sha1,
  31. const unsigned char *new_sha1,
  32. int old_sha1_valid,
  33. int new_sha1_valid,
  34. const char *old_name,
  35. const char *new_name)
  36. {
  37. struct diff_filespec *one, *two;
  38. if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
  39. !hashcmp(old_sha1, new_sha1) && (old_mode == new_mode))
  40. return;
  41. if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
  42. unsigned tmp;
  43. const unsigned char *tmp_u;
  44. const char *tmp_c;
  45. tmp = old_mode; old_mode = new_mode; new_mode = tmp;
  46. tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
  47. tmp_c = old_name; old_name = new_name; new_name = tmp_c;
  48. }
  49. if (opt->prefix &&
  50. (strncmp(old_name, opt->prefix, opt->prefix_length) ||
  51. strncmp(new_name, opt->prefix, opt->prefix_length)))
  52. return;
  53. one = alloc_filespec(old_name);
  54. two = alloc_filespec(new_name);
  55. fill_filespec(one, old_sha1, old_sha1_valid, old_mode);
  56. fill_filespec(two, new_sha1, new_sha1_valid, new_mode);
  57. diff_queue(&diff_queued_diff, one, two);
  58. }
  59. static int builtin_diff_b_f(struct rev_info *revs,
  60. int argc, const char **argv,
  61. struct blobinfo *blob)
  62. {
  63. /* Blob vs file in the working tree*/
  64. struct stat st;
  65. const char *path;
  66. if (argc > 1)
  67. usage(builtin_diff_usage);
  68. GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
  69. path = revs->prune_data.items[0].match;
  70. if (lstat(path, &st))
  71. die_errno(_("failed to stat '%s'"), path);
  72. if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
  73. die(_("'%s': not a regular file or symlink"), path);
  74. diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
  75. if (blob[0].mode == S_IFINVALID)
  76. blob[0].mode = canon_mode(st.st_mode);
  77. stuff_change(&revs->diffopt,
  78. blob[0].mode, canon_mode(st.st_mode),
  79. blob[0].sha1, null_sha1,
  80. 1, 0,
  81. path, path);
  82. diffcore_std(&revs->diffopt);
  83. diff_flush(&revs->diffopt);
  84. return 0;
  85. }
  86. static int builtin_diff_blobs(struct rev_info *revs,
  87. int argc, const char **argv,
  88. struct blobinfo *blob)
  89. {
  90. unsigned mode = canon_mode(S_IFREG | 0644);
  91. if (argc > 1)
  92. usage(builtin_diff_usage);
  93. if (blob[0].mode == S_IFINVALID)
  94. blob[0].mode = mode;
  95. if (blob[1].mode == S_IFINVALID)
  96. blob[1].mode = mode;
  97. stuff_change(&revs->diffopt,
  98. blob[0].mode, blob[1].mode,
  99. blob[0].sha1, blob[1].sha1,
  100. 1, 1,
  101. blob[0].name, blob[1].name);
  102. diffcore_std(&revs->diffopt);
  103. diff_flush(&revs->diffopt);
  104. return 0;
  105. }
  106. static int builtin_diff_index(struct rev_info *revs,
  107. int argc, const char **argv)
  108. {
  109. int cached = 0;
  110. while (1 < argc) {
  111. const char *arg = argv[1];
  112. if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
  113. cached = 1;
  114. else
  115. usage(builtin_diff_usage);
  116. argv++; argc--;
  117. }
  118. /*
  119. * Make sure there is one revision (i.e. pending object),
  120. * and there is no revision filtering parameters.
  121. */
  122. if (revs->pending.nr != 1 ||
  123. revs->max_count != -1 || revs->min_age != -1 ||
  124. revs->max_age != -1)
  125. usage(builtin_diff_usage);
  126. if (!cached) {
  127. setup_work_tree();
  128. if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
  129. perror("read_cache_preload");
  130. return -1;
  131. }
  132. } else if (read_cache() < 0) {
  133. perror("read_cache");
  134. return -1;
  135. }
  136. return run_diff_index(revs, cached);
  137. }
  138. static int builtin_diff_tree(struct rev_info *revs,
  139. int argc, const char **argv,
  140. struct object_array_entry *ent0,
  141. struct object_array_entry *ent1)
  142. {
  143. const unsigned char *(sha1[2]);
  144. int swap = 0;
  145. if (argc > 1)
  146. usage(builtin_diff_usage);
  147. /*
  148. * We saw two trees, ent0 and ent1. If ent1 is uninteresting,
  149. * swap them.
  150. */
  151. if (ent1->item->flags & UNINTERESTING)
  152. swap = 1;
  153. sha1[swap] = ent0->item->oid.hash;
  154. sha1[1 - swap] = ent1->item->oid.hash;
  155. diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
  156. log_tree_diff_flush(revs);
  157. return 0;
  158. }
  159. static int builtin_diff_combined(struct rev_info *revs,
  160. int argc, const char **argv,
  161. struct object_array_entry *ent,
  162. int ents)
  163. {
  164. struct sha1_array parents = SHA1_ARRAY_INIT;
  165. int i;
  166. if (argc > 1)
  167. usage(builtin_diff_usage);
  168. if (!revs->dense_combined_merges && !revs->combine_merges)
  169. revs->dense_combined_merges = revs->combine_merges = 1;
  170. for (i = 1; i < ents; i++)
  171. sha1_array_append(&parents, ent[i].item->oid.hash);
  172. diff_tree_combined(ent[0].item->oid.hash, &parents,
  173. revs->dense_combined_merges, revs);
  174. sha1_array_clear(&parents);
  175. return 0;
  176. }
  177. static void refresh_index_quietly(void)
  178. {
  179. struct lock_file *lock_file;
  180. int fd;
  181. lock_file = xcalloc(1, sizeof(struct lock_file));
  182. fd = hold_locked_index(lock_file, 0);
  183. if (fd < 0)
  184. return;
  185. discard_cache();
  186. read_cache();
  187. refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
  188. update_index_if_able(&the_index, lock_file);
  189. }
  190. static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
  191. {
  192. unsigned int options = 0;
  193. while (1 < argc && argv[1][0] == '-') {
  194. if (!strcmp(argv[1], "--base"))
  195. revs->max_count = 1;
  196. else if (!strcmp(argv[1], "--ours"))
  197. revs->max_count = 2;
  198. else if (!strcmp(argv[1], "--theirs"))
  199. revs->max_count = 3;
  200. else if (!strcmp(argv[1], "-q"))
  201. options |= DIFF_SILENT_ON_REMOVED;
  202. else if (!strcmp(argv[1], "-h"))
  203. usage(builtin_diff_usage);
  204. else
  205. return error(_("invalid option: %s"), argv[1]);
  206. argv++; argc--;
  207. }
  208. /*
  209. * "diff --base" should not combine merges because it was not
  210. * asked to. "diff -c" should not densify (if the user wants
  211. * dense one, --cc can be explicitly asked for, or just rely
  212. * on the default).
  213. */
  214. if (revs->max_count == -1 && !revs->combine_merges &&
  215. (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
  216. revs->combine_merges = revs->dense_combined_merges = 1;
  217. setup_work_tree();
  218. if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
  219. perror("read_cache_preload");
  220. return -1;
  221. }
  222. return run_diff_files(revs, options);
  223. }
  224. int cmd_diff(int argc, const char **argv, const char *prefix)
  225. {
  226. int i;
  227. struct rev_info rev;
  228. struct object_array ent = OBJECT_ARRAY_INIT;
  229. int blobs = 0, paths = 0;
  230. struct blobinfo blob[2];
  231. int nongit = 0, no_index = 0;
  232. int result = 0;
  233. /*
  234. * We could get N tree-ish in the rev.pending_objects list.
  235. * Also there could be M blobs there, and P pathspecs.
  236. *
  237. * N=0, M=0:
  238. * cache vs files (diff-files)
  239. * N=0, M=2:
  240. * compare two random blobs. P must be zero.
  241. * N=0, M=1, P=1:
  242. * compare a blob with a working tree file.
  243. *
  244. * N=1, M=0:
  245. * tree vs cache (diff-index --cached)
  246. *
  247. * N=2, M=0:
  248. * tree vs tree (diff-tree)
  249. *
  250. * N=0, M=0, P=2:
  251. * compare two filesystem entities (aka --no-index).
  252. *
  253. * Other cases are errors.
  254. */
  255. /* Were we asked to do --no-index explicitly? */
  256. for (i = 1; i < argc; i++) {
  257. if (!strcmp(argv[i], "--")) {
  258. i++;
  259. break;
  260. }
  261. if (!strcmp(argv[i], "--no-index"))
  262. no_index = DIFF_NO_INDEX_EXPLICIT;
  263. if (argv[i][0] != '-')
  264. break;
  265. }
  266. prefix = setup_git_directory_gently(&nongit);
  267. if (!no_index) {
  268. /*
  269. * Treat git diff with at least one path outside of the
  270. * repo the same as if the command would have been executed
  271. * outside of a git repository. In this case it behaves
  272. * the same way as "git diff --no-index <a> <b>", which acts
  273. * as a colourful "diff" replacement.
  274. */
  275. if (nongit || ((argc == i + 2) &&
  276. (!path_inside_repo(prefix, argv[i]) ||
  277. !path_inside_repo(prefix, argv[i + 1]))))
  278. no_index = DIFF_NO_INDEX_IMPLICIT;
  279. }
  280. if (!no_index)
  281. gitmodules_config();
  282. init_diff_ui_defaults();
  283. git_config(git_diff_ui_config, NULL);
  284. precompose_argv(argc, argv);
  285. init_revisions(&rev, prefix);
  286. if (no_index && argc != i + 2) {
  287. if (no_index == DIFF_NO_INDEX_IMPLICIT) {
  288. /*
  289. * There was no --no-index and there were not two
  290. * paths. It is possible that the user intended
  291. * to do an inside-repository operation.
  292. */
  293. fprintf(stderr, "Not a git repository\n");
  294. fprintf(stderr,
  295. "To compare two paths outside a working tree:\n");
  296. }
  297. /* Give the usage message for non-repository usage and exit. */
  298. usagef("git diff %s <path> <path>",
  299. no_index == DIFF_NO_INDEX_EXPLICIT ?
  300. "--no-index" : "[--no-index]");
  301. }
  302. if (no_index)
  303. /* If this is a no-index diff, just run it and exit there. */
  304. diff_no_index(&rev, argc, argv);
  305. /* Otherwise, we are doing the usual "git" diff */
  306. rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
  307. /* Scale to real terminal size and respect statGraphWidth config */
  308. rev.diffopt.stat_width = -1;
  309. rev.diffopt.stat_graph_width = -1;
  310. /* Default to let external and textconv be used */
  311. DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
  312. DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
  313. if (nongit)
  314. die(_("Not a git repository"));
  315. argc = setup_revisions(argc, argv, &rev, NULL);
  316. if (!rev.diffopt.output_format) {
  317. rev.diffopt.output_format = DIFF_FORMAT_PATCH;
  318. diff_setup_done(&rev.diffopt);
  319. }
  320. DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
  321. setup_diff_pager(&rev.diffopt);
  322. /*
  323. * Do we have --cached and not have a pending object, then
  324. * default to HEAD by hand. Eek.
  325. */
  326. if (!rev.pending.nr) {
  327. int i;
  328. for (i = 1; i < argc; i++) {
  329. const char *arg = argv[i];
  330. if (!strcmp(arg, "--"))
  331. break;
  332. else if (!strcmp(arg, "--cached") ||
  333. !strcmp(arg, "--staged")) {
  334. add_head_to_pending(&rev);
  335. if (!rev.pending.nr) {
  336. struct tree *tree;
  337. tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
  338. add_pending_object(&rev, &tree->object, "HEAD");
  339. }
  340. break;
  341. }
  342. }
  343. }
  344. for (i = 0; i < rev.pending.nr; i++) {
  345. struct object_array_entry *entry = &rev.pending.objects[i];
  346. struct object *obj = entry->item;
  347. const char *name = entry->name;
  348. int flags = (obj->flags & UNINTERESTING);
  349. if (!obj->parsed)
  350. obj = parse_object(obj->oid.hash);
  351. obj = deref_tag(obj, NULL, 0);
  352. if (!obj)
  353. die(_("invalid object '%s' given."), name);
  354. if (obj->type == OBJ_COMMIT)
  355. obj = &((struct commit *)obj)->tree->object;
  356. if (obj->type == OBJ_TREE) {
  357. obj->flags |= flags;
  358. add_object_array(obj, name, &ent);
  359. } else if (obj->type == OBJ_BLOB) {
  360. if (2 <= blobs)
  361. die(_("more than two blobs given: '%s'"), name);
  362. hashcpy(blob[blobs].sha1, obj->oid.hash);
  363. blob[blobs].name = name;
  364. blob[blobs].mode = entry->mode;
  365. blobs++;
  366. } else {
  367. die(_("unhandled object '%s' given."), name);
  368. }
  369. }
  370. if (rev.prune_data.nr)
  371. paths += rev.prune_data.nr;
  372. /*
  373. * Now, do the arguments look reasonable?
  374. */
  375. if (!ent.nr) {
  376. switch (blobs) {
  377. case 0:
  378. result = builtin_diff_files(&rev, argc, argv);
  379. break;
  380. case 1:
  381. if (paths != 1)
  382. usage(builtin_diff_usage);
  383. result = builtin_diff_b_f(&rev, argc, argv, blob);
  384. break;
  385. case 2:
  386. if (paths)
  387. usage(builtin_diff_usage);
  388. result = builtin_diff_blobs(&rev, argc, argv, blob);
  389. break;
  390. default:
  391. usage(builtin_diff_usage);
  392. }
  393. }
  394. else if (blobs)
  395. usage(builtin_diff_usage);
  396. else if (ent.nr == 1)
  397. result = builtin_diff_index(&rev, argc, argv);
  398. else if (ent.nr == 2)
  399. result = builtin_diff_tree(&rev, argc, argv,
  400. &ent.objects[0], &ent.objects[1]);
  401. else if (ent.objects[0].item->flags & UNINTERESTING) {
  402. /*
  403. * diff A...B where there is at least one merge base
  404. * between A and B. We have ent.objects[0] ==
  405. * merge-base, ent.objects[ents-2] == A, and
  406. * ent.objects[ents-1] == B. Show diff between the
  407. * base and B. Note that we pick one merge base at
  408. * random if there are more than one.
  409. */
  410. result = builtin_diff_tree(&rev, argc, argv,
  411. &ent.objects[0],
  412. &ent.objects[ent.nr-1]);
  413. } else
  414. result = builtin_diff_combined(&rev, argc, argv,
  415. ent.objects, ent.nr);
  416. result = diff_result_code(&rev.diffopt, result);
  417. if (1 < rev.diffopt.skip_stat_unmatch)
  418. refresh_index_quietly();
  419. return result;
  420. }