[PATCH] simplify Makefile
[git/spearce.git] / show-diff.c
blob2ff0bd4ea636db2ad54e9ff34e1d70ac7a9a98f0
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 static char *sq_expand(char *src)
40 static char *buf = NULL;
41 int cnt, c;
42 char *cp;
44 /* count bytes needed to store the quoted string. */
45 for (cnt = 1, cp = src; *cp; cnt++, cp++)
46 if (*cp == '\'')
47 cnt += 3;
49 if (! (buf = malloc(cnt)))
50 return buf;
51 cp = buf;
52 while ((c = *src++)) {
53 if (c != '\'')
54 *cp++ = c;
55 else {
56 cp = strcpy(cp, "'\\''");
57 cp += 4;
60 *cp = 0;
61 return buf;
64 static void show_differences(char *name, char *label, void *old_contents,
65 unsigned long long old_size, int reverse)
67 FILE *f;
68 char *name_sq = sq_expand(name);
69 char *label_sq = (name != label) ? sq_expand(label) : name_sq;
70 char *diff_arg = reverse ? diff_arg_reverse : diff_arg_forward;
71 int cmd_size = strlen(name_sq) + strlen(label_sq) * 2 +
72 strlen(diff_cmd) + strlen(diff_opts) + strlen(diff_arg);
73 char *cmd = malloc(cmd_size);
74 int next_at;
76 fflush(stdout);
77 next_at = snprintf(cmd, cmd_size, diff_cmd, label_sq, label_sq);
78 next_at += snprintf(cmd+next_at, cmd_size-next_at, "%s", diff_opts);
79 next_at += snprintf(cmd+next_at, cmd_size-next_at, diff_arg, name_sq);
80 f = popen(cmd, "w");
81 if (old_size)
82 fwrite(old_contents, old_size, 1, f);
83 pclose(f);
84 if (label_sq != name_sq)
85 free(label_sq);
86 free(name_sq);
87 free(cmd);
90 static void show_diff_empty(struct cache_entry *ce, int reverse)
92 char *old;
93 unsigned long int size;
94 unsigned char type[20];
96 old = read_sha1_file(ce->sha1, type, &size);
97 if (! old) {
98 error("unable to read blob object for %s (%s)", ce->name,
99 sha1_to_hex(ce->sha1));
100 return;
102 show_differences("/dev/null", ce->name, old, size, reverse);
105 static const char *show_diff_usage = "show-diff [-q] [-s] [-z] [paths...]";
107 static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
109 int i;
110 int namelen = ce_namelen(ce);
111 for (i = 0; i < cnt; i++) {
112 int speclen = strlen(spec[i]);
113 if (! strncmp(spec[i], ce->name, speclen) &&
114 speclen <= namelen &&
115 (ce->name[speclen] == 0 ||
116 ce->name[speclen] == '/'))
117 return 1;
119 return 0;
122 int main(int argc, char **argv)
124 int silent = 0;
125 int silent_on_nonexisting_files = 0;
126 int machine_readable = 0;
127 int reverse = 0;
128 int entries = read_cache();
129 int i;
131 while (1 < argc && argv[1][0] == '-') {
132 if (!strcmp(argv[1], "-R"))
133 reverse = 1;
134 else if (!strcmp(argv[1], "-s"))
135 silent_on_nonexisting_files = silent = 1;
136 else if (!strcmp(argv[1], "-q"))
137 silent_on_nonexisting_files = 1;
138 else if (!strcmp(argv[1], "-z"))
139 machine_readable = 1;
140 else
141 usage(show_diff_usage);
142 argv++; argc--;
145 /* At this point, if argc == 1, then we are doing everything.
146 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
148 if (entries < 0) {
149 perror("read_cache");
150 exit(1);
152 prepare_diff_cmd();
153 for (i = 0; i < entries; i++) {
154 struct stat st;
155 struct cache_entry *ce = active_cache[i];
156 int changed;
157 unsigned long size;
158 char type[20];
159 void *old;
161 if (1 < argc &&
162 ! matches_pathspec(ce, argv+1, argc-1))
163 continue;
165 if (ce_stage(ce)) {
166 if (machine_readable)
167 printf("U %s%c", ce->name, 0);
168 else
169 printf("%s: Unmerged\n",
170 ce->name);
171 while (i < entries &&
172 !strcmp(ce->name, active_cache[i]->name))
173 i++;
174 i--; /* compensate for loop control increments */
175 continue;
178 if (stat(ce->name, &st) < 0) {
179 if (errno == ENOENT && silent_on_nonexisting_files)
180 continue;
181 if (machine_readable)
182 printf("X %s%c", ce->name, 0);
183 else {
184 printf("%s: %s\n", ce->name, strerror(errno));
185 if (errno == ENOENT)
186 show_diff_empty(ce, reverse);
188 continue;
190 changed = cache_match_stat(ce, &st);
191 if (!changed)
192 continue;
193 if (!machine_readable)
194 printf("%s: %s\n", ce->name, sha1_to_hex(ce->sha1));
195 else {
196 printf("%s %s%c", sha1_to_hex(ce->sha1), ce->name, 0);
197 continue;
199 if (silent)
200 continue;
202 old = read_sha1_file(ce->sha1, type, &size);
203 if (! old)
204 error("unable to read blob object for %s (%s)",
205 ce->name, sha1_to_hex(ce->sha1));
206 else
207 show_differences(ce->name, ce->name, old, size,
208 reverse);
209 free(old);
211 return 0;