blob: cf09559e422d66a06936eef050203498f00f4559 [file] [log] [blame]
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +07001#define USE_THE_INDEX_COMPATIBILITY_MACROS
Linus Torvaldse74f8f62005-04-19 21:00:09 -07002#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Junio C Hamanof92a4462005-04-27 09:21:00 -07004#include "diff.h"
Sergey Organov19b25172021-05-21 00:46:59 +03005#include "diff-merges.h"
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -07006#include "commit.h"
7#include "revision.h"
Peter Eriksene8cc9cd2006-05-23 14:15:36 +02008#include "builtin.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +02009#include "submodule.h"
Linus Torvaldsb5af9102005-04-22 17:15:28 -070010
Petr Baudis4d1f1192005-07-29 11:01:26 +020011static const char diff_cache_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020012"git diff-index [-m] [--cached] "
Alex Henrie9c9b4f22015-01-13 00:44:47 -070013"[<common-diff-options>] <tree-ish> [<path>...]"
Junio C Hamanodda2d792005-07-13 12:52:35 -070014COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanoc5bac172005-04-20 19:49:16 -070015
Linus Torvaldsa633fca2006-07-28 22:44:25 -070016int cmd_diff_index(int argc, const char **argv, const char *prefix)
Linus Torvaldse74f8f62005-04-19 21:00:09 -070017{
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070018 struct rev_info rev;
Denton Liu4c3fe822020-09-20 04:22:22 -070019 unsigned int option = 0;
Linus Torvalds6c56c532005-05-24 18:10:11 -070020 int i;
Alex Riesen41bbf9d2007-03-14 01:17:04 +010021 int result;
Linus Torvaldse74f8f62005-04-19 21:00:09 -070022
Junio C Hamano5a88f972017-06-01 13:38:16 +090023 if (argc == 2 && !strcmp(argv[1], "-h"))
24 usage(diff_cache_usage);
25
Marc Branchaud37590ce2017-05-08 12:03:37 -040026 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +020027 repo_init_revisions(the_repository, &rev, prefix);
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070028 rev.abbrev = 0;
Torsten Bögershausen5c327502021-02-03 17:28:23 +010029 prefix = precompose_argv_prefix(argc, argv, prefix);
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070030
Sergey Organov19b25172021-05-21 00:46:59 +030031 /*
32 * We need no diff for merges options, and we need to avoid conflict
33 * with our own meaning of "-m".
34 */
35 diff_merges_suppress_options_parsing();
36
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070037 argc = setup_revisions(argc, argv, &rev, NULL);
Linus Torvalds6c56c532005-05-24 18:10:11 -070038 for (i = 1; i < argc; i++) {
39 const char *arg = argv[i];
Junio C Hamanoa6080a02007-06-07 00:04:01 -070040
Junio C Hamano5c21ac02006-04-22 03:58:04 -070041 if (!strcmp(arg, "--cached"))
Denton Liu4c3fe822020-09-20 04:22:22 -070042 option |= DIFF_INDEX_CACHED;
Denton Liu0f5a1d42020-09-20 04:22:25 -070043 else if (!strcmp(arg, "--merge-base"))
44 option |= DIFF_INDEX_MERGE_BASE;
Sergey Organov19b25172021-05-21 00:46:59 +030045 else if (!strcmp(arg, "-m"))
46 rev.match_missing = 1;
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070047 else
Junio C Hamano6b5ee132005-09-21 00:00:47 -070048 usage(diff_cache_usage);
Linus Torvaldse74f8f62005-04-19 21:00:09 -070049 }
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +030050 if (!rev.diffopt.output_format)
51 rev.diffopt.output_format = DIFF_FORMAT_RAW;
52
Junio C Hamano1eb41362021-02-11 11:57:50 -080053 rev.diffopt.rotate_to_strict = 1;
54
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070055 /*
56 * Make sure there is one revision (i.e. pending object),
57 * and there is no revision filtering parameters.
58 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -070059 if (rev.pending.nr != 1 ||
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070060 rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
Junio C Hamano4727f642005-06-19 13:14:05 -070061 usage(diff_cache_usage);
Denton Liu4c3fe822020-09-20 04:22:22 -070062 if (!(option & DIFF_INDEX_CACHED)) {
Nguyễn Thái Ngọc Duy4f38f6b2008-08-28 20:02:12 +070063 setup_work_tree();
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +070064 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
Karsten Blees7349afd2012-10-30 10:50:42 +010065 perror("read_cache_preload");
66 return -1;
67 }
68 } else if (read_cache() < 0) {
Junio C Hamanob4e1e4a2007-02-09 18:51:40 -080069 perror("read_cache");
70 return -1;
71 }
Denton Liu4c3fe822020-09-20 04:22:22 -070072 result = run_diff_index(&rev, option);
Martin Ågren886e1082017-10-01 19:42:08 +020073 UNLEAK(rev);
Junio C Hamanoda31b352007-12-13 23:40:27 -080074 return diff_result_code(&rev.diffopt, result);
Linus Torvaldse74f8f62005-04-19 21:00:09 -070075}