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