[PATCH] Documentation: push-pull commands into a separate category.
[git.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 int diff_output_format = DIFF_FORMAT_HUMAN;
15 static int detect_rename = 0;
16 static int find_copies_harder = 0;
17 static int diff_setup_opt = 0;
18 static int diff_score_opt = 0;
19 static const char *pickaxe = NULL;
20 static int pickaxe_opts = 0;
21 static int diff_break_opt = -1;
22 static const char *orderfile = NULL;
23 static const char *diff_filter = NULL;
24 static int silent = 0;
25
26 static void show_unmerge(const char *path)
27 {
28         diff_unmerge(path);
29 }
30
31 static void show_file(int pfx, struct cache_entry *ce)
32 {
33         diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);
34 }
35
36 static void show_modified(int oldmode, int mode,
37                           const unsigned char *old_sha1, const unsigned char *sha1,
38                           char *path)
39 {
40         diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
41 }
42
43 int main(int argc, const char **argv)
44 {
45         static const unsigned char null_sha1[20] = { 0, };
46         int entries = read_cache();
47         int i;
48
49         while (1 < argc && argv[1][0] == '-') {
50                 if (!strcmp(argv[1], "-p") || !strcmp(argv[1], "-u"))
51                         diff_output_format = DIFF_FORMAT_PATCH;
52                 else if (!strcmp(argv[1], "-q"))
53                         silent = 1;
54                 else if (!strcmp(argv[1], "-r"))
55                         ; /* no-op */
56                 else if (!strcmp(argv[1], "-s"))
57                         ; /* no-op */
58                 else if (!strcmp(argv[1], "-z"))
59                         diff_output_format = DIFF_FORMAT_MACHINE;
60                 else if (!strcmp(argv[1], "--name-only"))
61                         diff_output_format = DIFF_FORMAT_NAME;
62                 else if (!strcmp(argv[1], "--name-only-z"))
63                         diff_output_format = DIFF_FORMAT_NAME_Z;
64                 else if (!strcmp(argv[1], "-R"))
65                         diff_setup_opt |= DIFF_SETUP_REVERSE;
66                 else if (!strncmp(argv[1], "-S", 2))
67                         pickaxe = argv[1] + 2;
68                 else if (!strncmp(argv[1], "-O", 2))
69                         orderfile = argv[1] + 2;
70                 else if (!strncmp(argv[1], "--diff-filter=", 14))
71                         diff_filter = argv[1] + 14;
72                 else if (!strcmp(argv[1], "--pickaxe-all"))
73                         pickaxe_opts = DIFF_PICKAXE_ALL;
74                 else if (!strncmp(argv[1], "-B", 2)) {
75                         if ((diff_break_opt =
76                              diff_scoreopt_parse(argv[1])) == -1)
77                                 usage(diff_files_usage);
78                 }
79                 else if (!strncmp(argv[1], "-M", 2)) {
80                         if ((diff_score_opt =
81                              diff_scoreopt_parse(argv[1])) == -1)
82                                 usage(diff_files_usage);
83                         detect_rename = DIFF_DETECT_RENAME;
84                 }
85                 else if (!strncmp(argv[1], "-C", 2)) {
86                         if ((diff_score_opt =
87                              diff_scoreopt_parse(argv[1])) == -1)
88                                 usage(diff_files_usage);
89                         detect_rename = DIFF_DETECT_COPY;
90                 }
91                 else if (!strcmp(argv[1], "--find-copies-harder"))
92                         find_copies_harder = 1;
93                 else
94                         usage(diff_files_usage);
95                 argv++; argc--;
96         }
97
98         if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
99                 usage(diff_files_usage);
100
101         /* At this point, if argc == 1, then we are doing everything.
102          * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
103          */
104         if (entries < 0) {
105                 perror("read_cache");
106                 exit(1);
107         }
108
109         diff_setup(diff_setup_opt);
110
111         for (i = 0; i < entries; i++) {
112                 struct stat st;
113                 unsigned int oldmode;
114                 struct cache_entry *ce = active_cache[i];
115                 int changed;
116
117                 if (ce_stage(ce)) {
118                         show_unmerge(ce->name);
119                         while (i < entries &&
120                                !strcmp(ce->name, active_cache[i]->name))
121                                 i++;
122                         i--; /* compensate for loop control increments */
123                         continue;
124                 }
125
126                 if (lstat(ce->name, &st) < 0) {
127                         if (errno != ENOENT && errno != ENOTDIR) {
128                                 perror(ce->name);
129                                 continue;
130                         }
131                         if (silent)
132                                 continue;
133                         show_file('-', ce);
134                         continue;
135                 }
136                 changed = ce_match_stat(ce, &st);
137                 if (!changed && !find_copies_harder)
138                         continue;
139                 oldmode = ntohl(ce->ce_mode);
140                 show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
141                               ce->sha1, (changed ? null_sha1 : ce->sha1),
142                               ce->name);
143         }
144         diffcore_std((1 < argc) ? argv + 1 : NULL,
145                      detect_rename, diff_score_opt,
146                      pickaxe, pickaxe_opts,
147                      diff_break_opt,
148                      orderfile, diff_filter);
149         diff_flush(diff_output_format);
150         return 0;
151 }