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{
37static const unsigned char null_sha1[20] = { 0, };
38const char **pathspec;
39const char *prefix = setup_git_directory();
40int entries, i;
4142
diff_setup(&diff_options);
43while (1 < argc && argv[1][0] == '-') {
44if (!strcmp(argv[1], "-q"))
45silent = 1;
46else if (!strcmp(argv[1], "-r"))
47; /* no-op */
48else if (!strcmp(argv[1], "-s"))
49; /* no-op */
50else {
51int diff_opt_cnt;
52diff_opt_cnt = diff_opt_parse(&diff_options,
53argv+1, argc-1);
54if (diff_opt_cnt < 0)
55usage(diff_files_usage);
56else if (diff_opt_cnt) {
57argv += diff_opt_cnt;
58argc -= diff_opt_cnt;
59continue;
60}
61else
62usage(diff_files_usage);
63}
64argv++; argc--;
65}
6667
/* Find the directory, and set up the pathspec */
68pathspec = get_pathspec(prefix, argv + 1);
69entries = read_cache();
7071
if (diff_setup_done(&diff_options) < 0)
72usage(diff_files_usage);
7374
/* At this point, if argc == 1, then we are doing everything.
75* Otherwise argv[1] .. argv[argc-1] have the explicit paths.
76*/
77if (entries < 0) {
78perror("read_cache");
79exit(1);
80}
8182
for (i = 0; i < entries; i++) {
83struct stat st;
84unsigned int oldmode;
85struct cache_entry *ce = active_cache[i];
86int changed;
8788
if (!ce_path_match(ce, pathspec))
89continue;
9091
if (ce_stage(ce)) {
92show_unmerge(ce->name);
93while (i < entries &&
94!strcmp(ce->name, active_cache[i]->name))
95i++;
96i--; /* compensate for loop control increments */
97continue;
98}
99100
if (lstat(ce->name, &st) < 0) {
101if (errno != ENOENT && errno != ENOTDIR) {
102perror(ce->name);
103continue;
104}
105if (silent)
106continue;
107show_file('-', ce);
108continue;
109}
110changed = ce_match_stat(ce, &st);
111if (!changed && !diff_options.find_copies_harder)
112continue;
113oldmode = ntohl(ce->ce_mode);
114show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
115ce->sha1, (changed ? null_sha1 : ce->sha1),
116ce->name);
117}
118diffcore_std(&diff_options);
119diff_flush(&diff_options);
120return 0;
121}