blob: 1e352dd8f77c281e41d702af81378cc9e213aaad [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 15:16:10 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +07006#define USE_THE_INDEX_COMPATIBILITY_MACROS
Linus Torvaldse83c5162005-04-07 15:13:13 -07007#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07008#include "config.h"
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -07009#include "diff.h"
Linus Torvalds91539832006-04-17 11:59:32 -070010#include "commit.h"
11#include "revision.h"
Peter Eriksene8cc9cd2006-05-23 14:15:36 +020012#include "builtin.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +020013#include "submodule.h"
Christopher Lic0fb9762005-04-12 02:04:44 -070014
Petr Baudis4d1f1192005-07-29 11:01:26 +020015static const char diff_files_usage[] =
Alex Henrie9c9b4f22015-01-13 00:44:47 -070016"git diff-files [-q] [-0 | -1 | -2 | -3 | -c | --cc] [<common-diff-options>] [<path>...]"
Junio C Hamanodda2d792005-07-13 12:52:35 -070017COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanob8f80922005-04-16 21:29:45 -070018
Linus Torvaldsa633fca2006-07-28 22:44:25 -070019int cmd_diff_files(int argc, const char **argv, const char *prefix)
Linus Torvaldse83c5162005-04-07 15:13:13 -070020{
Junio C Hamano6973dca2006-04-21 23:57:45 -070021 struct rev_info rev;
Alex Riesen41bbf9d2007-03-14 01:17:04 +010022 int result;
Junio C Hamano6304c292008-05-23 18:15:03 -070023 unsigned options = 0;
Linus Torvaldse83c5162005-04-07 15:13:13 -070024
Junio C Hamano5a88f972017-06-01 13:38:16 +090025 if (argc == 2 && !strcmp(argv[1], "-h"))
26 usage(diff_files_usage);
27
Marc Branchaud37590ce2017-05-08 12:03:37 -040028 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +020029 repo_init_revisions(the_repository, &rev, prefix);
Junio C Hamano6973dca2006-04-21 23:57:45 -070030 rev.abbrev = 0;
Srinidhi Kaushikfeea6942020-06-20 22:08:45 +053031
32 /*
33 * Consider "intent-to-add" files as new by default, unless
34 * explicitly specified in the command line or anywhere else.
35 */
36 rev.diffopt.ita_invisible_in_index = 1;
37
Alexander Rinass90a78b82016-05-13 22:41:02 +020038 precompose_argv(argc, argv);
Junio C Hamano6973dca2006-04-21 23:57:45 -070039
Junio C Hamano6304c292008-05-23 18:15:03 -070040 argc = setup_revisions(argc, argv, &rev, NULL);
41 while (1 < argc && argv[1][0] == '-') {
42 if (!strcmp(argv[1], "--base"))
43 rev.max_count = 1;
44 else if (!strcmp(argv[1], "--ours"))
45 rev.max_count = 2;
46 else if (!strcmp(argv[1], "--theirs"))
47 rev.max_count = 3;
48 else if (!strcmp(argv[1], "-q"))
49 options |= DIFF_SILENT_ON_REMOVED;
50 else
51 usage(diff_files_usage);
52 argv++; argc--;
53 }
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +030054 if (!rev.diffopt.output_format)
55 rev.diffopt.output_format = DIFF_FORMAT_RAW;
Junio C Hamano6304c292008-05-23 18:15:03 -070056
57 /*
58 * Make sure there are NO revision (i.e. pending object) parameter,
59 * rev.max_count is reasonable (0 <= n <= 3), and
60 * there is no other revision filtering parameters.
61 */
62 if (rev.pending.nr ||
63 rev.min_age != -1 || rev.max_age != -1 ||
64 3 < rev.max_count)
65 usage(diff_files_usage);
66
Junio C Hamano903e09a2008-09-18 00:32:37 -070067 /*
68 * "diff-files --base -p" should not combine merges because it
69 * was not asked to. "diff-files -c -p" should not densify
70 * (the user should ask with "diff-files --cc" explicitly).
71 */
72 if (rev.max_count == -1 && !rev.combine_merges &&
Junio C Hamano6304c292008-05-23 18:15:03 -070073 (rev.diffopt.output_format & DIFF_FORMAT_PATCH))
74 rev.combine_merges = rev.dense_combined_merges = 1;
75
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +070076 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
Linus Torvalds671c9b72008-11-13 16:36:30 -080077 perror("read_cache_preload");
Junio C Hamano6304c292008-05-23 18:15:03 -070078 return -1;
79 }
80 result = run_diff_files(&rev, options);
Junio C Hamanoda31b352007-12-13 23:40:27 -080081 return diff_result_code(&rev.diffopt, result);
Linus Torvaldse83c5162005-04-07 15:13:13 -070082}