2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 static void show_differences(char *name,
9 void *old_contents, unsigned long long old_size)
11 static char cmd[1000];
14 snprintf(cmd, sizeof(cmd), "diff -L %s -u -N - %s", name, name);
17 fwrite(old_contents, old_size, 1, f);
21 static void show_diff_empty(struct cache_entry *ce)
24 unsigned long int size;
26 unsigned char type[20], *p, *end;
28 old = read_sha1_file(ce->sha1, type, &size);
33 printf("--- %s\n", ce->name);
34 printf("+++ /dev/null\n");
40 printf("@@ -1,%d +0,0 @@\n", lines);
58 static const char *show_diff_usage = "show-diff [-s] [-q] [paths...]";
60 static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
63 int namelen = ce_namelen(ce);
64 for (i = 0; i < cnt; i++) {
65 int speclen = strlen(spec[i]);
66 if (! strncmp(spec[i], ce->name, speclen) &&
68 (ce->name[speclen] == 0 ||
69 ce->name[speclen] == '/'))
75 int main(int argc, char **argv)
78 int silent_on_nonexisting_files = 0;
79 int entries = read_cache();
82 while (1 < argc && argv[1][0] == '-') {
83 if (!strcmp(argv[1], "-s"))
84 silent_on_nonexisting_files = silent = 1;
85 else if (!strcmp(argv[1], "-q"))
86 silent_on_nonexisting_files = 1;
88 usage(show_diff_usage);
92 /* At this point, if argc == 1, then we are doing everything.
93 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
99 for (i = 0; i < entries; i++) {
101 struct cache_entry *ce = active_cache[i];
108 ! matches_pathspec(ce, argv+1, argc-1))
111 if (stat(ce->name, &st) < 0) {
112 if (errno == ENOENT && silent_on_nonexisting_files)
114 printf("%s: %s\n", ce->name, strerror(errno));
119 changed = cache_match_stat(ce, &st);
122 printf("%s: ", ce->name);
123 for (n = 0; n < 20; n++)
124 printf("%02x", ce->sha1[n]);
130 new = read_sha1_file(ce->sha1, type, &size);
131 show_differences(ce->name, new, size);