[PATCH] show-diff.c: -R option for reverse diff.
[git/jrn.git] / show-diff.c
blob4456230a568afd6ad090daf6ed2f77b4361434d7
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 static char *diff_cmd = "diff -L 'a/%s' -L 'b/%s' ";
9 static char *diff_opts = "-p -u";
10 static char *diff_arg_forward = " - '%s'";
11 static char *diff_arg_reverse = " '%s' -";
13 static void prepare_diff_cmd(void)
16 * Default values above are meant to match the
17 * Linux kernel development style. Examples of
18 * alternative styles you can specify via environment
19 * variables are:
21 * GIT_DIFF_CMD="diff -L '%s' -L '%s'"
22 * GIT_DIFF_OPTS="-c";
24 diff_cmd = getenv("GIT_DIFF_CMD") ? : diff_cmd;
25 diff_opts = getenv("GIT_DIFF_OPTS") ? : diff_opts;
28 /* Help to copy the thing properly quoted for the shell safety.
29 * any single quote is replaced with '\'', and the caller is
30 * expected to enclose the result within a single quote pair.
32 * E.g.
33 * original sq_expand result
34 * name ==> name ==> 'name'
35 * a b ==> a b ==> 'a b'
36 * a'b ==> a'\''b ==> 'a'\''b'
38 * NOTE! The returned memory belongs to this function so
39 * do not free it.
41 static char *sq_expand(char *src)
43 static char *buf = NULL;
44 int cnt, c;
45 char *cp;
47 /* count bytes needed to store the quoted string. */
48 for (cnt = 1, cp = src; *cp; cnt++, cp++)
49 if (*cp == '\'')
50 cnt += 3;
52 if (! (buf = malloc(cnt)))
53 return buf;
54 cp = buf;
55 while ((c = *src++)) {
56 if (c != '\'')
57 *cp++ = c;
58 else {
59 cp = strcpy(cp, "'\\''");
60 cp += 4;
63 *cp = 0;
64 return buf;
67 static void show_differences(char *name, char *label, void *old_contents,
68 unsigned long long old_size, int reverse)
70 FILE *f;
71 char *name_sq = sq_expand(name);
72 char *label_sq = (name != label) ? sq_expand(label) : name_sq;
73 char *diff_arg = reverse ? diff_arg_reverse : diff_arg_forward;
74 int cmd_size = strlen(name_sq) + strlen(label_sq) * 2 +
75 strlen(diff_cmd) + strlen(diff_opts) + strlen(diff_arg);
76 char *cmd = malloc(cmd_size);
77 int next_at;
79 fflush(stdout);
80 next_at = snprintf(cmd, cmd_size, diff_cmd, label_sq, label_sq);
81 next_at += snprintf(cmd+next_at, cmd_size-next_at, "%s", diff_opts);
82 next_at += snprintf(cmd+next_at, cmd_size-next_at, diff_arg, name_sq);
83 f = popen(cmd, "w");
84 if (old_size)
85 fwrite(old_contents, old_size, 1, f);
86 pclose(f);
87 if (label_sq != name_sq)
88 free(label_sq);
89 free(name_sq);
90 free(cmd);
93 static void show_diff_empty(struct cache_entry *ce, int reverse)
95 char *old;
96 unsigned long int size;
97 unsigned char type[20];
99 old = read_sha1_file(ce->sha1, type, &size);
100 if (! old) {
101 error("unable to read blob object for %s (%s)", ce->name,
102 sha1_to_hex(ce->sha1));
103 return;
105 show_differences("/dev/null", ce->name, old, size, reverse);
108 static const char *show_diff_usage = "show-diff [-q] [-s] [-z] [paths...]";
110 static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
112 int i;
113 int namelen = ce_namelen(ce);
114 for (i = 0; i < cnt; i++) {
115 int speclen = strlen(spec[i]);
116 if (! strncmp(spec[i], ce->name, speclen) &&
117 speclen <= namelen &&
118 (ce->name[speclen] == 0 ||
119 ce->name[speclen] == '/'))
120 return 1;
122 return 0;
125 int main(int argc, char **argv)
127 int silent = 0;
128 int silent_on_nonexisting_files = 0;
129 int machine_readable = 0;
130 int reverse = 0;
131 int entries = read_cache();
132 int i;
134 while (1 < argc && argv[1][0] == '-') {
135 if (!strcmp(argv[1], "-R"))
136 reverse = 1;
137 else if (!strcmp(argv[1], "-s"))
138 silent_on_nonexisting_files = silent = 1;
139 else if (!strcmp(argv[1], "-q"))
140 silent_on_nonexisting_files = 1;
141 else if (!strcmp(argv[1], "-z"))
142 machine_readable = 1;
143 else
144 usage(show_diff_usage);
145 argv++; argc--;
148 /* At this point, if argc == 1, then we are doing everything.
149 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
151 if (entries < 0) {
152 perror("read_cache");
153 exit(1);
155 prepare_diff_cmd();
156 for (i = 0; i < entries; i++) {
157 struct stat st;
158 struct cache_entry *ce = active_cache[i];
159 int changed;
160 unsigned long size;
161 char type[20];
162 void *old;
164 if (1 < argc &&
165 ! matches_pathspec(ce, argv+1, argc-1))
166 continue;
168 if (ce_stage(ce)) {
169 if (machine_readable)
170 printf("U %s%c", ce->name, 0);
171 else
172 printf("%s: Unmerged\n",
173 ce->name);
174 while (i < entries &&
175 !strcmp(ce->name, active_cache[i]->name))
176 i++;
177 i--; /* compensate for loop control increments */
178 continue;
181 if (stat(ce->name, &st) < 0) {
182 if (errno == ENOENT && silent_on_nonexisting_files)
183 continue;
184 if (machine_readable)
185 printf("X %s%c", ce->name, 0);
186 else {
187 printf("%s: %s\n", ce->name, strerror(errno));
188 if (errno == ENOENT)
189 show_diff_empty(ce, reverse);
191 continue;
193 changed = cache_match_stat(ce, &st);
194 if (!changed)
195 continue;
196 if (!machine_readable)
197 printf("%s: %s\n", ce->name, sha1_to_hex(ce->sha1));
198 else {
199 printf("%s %s%c", sha1_to_hex(ce->sha1), ce->name, 0);
200 continue;
202 if (silent)
203 continue;
205 old = read_sha1_file(ce->sha1, type, &size);
206 if (! old)
207 error("unable to read blob object for %s (%s)",
208 ce->name, sha1_to_hex(ce->sha1));
209 else
210 show_differences(ce->name, ce->name, old, size,
211 reverse);
212 free(old);
214 return 0;