2005-04-08 00:16:10 +02:00
|
|
|
/*
|
|
|
|
* GIT - The information manager from hell
|
|
|
|
*
|
|
|
|
* Copyright (C) Linus Torvalds, 2005
|
|
|
|
*/
|
2005-04-08 00:13:13 +02:00
|
|
|
#include "cache.h"
|
2005-04-26 03:22:47 +02:00
|
|
|
#include "diff.h"
|
2005-04-12 11:04:44 +02:00
|
|
|
|
2005-04-17 06:29:45 +02:00
|
|
|
static const char *show_diff_usage = "show-diff [-q] [-s] [-z] [paths...]";
|
2005-04-17 06:29:45 +02:00
|
|
|
|
|
|
|
static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int namelen = ce_namelen(ce);
|
|
|
|
for (i = 0; i < cnt; i++) {
|
|
|
|
int speclen = strlen(spec[i]);
|
|
|
|
if (! strncmp(spec[i], ce->name, speclen) &&
|
|
|
|
speclen <= namelen &&
|
|
|
|
(ce->name[speclen] == 0 ||
|
|
|
|
ce->name[speclen] == '/'))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-08 00:13:13 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2005-04-13 10:40:09 +02:00
|
|
|
int silent = 0;
|
2005-04-16 00:08:09 +02:00
|
|
|
int silent_on_nonexisting_files = 0;
|
2005-04-17 06:29:45 +02:00
|
|
|
int machine_readable = 0;
|
2005-04-19 00:07:24 +02:00
|
|
|
int reverse = 0;
|
2005-04-08 00:13:13 +02:00
|
|
|
int entries = read_cache();
|
|
|
|
int i;
|
|
|
|
|
2005-04-17 06:29:45 +02:00
|
|
|
while (1 < argc && argv[1][0] == '-') {
|
2005-04-19 00:07:24 +02:00
|
|
|
if (!strcmp(argv[1], "-R"))
|
|
|
|
reverse = 1;
|
|
|
|
else if (!strcmp(argv[1], "-s"))
|
2005-04-16 00:08:09 +02:00
|
|
|
silent_on_nonexisting_files = silent = 1;
|
2005-04-17 06:29:45 +02:00
|
|
|
else if (!strcmp(argv[1], "-q"))
|
2005-04-16 00:08:09 +02:00
|
|
|
silent_on_nonexisting_files = 1;
|
2005-04-17 06:29:45 +02:00
|
|
|
else if (!strcmp(argv[1], "-z"))
|
2005-04-17 06:29:45 +02:00
|
|
|
machine_readable = 1;
|
2005-04-17 06:29:45 +02:00
|
|
|
else
|
|
|
|
usage(show_diff_usage);
|
|
|
|
argv++; argc--;
|
2005-04-13 10:40:09 +02:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:29:45 +02:00
|
|
|
/* At this point, if argc == 1, then we are doing everything.
|
|
|
|
* Otherwise argv[1] .. argv[argc-1] have the explicit paths.
|
|
|
|
*/
|
2005-04-08 00:13:13 +02:00
|
|
|
if (entries < 0) {
|
|
|
|
perror("read_cache");
|
|
|
|
exit(1);
|
|
|
|
}
|
2005-04-19 00:07:24 +02:00
|
|
|
prepare_diff_cmd();
|
2005-04-08 00:13:13 +02:00
|
|
|
for (i = 0; i < entries; i++) {
|
|
|
|
struct stat st;
|
|
|
|
struct cache_entry *ce = active_cache[i];
|
2005-04-17 06:29:45 +02:00
|
|
|
int changed;
|
2005-04-08 00:13:13 +02:00
|
|
|
unsigned long size;
|
|
|
|
char type[20];
|
2005-04-17 06:29:45 +02:00
|
|
|
void *old;
|
2005-04-08 00:13:13 +02:00
|
|
|
|
2005-04-17 06:29:45 +02:00
|
|
|
if (1 < argc &&
|
2005-04-17 06:29:45 +02:00
|
|
|
! matches_pathspec(ce, argv+1, argc-1))
|
|
|
|
continue;
|
|
|
|
|
2005-04-17 06:29:45 +02:00
|
|
|
if (ce_stage(ce)) {
|
|
|
|
if (machine_readable)
|
|
|
|
printf("U %s%c", ce->name, 0);
|
|
|
|
else
|
|
|
|
printf("%s: Unmerged\n",
|
|
|
|
ce->name);
|
|
|
|
while (i < entries &&
|
|
|
|
!strcmp(ce->name, active_cache[i]->name))
|
|
|
|
i++;
|
|
|
|
i--; /* compensate for loop control increments */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-04-08 00:13:13 +02:00
|
|
|
if (stat(ce->name, &st) < 0) {
|
2005-04-16 00:08:09 +02:00
|
|
|
if (errno == ENOENT && silent_on_nonexisting_files)
|
|
|
|
continue;
|
2005-04-17 06:29:45 +02:00
|
|
|
if (machine_readable)
|
|
|
|
printf("X %s%c", ce->name, 0);
|
|
|
|
else {
|
|
|
|
printf("%s: %s\n", ce->name, strerror(errno));
|
|
|
|
if (errno == ENOENT)
|
2005-04-26 03:22:47 +02:00
|
|
|
show_diff_empty(ce->sha1, ce->name,
|
|
|
|
reverse);
|
2005-04-17 06:29:45 +02:00
|
|
|
}
|
2005-04-08 00:13:13 +02:00
|
|
|
continue;
|
|
|
|
}
|
2005-04-09 18:48:20 +02:00
|
|
|
changed = cache_match_stat(ce, &st);
|
2005-04-12 11:01:12 +02:00
|
|
|
if (!changed)
|
2005-04-08 00:13:13 +02:00
|
|
|
continue;
|
2005-04-17 06:29:45 +02:00
|
|
|
if (!machine_readable)
|
|
|
|
printf("%s: %s\n", ce->name, sha1_to_hex(ce->sha1));
|
|
|
|
else {
|
|
|
|
printf("%s %s%c", sha1_to_hex(ce->sha1), ce->name, 0);
|
|
|
|
continue;
|
|
|
|
}
|
2005-04-13 10:40:09 +02:00
|
|
|
if (silent)
|
|
|
|
continue;
|
|
|
|
|
2005-04-17 06:29:45 +02:00
|
|
|
old = read_sha1_file(ce->sha1, type, &size);
|
2005-04-19 00:07:24 +02:00
|
|
|
if (! old)
|
|
|
|
error("unable to read blob object for %s (%s)",
|
|
|
|
ce->name, sha1_to_hex(ce->sha1));
|
|
|
|
else
|
2005-04-19 00:07:24 +02:00
|
|
|
show_differences(ce->name, ce->name, old, size,
|
|
|
|
reverse);
|
2005-04-17 06:29:45 +02:00
|
|
|
free(old);
|
2005-04-08 00:13:13 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|