blob: d979824f9395a0cbc8ffffa9cede524589710d43 [file] [log] [blame]
Linus Torvaldse74f8f62005-04-19 21:00:09 -07001#include "cache.h"
Junio C Hamanof92a4462005-04-27 09:21:00 -07002#include "diff.h"
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -07003#include "commit.h"
4#include "revision.h"
Peter Eriksene8cc9cd2006-05-23 14:15:36 +02005#include "builtin.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +02006#include "submodule.h"
Linus Torvaldsb5af9102005-04-22 17:15:28 -07007
Petr Baudis4d1f1192005-07-29 11:01:26 +02008static const char diff_cache_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +02009"git diff-index [-m] [--cached] "
Alex Henrie9c9b4f22015-01-13 00:44:47 -070010"[<common-diff-options>] <tree-ish> [<path>...]"
Junio C Hamanodda2d792005-07-13 12:52:35 -070011COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanoc5bac172005-04-20 19:49:16 -070012
Linus Torvaldsa633fca2006-07-28 22:44:25 -070013int cmd_diff_index(int argc, const char **argv, const char *prefix)
Linus Torvaldse74f8f62005-04-19 21:00:09 -070014{
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070015 struct rev_info rev;
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070016 int cached = 0;
Linus Torvalds6c56c532005-05-24 18:10:11 -070017 int i;
Alex Riesen41bbf9d2007-03-14 01:17:04 +010018 int result;
Linus Torvaldse74f8f62005-04-19 21:00:09 -070019
Linus Torvaldsa633fca2006-07-28 22:44:25 -070020 init_revisions(&rev, prefix);
Jens Lehmann302ad7a2010-08-06 00:40:48 +020021 gitmodules_config();
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010022 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070023 rev.abbrev = 0;
24
25 argc = setup_revisions(argc, argv, &rev, NULL);
Linus Torvalds6c56c532005-05-24 18:10:11 -070026 for (i = 1; i < argc; i++) {
27 const char *arg = argv[i];
Junio C Hamanoa6080a02007-06-07 00:04:01 -070028
Junio C Hamano5c21ac02006-04-22 03:58:04 -070029 if (!strcmp(arg, "--cached"))
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070030 cached = 1;
31 else
Junio C Hamano6b5ee132005-09-21 00:00:47 -070032 usage(diff_cache_usage);
Linus Torvaldse74f8f62005-04-19 21:00:09 -070033 }
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +030034 if (!rev.diffopt.output_format)
35 rev.diffopt.output_format = DIFF_FORMAT_RAW;
36
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070037 /*
38 * Make sure there is one revision (i.e. pending object),
39 * and there is no revision filtering parameters.
40 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -070041 if (rev.pending.nr != 1 ||
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070042 rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
Junio C Hamano4727f642005-06-19 13:14:05 -070043 usage(diff_cache_usage);
Karsten Blees7349afd2012-10-30 10:50:42 +010044 if (!cached) {
Nguyễn Thái Ngọc Duy4f38f6b2008-08-28 20:02:12 +070045 setup_work_tree();
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +070046 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
Karsten Blees7349afd2012-10-30 10:50:42 +010047 perror("read_cache_preload");
48 return -1;
49 }
50 } else if (read_cache() < 0) {
Junio C Hamanob4e1e4a2007-02-09 18:51:40 -080051 perror("read_cache");
52 return -1;
53 }
Alex Riesen41bbf9d2007-03-14 01:17:04 +010054 result = run_diff_index(&rev, cached);
Junio C Hamanoda31b352007-12-13 23:40:27 -080055 return diff_result_code(&rev.diffopt, result);
Linus Torvaldse74f8f62005-04-19 21:00:09 -070056}