Merge branch 'mt/clean-clean'
[git] / builtin / diff-files.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
7 #include "cache.h"
8 #include "config.h"
9 #include "diff.h"
10 #include "diff-merges.h"
11 #include "commit.h"
12 #include "revision.h"
13 #include "builtin.h"
14 #include "submodule.h"
15
16 static const char diff_files_usage[] =
17 "git diff-files [-q] [-0 | -1 | -2 | -3 | -c | --cc] [<common-diff-options>] [<path>...]"
18 COMMON_DIFF_OPTIONS_HELP;
19
20 int cmd_diff_files(int argc, const char **argv, const char *prefix)
21 {
22         struct rev_info rev;
23         int result;
24         unsigned options = 0;
25
26         if (argc == 2 && !strcmp(argv[1], "-h"))
27                 usage(diff_files_usage);
28
29         git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
30         repo_init_revisions(the_repository, &rev, prefix);
31         rev.abbrev = 0;
32
33         /*
34          * Consider "intent-to-add" files as new by default, unless
35          * explicitly specified in the command line or anywhere else.
36          */
37         rev.diffopt.ita_invisible_in_index = 1;
38
39         prefix = precompose_argv_prefix(argc, argv, prefix);
40
41         argc = setup_revisions(argc, argv, &rev, NULL);
42         while (1 < argc && argv[1][0] == '-') {
43                 if (!strcmp(argv[1], "--base"))
44                         rev.max_count = 1;
45                 else if (!strcmp(argv[1], "--ours"))
46                         rev.max_count = 2;
47                 else if (!strcmp(argv[1], "--theirs"))
48                         rev.max_count = 3;
49                 else if (!strcmp(argv[1], "-q"))
50                         options |= DIFF_SILENT_ON_REMOVED;
51                 else
52                         usage(diff_files_usage);
53                 argv++; argc--;
54         }
55         if (!rev.diffopt.output_format)
56                 rev.diffopt.output_format = DIFF_FORMAT_RAW;
57         rev.diffopt.rotate_to_strict = 1;
58
59         /*
60          * Make sure there are NO revision (i.e. pending object) parameter,
61          * rev.max_count is reasonable (0 <= n <= 3), and
62          * there is no other revision filtering parameters.
63          */
64         if (rev.pending.nr ||
65             rev.min_age != -1 || rev.max_age != -1 ||
66             3 < rev.max_count)
67                 usage(diff_files_usage);
68
69         /*
70          * "diff-files --base -p" should not combine merges because it
71          * was not asked to.  "diff-files -c -p" should not densify
72          * (the user should ask with "diff-files --cc" explicitly).
73          */
74         if (rev.max_count == -1 &&
75             (rev.diffopt.output_format & DIFF_FORMAT_PATCH))
76                 diff_merges_set_dense_combined_if_unset(&rev);
77
78         if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
79                 perror("read_cache_preload");
80                 return -1;
81         }
82         result = run_diff_files(&rev, options);
83         return diff_result_code(&rev.diffopt, result);
84 }