]> git.ipfire.org Git - thirdparty/git.git/blame - diff-files.c
Documentation: "pack-file" is not literal in unpack-objects
[thirdparty/git.git] / diff-files.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163 6#include "cache.h"
4a6bf9e1 7#include "diff.h"
c0fb976a 8
4d1f1190 9static const char diff_files_usage[] =
dda2d79a
JH
10"git-diff-files [-q] "
11"[<common diff options>] [<path>...]"
12COMMON_DIFF_OPTIONS_HELP;
b8f80925 13
e68b6f15
LT
14static int diff_output_format = DIFF_FORMAT_RAW;
15static int diff_line_termination = '\n';
5c97558c 16static int detect_rename = 0;
4727f640 17static int find_copies_harder = 0;
19feebc8 18static int diff_setup_opt = 0;
57fe64a4 19static int diff_score_opt = 0;
057c7d30 20static const char *pickaxe = NULL;
367cec1c 21static int pickaxe_opts = 0;
f345b0a0 22static int diff_break_opt = -1;
af5323e0 23static const char *orderfile = NULL;
f2ce9fde 24static const char *diff_filter = NULL;
0a7668e9 25static int silent = 0;
0a7668e9 26
4a6bf9e1 27static void show_unmerge(const char *path)
0a7668e9 28{
57fe64a4 29 diff_unmerge(path);
4a6bf9e1
JH
30}
31
32static void show_file(int pfx, struct cache_entry *ce)
33{
57fe64a4 34 diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);
4a6bf9e1
JH
35}
36
37static void show_modified(int oldmode, int mode,
bf0f910d 38 const unsigned char *old_sha1, const unsigned char *sha1,
4a6bf9e1
JH
39 char *path)
40{
57fe64a4 41 diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
0a7668e9
LT
42}
43
6b14d7fa 44int main(int argc, const char **argv)
e83c5163 45{
bf0f910d 46 static const unsigned char null_sha1[20] = { 0, };
c0fd1f51 47 const char **pathspec;
e83c5163
LT
48 int entries = read_cache();
49 int i;
50
b8f80925 51 while (1 < argc && argv[1][0] == '-') {
acb46f87 52 if (!strcmp(argv[1], "-p") || !strcmp(argv[1], "-u"))
81e50eab 53 diff_output_format = DIFF_FORMAT_PATCH;
b8f80925 54 else if (!strcmp(argv[1], "-q"))
d15aa430 55 silent = 1;
0a7668e9 56 else if (!strcmp(argv[1], "-r"))
4a6bf9e1 57 ; /* no-op */
d15aa430
JH
58 else if (!strcmp(argv[1], "-s"))
59 ; /* no-op */
60 else if (!strcmp(argv[1], "-z"))
e68b6f15 61 diff_line_termination = 0;
52f28529
JH
62 else if (!strcmp(argv[1], "--name-only"))
63 diff_output_format = DIFF_FORMAT_NAME;
57fe64a4 64 else if (!strcmp(argv[1], "-R"))
19feebc8 65 diff_setup_opt |= DIFF_SETUP_REVERSE;
e25de756 66 else if (!strncmp(argv[1], "-S", 2))
52e95789 67 pickaxe = argv[1] + 2;
af5323e0
JH
68 else if (!strncmp(argv[1], "-O", 2))
69 orderfile = argv[1] + 2;
f2ce9fde
JH
70 else if (!strncmp(argv[1], "--diff-filter=", 14))
71 diff_filter = argv[1] + 14;
367cec1c
JH
72 else if (!strcmp(argv[1], "--pickaxe-all"))
73 pickaxe_opts = DIFF_PICKAXE_ALL;
0e3994fa
JH
74 else if (!strncmp(argv[1], "-B", 2)) {
75 if ((diff_break_opt =
76 diff_scoreopt_parse(argv[1])) == -1)
77 usage(diff_files_usage);
78 }
57fe64a4 79 else if (!strncmp(argv[1], "-M", 2)) {
0e3994fa
JH
80 if ((diff_score_opt =
81 diff_scoreopt_parse(argv[1])) == -1)
82 usage(diff_files_usage);
6b14d7fa 83 detect_rename = DIFF_DETECT_RENAME;
5c97558c 84 }
427dcb4b 85 else if (!strncmp(argv[1], "-C", 2)) {
0e3994fa
JH
86 if ((diff_score_opt =
87 diff_scoreopt_parse(argv[1])) == -1)
88 usage(diff_files_usage);
6b14d7fa 89 detect_rename = DIFF_DETECT_COPY;
427dcb4b 90 }
4727f640
JH
91 else if (!strcmp(argv[1], "--find-copies-harder"))
92 find_copies_harder = 1;
b8f80925 93 else
17710391 94 usage(diff_files_usage);
b8f80925 95 argv++; argc--;
e2e5e98a
PB
96 }
97
c0fd1f51
LT
98 /* Do we have a pathspec? */
99 pathspec = (argc > 1) ? argv + 1 : NULL;
100
4727f640
JH
101 if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
102 usage(diff_files_usage);
103
b8f80925
JH
104 /* At this point, if argc == 1, then we are doing everything.
105 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
106 */
e83c5163
LT
107 if (entries < 0) {
108 perror("read_cache");
109 exit(1);
110 }
be3cfa85 111
19feebc8 112 diff_setup(diff_setup_opt);
5c97558c 113
e83c5163
LT
114 for (i = 0; i < entries; i++) {
115 struct stat st;
67574c40 116 unsigned int oldmode;
e83c5163 117 struct cache_entry *ce = active_cache[i];
d94c6128 118 int changed;
e83c5163 119
c0fd1f51
LT
120 if (!ce_path_match(ce, pathspec))
121 continue;
122
9fec8b26 123 if (ce_stage(ce)) {
4a6bf9e1 124 show_unmerge(ce->name);
9fec8b26
JH
125 while (i < entries &&
126 !strcmp(ce->name, active_cache[i]->name))
127 i++;
128 i--; /* compensate for loop control increments */
129 continue;
130 }
57fe64a4 131
ffbe1add 132 if (lstat(ce->name, &st) < 0) {
41174694 133 if (errno != ENOENT && errno != ENOTDIR) {
0a7668e9 134 perror(ce->name);
ca2a0798 135 continue;
57fe64a4 136 }
d15aa430 137 if (silent)
0a7668e9 138 continue;
4a6bf9e1 139 show_file('-', ce);
e83c5163
LT
140 continue;
141 }
5d728c84 142 changed = ce_match_stat(ce, &st);
4727f640 143 if (!changed && !find_copies_harder)
e83c5163 144 continue;
0a7668e9 145 oldmode = ntohl(ce->ce_mode);
67574c40 146 show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
4727f640 147 ce->sha1, (changed ? null_sha1 : ce->sha1),
4a6bf9e1 148 ce->name);
e83c5163 149 }
c0fd1f51 150 diffcore_std(pathspec,
befe8639 151 detect_rename, diff_score_opt,
f345b0a0 152 pickaxe, pickaxe_opts,
af5323e0 153 diff_break_opt,
f2ce9fde 154 orderfile, diff_filter);
e68b6f15 155 diff_flush(diff_output_format, diff_line_termination);
e83c5163
LT
156 return 0;
157}