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