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