blob: 17bf84d18f802d3f223e7408fee644b94878b35f [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 */
Linus Torvaldse83c5162005-04-07 15:13:13 -07006#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07007#include "config.h"
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -07008#include "diff.h"
Linus Torvalds91539832006-04-17 11:59:32 -07009#include "commit.h"
10#include "revision.h"
Peter Eriksene8cc9cd2006-05-23 14:15:36 +020011#include "builtin.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +020012#include "submodule.h"
Christopher Lic0fb9762005-04-12 02:04:44 -070013
Petr Baudis4d1f1192005-07-29 11:01:26 +020014static const char diff_files_usage[] =
Alex Henrie9c9b4f22015-01-13 00:44:47 -070015"git diff-files [-q] [-0 | -1 | -2 | -3 | -c | --cc] [<common-diff-options>] [<path>...]"
Junio C Hamanodda2d792005-07-13 12:52:35 -070016COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanob8f80922005-04-16 21:29:45 -070017
Linus Torvaldsa633fca2006-07-28 22:44:25 -070018int cmd_diff_files(int argc, const char **argv, const char *prefix)
Linus Torvaldse83c5162005-04-07 15:13:13 -070019{
Junio C Hamano6973dca2006-04-21 23:57:45 -070020 struct rev_info rev;
Alex Riesen41bbf9d2007-03-14 01:17:04 +010021 int result;
Junio C Hamano6304c292008-05-23 18:15:03 -070022 unsigned options = 0;
Linus Torvaldse83c5162005-04-07 15:13:13 -070023
Junio C Hamano5a88f972017-06-01 13:38:16 +090024 if (argc == 2 && !strcmp(argv[1], "-h"))
25 usage(diff_files_usage);
26
Marc Branchaud37590ce2017-05-08 12:03:37 -040027 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
Linus Torvaldsa633fca2006-07-28 22:44:25 -070028 init_revisions(&rev, prefix);
Jens Lehmann302ad7a2010-08-06 00:40:48 +020029 gitmodules_config();
Junio C Hamano6973dca2006-04-21 23:57:45 -070030 rev.abbrev = 0;
Alexander Rinass90a78b82016-05-13 22:41:02 +020031 precompose_argv(argc, argv);
Junio C Hamano6973dca2006-04-21 23:57:45 -070032
Junio C Hamano6304c292008-05-23 18:15:03 -070033 argc = setup_revisions(argc, argv, &rev, NULL);
34 while (1 < argc && argv[1][0] == '-') {
35 if (!strcmp(argv[1], "--base"))
36 rev.max_count = 1;
37 else if (!strcmp(argv[1], "--ours"))
38 rev.max_count = 2;
39 else if (!strcmp(argv[1], "--theirs"))
40 rev.max_count = 3;
41 else if (!strcmp(argv[1], "-q"))
42 options |= DIFF_SILENT_ON_REMOVED;
43 else
44 usage(diff_files_usage);
45 argv++; argc--;
46 }
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +030047 if (!rev.diffopt.output_format)
48 rev.diffopt.output_format = DIFF_FORMAT_RAW;
Junio C Hamano6304c292008-05-23 18:15:03 -070049
50 /*
51 * Make sure there are NO revision (i.e. pending object) parameter,
52 * rev.max_count is reasonable (0 <= n <= 3), and
53 * there is no other revision filtering parameters.
54 */
55 if (rev.pending.nr ||
56 rev.min_age != -1 || rev.max_age != -1 ||
57 3 < rev.max_count)
58 usage(diff_files_usage);
59
Junio C Hamano903e09a2008-09-18 00:32:37 -070060 /*
61 * "diff-files --base -p" should not combine merges because it
62 * was not asked to. "diff-files -c -p" should not densify
63 * (the user should ask with "diff-files --cc" explicitly).
64 */
65 if (rev.max_count == -1 && !rev.combine_merges &&
Junio C Hamano6304c292008-05-23 18:15:03 -070066 (rev.diffopt.output_format & DIFF_FORMAT_PATCH))
67 rev.combine_merges = rev.dense_combined_merges = 1;
68
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +070069 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
Linus Torvalds671c9b72008-11-13 16:36:30 -080070 perror("read_cache_preload");
Junio C Hamano6304c292008-05-23 18:15:03 -070071 return -1;
72 }
73 result = run_diff_files(&rev, options);
Junio C Hamanoda31b352007-12-13 23:40:27 -080074 return diff_result_code(&rev.diffopt, result);
Linus Torvaldse83c5162005-04-07 15:13:13 -070075}