[PATCH] Tweak diffcore-rename heuristics.
[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
52e95789
JH
95 diff_setup(detect_rename, diff_score_opt, pickaxe,
96 reverse_diff, (generate_patch ? -1 : line_termination),
e99d59ff 97 NULL, 0);
5c97558c 98
e83c5163
LT
99 for (i = 0; i < entries; i++) {
100 struct stat st;
0a7668e9 101 unsigned int oldmode, mode;
e83c5163 102 struct cache_entry *ce = active_cache[i];
d94c6128 103 int changed;
e83c5163 104
d2522a65 105 if (1 < argc &&
b8f80925
JH
106 ! matches_pathspec(ce, argv+1, argc-1))
107 continue;
108
9fec8b26 109 if (ce_stage(ce)) {
4a6bf9e1 110 show_unmerge(ce->name);
9fec8b26
JH
111 while (i < entries &&
112 !strcmp(ce->name, active_cache[i]->name))
113 i++;
114 i--; /* compensate for loop control increments */
115 continue;
116 }
57fe64a4 117
ffbe1add 118 if (lstat(ce->name, &st) < 0) {
41174694 119 if (errno != ENOENT && errno != ENOTDIR) {
0a7668e9 120 perror(ce->name);
ca2a0798 121 continue;
57fe64a4 122 }
d15aa430 123 if (silent)
0a7668e9 124 continue;
4a6bf9e1 125 show_file('-', ce);
e83c5163
LT
126 continue;
127 }
5d728c84 128 changed = ce_match_stat(ce, &st);
c3e7fbcb 129 if (!changed && detect_rename < 2)
e83c5163 130 continue;
e2e5e98a 131
0a7668e9 132 oldmode = ntohl(ce->ce_mode);
95649d6c
JH
133 mode = (S_ISLNK(st.st_mode) ? S_IFLNK :
134 S_IFREG | ce_permissions(st.st_mode));
0a7668e9 135
4a6bf9e1
JH
136 show_modified(oldmode, mode, ce->sha1, null_sha1,
137 ce->name);
e83c5163 138 }
57fe64a4 139 diff_flush();
e83c5163
LT
140 return 0;
141}