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] [-z] [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 machine_readable
= 0;
80 int entries
= read_cache();
83 while (1 < argc
&& argv
[1][0] == '-') {
84 if (!strcmp(argv
[1], "-s"))
85 silent_on_nonexisting_files
= silent
= 1;
86 else if (!strcmp(argv
[1], "-q"))
87 silent_on_nonexisting_files
= 1;
88 else if (!strcmp(argv
[1], "-z")) {
92 usage(show_diff_usage
);
96 /* At this point, if argc == 1, then we are doing everything.
97 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
100 perror("read_cache");
103 for (i
= 0; i
< entries
; i
++) {
105 struct cache_entry
*ce
= active_cache
[i
];
112 ! matches_pathspec(ce
, argv
+1, argc
-1))
115 if (stat(ce
->name
, &st
) < 0) {
116 if (errno
== ENOENT
&& silent_on_nonexisting_files
)
118 if (machine_readable
)
119 printf("X %s%c", ce
->name
, 0);
121 printf("%s: %s\n", ce
->name
, strerror(errno
));
127 changed
= cache_match_stat(ce
, &st
);
130 if (!machine_readable
)
131 printf("%s: %s\n", ce
->name
, sha1_to_hex(ce
->sha1
));
133 printf("%s %s%c", sha1_to_hex(ce
->sha1
), ce
->name
, 0);
140 new = read_sha1_file(ce
->sha1
, type
, &size
);
141 show_differences(ce
->name
, new, size
);