[PATCH] diff-cache buglet
[git/gitweb-caching.git] / show-diff.c
blob967c52399016ad99a8fde66ec7c36b0728c4744e
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 *show_diff_usage = "show-diff [-q] [-s] [-z] [paths...]";
11 static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
13 int i;
14 int namelen = ce_namelen(ce);
15 for (i = 0; i < cnt; i++) {
16 int speclen = strlen(spec[i]);
17 if (! strncmp(spec[i], ce->name, speclen) &&
18 speclen <= namelen &&
19 (ce->name[speclen] == 0 ||
20 ce->name[speclen] == '/'))
21 return 1;
23 return 0;
26 int main(int argc, char **argv)
28 int silent = 0;
29 int silent_on_nonexisting_files = 0;
30 int machine_readable = 0;
31 int reverse = 0;
32 int entries = read_cache();
33 int i;
35 while (1 < argc && argv[1][0] == '-') {
36 if (!strcmp(argv[1], "-R"))
37 reverse = 1;
38 else if (!strcmp(argv[1], "-s"))
39 silent_on_nonexisting_files = silent = 1;
40 else if (!strcmp(argv[1], "-q"))
41 silent_on_nonexisting_files = 1;
42 else if (!strcmp(argv[1], "-z"))
43 machine_readable = 1;
44 else
45 usage(show_diff_usage);
46 argv++; argc--;
49 /* At this point, if argc == 1, then we are doing everything.
50 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
52 if (entries < 0) {
53 perror("read_cache");
54 exit(1);
57 for (i = 0; i < entries; i++) {
58 struct stat st;
59 struct cache_entry *ce = active_cache[i];
60 int changed;
62 if (1 < argc &&
63 ! matches_pathspec(ce, argv+1, argc-1))
64 continue;
66 if (ce_stage(ce)) {
67 if (machine_readable)
68 printf("U %s%c", ce->name, 0);
69 else
70 printf("%s: Unmerged\n",
71 ce->name);
72 while (i < entries &&
73 !strcmp(ce->name, active_cache[i]->name))
74 i++;
75 i--; /* compensate for loop control increments */
76 continue;
79 if (stat(ce->name, &st) < 0) {
80 if (errno == ENOENT && silent_on_nonexisting_files)
81 continue;
82 if (machine_readable)
83 printf("X %s%c", ce->name, 0);
84 else {
85 printf("%s: %s\n", ce->name, strerror(errno));
86 if (errno == ENOENT)
87 show_diff_empty(ce, reverse);
89 continue;
91 changed = cache_match_stat(ce, &st);
92 if (!changed)
93 continue;
94 if (!machine_readable)
95 printf("%s: %s\n", ce->name, sha1_to_hex(ce->sha1));
96 else {
97 printf("%s %s%c", sha1_to_hex(ce->sha1), ce->name, 0);
98 continue;
100 if (silent)
101 continue;
103 show_differences(ce, reverse);
105 return 0;