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] [<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;
17 static void show_unmerge(const char *path)
19 diff_unmerge(&diff_options, path);
22 static void show_file(int pfx, struct cache_entry *ce)
24 diff_addremove(&diff_options, pfx, ntohl(ce->ce_mode),
25 ce->sha1, ce->name, NULL);
28 static void show_modified(int oldmode, int mode,
29 const unsigned char *old_sha1, const unsigned char *sha1,
32 diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);
35 int main(int argc, const char **argv)
37 const char **pathspec;
38 const char *prefix = setup_git_directory();
41 git_config(git_diff_config);
42 diff_setup(&diff_options);
43 while (1 < argc && argv[1][0] == '-') {
44 if (!strcmp(argv[1], "--")) {
49 if (!strcmp(argv[1], "-0"))
50 diff_unmerged_stage = 0;
51 else if (!strcmp(argv[1], "-1"))
52 diff_unmerged_stage = 1;
53 else if (!strcmp(argv[1], "-2"))
54 diff_unmerged_stage = 2;
55 else if (!strcmp(argv[1], "-3"))
56 diff_unmerged_stage = 3;
57 else if (!strcmp(argv[1], "--base"))
58 diff_unmerged_stage = 1;
59 else if (!strcmp(argv[1], "--ours"))
60 diff_unmerged_stage = 2;
61 else if (!strcmp(argv[1], "--theirs"))
62 diff_unmerged_stage = 3;
63 else if (!strcmp(argv[1], "-q"))
65 else if (!strcmp(argv[1], "-r"))
67 else if (!strcmp(argv[1], "-s"))
71 diff_opt_cnt = diff_opt_parse(&diff_options,
74 usage(diff_files_usage);
75 else if (diff_opt_cnt) {
81 usage(diff_files_usage);
86 /* Find the directory, and set up the pathspec */
87 pathspec = get_pathspec(prefix, argv + 1);
88 entries = read_cache();
90 if (diff_setup_done(&diff_options) < 0)
91 usage(diff_files_usage);
93 /* At this point, if argc == 1, then we are doing everything.
94 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
101 for (i = 0; i < entries; i++) {
103 unsigned int oldmode, newmode;
104 struct cache_entry *ce = active_cache[i];
107 if (!ce_path_match(ce, pathspec))
111 show_unmerge(ce->name);
112 while (i < entries) {
113 struct cache_entry *nce = active_cache[i];
115 if (strcmp(ce->name, nce->name))
117 /* diff against the proper unmerged stage */
118 if (ce_stage(nce) == diff_unmerged_stage)
123 * Compensate for loop update
127 * Show the diff for the 'ce' if we found the one
128 * from the desired stage.
130 if (ce_stage(ce) != diff_unmerged_stage)
134 if (lstat(ce->name, &st) < 0) {
135 if (errno != ENOENT && errno != ENOTDIR) {
144 changed = ce_match_stat(ce, &st);
145 if (!changed && !diff_options.find_copies_harder)
147 oldmode = ntohl(ce->ce_mode);
149 newmode = DIFF_FILE_CANON_MODE(st.st_mode);
150 if (!trust_executable_bit &&
151 S_ISREG(newmode) && S_ISREG(oldmode) &&
152 ((newmode ^ oldmode) == 0111))
154 show_modified(oldmode, newmode,
155 ce->sha1, (changed ? null_sha1 : ce->sha1),
158 diffcore_std(&diff_options);
159 diff_flush(&diff_options);