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