diff-cache.c: use the "U <pathname>" format for unmerged entries.
[git.git] / show-diff.c
blob4557bd82f302eb951e02281a67a84c959122031b
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 static const char *show_diff_usage = "show-diff [-q] [-s] [-z] [paths...]";
10 static int recursive = 0;
11 static int line_termination = '\n';
12 static int silent = 0;
13 static int silent_on_nonexisting_files = 0;
15 static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
17 int i;
18 int namelen = ce_namelen(ce);
19 for (i = 0; i < cnt; i++) {
20 int speclen = strlen(spec[i]);
21 if (! strncmp(spec[i], ce->name, speclen) &&
22 speclen <= namelen &&
23 (ce->name[speclen] == 0 ||
24 ce->name[speclen] == '/'))
25 return 1;
27 return 0;
30 static void show_file(const char *prefix, struct cache_entry *ce)
32 printf("%s%o\t%s\t%s\t%s%c", prefix, ntohl(ce->ce_mode), "blob",
33 sha1_to_hex(ce->sha1), ce->name, line_termination);
36 int main(int argc, char **argv)
38 static const char null_sha1_hex[] = "0000000000000000000000000000000000000000";
39 int entries = read_cache();
40 int i;
42 while (1 < argc && argv[1][0] == '-') {
43 if (!strcmp(argv[1], "-s"))
44 silent_on_nonexisting_files = silent = 1;
45 else if (!strcmp(argv[1], "-q"))
46 silent_on_nonexisting_files = 1;
47 else if (!strcmp(argv[1], "-z"))
48 line_termination = 0;
49 else if (!strcmp(argv[1], "-r"))
50 recursive = 1; /* No-op */
51 else
52 usage(show_diff_usage);
53 argv++; argc--;
56 /* At this point, if argc == 1, then we are doing everything.
57 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
59 if (entries < 0) {
60 perror("read_cache");
61 exit(1);
64 for (i = 0; i < entries; i++) {
65 struct stat st;
66 unsigned int oldmode, mode;
67 struct cache_entry *ce = active_cache[i];
68 int changed;
70 if (1 < argc &&
71 ! matches_pathspec(ce, argv+1, argc-1))
72 continue;
74 if (ce_stage(ce)) {
75 printf("U %s%c", ce->name, line_termination);
77 while (i < entries &&
78 !strcmp(ce->name, active_cache[i]->name))
79 i++;
80 i--; /* compensate for loop control increments */
81 continue;
84 if (stat(ce->name, &st) < 0) {
85 if (errno != ENOENT) {
86 perror(ce->name);
87 continue;
89 if (silent_on_nonexisting_files)
90 continue;
91 show_file("-", ce);
92 continue;
94 changed = cache_match_stat(ce, &st);
95 if (!changed)
96 continue;
98 oldmode = ntohl(ce->ce_mode);
99 mode = S_IFREG | ce_permissions(st.st_mode);
101 printf("*%o->%o\t%s\t%s->%s\t%s%c",
102 oldmode, mode, "blob",
103 sha1_to_hex(ce->sha1), null_sha1_hex,
104 ce->name, line_termination);
106 return 0;