git-cvsimport-script: provide direct support for cvsps -z option
[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] [-R] [-B] [-M] [-C] [--find-copies-harder] [-O<orderfile>] [-S<string>] [--pickaxe-all] [<path>...]";
11
12 static int diff_output_format = DIFF_FORMAT_HUMAN;
13 static int detect_rename = 0;
14 static int find_copies_harder = 0;
15 static int diff_setup_opt = 0;
16 static int diff_score_opt = 0;
17 static const char *pickaxe = NULL;
18 static int pickaxe_opts = 0;
19 static int diff_break_opt = -1;
20 static const char *orderfile = NULL;
21 static const char *diff_filter = NULL;
22 static int silent = 0;
23
24 static void show_unmerge(const char *path)
25 {
26         diff_unmerge(path);
27 }
28
29 static 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
34 static 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
41 int 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"))
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], "-R"))
59                         diff_setup_opt |= DIFF_SETUP_REVERSE;
60                 else if (!strncmp(argv[1], "-S", 2))
61                         pickaxe = argv[1] + 2;
62                 else if (!strncmp(argv[1], "-O", 2))
63                         orderfile = argv[1] + 2;
64                 else if (!strncmp(argv[1], "--diff-filter=", 14))
65                         diff_filter = argv[1] + 14;
66                 else if (!strcmp(argv[1], "--pickaxe-all"))
67                         pickaxe_opts = DIFF_PICKAXE_ALL;
68                 else if (!strncmp(argv[1], "-B", 2)) {
69                         if ((diff_break_opt =
70                              diff_scoreopt_parse(argv[1])) == -1)
71                                 usage(diff_files_usage);
72                 }
73                 else if (!strncmp(argv[1], "-M", 2)) {
74                         if ((diff_score_opt =
75                              diff_scoreopt_parse(argv[1])) == -1)
76                                 usage(diff_files_usage);
77                         detect_rename = DIFF_DETECT_RENAME;
78                 }
79                 else if (!strncmp(argv[1], "-C", 2)) {
80                         if ((diff_score_opt =
81                              diff_scoreopt_parse(argv[1])) == -1)
82                                 usage(diff_files_usage);
83                         detect_rename = DIFF_DETECT_COPY;
84                 }
85                 else if (!strcmp(argv[1], "--find-copies-harder"))
86                         find_copies_harder = 1;
87                 else
88                         usage(diff_files_usage);
89                 argv++; argc--;
90         }
91
92         if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
93                 usage(diff_files_usage);
94
95         /* At this point, if argc == 1, then we are doing everything.
96          * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
97          */
98         if (entries < 0) {
99                 perror("read_cache");
100                 exit(1);
101         }
102
103         diff_setup(diff_setup_opt);
104
105         for (i = 0; i < entries; i++) {
106                 struct stat st;
107                 unsigned int oldmode;
108                 struct cache_entry *ce = active_cache[i];
109                 int changed;
110
111                 if (ce_stage(ce)) {
112                         show_unmerge(ce->name);
113                         while (i < entries &&
114                                !strcmp(ce->name, active_cache[i]->name))
115                                 i++;
116                         i--; /* compensate for loop control increments */
117                         continue;
118                 }
119
120                 if (lstat(ce->name, &st) < 0) {
121                         if (errno != ENOENT && errno != ENOTDIR) {
122                                 perror(ce->name);
123                                 continue;
124                         }
125                         if (silent)
126                                 continue;
127                         show_file('-', ce);
128                         continue;
129                 }
130                 changed = ce_match_stat(ce, &st);
131                 if (!changed && !find_copies_harder)
132                         continue;
133                 oldmode = ntohl(ce->ce_mode);
134                 show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
135                               ce->sha1, (changed ? null_sha1 : ce->sha1),
136                               ce->name);
137         }
138         diffcore_std((1 < argc) ? argv + 1 : NULL,
139                      detect_rename, diff_score_opt,
140                      pickaxe, pickaxe_opts,
141                      diff_break_opt,
142                      orderfile, diff_filter);
143         diff_flush(diff_output_format);
144         return 0;
145 }