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