blob: 70103c40952093100e41361e15ab6dbe54ef0268 [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"
Sergey Organov3b6c17b2020-12-21 18:19:39 +030010#include "diff-merges.h"
Linus Torvalds91539832006-04-17 11:59:32 -070011#include "commit.h"
12#include "revision.h"
Peter Eriksene8cc9cd2006-05-23 14:15:36 +020013#include "builtin.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +020014#include "submodule.h"
Christopher Lic0fb9762005-04-12 02:04:44 -070015
Petr Baudis4d1f1192005-07-29 11:01:26 +020016static const char diff_files_usage[] =
Alex Henrie9c9b4f22015-01-13 00:44:47 -070017"git diff-files [-q] [-0 | -1 | -2 | -3 | -c | --cc] [<common-diff-options>] [<path>...]"
Junio C Hamanodda2d792005-07-13 12:52:35 -070018COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanob8f80922005-04-16 21:29:45 -070019
Linus Torvaldsa633fca2006-07-28 22:44:25 -070020int cmd_diff_files(int argc, const char **argv, const char *prefix)
Linus Torvaldse83c5162005-04-07 15:13:13 -070021{
Junio C Hamano6973dca2006-04-21 23:57:45 -070022 struct rev_info rev;
Alex Riesen41bbf9d2007-03-14 01:17:04 +010023 int result;
Junio C Hamano6304c292008-05-23 18:15:03 -070024 unsigned options = 0;
Linus Torvaldse83c5162005-04-07 15:13:13 -070025
Junio C Hamano5a88f972017-06-01 13:38:16 +090026 if (argc == 2 && !strcmp(argv[1], "-h"))
27 usage(diff_files_usage);
28
Marc Branchaud37590ce2017-05-08 12:03:37 -040029 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +020030 repo_init_revisions(the_repository, &rev, prefix);
Junio C Hamano6973dca2006-04-21 23:57:45 -070031 rev.abbrev = 0;
Srinidhi Kaushikfeea6942020-06-20 22:08:45 +053032
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
Torsten Bögershausen5c327502021-02-03 17:28:23 +010039 prefix = precompose_argv_prefix(argc, argv, prefix);
Junio C Hamano6973dca2006-04-21 23:57:45 -070040
Junio C Hamano6304c292008-05-23 18:15:03 -070041 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 }
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +030055 if (!rev.diffopt.output_format)
56 rev.diffopt.output_format = DIFF_FORMAT_RAW;
Junio C Hamano1eb41362021-02-11 11:57:50 -080057 rev.diffopt.rotate_to_strict = 1;
Junio C Hamano6304c292008-05-23 18:15:03 -070058
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
Junio C Hamano903e09a2008-09-18 00:32:37 -070069 /*
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 */
Sergey Organov3b6c17b2020-12-21 18:19:39 +030074 if (rev.max_count == -1 &&
Junio C Hamano6304c292008-05-23 18:15:03 -070075 (rev.diffopt.output_format & DIFF_FORMAT_PATCH))
Sergey Organov3b6c17b2020-12-21 18:19:39 +030076 diff_merges_set_dense_combined_if_unset(&rev);
Junio C Hamano6304c292008-05-23 18:15:03 -070077
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +070078 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
Linus Torvalds671c9b72008-11-13 16:36:30 -080079 perror("read_cache_preload");
Junio C Hamano6304c292008-05-23 18:15:03 -070080 return -1;
81 }
82 result = run_diff_files(&rev, options);
Junio C Hamanoda31b352007-12-13 23:40:27 -080083 return diff_result_code(&rev.diffopt, result);
Linus Torvaldse83c5162005-04-07 15:13:13 -070084}