1/*
2* GIT - The information manager from hell
3*
4* Copyright (C) Linus Torvalds, 2005
5*/
6#include "cache.h"
7#include "diff.h"
89
static const char diff_files_usage[] =
10"git-diff-files [-q] "
11"[<common diff options>] [<path>...]"
12COMMON_DIFF_OPTIONS_HELP;
1314
static struct diff_options diff_options;
15static int silent = 0;
1617
static void show_unmerge(const char *path)
18{
19diff_unmerge(&diff_options, path);
20}
2122
static void show_file(int pfx, struct cache_entry *ce)
23{
24diff_addremove(&diff_options, pfx, ntohl(ce->ce_mode),
25ce->sha1, ce->name, NULL);
26}
2728
static void show_modified(int oldmode, int mode,
29const unsigned char *old_sha1, const unsigned char *sha1,
30char *path)
31{
32diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);
33}
3435
int main(int argc, const char **argv)
36{
37const char **pathspec;
38const char *prefix = setup_git_directory();
39int entries, i;
4041
git_config(git_diff_config);
42diff_setup(&diff_options);
43while (1 < argc && argv[1][0] == '-') {
44if (!strcmp(argv[1], "--")) {
45argv++;
46argc--;
47break;
48}
49if (!strcmp(argv[1], "-q"))
50silent = 1;
51else if (!strcmp(argv[1], "-r"))
52; /* no-op */
53else if (!strcmp(argv[1], "-s"))
54; /* no-op */
55else {
56int diff_opt_cnt;
57diff_opt_cnt = diff_opt_parse(&diff_options,
58argv+1, argc-1);
59if (diff_opt_cnt < 0)
60usage(diff_files_usage);
61else if (diff_opt_cnt) {
62argv += diff_opt_cnt;
63argc -= diff_opt_cnt;
64continue;
65}
66else
67usage(diff_files_usage);
68}
69argv++; argc--;
70}
7172
/* Find the directory, and set up the pathspec */
73pathspec = get_pathspec(prefix, argv + 1);
74entries = read_cache();
7576
if (diff_setup_done(&diff_options) < 0)
77usage(diff_files_usage);
7879
/* At this point, if argc == 1, then we are doing everything.
80* Otherwise argv[1] .. argv[argc-1] have the explicit paths.
81*/
82if (entries < 0) {
83perror("read_cache");
84exit(1);
85}
8687
for (i = 0; i < entries; i++) {
88struct stat st;
89unsigned int oldmode, newmode;
90struct cache_entry *ce = active_cache[i];
91int changed;
9293
if (!ce_path_match(ce, pathspec))
94continue;
9596
if (ce_stage(ce)) {
97show_unmerge(ce->name);
98while (i < entries &&
99!strcmp(ce->name, active_cache[i]->name))
100i++;
101i--; /* compensate for loop control increments */
102continue;
103}
104105
if (lstat(ce->name, &st) < 0) {
106if (errno != ENOENT && errno != ENOTDIR) {
107perror(ce->name);
108continue;
109}
110if (silent)
111continue;
112show_file('-', ce);
113continue;
114}
115changed = ce_match_stat(ce, &st);
116if (!changed && !diff_options.find_copies_harder)
117continue;
118oldmode = ntohl(ce->ce_mode);
119120
newmode = DIFF_FILE_CANON_MODE(st.st_mode);
121if (!trust_executable_bit &&
122S_ISREG(newmode) && S_ISREG(oldmode) &&
123((newmode ^ oldmode) == 0111))
124newmode = oldmode;
125show_modified(oldmode, newmode,
126ce->sha1, (changed ? null_sha1 : ce->sha1),
127ce->name);
128}
129diffcore_std(&diff_options);
130diff_flush(&diff_options);
131return 0;
132}