git-whatchanged: allow other pagers
[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 =
57fe64a4 10"git-diff-files [-p] [-q] [-r] [-z] [-M] [-R] [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;
0a7668e9 17static int silent = 0;
0a7668e9 18
b8f80925
JH
19static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
20{
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;
30 }
31 return 0;
32}
33
4a6bf9e1 34static void show_unmerge(const char *path)
0a7668e9 35{
57fe64a4 36 diff_unmerge(path);
4a6bf9e1
JH
37}
38
39static void show_file(int pfx, struct cache_entry *ce)
40{
57fe64a4 41 diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);
4a6bf9e1
JH
42}
43
44static void show_modified(int oldmode, int mode,
bf0f910d 45 const unsigned char *old_sha1, const unsigned char *sha1,
4a6bf9e1
JH
46 char *path)
47{
57fe64a4 48 diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
0a7668e9
LT
49}
50
e83c5163
LT
51int main(int argc, char **argv)
52{
bf0f910d 53 static const unsigned char null_sha1[20] = { 0, };
e83c5163
LT
54 int entries = read_cache();
55 int i;
56
b8f80925 57 while (1 < argc && argv[1][0] == '-') {
d15aa430 58 if (!strcmp(argv[1], "-p"))
4a6bf9e1 59 generate_patch = 1;
b8f80925 60 else if (!strcmp(argv[1], "-q"))
d15aa430 61 silent = 1;
0a7668e9 62 else if (!strcmp(argv[1], "-r"))
4a6bf9e1 63 ; /* no-op */
d15aa430
JH
64 else if (!strcmp(argv[1], "-s"))
65 ; /* no-op */
66 else if (!strcmp(argv[1], "-z"))
67 line_termination = 0;
57fe64a4
JH
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]);
5c97558c
JH
72 detect_rename = generate_patch = 1;
73 }
b8f80925 74 else
17710391 75 usage(diff_files_usage);
b8f80925 76 argv++; argc--;
e2e5e98a
PB
77 }
78
b8f80925
JH
79 /* At this point, if argc == 1, then we are doing everything.
80 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
81 */
e83c5163
LT
82 if (entries < 0) {
83 perror("read_cache");
84 exit(1);
85 }
be3cfa85 86
57fe64a4
JH
87 diff_setup(detect_rename, diff_score_opt, reverse_diff,
88 (generate_patch ? -1 : line_termination),
e99d59ff 89 NULL, 0);
5c97558c 90
e83c5163
LT
91 for (i = 0; i < entries; i++) {
92 struct stat st;
0a7668e9 93 unsigned int oldmode, mode;
e83c5163 94 struct cache_entry *ce = active_cache[i];
d94c6128 95 int changed;
e83c5163 96
d2522a65 97 if (1 < argc &&
b8f80925
JH
98 ! matches_pathspec(ce, argv+1, argc-1))
99 continue;
100
9fec8b26 101 if (ce_stage(ce)) {
4a6bf9e1 102 show_unmerge(ce->name);
9fec8b26
JH
103 while (i < entries &&
104 !strcmp(ce->name, active_cache[i]->name))
105 i++;
106 i--; /* compensate for loop control increments */
107 continue;
108 }
57fe64a4 109
ffbe1add 110 if (lstat(ce->name, &st) < 0) {
41174694 111 if (errno != ENOENT && errno != ENOTDIR) {
0a7668e9 112 perror(ce->name);
ca2a0798 113 continue;
57fe64a4 114 }
d15aa430 115 if (silent)
0a7668e9 116 continue;
4a6bf9e1 117 show_file('-', ce);
e83c5163
LT
118 continue;
119 }
5d728c84 120 changed = ce_match_stat(ce, &st);
5e76011c 121 if (!changed)
e83c5163 122 continue;
e2e5e98a 123
0a7668e9 124 oldmode = ntohl(ce->ce_mode);
95649d6c
JH
125 mode = (S_ISLNK(st.st_mode) ? S_IFLNK :
126 S_IFREG | ce_permissions(st.st_mode));
0a7668e9 127
4a6bf9e1
JH
128 show_modified(oldmode, mode, ce->sha1, null_sha1,
129 ce->name);
e83c5163 130 }
57fe64a4 131 diff_flush();
e83c5163
LT
132 return 0;
133}