6d2aec34067071894f8e6abc5a94fb7415bc328f
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
7#include "diff.h"
8
9static const char *diff_files_usage =
10"git-diff-files [-p] [-q] [-r] [-z] [-R] [-B] [-M] [-C] [--find-copies-harder] [-O<orderfile>] [-S<string>] [--pickaxe-all] [<path>...]";
11
12static int diff_output_format = DIFF_FORMAT_HUMAN;
13static int detect_rename = 0;
14static int find_copies_harder = 0;
15static int diff_setup_opt = 0;
16static int diff_score_opt = 0;
17static const char *pickaxe = NULL;
18static int pickaxe_opts = 0;
19static int diff_break_opt = -1;
20static const char *orderfile = NULL;
21static const char *diff_filter = NULL;
22static int silent = 0;
23
24static void show_unmerge(const char *path)
25{
26 diff_unmerge(path);
27}
28
29static void show_file(int pfx, struct cache_entry *ce)
30{
31 diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);
32}
33
34static void show_modified(int oldmode, int mode,
35 const unsigned char *old_sha1, const unsigned char *sha1,
36 char *path)
37{
38 diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
39}
40
41int main(int argc, const char **argv)
42{
43 static const unsigned char null_sha1[20] = { 0, };
44 int entries = read_cache();
45 int i;
46
47 while (1 < argc && argv[1][0] == '-') {
48 if (!strcmp(argv[1], "-p") || !strcmp(argv[1], "-u"))
49 diff_output_format = DIFF_FORMAT_PATCH;
50 else if (!strcmp(argv[1], "-q"))
51 silent = 1;
52 else if (!strcmp(argv[1], "-r"))
53 ; /* no-op */
54 else if (!strcmp(argv[1], "-s"))
55 ; /* no-op */
56 else if (!strcmp(argv[1], "-z"))
57 diff_output_format = DIFF_FORMAT_MACHINE;
58 else if (!strcmp(argv[1], "--name-only"))
59 diff_output_format = DIFF_FORMAT_NAME;
60 else if (!strcmp(argv[1], "--name-only-z"))
61 diff_output_format = DIFF_FORMAT_NAME_Z;
62 else if (!strcmp(argv[1], "-R"))
63 diff_setup_opt |= DIFF_SETUP_REVERSE;
64 else if (!strncmp(argv[1], "-S", 2))
65 pickaxe = argv[1] + 2;
66 else if (!strncmp(argv[1], "-O", 2))
67 orderfile = argv[1] + 2;
68 else if (!strncmp(argv[1], "--diff-filter=", 14))
69 diff_filter = argv[1] + 14;
70 else if (!strcmp(argv[1], "--pickaxe-all"))
71 pickaxe_opts = DIFF_PICKAXE_ALL;
72 else if (!strncmp(argv[1], "-B", 2)) {
73 if ((diff_break_opt =
74 diff_scoreopt_parse(argv[1])) == -1)
75 usage(diff_files_usage);
76 }
77 else if (!strncmp(argv[1], "-M", 2)) {
78 if ((diff_score_opt =
79 diff_scoreopt_parse(argv[1])) == -1)
80 usage(diff_files_usage);
81 detect_rename = DIFF_DETECT_RENAME;
82 }
83 else if (!strncmp(argv[1], "-C", 2)) {
84 if ((diff_score_opt =
85 diff_scoreopt_parse(argv[1])) == -1)
86 usage(diff_files_usage);
87 detect_rename = DIFF_DETECT_COPY;
88 }
89 else if (!strcmp(argv[1], "--find-copies-harder"))
90 find_copies_harder = 1;
91 else
92 usage(diff_files_usage);
93 argv++; argc--;
94 }
95
96 if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
97 usage(diff_files_usage);
98
99 /* At this point, if argc == 1, then we are doing everything.
100 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
101 */
102 if (entries < 0) {
103 perror("read_cache");
104 exit(1);
105 }
106
107 diff_setup(diff_setup_opt);
108
109 for (i = 0; i < entries; i++) {
110 struct stat st;
111 unsigned int oldmode;
112 struct cache_entry *ce = active_cache[i];
113 int changed;
114
115 if (ce_stage(ce)) {
116 show_unmerge(ce->name);
117 while (i < entries &&
118 !strcmp(ce->name, active_cache[i]->name))
119 i++;
120 i--; /* compensate for loop control increments */
121 continue;
122 }
123
124 if (lstat(ce->name, &st) < 0) {
125 if (errno != ENOENT && errno != ENOTDIR) {
126 perror(ce->name);
127 continue;
128 }
129 if (silent)
130 continue;
131 show_file('-', ce);
132 continue;
133 }
134 changed = ce_match_stat(ce, &st);
135 if (!changed && !find_copies_harder)
136 continue;
137 oldmode = ntohl(ce->ce_mode);
138 show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
139 ce->sha1, (changed ? null_sha1 : ce->sha1),
140 ce->name);
141 }
142 diffcore_std((1 < argc) ? argv + 1 : NULL,
143 detect_rename, diff_score_opt,
144 pickaxe, pickaxe_opts,
145 diff_break_opt,
146 orderfile, diff_filter);
147 diff_flush(diff_output_format);
148 return 0;
149}