2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 static char *diff_cmd = "diff -L '%s' -u -N - '%s'";
11 /* Help to copy the thing properly quoted for the shell safety.
12 * any single quote is replaced with '\'', and the caller is
13 * expected to enclose the result within a single quote pair.
16 * original sq_expand result
17 * name ==> name ==> 'name'
18 * a b ==> a b ==> 'a b'
19 * a'b ==> a'\''b ==> 'a'\''b'
21 * NOTE! The returned memory belongs to this function so
24 static char *sq_expand(char *src)
26 static char *buf = NULL;
27 static int buf_size = -1;
31 /* count single quote characters */
32 for (cnt = 0, cp = src; *cp; cnt++, cp++)
43 while ((c = *src++)) {
47 cp = strcpy(cp, "'\\''");
55 static void show_differences(char *name, void *old_contents,
56 unsigned long long old_size)
59 static char *cmd = NULL;
60 static int cmd_size = -1;
62 char *name_sq = sq_expand(name);
63 int cmd_required_length = strlen(name_sq) * 2 + strlen(diff_cmd);
65 if (cmd_size < cmd_required_length) {
67 cmd_size = cmd_required_length;
68 cmd = malloc(cmd_required_length);
70 snprintf(cmd, cmd_size, diff_cmd, name_sq, name_sq);
73 fwrite(old_contents, old_size, 1, f);
77 static void show_diff_empty(struct cache_entry *ce)
80 unsigned long int size;
82 unsigned char type[20], *p, *end;
84 old = read_sha1_file(ce->sha1, type, &size);
89 printf("--- %s\n", ce->name);
90 printf("+++ /dev/null\n");
96 printf("@@ -1,%d +0,0 @@\n", lines);
114 static const char *show_diff_usage = "show-diff [-q] [-s] [-z] [paths...]";
116 static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
119 int namelen = ce_namelen(ce);
120 for (i = 0; i < cnt; i++) {
121 int speclen = strlen(spec[i]);
122 if (! strncmp(spec[i], ce->name, speclen) &&
123 speclen <= namelen &&
124 (ce->name[speclen] == 0 ||
125 ce->name[speclen] == '/'))
131 int main(int argc, char **argv)
134 int silent_on_nonexisting_files = 0;
135 int machine_readable = 0;
136 int entries = read_cache();
139 while (1 < argc && argv[1][0] == '-') {
140 if (!strcmp(argv[1], "-s"))
141 silent_on_nonexisting_files = silent = 1;
142 else if (!strcmp(argv[1], "-q"))
143 silent_on_nonexisting_files = 1;
144 else if (!strcmp(argv[1], "-z"))
145 machine_readable = 1;
147 usage(show_diff_usage);
151 /* At this point, if argc == 1, then we are doing everything.
152 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
155 perror("read_cache");
158 for (i = 0; i < entries; i++) {
160 struct cache_entry *ce = active_cache[i];
167 ! matches_pathspec(ce, argv+1, argc-1))
170 if (stat(ce->name, &st) < 0) {
171 if (errno == ENOENT && silent_on_nonexisting_files)
173 if (machine_readable)
174 printf("X %s%c", ce->name, 0);
176 printf("%s: %s\n", ce->name, strerror(errno));
182 changed = cache_match_stat(ce, &st);
185 if (!machine_readable)
186 printf("%s: %s\n", ce->name, sha1_to_hex(ce->sha1));
188 printf("%s %s%c", sha1_to_hex(ce->sha1), ce->name, 0);
195 old = read_sha1_file(ce->sha1, type, &size);
196 show_differences(ce->name, old, size);