2005-04-08 06:16:10 +08:00
|
|
|
/*
|
|
|
|
* GIT - The information manager from hell
|
|
|
|
*
|
|
|
|
* Copyright (C) Linus Torvalds, 2005
|
|
|
|
*/
|
2005-04-08 06:13:13 +08:00
|
|
|
#include "cache.h"
|
2005-04-12 17:04:44 +08:00
|
|
|
|
2005-04-17 12:29:45 +08:00
|
|
|
static const char *show_diff_usage = "show-diff [-q] [-s] [-z] [paths...]";
|
2005-04-17 12:29:45 +08:00
|
|
|
|
2005-04-27 08:17:36 +08:00
|
|
|
static int recursive = 0;
|
|
|
|
static int line_termination = '\n';
|
|
|
|
static int silent = 0;
|
|
|
|
static int silent_on_nonexisting_files = 0;
|
|
|
|
|
2005-04-17 12:29:45 +08: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-27 08:17:36 +08:00
|
|
|
static void show_file(const char *prefix, struct cache_entry *ce)
|
|
|
|
{
|
|
|
|
printf("%s%o\t%s\t%s\t%s%c", prefix, ntohl(ce->ce_mode), "blob",
|
|
|
|
sha1_to_hex(ce->sha1), ce->name, line_termination);
|
|
|
|
}
|
|
|
|
|
2005-04-08 06:13:13 +08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2005-04-27 08:17:36 +08:00
|
|
|
static const char null_sha1_hex[] = "0000000000000000000000000000000000000000";
|
2005-04-08 06:13:13 +08:00
|
|
|
int entries = read_cache();
|
|
|
|
int i;
|
|
|
|
|
2005-04-17 12:29:45 +08:00
|
|
|
while (1 < argc && argv[1][0] == '-') {
|
2005-04-27 08:17:36 +08:00
|
|
|
if (!strcmp(argv[1], "-s"))
|
2005-04-16 06:08:09 +08:00
|
|
|
silent_on_nonexisting_files = silent = 1;
|
2005-04-17 12:29:45 +08:00
|
|
|
else if (!strcmp(argv[1], "-q"))
|
2005-04-16 06:08:09 +08:00
|
|
|
silent_on_nonexisting_files = 1;
|
2005-04-17 12:29:45 +08:00
|
|
|
else if (!strcmp(argv[1], "-z"))
|
2005-04-27 08:17:36 +08:00
|
|
|
line_termination = 0;
|
|
|
|
else if (!strcmp(argv[1], "-r"))
|
|
|
|
recursive = 1; /* No-op */
|
2005-04-17 12:29:45 +08:00
|
|
|
else
|
|
|
|
usage(show_diff_usage);
|
|
|
|
argv++; argc--;
|
2005-04-13 16:40:09 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 12:29:45 +08: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 06:13:13 +08:00
|
|
|
if (entries < 0) {
|
|
|
|
perror("read_cache");
|
|
|
|
exit(1);
|
|
|
|
}
|
2005-04-27 00:25:05 +08:00
|
|
|
|
2005-04-08 06:13:13 +08:00
|
|
|
for (i = 0; i < entries; i++) {
|
|
|
|
struct stat st;
|
2005-04-27 08:17:36 +08:00
|
|
|
unsigned int oldmode, mode;
|
2005-04-08 06:13:13 +08:00
|
|
|
struct cache_entry *ce = active_cache[i];
|
2005-04-17 12:29:45 +08:00
|
|
|
int changed;
|
2005-04-08 06:13:13 +08:00
|
|
|
|
2005-04-17 12:29:45 +08:00
|
|
|
if (1 < argc &&
|
2005-04-17 12:29:45 +08:00
|
|
|
! matches_pathspec(ce, argv+1, argc-1))
|
|
|
|
continue;
|
|
|
|
|
2005-04-17 12:29:45 +08:00
|
|
|
if (ce_stage(ce)) {
|
2005-04-27 08:37:11 +08:00
|
|
|
printf("U %s%c", ce->name, line_termination);
|
2005-04-27 08:17:36 +08:00
|
|
|
|
2005-04-17 12:29:45 +08:00
|
|
|
while (i < entries &&
|
|
|
|
!strcmp(ce->name, active_cache[i]->name))
|
|
|
|
i++;
|
|
|
|
i--; /* compensate for loop control increments */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-04-08 06:13:13 +08:00
|
|
|
if (stat(ce->name, &st) < 0) {
|
2005-04-27 08:17:36 +08:00
|
|
|
if (errno != ENOENT) {
|
|
|
|
perror(ce->name);
|
2005-04-16 06:08:09 +08:00
|
|
|
continue;
|
2005-04-27 08:17:36 +08:00
|
|
|
}
|
|
|
|
if (silent_on_nonexisting_files)
|
|
|
|
continue;
|
|
|
|
show_file("-", ce);
|
2005-04-08 06:13:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
2005-04-10 00:48:20 +08:00
|
|
|
changed = cache_match_stat(ce, &st);
|
2005-04-12 17:01:12 +08:00
|
|
|
if (!changed)
|
2005-04-08 06:13:13 +08:00
|
|
|
continue;
|
2005-04-13 16:40:09 +08:00
|
|
|
|
2005-04-27 08:17:36 +08:00
|
|
|
oldmode = ntohl(ce->ce_mode);
|
|
|
|
mode = S_IFREG | ce_permissions(st.st_mode);
|
|
|
|
|
|
|
|
printf("*%o->%o\t%s\t%s->%s\t%s%c",
|
|
|
|
oldmode, mode, "blob",
|
|
|
|
sha1_to_hex(ce->sha1), null_sha1_hex,
|
|
|
|
ce->name, line_termination);
|
2005-04-08 06:13:13 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|