[PATCH] Diff overhaul, adding half of copy detection.
[alt-git.git] / diff-files.c
blobd0202549222fe0ee80e07e8ed9a670f0dedf2db2
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "diff.h"
9 static const char *diff_files_usage =
10 "git-diff-files [-p] [-q] [-r] [-z] [-M] [-C] [-R] [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 int silent = 0;
19 static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
21 int i;
22 int namelen = ce_namelen(ce);
23 for (i = 0; i < cnt; i++) {
24 int speclen = strlen(spec[i]);
25 if (! strncmp(spec[i], ce->name, speclen) &&
26 speclen <= namelen &&
27 (ce->name[speclen] == 0 ||
28 ce->name[speclen] == '/'))
29 return 1;
31 return 0;
34 static void show_unmerge(const char *path)
36 diff_unmerge(path);
39 static void show_file(int pfx, struct cache_entry *ce)
41 diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);
44 static void show_modified(int oldmode, int mode,
45 const unsigned char *old_sha1, const unsigned char *sha1,
46 char *path)
48 diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
51 int main(int argc, char **argv)
53 static const unsigned char null_sha1[20] = { 0, };
54 int entries = read_cache();
55 int i;
57 while (1 < argc && argv[1][0] == '-') {
58 if (!strcmp(argv[1], "-p"))
59 generate_patch = 1;
60 else if (!strcmp(argv[1], "-q"))
61 silent = 1;
62 else if (!strcmp(argv[1], "-r"))
63 ; /* no-op */
64 else if (!strcmp(argv[1], "-s"))
65 ; /* no-op */
66 else if (!strcmp(argv[1], "-z"))
67 line_termination = 0;
68 else if (!strcmp(argv[1], "-R"))
69 reverse_diff = 1;
70 else if (!strncmp(argv[1], "-M", 2)) {
71 diff_score_opt = diff_scoreopt_parse(argv[1]);
72 detect_rename = generate_patch = 1;
74 else if (!strncmp(argv[1], "-C", 2)) {
75 diff_score_opt = diff_scoreopt_parse(argv[1]);
76 detect_rename = 2;
77 generate_patch = 1;
79 else
80 usage(diff_files_usage);
81 argv++; argc--;
84 /* At this point, if argc == 1, then we are doing everything.
85 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
87 if (entries < 0) {
88 perror("read_cache");
89 exit(1);
92 diff_setup(detect_rename, diff_score_opt, reverse_diff,
93 (generate_patch ? -1 : line_termination),
94 NULL, 0);
96 for (i = 0; i < entries; i++) {
97 struct stat st;
98 unsigned int oldmode, mode;
99 struct cache_entry *ce = active_cache[i];
100 int changed;
102 if (1 < argc &&
103 ! matches_pathspec(ce, argv+1, argc-1))
104 continue;
106 if (ce_stage(ce)) {
107 show_unmerge(ce->name);
108 while (i < entries &&
109 !strcmp(ce->name, active_cache[i]->name))
110 i++;
111 i--; /* compensate for loop control increments */
112 continue;
115 if (lstat(ce->name, &st) < 0) {
116 if (errno != ENOENT && errno != ENOTDIR) {
117 perror(ce->name);
118 continue;
120 if (silent)
121 continue;
122 show_file('-', ce);
123 continue;
125 changed = ce_match_stat(ce, &st);
126 if (!changed)
127 continue;
129 oldmode = ntohl(ce->ce_mode);
130 mode = (S_ISLNK(st.st_mode) ? S_IFLNK :
131 S_IFREG | ce_permissions(st.st_mode));
133 show_modified(oldmode, mode, ce->sha1, null_sha1,
134 ce->name);
136 diff_flush();
137 return 0;