2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 static const char *diff_files_usage =
10 "git-diff-files [-p] [-q] [-r] [-z] [-M] [-C] [-R] [-S<string>] [paths...]";
12 static int generate_patch = 0;
13 static int line_termination = '\n';
14 static int detect_rename = 0;
15 static int reverse_diff = 0;
16 static int diff_score_opt = 0;
17 static const char *pickaxe = NULL;
18 static int silent = 0;
20 static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
23 int namelen = ce_namelen(ce);
24 for (i = 0; i < cnt; i++) {
25 int speclen = strlen(spec[i]);
26 if (! strncmp(spec[i], ce->name, speclen) &&
28 (ce->name[speclen] == 0 ||
29 ce->name[speclen] == '/'))
35 static void show_unmerge(const char *path)
40 static void show_file(int pfx, struct cache_entry *ce)
42 diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);
45 static void show_modified(int oldmode, int mode,
46 const unsigned char *old_sha1, const unsigned char *sha1,
49 diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
52 int main(int argc, char **argv)
54 static const unsigned char null_sha1[20] = { 0, };
55 int entries = read_cache();
58 while (1 < argc && argv[1][0] == '-') {
59 if (!strcmp(argv[1], "-p"))
61 else if (!strcmp(argv[1], "-q"))
63 else if (!strcmp(argv[1], "-r"))
65 else if (!strcmp(argv[1], "-s"))
67 else if (!strcmp(argv[1], "-z"))
69 else if (!strcmp(argv[1], "-R"))
71 else if (!strcmp(argv[1], "-S"))
72 pickaxe = argv[1] + 2;
73 else if (!strncmp(argv[1], "-M", 2)) {
74 diff_score_opt = diff_scoreopt_parse(argv[1]);
75 detect_rename = generate_patch = 1;
77 else if (!strncmp(argv[1], "-C", 2)) {
78 diff_score_opt = diff_scoreopt_parse(argv[1]);
83 usage(diff_files_usage);
87 /* At this point, if argc == 1, then we are doing everything.
88 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
95 diff_setup(detect_rename, diff_score_opt, pickaxe,
96 reverse_diff, (generate_patch ? -1 : line_termination),
99 for (i = 0; i < entries; i++) {
101 unsigned int oldmode, mode;
102 struct cache_entry *ce = active_cache[i];
106 ! matches_pathspec(ce, argv+1, argc-1))
110 show_unmerge(ce->name);
111 while (i < entries &&
112 !strcmp(ce->name, active_cache[i]->name))
114 i--; /* compensate for loop control increments */
118 if (lstat(ce->name, &st) < 0) {
119 if (errno != ENOENT && errno != ENOTDIR) {
128 changed = ce_match_stat(ce, &st);
129 if (!changed && detect_rename < 2)
132 oldmode = ntohl(ce->ce_mode);
133 mode = (S_ISLNK(st.st_mode) ? S_IFLNK :
134 S_IFREG | ce_permissions(st.st_mode));
136 show_modified(oldmode, mode, ce->sha1, null_sha1,