blob: 15c61fd8d1ef891b013404ea5e7cbe42befac7bd [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"
Junio C Hamano4a6bf9e2005-04-27 09:21:00 -07007#include "diff.h"
Linus Torvalds91539832006-04-17 11:59:32 -07008#include "commit.h"
9#include "revision.h"
Peter Eriksene8cc9cd2006-05-23 14:15:36 +020010#include "builtin.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +020011#include "submodule.h"
Christopher Lic0fb9762005-04-12 02:04:44 -070012
Petr Baudis4d1f1192005-07-29 11:01:26 +020013static const char diff_files_usage[] =
Alex Henrie9c9b4f22015-01-13 00:44:47 -070014"git diff-files [-q] [-0 | -1 | -2 | -3 | -c | --cc] [<common-diff-options>] [<path>...]"
Junio C Hamanodda2d792005-07-13 12:52:35 -070015COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanob8f80922005-04-16 21:29:45 -070016
Linus Torvaldsa633fca2006-07-28 22:44:25 -070017int cmd_diff_files(int argc, const char **argv, const char *prefix)
Linus Torvaldse83c5162005-04-07 15:13:13 -070018{
Junio C Hamano6973dca2006-04-21 23:57:45 -070019 struct rev_info rev;
Alex Riesen41bbf9d2007-03-14 01:17:04 +010020 int result;
Junio C Hamano6304c292008-05-23 18:15:03 -070021 unsigned options = 0;
Linus Torvaldse83c5162005-04-07 15:13:13 -070022
Linus Torvaldsa633fca2006-07-28 22:44:25 -070023 init_revisions(&rev, prefix);
Jens Lehmann302ad7a2010-08-06 00:40:48 +020024 gitmodules_config();
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010025 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
Junio C Hamano6973dca2006-04-21 23:57:45 -070026 rev.abbrev = 0;
Alexander Rinass90a78b82016-05-13 22:41:02 +020027 precompose_argv(argc, argv);
Junio C Hamano6973dca2006-04-21 23:57:45 -070028
Junio C Hamano6304c292008-05-23 18:15:03 -070029 argc = setup_revisions(argc, argv, &rev, NULL);
30 while (1 < argc && argv[1][0] == '-') {
31 if (!strcmp(argv[1], "--base"))
32 rev.max_count = 1;
33 else if (!strcmp(argv[1], "--ours"))
34 rev.max_count = 2;
35 else if (!strcmp(argv[1], "--theirs"))
36 rev.max_count = 3;
37 else if (!strcmp(argv[1], "-q"))
38 options |= DIFF_SILENT_ON_REMOVED;
39 else
40 usage(diff_files_usage);
41 argv++; argc--;
42 }
Timo Hirvonenc9b5ef92006-06-24 20:24:14 +030043 if (!rev.diffopt.output_format)
44 rev.diffopt.output_format = DIFF_FORMAT_RAW;
Junio C Hamano6304c292008-05-23 18:15:03 -070045
46 /*
47 * Make sure there are NO revision (i.e. pending object) parameter,
48 * rev.max_count is reasonable (0 <= n <= 3), and
49 * there is no other revision filtering parameters.
50 */
51 if (rev.pending.nr ||
52 rev.min_age != -1 || rev.max_age != -1 ||
53 3 < rev.max_count)
54 usage(diff_files_usage);
55
Junio C Hamano903e09a2008-09-18 00:32:37 -070056 /*
57 * "diff-files --base -p" should not combine merges because it
58 * was not asked to. "diff-files -c -p" should not densify
59 * (the user should ask with "diff-files --cc" explicitly).
60 */
61 if (rev.max_count == -1 && !rev.combine_merges &&
Junio C Hamano6304c292008-05-23 18:15:03 -070062 (rev.diffopt.output_format & DIFF_FORMAT_PATCH))
63 rev.combine_merges = rev.dense_combined_merges = 1;
64
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +070065 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
Linus Torvalds671c9b72008-11-13 16:36:30 -080066 perror("read_cache_preload");
Junio C Hamano6304c292008-05-23 18:15:03 -070067 return -1;
68 }
69 result = run_diff_files(&rev, options);
Junio C Hamanoda31b352007-12-13 23:40:27 -080070 return diff_result_code(&rev.diffopt, result);
Linus Torvaldse83c5162005-04-07 15:13:13 -070071}