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