2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
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
,
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();
43 git_config(git_diff_config
);
44 diff_setup(&diff_options
);
45 while (1 < argc
&& argv
[1][0] == '-') {
46 if (!strcmp(argv
[1], "--")) {
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"))
67 else if (!strcmp(argv
[1], "-r"))
69 else if (!strcmp(argv
[1], "-s"))
71 else if (!strcmp(argv
[1], "-c"))
73 else if (!strcmp(argv
[1], "--cc"))
74 dense_combined_merges
= combine_merges
= 1;
77 diff_opt_cnt
= diff_opt_parse(&diff_options
,
80 usage(diff_files_usage
);
81 else if (diff_opt_cnt
) {
87 usage(diff_files_usage
);
92 diff_options
.output_format
= DIFF_FORMAT_PATCH
;
95 /* Find the directory, and set up the pathspec */
96 pathspec
= get_pathspec(prefix
, argv
+ 1);
97 entries
= read_cache();
99 if (diff_setup_done(&diff_options
) < 0)
100 usage(diff_files_usage
);
102 /* At this point, if argc == 1, then we are doing everything.
103 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
106 perror("read_cache");
110 for (i
= 0; i
< entries
; i
++) {
112 unsigned int oldmode
, newmode
;
113 struct cache_entry
*ce
= active_cache
[i
];
116 if (!ce_path_match(ce
, pathspec
))
121 struct combine_diff_path p
;
122 struct combine_diff_parent filler
[5];
124 int num_compare_stages
= 0;
126 combine
.p
.next
= NULL
;
127 combine
.p
.len
= ce_namelen(ce
);
128 combine
.p
.path
= xmalloc(combine
.p
.len
+ 1);
129 memcpy(combine
.p
.path
, ce
->name
, combine
.p
.len
);
130 combine
.p
.path
[combine
.p
.len
] = 0;
132 memset(combine
.p
.sha1
, 0, 20);
133 memset(&combine
.p
.parent
[0], 0,
134 sizeof(combine
.filler
));
136 while (i
< entries
) {
137 struct cache_entry
*nce
= active_cache
[i
];
140 if (strcmp(ce
->name
, nce
->name
))
143 /* Stage #2 (ours) is the first parent,
144 * stage #3 (theirs) is the second.
146 stage
= ce_stage(nce
);
148 int mode
= ntohl(nce
->ce_mode
);
149 num_compare_stages
++;
150 memcpy(combine
.p
.parent
[stage
-2].sha1
,
152 combine
.p
.parent
[stage
-2].mode
=
153 DIFF_FILE_CANON_MODE(mode
);
156 /* diff against the proper unmerged stage */
157 if (stage
== diff_unmerged_stage
)
162 * Compensate for loop update
166 if (combine_merges
&& num_compare_stages
== 2) {
167 show_combined_diff(&combine
.p
, 2,
168 dense_combined_merges
,
170 free(combine
.p
.path
);
173 free(combine
.p
.path
);
176 * Show the diff for the 'ce' if we found the one
177 * from the desired stage.
179 show_unmerge(ce
->name
);
180 if (ce_stage(ce
) != diff_unmerged_stage
)
184 if (lstat(ce
->name
, &st
) < 0) {
185 if (errno
!= ENOENT
&& errno
!= ENOTDIR
) {
194 changed
= ce_match_stat(ce
, &st
);
195 if (!changed
&& !diff_options
.find_copies_harder
)
197 oldmode
= ntohl(ce
->ce_mode
);
199 newmode
= DIFF_FILE_CANON_MODE(st
.st_mode
);
200 if (!trust_executable_bit
&&
201 S_ISREG(newmode
) && S_ISREG(oldmode
) &&
202 ((newmode
^ oldmode
) == 0111))
204 show_modified(oldmode
, newmode
,
205 ce
->sha1
, (changed
? null_sha1
: ce
->sha1
),
208 diffcore_std(&diff_options
);
209 diff_flush(&diff_options
);