2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 static char *diff_cmd = "diff -L 'a/%s' -L 'b/%s' ";
9 static char *diff_opts = "-p -u";
11 static void prepare_diff_cmd(void)
14 * Default values above are meant to match the
15 * Linux kernel development style. Examples of
16 * alternative styles you can specify via environment
19 * GIT_DIFF_CMD="diff -L '%s' -L '%s'"
22 diff_cmd = getenv("GIT_DIFF_CMD") ? : diff_cmd;
23 diff_opts = getenv("GIT_DIFF_OPTS") ? : diff_opts;
26 /* Help to copy the thing properly quoted for the shell safety.
27 * any single quote is replaced with '\'', and the caller is
28 * expected to enclose the result within a single quote pair.
31 * original sq_expand result
32 * name ==> name ==> 'name'
33 * a b ==> a b ==> 'a b'
34 * a'b ==> a'\''b ==> 'a'\''b'
36 * NOTE! The returned memory belongs to this function so
39 static char *sq_expand(char *src)
41 static char *buf = NULL;
45 /* count bytes needed to store the quoted string. */
46 for (cnt = 1, cp = src; *cp; cnt++, cp++)
50 if (! (buf = malloc(cnt)))
53 while ((c = *src++)) {
57 cp = strcpy(cp, "'\\''");
65 static void show_differences(char *name, char *label, void *old_contents,
66 unsigned long long old_size)
68 static char *diff_arg = " - '%s'";
70 char *name_sq = sq_expand(name);
71 char *label_sq = (name != label) ? sq_expand(label) : name_sq;
72 int cmd_size = strlen(name_sq) + strlen(label_sq) * 2 +
73 strlen(diff_cmd) + strlen(diff_opts) + strlen(diff_arg);
74 char *cmd = malloc(cmd_size);
78 next_at = snprintf(cmd, cmd_size, diff_cmd, label_sq, label_sq);
79 next_at += snprintf(cmd+next_at, cmd_size-next_at, "%s", diff_opts);
80 next_at += snprintf(cmd+next_at, cmd_size-next_at, diff_arg, name_sq);
83 fwrite(old_contents, old_size, 1, f);
85 if (label_sq != name_sq)
91 static void show_diff_empty(struct cache_entry *ce)
94 unsigned long int size;
95 unsigned char type[20];
97 old = read_sha1_file(ce->sha1, type, &size);
99 error("unable to read blob object for %s (%s)", ce->name,
100 sha1_to_hex(ce->sha1));
103 show_differences("/dev/null", ce->name, old, size);
106 static const char *show_diff_usage = "show-diff [-q] [-s] [-z] [paths...]";
108 static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
111 int namelen = ce_namelen(ce);
112 for (i = 0; i < cnt; i++) {
113 int speclen = strlen(spec[i]);
114 if (! strncmp(spec[i], ce->name, speclen) &&
115 speclen <= namelen &&
116 (ce->name[speclen] == 0 ||
117 ce->name[speclen] == '/'))
123 int main(int argc, char **argv)
126 int silent_on_nonexisting_files = 0;
127 int machine_readable = 0;
128 int entries = read_cache();
131 while (1 < argc && argv[1][0] == '-') {
132 if (!strcmp(argv[1], "-s"))
133 silent_on_nonexisting_files = silent = 1;
134 else if (!strcmp(argv[1], "-q"))
135 silent_on_nonexisting_files = 1;
136 else if (!strcmp(argv[1], "-z"))
137 machine_readable = 1;
139 usage(show_diff_usage);
143 /* At this point, if argc == 1, then we are doing everything.
144 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
147 perror("read_cache");
151 for (i = 0; i < entries; i++) {
153 struct cache_entry *ce = active_cache[i];
160 ! matches_pathspec(ce, argv+1, argc-1))
164 if (machine_readable)
165 printf("U %s%c", ce->name, 0);
167 printf("%s: Unmerged\n",
169 while (i < entries &&
170 !strcmp(ce->name, active_cache[i]->name))
172 i--; /* compensate for loop control increments */
176 if (stat(ce->name, &st) < 0) {
177 if (errno == ENOENT && silent_on_nonexisting_files)
179 if (machine_readable)
180 printf("X %s%c", ce->name, 0);
182 printf("%s: %s\n", ce->name, strerror(errno));
188 changed = cache_match_stat(ce, &st);
191 if (!machine_readable)
192 printf("%s: %s\n", ce->name, sha1_to_hex(ce->sha1));
194 printf("%s %s%c", sha1_to_hex(ce->sha1), ce->name, 0);
200 old = read_sha1_file(ce->sha1, type, &size);
202 error("unable to read blob object for %s (%s)",
203 ce->name, sha1_to_hex(ce->sha1));
205 show_differences(ce->name, ce->name, old, size);