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