blob: 018011f29ea26be24b567e9af380699dd7d893a3 [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 */
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00006#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07007#include "config.h"
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -07008#include "diff.h"
Sergey Organov3b6c17b2020-12-21 18:19:39 +03009#include "diff-merges.h"
Linus Torvalds91539832006-04-17 11:59:32 -070010#include "commit.h"
Elijah Newrenfbffdfb2023-05-16 06:33:52 +000011#include "preload-index.h"
Elijah Newrendf6e8742023-05-16 06:34:00 +000012#include "repository.h"
Linus Torvalds91539832006-04-17 11:59:32 -070013#include "revision.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>...]"
Ævar Arnfjörð Bjarmasonacf78282022-10-13 17:39:01 +020017"\n"
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 */
Shuqi Liang8c30be92023-05-09 15:42:41 -040030
31 prepare_repo_settings(the_repository);
32 the_repository->settings.command_requires_full_index = 0;
33
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +020034 repo_init_revisions(the_repository, &rev, prefix);
Junio C Hamano6973dca2006-04-21 23:57:45 -070035 rev.abbrev = 0;
Srinidhi Kaushikfeea6942020-06-20 22:08:45 +053036
37 /*
38 * Consider "intent-to-add" files as new by default, unless
39 * explicitly specified in the command line or anywhere else.
40 */
41 rev.diffopt.ita_invisible_in_index = 1;
42
Torsten Bögershausen5c327502021-02-03 17:28:23 +010043 prefix = precompose_argv_prefix(argc, argv, prefix);
Junio C Hamano6973dca2006-04-21 23:57:45 -070044
Junio C Hamano6304c292008-05-23 18:15:03 -070045 argc = setup_revisions(argc, argv, &rev, NULL);
46 while (1 < argc && argv[1][0] == '-') {
47 if (!strcmp(argv[1], "--base"))
48 rev.max_count = 1;
49 else if (!strcmp(argv[1], "--ours"))
50 rev.max_count = 2;
51 else if (!strcmp(argv[1], "--theirs"))
52 rev.max_count = 3;
53 else if (!strcmp(argv[1], "-q"))
54 options |= DIFF_SILENT_ON_REMOVED;
55 else
56 usage(diff_files_usage);
57 argv++; argc--;
58 }
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +030059 if (!rev.diffopt.output_format)
60 rev.diffopt.output_format = DIFF_FORMAT_RAW;
Junio C Hamano1eb41362021-02-11 11:57:50 -080061 rev.diffopt.rotate_to_strict = 1;
Junio C Hamano6304c292008-05-23 18:15:03 -070062
63 /*
64 * Make sure there are NO revision (i.e. pending object) parameter,
65 * rev.max_count is reasonable (0 <= n <= 3), and
66 * there is no other revision filtering parameters.
67 */
68 if (rev.pending.nr ||
69 rev.min_age != -1 || rev.max_age != -1 ||
70 3 < rev.max_count)
71 usage(diff_files_usage);
72
Junio C Hamano903e09a2008-09-18 00:32:37 -070073 /*
74 * "diff-files --base -p" should not combine merges because it
75 * was not asked to. "diff-files -c -p" should not densify
76 * (the user should ask with "diff-files --cc" explicitly).
77 */
Sergey Organov3b6c17b2020-12-21 18:19:39 +030078 if (rev.max_count == -1 &&
Junio C Hamano6304c292008-05-23 18:15:03 -070079 (rev.diffopt.output_format & DIFF_FORMAT_PATCH))
Sergey Organov3b6c17b2020-12-21 18:19:39 +030080 diff_merges_set_dense_combined_if_unset(&rev);
Junio C Hamano6304c292008-05-23 18:15:03 -070081
Jeff Kingf126f6e2023-08-21 16:15:10 -040082 if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0)
83 die_errno("repo_read_index_preload");
Jeff King25bd3ac2023-08-21 16:18:55 -040084 run_diff_files(&rev, options);
Jeff King5cc6b2d2023-08-21 16:20:46 -040085 result = diff_result_code(&rev.diffopt);
Ævar Arnfjörð Bjarmason0139c582022-04-13 22:01:40 +020086 release_revisions(&rev);
87 return result;
Linus Torvaldse83c5162005-04-07 15:13:13 -070088}