blob: 93ec6424234c65f486dcfca724c77a48e5e91f91 [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"
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -07005#include "commit.h"
6#include "revision.h"
Peter Eriksene8cc9cd2006-05-23 14:15:36 +02007#include "builtin.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +02008#include "submodule.h"
Linus Torvaldsb5af9102005-04-22 17:15:28 -07009
Petr Baudis4d1f1192005-07-29 11:01:26 +020010static const char diff_cache_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020011"git diff-index [-m] [--cached] "
Alex Henrie9c9b4f22015-01-13 00:44:47 -070012"[<common-diff-options>] <tree-ish> [<path>...]"
Junio C Hamanodda2d792005-07-13 12:52:35 -070013COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanoc5bac172005-04-20 19:49:16 -070014
Linus Torvaldsa633fca2006-07-28 22:44:25 -070015int cmd_diff_index(int argc, const char **argv, const char *prefix)
Linus Torvaldse74f8f62005-04-19 21:00:09 -070016{
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070017 struct rev_info rev;
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070018 int cached = 0;
Linus Torvalds6c56c532005-05-24 18:10:11 -070019 int i;
Alex Riesen41bbf9d2007-03-14 01:17:04 +010020 int result;
Linus Torvaldse74f8f62005-04-19 21:00:09 -070021
Junio C Hamano5a88f972017-06-01 13:38:16 +090022 if (argc == 2 && !strcmp(argv[1], "-h"))
23 usage(diff_cache_usage);
24
Marc Branchaud37590ce2017-05-08 12:03:37 -040025 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +020026 repo_init_revisions(the_repository, &rev, prefix);
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070027 rev.abbrev = 0;
Alexander Rinass90a78b82016-05-13 22:41:02 +020028 precompose_argv(argc, argv);
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070029
30 argc = setup_revisions(argc, argv, &rev, NULL);
Linus Torvalds6c56c532005-05-24 18:10:11 -070031 for (i = 1; i < argc; i++) {
32 const char *arg = argv[i];
Junio C Hamanoa6080a02007-06-07 00:04:01 -070033
Junio C Hamano5c21ac02006-04-22 03:58:04 -070034 if (!strcmp(arg, "--cached"))
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070035 cached = 1;
36 else
Junio C Hamano6b5ee132005-09-21 00:00:47 -070037 usage(diff_cache_usage);
Linus Torvaldse74f8f62005-04-19 21:00:09 -070038 }
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +030039 if (!rev.diffopt.output_format)
40 rev.diffopt.output_format = DIFF_FORMAT_RAW;
41
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070042 /*
43 * Make sure there is one revision (i.e. pending object),
44 * and there is no revision filtering parameters.
45 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -070046 if (rev.pending.nr != 1 ||
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070047 rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
Junio C Hamano4727f642005-06-19 13:14:05 -070048 usage(diff_cache_usage);
Karsten Blees7349afd2012-10-30 10:50:42 +010049 if (!cached) {
Nguyễn Thái Ngọc Duy4f38f6b2008-08-28 20:02:12 +070050 setup_work_tree();
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +070051 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
Karsten Blees7349afd2012-10-30 10:50:42 +010052 perror("read_cache_preload");
53 return -1;
54 }
55 } else if (read_cache() < 0) {
Junio C Hamanob4e1e4a2007-02-09 18:51:40 -080056 perror("read_cache");
57 return -1;
58 }
Alex Riesen41bbf9d2007-03-14 01:17:04 +010059 result = run_diff_index(&rev, cached);
Martin Ågren886e1082017-10-01 19:42:08 +020060 UNLEAK(rev);
Junio C Hamanoda31b352007-12-13 23:40:27 -080061 return diff_result_code(&rev.diffopt, result);
Linus Torvaldse74f8f62005-04-19 21:00:09 -070062}