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