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-19 00:07:24 +02:00
|
|
|
static char *diff_cmd = "diff -L 'a/%s' -L 'b/%s' ";
|
|
|
|
static char *diff_opts = "-p -u";
|
2005-04-19 00:07:24 +02:00
|
|
|
static char *diff_arg_forward = " - '%s'";
|
|
|
|
static char *diff_arg_reverse = " '%s' -";
|
2005-04-19 00:07:24 +02:00
|
|
|
|
|
|
|
static void prepare_diff_cmd(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Default values above are meant to match the
|
|
|
|
* Linux kernel development style. Examples of
|
|
|
|
* alternative styles you can specify via environment
|
|
|
|
* variables are:
|
|
|
|
*
|
|
|
|
* GIT_DIFF_CMD="diff -L '%s' -L '%s'"
|
|
|
|
* GIT_DIFF_OPTS="-c";
|
|
|
|
*/
|
|
|
|
diff_cmd = getenv("GIT_DIFF_CMD") ? : diff_cmd;
|
|
|
|
diff_opts = getenv("GIT_DIFF_OPTS") ? : diff_opts;
|
|
|
|
}
|
2005-04-17 06:29:45 +02:00
|
|
|
|
|
|
|
/* Help to copy the thing properly quoted for the shell safety.
|
|
|
|
* any single quote is replaced with '\'', and the caller is
|
|
|
|
* expected to enclose the result within a single quote pair.
|
|
|
|
*
|
|
|
|
* E.g.
|
|
|
|
* original sq_expand result
|
|
|
|
* name ==> name ==> 'name'
|
|
|
|
* a b ==> a b ==> 'a b'
|
|
|
|
* a'b ==> a'\''b ==> 'a'\''b'
|
|
|
|
*/
|
|
|
|
static char *sq_expand(char *src)
|
|
|
|
{
|
|
|
|
static char *buf = NULL;
|
|
|
|
int cnt, c;
|
|
|
|
char *cp;
|
|
|
|
|
2005-04-18 05:31:40 +02:00
|
|
|
/* count bytes needed to store the quoted string. */
|
|
|
|
for (cnt = 1, cp = src; *cp; cnt++, cp++)
|
2005-04-17 06:29:45 +02:00
|
|
|
if (*cp == '\'')
|
|
|
|
cnt += 3;
|
|
|
|
|
2005-04-19 00:07:24 +02:00
|
|
|
if (! (buf = malloc(cnt)))
|
|
|
|
return buf;
|
2005-04-17 06:29:45 +02:00
|
|
|
cp = buf;
|
|
|
|
while ((c = *src++)) {
|
|
|
|
if (c != '\'')
|
|
|
|
*cp++ = c;
|
|
|
|
else {
|
|
|
|
cp = strcpy(cp, "'\\''");
|
|
|
|
cp += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*cp = 0;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2005-04-19 00:07:24 +02:00
|
|
|
static void show_differences(char *name, char *label, void *old_contents,
|
2005-04-19 00:07:24 +02:00
|
|
|
unsigned long long old_size, int reverse)
|
2005-04-08 00:13:13 +02:00
|
|
|
{
|
|
|
|
FILE *f;
|
2005-04-17 06:29:45 +02:00
|
|
|
char *name_sq = sq_expand(name);
|
2005-04-19 00:07:24 +02:00
|
|
|
char *label_sq = (name != label) ? sq_expand(label) : name_sq;
|
2005-04-19 00:07:24 +02:00
|
|
|
char *diff_arg = reverse ? diff_arg_reverse : diff_arg_forward;
|
2005-04-19 00:07:24 +02:00
|
|
|
int cmd_size = strlen(name_sq) + strlen(label_sq) * 2 +
|
|
|
|
strlen(diff_cmd) + strlen(diff_opts) + strlen(diff_arg);
|
2005-04-19 00:07:24 +02:00
|
|
|
char *cmd = malloc(cmd_size);
|
2005-04-19 00:07:24 +02:00
|
|
|
int next_at;
|
2005-04-08 00:13:13 +02:00
|
|
|
|
2005-04-19 00:07:24 +02:00
|
|
|
fflush(stdout);
|
2005-04-19 00:07:24 +02:00
|
|
|
next_at = snprintf(cmd, cmd_size, diff_cmd, label_sq, label_sq);
|
|
|
|
next_at += snprintf(cmd+next_at, cmd_size-next_at, "%s", diff_opts);
|
|
|
|
next_at += snprintf(cmd+next_at, cmd_size-next_at, diff_arg, name_sq);
|
2005-04-08 00:13:13 +02:00
|
|
|
f = popen(cmd, "w");
|
2005-04-12 11:04:44 +02:00
|
|
|
if (old_size)
|
|
|
|
fwrite(old_contents, old_size, 1, f);
|
2005-04-08 00:13:13 +02:00
|
|
|
pclose(f);
|
2005-04-19 00:07:24 +02:00
|
|
|
if (label_sq != name_sq)
|
|
|
|
free(label_sq);
|
2005-04-19 00:07:24 +02:00
|
|
|
free(name_sq);
|
|
|
|
free(cmd);
|
2005-04-08 00:13:13 +02:00
|
|
|
}
|
|
|
|
|
2005-04-19 00:07:24 +02:00
|
|
|
static void show_diff_empty(struct cache_entry *ce, int reverse)
|
2005-04-12 11:04:44 +02:00
|
|
|
{
|
|
|
|
char *old;
|
|
|
|
unsigned long int size;
|
2005-04-19 00:07:24 +02:00
|
|
|
unsigned char type[20];
|
2005-04-12 11:04:44 +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));
|
|
|
|
return;
|
|
|
|
}
|
2005-04-19 00:07:24 +02:00
|
|
|
show_differences("/dev/null", ce->name, old, size, reverse);
|
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-19 00:07:24 +02:00
|
|
|
show_diff_empty(ce, 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;
|
|
|
|
}
|