2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
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
;
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;
21 static void show_unmerge(const char *path
)
23 diff_unmerge(&rev
.diffopt
, path
);
26 static void show_file(int pfx
, struct cache_entry
*ce
)
28 diff_addremove(&rev
.diffopt
, pfx
, ntohl(ce
->ce_mode
),
29 ce
->sha1
, ce
->name
, NULL
);
32 static void show_modified(int oldmode
, int mode
,
33 const unsigned char *old_sha1
, const unsigned char *sha1
,
36 diff_change(&rev
.diffopt
, oldmode
, mode
, old_sha1
, sha1
, path
, NULL
);
39 int main(int argc
, const char **argv
)
41 const char **pathspec
;
42 const char *prefix
= setup_git_directory();
45 git_config(git_diff_config
);
46 diff_setup(&rev
.diffopt
);
47 while (1 < argc
&& argv
[1][0] == '-') {
48 if (!strcmp(argv
[1], "--")) {
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"))
69 else if (!strcmp(argv
[1], "-r"))
71 else if (!strcmp(argv
[1], "-s"))
73 else if (!strcmp(argv
[1], "-c"))
75 else if (!strcmp(argv
[1], "--cc"))
76 dense_combined_merges
= combine_merges
= 1;
79 diff_opt_cnt
= diff_opt_parse(&rev
.diffopt
,
82 usage(diff_files_usage
);
83 else if (diff_opt_cnt
) {
89 usage(diff_files_usage
);
93 if (dense_combined_merges
)
94 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
96 /* Find the directory, and set up the pathspec */
97 pathspec
= get_pathspec(prefix
, argv
+ 1);
98 entries
= read_cache();
100 if (diff_setup_done(&rev
.diffopt
) < 0)
101 usage(diff_files_usage
);
103 /* At this point, if argc == 1, then we are doing everything.
104 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
107 perror("read_cache");
111 for (i
= 0; i
< entries
; i
++) {
113 unsigned int oldmode
, newmode
;
114 struct cache_entry
*ce
= active_cache
[i
];
117 if (!ce_path_match(ce
, pathspec
))
122 struct combine_diff_path p
;
123 struct combine_diff_parent filler
[5];
125 int num_compare_stages
= 0;
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;
133 memset(combine
.p
.sha1
, 0, 20);
134 memset(&combine
.p
.parent
[0], 0,
135 sizeof(combine
.filler
));
137 while (i
< entries
) {
138 struct cache_entry
*nce
= active_cache
[i
];
141 if (strcmp(ce
->name
, nce
->name
))
144 /* Stage #2 (ours) is the first parent,
145 * stage #3 (theirs) is the second.
147 stage
= ce_stage(nce
);
149 int mode
= ntohl(nce
->ce_mode
);
150 num_compare_stages
++;
151 memcpy(combine
.p
.parent
[stage
-2].sha1
,
153 combine
.p
.parent
[stage
-2].mode
=
155 combine
.p
.parent
[stage
-2].status
=
156 DIFF_STATUS_MODIFIED
;
159 /* diff against the proper unmerged stage */
160 if (stage
== diff_unmerged_stage
)
165 * Compensate for loop update
169 if (combine_merges
&& num_compare_stages
== 2) {
170 show_combined_diff(&combine
.p
, 2,
171 dense_combined_merges
,
173 free(combine
.p
.path
);
176 free(combine
.p
.path
);
179 * Show the diff for the 'ce' if we found the one
180 * from the desired stage.
182 show_unmerge(ce
->name
);
183 if (ce_stage(ce
) != diff_unmerged_stage
)
187 if (lstat(ce
->name
, &st
) < 0) {
188 if (errno
!= ENOENT
&& errno
!= ENOTDIR
) {
197 changed
= ce_match_stat(ce
, &st
, 0);
198 if (!changed
&& !rev
.diffopt
.find_copies_harder
)
200 oldmode
= ntohl(ce
->ce_mode
);
202 newmode
= canon_mode(st
.st_mode
);
203 if (!trust_executable_bit
&&
204 S_ISREG(newmode
) && S_ISREG(oldmode
) &&
205 ((newmode
^ oldmode
) == 0111))
207 show_modified(oldmode
, newmode
,
208 ce
->sha1
, (changed
? null_sha1
: ce
->sha1
),
211 diffcore_std(&rev
.diffopt
);
212 diff_flush(&rev
.diffopt
);