[PATCH] Prepare diffcore interface for diff-tree header supression.
[git/git.git] / diff-files.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163 6#include "cache.h"
4a6bf9e1 7#include "diff.h"
c0fb976a 8
17710391 9static const char *diff_files_usage =
52e95789 10"git-diff-files [-p] [-q] [-r] [-z] [-M] [-C] [-R] [-S<string>] [paths...]";
b8f80925 11
4a6bf9e1 12static int generate_patch = 0;
0a7668e9 13static int line_termination = '\n';
5c97558c 14static int detect_rename = 0;
57fe64a4
JH
15static int reverse_diff = 0;
16static int diff_score_opt = 0;
057c7d30 17static const char *pickaxe = NULL;
0a7668e9 18static int silent = 0;
0a7668e9 19
b8f80925
JH
20static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
21{
22 int i;
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) &&
27 speclen <= namelen &&
28 (ce->name[speclen] == 0 ||
29 ce->name[speclen] == '/'))
30 return 1;
31 }
32 return 0;
33}
34
4a6bf9e1 35static void show_unmerge(const char *path)
0a7668e9 36{
57fe64a4 37 diff_unmerge(path);
4a6bf9e1
JH
38}
39
40static void show_file(int pfx, struct cache_entry *ce)
41{
57fe64a4 42 diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);
4a6bf9e1
JH
43}
44
45static void show_modified(int oldmode, int mode,
bf0f910d 46 const unsigned char *old_sha1, const unsigned char *sha1,
4a6bf9e1
JH
47 char *path)
48{
57fe64a4 49 diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
0a7668e9
LT
50}
51
e83c5163
LT
52int main(int argc, char **argv)
53{
bf0f910d 54 static const unsigned char null_sha1[20] = { 0, };
e83c5163
LT
55 int entries = read_cache();
56 int i;
57
b8f80925 58 while (1 < argc && argv[1][0] == '-') {
d15aa430 59 if (!strcmp(argv[1], "-p"))
4a6bf9e1 60 generate_patch = 1;
b8f80925 61 else if (!strcmp(argv[1], "-q"))
d15aa430 62 silent = 1;
0a7668e9 63 else if (!strcmp(argv[1], "-r"))
4a6bf9e1 64 ; /* no-op */
d15aa430
JH
65 else if (!strcmp(argv[1], "-s"))
66 ; /* no-op */
67 else if (!strcmp(argv[1], "-z"))
68 line_termination = 0;
57fe64a4
JH
69 else if (!strcmp(argv[1], "-R"))
70 reverse_diff = 1;
52e95789
JH
71 else if (!strcmp(argv[1], "-S"))
72 pickaxe = argv[1] + 2;
57fe64a4
JH
73 else if (!strncmp(argv[1], "-M", 2)) {
74 diff_score_opt = diff_scoreopt_parse(argv[1]);
5c97558c
JH
75 detect_rename = generate_patch = 1;
76 }
427dcb4b
JH
77 else if (!strncmp(argv[1], "-C", 2)) {
78 diff_score_opt = diff_scoreopt_parse(argv[1]);
79 detect_rename = 2;
80 generate_patch = 1;
81 }
b8f80925 82 else
17710391 83 usage(diff_files_usage);
b8f80925 84 argv++; argc--;
e2e5e98a
PB
85 }
86
b8f80925
JH
87 /* At this point, if argc == 1, then we are doing everything.
88 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
89 */
e83c5163
LT
90 if (entries < 0) {
91 perror("read_cache");
92 exit(1);
93 }
be3cfa85 94
38c6f780 95 diff_setup(reverse_diff, (generate_patch ? -1 : line_termination));
5c97558c 96
e83c5163
LT
97 for (i = 0; i < entries; i++) {
98 struct stat st;
0a7668e9 99 unsigned int oldmode, mode;
e83c5163 100 struct cache_entry *ce = active_cache[i];
d94c6128 101 int changed;
e83c5163 102
d2522a65 103 if (1 < argc &&
b8f80925
JH
104 ! matches_pathspec(ce, argv+1, argc-1))
105 continue;
106
9fec8b26 107 if (ce_stage(ce)) {
4a6bf9e1 108 show_unmerge(ce->name);
9fec8b26
JH
109 while (i < entries &&
110 !strcmp(ce->name, active_cache[i]->name))
111 i++;
112 i--; /* compensate for loop control increments */
113 continue;
114 }
57fe64a4 115
ffbe1add 116 if (lstat(ce->name, &st) < 0) {
41174694 117 if (errno != ENOENT && errno != ENOTDIR) {
0a7668e9 118 perror(ce->name);
ca2a0798 119 continue;
57fe64a4 120 }
d15aa430 121 if (silent)
0a7668e9 122 continue;
4a6bf9e1 123 show_file('-', ce);
e83c5163
LT
124 continue;
125 }
5d728c84 126 changed = ce_match_stat(ce, &st);
c3e7fbcb 127 if (!changed && detect_rename < 2)
e83c5163 128 continue;
e2e5e98a 129
0a7668e9 130 oldmode = ntohl(ce->ce_mode);
95649d6c
JH
131 mode = (S_ISLNK(st.st_mode) ? S_IFLNK :
132 S_IFREG | ce_permissions(st.st_mode));
0a7668e9 133
4a6bf9e1
JH
134 show_modified(oldmode, mode, ce->sha1, null_sha1,
135 ce->name);
e83c5163 136 }
38c6f780
JH
137 if (detect_rename)
138 diff_detect_rename(detect_rename, diff_score_opt);
139 if (pickaxe)
140 diff_pickaxe(pickaxe);
141 diff_flush(NULL, 0);
e83c5163
LT
142 return 0;
143}