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