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