Stop carrying Geert's similarity estimator.
[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 #include "commit.h"
9 #include "revision.h"
10
11 static const char diff_files_usage[] =
12 "git-diff-files [-q] [-0/-1/2/3 |-c|--cc] [<common diff options>] [<path>...]"
13 COMMON_DIFF_OPTIONS_HELP;
14
15 static struct rev_info rev;
16 static int silent = 0;
17 static int diff_unmerged_stage = 2;
18 static int combine_merges = 0;
19 static int dense_combined_merges = 0;
20
21 static void show_unmerge(const char *path)
22 {
23         diff_unmerge(&rev.diffopt, path);
24 }
25
26 static void show_file(int pfx, struct cache_entry *ce)
27 {
28         diff_addremove(&rev.diffopt, pfx, ntohl(ce->ce_mode),
29                        ce->sha1, ce->name, NULL);
30 }
31
32 static void show_modified(int oldmode, int mode,
33                           const unsigned char *old_sha1, const unsigned char *sha1,
34                           char *path)
35 {
36         diff_change(&rev.diffopt, oldmode, mode, old_sha1, sha1, path, NULL);
37 }
38
39 int main(int argc, const char **argv)
40 {
41         const char **pathspec;
42         const char *prefix = setup_git_directory();
43         int entries, i;
44
45         git_config(git_diff_config);
46         diff_setup(&rev.diffopt);
47         while (1 < argc && argv[1][0] == '-') {
48                 if (!strcmp(argv[1], "--")) {
49                         argv++;
50                         argc--;
51                         break;
52                 }
53                 if (!strcmp(argv[1], "-0"))
54                         diff_unmerged_stage = 0;
55                 else if (!strcmp(argv[1], "-1"))
56                         diff_unmerged_stage = 1;
57                 else if (!strcmp(argv[1], "-2"))
58                         diff_unmerged_stage = 2;
59                 else if (!strcmp(argv[1], "-3"))
60                         diff_unmerged_stage = 3;
61                 else if (!strcmp(argv[1], "--base"))
62                         diff_unmerged_stage = 1;
63                 else if (!strcmp(argv[1], "--ours"))
64                         diff_unmerged_stage = 2;
65                 else if (!strcmp(argv[1], "--theirs"))
66                         diff_unmerged_stage = 3;
67                 else if (!strcmp(argv[1], "-q"))
68                         silent = 1;
69                 else if (!strcmp(argv[1], "-r"))
70                         ; /* no-op */
71                 else if (!strcmp(argv[1], "-s"))
72                         ; /* no-op */
73                 else if (!strcmp(argv[1], "-c"))
74                         combine_merges = 1;
75                 else if (!strcmp(argv[1], "--cc"))
76                         dense_combined_merges = combine_merges = 1;
77                 else {
78                         int diff_opt_cnt;
79                         diff_opt_cnt = diff_opt_parse(&rev.diffopt,
80                                                       argv+1, argc-1);
81                         if (diff_opt_cnt < 0)
82                                 usage(diff_files_usage);
83                         else if (diff_opt_cnt) {
84                                 argv += diff_opt_cnt;
85                                 argc -= diff_opt_cnt;
86                                 continue;
87                         }
88                         else
89                                 usage(diff_files_usage);
90                 }
91                 argv++; argc--;
92         }
93         if (dense_combined_merges)
94                 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
95
96         /* Find the directory, and set up the pathspec */
97         pathspec = get_pathspec(prefix, argv + 1);
98         entries = read_cache();
99
100         if (diff_setup_done(&rev.diffopt) < 0)
101                 usage(diff_files_usage);
102
103         /* At this point, if argc == 1, then we are doing everything.
104          * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
105          */
106         if (entries < 0) {
107                 perror("read_cache");
108                 exit(1);
109         }
110
111         for (i = 0; i < entries; i++) {
112                 struct stat st;
113                 unsigned int oldmode, newmode;
114                 struct cache_entry *ce = active_cache[i];
115                 int changed;
116
117                 if (!ce_path_match(ce, pathspec))
118                         continue;
119
120                 if (ce_stage(ce)) {
121                         struct {
122                                 struct combine_diff_path p;
123                                 struct combine_diff_parent filler[5];
124                         } combine;
125                         int num_compare_stages = 0;
126
127                         combine.p.next = NULL;
128                         combine.p.len = ce_namelen(ce);
129                         combine.p.path = xmalloc(combine.p.len + 1);
130                         memcpy(combine.p.path, ce->name, combine.p.len);
131                         combine.p.path[combine.p.len] = 0;
132                         combine.p.mode = 0;
133                         memset(combine.p.sha1, 0, 20);
134                         memset(&combine.p.parent[0], 0,
135                                sizeof(combine.filler));
136
137                         while (i < entries) {
138                                 struct cache_entry *nce = active_cache[i];
139                                 int stage;
140
141                                 if (strcmp(ce->name, nce->name))
142                                         break;
143
144                                 /* Stage #2 (ours) is the first parent,
145                                  * stage #3 (theirs) is the second.
146                                  */
147                                 stage = ce_stage(nce);
148                                 if (2 <= stage) {
149                                         int mode = ntohl(nce->ce_mode);
150                                         num_compare_stages++;
151                                         memcpy(combine.p.parent[stage-2].sha1,
152                                                nce->sha1, 20);
153                                         combine.p.parent[stage-2].mode =
154                                                 canon_mode(mode);
155                                         combine.p.parent[stage-2].status =
156                                                 DIFF_STATUS_MODIFIED;
157                                 }
158
159                                 /* diff against the proper unmerged stage */
160                                 if (stage == diff_unmerged_stage)
161                                         ce = nce;
162                                 i++;
163                         }
164                         /*
165                          * Compensate for loop update
166                          */
167                         i--;
168
169                         if (combine_merges && num_compare_stages == 2) {
170                                 show_combined_diff(&combine.p, 2,
171                                                    dense_combined_merges,
172                                                    &rev);
173                                 free(combine.p.path);
174                                 continue;
175                         }
176                         free(combine.p.path);
177
178                         /*
179                          * Show the diff for the 'ce' if we found the one
180                          * from the desired stage.
181                          */
182                         show_unmerge(ce->name);
183                         if (ce_stage(ce) != diff_unmerged_stage)
184                                 continue;
185                 }
186
187                 if (lstat(ce->name, &st) < 0) {
188                         if (errno != ENOENT && errno != ENOTDIR) {
189                                 perror(ce->name);
190                                 continue;
191                         }
192                         if (silent)
193                                 continue;
194                         show_file('-', ce);
195                         continue;
196                 }
197                 changed = ce_match_stat(ce, &st, 0);
198                 if (!changed && !rev.diffopt.find_copies_harder)
199                         continue;
200                 oldmode = ntohl(ce->ce_mode);
201
202                 newmode = canon_mode(st.st_mode);
203                 if (!trust_executable_bit &&
204                     S_ISREG(newmode) && S_ISREG(oldmode) &&
205                     ((newmode ^ oldmode) == 0111))
206                         newmode = oldmode;
207                 show_modified(oldmode, newmode,
208                               ce->sha1, (changed ? null_sha1 : ce->sha1),
209                               ce->name);
210         }
211         diffcore_std(&rev.diffopt);
212         diff_flush(&rev.diffopt);
213         return 0;
214 }