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