blob: 04837494feba401c7f689eab5574768d3fd126de [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"
Linus Torvaldsb5af9102005-04-22 17:15:28 -07006
Petr Baudis4d1f1192005-07-29 11:01:26 +02007static const char diff_cache_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +02008"git diff-index [-m] [--cached] "
Junio C Hamanodda2d792005-07-13 12:52:35 -07009"[<common diff options>] <tree-ish> [<path>...]"
10COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanoc5bac172005-04-20 19:49:16 -070011
Linus Torvaldsa633fca2006-07-28 22:44:25 -070012int cmd_diff_index(int argc, const char **argv, const char *prefix)
Linus Torvaldse74f8f62005-04-19 21:00:09 -070013{
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070014 struct rev_info rev;
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070015 int cached = 0;
Linus Torvalds6c56c532005-05-24 18:10:11 -070016 int i;
Alex Riesen41bbf9d2007-03-14 01:17:04 +010017 int result;
Linus Torvaldse74f8f62005-04-19 21:00:09 -070018
Linus Torvaldsa633fca2006-07-28 22:44:25 -070019 init_revisions(&rev, prefix);
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010020 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070021 rev.abbrev = 0;
22
23 argc = setup_revisions(argc, argv, &rev, NULL);
Linus Torvalds6c56c532005-05-24 18:10:11 -070024 for (i = 1; i < argc; i++) {
25 const char *arg = argv[i];
Junio C Hamanoa6080a02007-06-07 00:04:01 -070026
Junio C Hamano5c21ac02006-04-22 03:58:04 -070027 if (!strcmp(arg, "--cached"))
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070028 cached = 1;
29 else
Junio C Hamano6b5ee132005-09-21 00:00:47 -070030 usage(diff_cache_usage);
Linus Torvaldse74f8f62005-04-19 21:00:09 -070031 }
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +030032 if (!rev.diffopt.output_format)
33 rev.diffopt.output_format = DIFF_FORMAT_RAW;
34
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070035 /*
36 * Make sure there is one revision (i.e. pending object),
37 * and there is no revision filtering parameters.
38 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -070039 if (rev.pending.nr != 1 ||
Junio C Hamanoe09ad6e2006-04-22 02:43:00 -070040 rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
Junio C Hamano4727f642005-06-19 13:14:05 -070041 usage(diff_cache_usage);
Nguyễn Thái Ngọc Duy4f38f6b2008-08-28 20:02:12 +070042 if (!cached)
43 setup_work_tree();
Junio C Hamanob4e1e4a2007-02-09 18:51:40 -080044 if (read_cache() < 0) {
45 perror("read_cache");
46 return -1;
47 }
Alex Riesen41bbf9d2007-03-14 01:17:04 +010048 result = run_diff_index(&rev, cached);
Junio C Hamanoda31b352007-12-13 23:40:27 -080049 return diff_result_code(&rev.diffopt, result);
Linus Torvaldse74f8f62005-04-19 21:00:09 -070050}