You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
426 lines
11 KiB
426 lines
11 KiB
19 years ago
|
/*
|
||
|
* Builtin "git diff"
|
||
|
*
|
||
|
* Copyright (c) 2006 Junio C Hamano
|
||
|
*/
|
||
|
#include "cache.h"
|
||
17 years ago
|
#include "color.h"
|
||
19 years ago
|
#include "commit.h"
|
||
|
#include "blob.h"
|
||
|
#include "tag.h"
|
||
|
#include "diff.h"
|
||
|
#include "diffcore.h"
|
||
|
#include "revision.h"
|
||
|
#include "log-tree.h"
|
||
|
#include "builtin.h"
|
||
|
|
||
|
struct blobinfo {
|
||
|
unsigned char sha1[20];
|
||
|
const char *name;
|
||
18 years ago
|
unsigned mode;
|
||
19 years ago
|
};
|
||
|
|
||
|
static const char builtin_diff_usage[] =
|
||
17 years ago
|
"git diff <options> <rev>{0,2} -- <path>*";
|
||
19 years ago
|
|
||
|
static void stuff_change(struct diff_options *opt,
|
||
|
unsigned old_mode, unsigned new_mode,
|
||
|
const unsigned char *old_sha1,
|
||
|
const unsigned char *new_sha1,
|
||
|
const char *old_name,
|
||
|
const char *new_name)
|
||
|
{
|
||
|
struct diff_filespec *one, *two;
|
||
|
|
||
19 years ago
|
if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
|
||
18 years ago
|
!hashcmp(old_sha1, new_sha1) && (old_mode == new_mode))
|
||
19 years ago
|
return;
|
||
|
|
||
17 years ago
|
if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
|
||
19 years ago
|
unsigned tmp;
|
||
19 years ago
|
const unsigned char *tmp_u;
|
||
19 years ago
|
const char *tmp_c;
|
||
|
tmp = old_mode; old_mode = new_mode; new_mode = tmp;
|
||
|
tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
|
||
|
tmp_c = old_name; old_name = new_name; new_name = tmp_c;
|
||
|
}
|
||
17 years ago
|
|
||
|
if (opt->prefix &&
|
||
|
(strncmp(old_name, opt->prefix, opt->prefix_length) ||
|
||
|
strncmp(new_name, opt->prefix, opt->prefix_length)))
|
||
|
return;
|
||
|
|
||
19 years ago
|
one = alloc_filespec(old_name);
|
||
|
two = alloc_filespec(new_name);
|
||
|
fill_filespec(one, old_sha1, old_mode);
|
||
|
fill_filespec(two, new_sha1, new_mode);
|
||
|
|
||
|
diff_queue(&diff_queued_diff, one, two);
|
||
|
}
|
||
|
|
||
|
static int builtin_diff_b_f(struct rev_info *revs,
|
||
|
int argc, const char **argv,
|
||
|
struct blobinfo *blob,
|
||
|
const char *path)
|
||
|
{
|
||
|
/* Blob vs file in the working tree*/
|
||
|
struct stat st;
|
||
|
|
||
19 years ago
|
if (argc > 1)
|
||
|
usage(builtin_diff_usage);
|
||
|
|
||
19 years ago
|
if (lstat(path, &st))
|
||
16 years ago
|
die_errno("failed to stat '%s'", path);
|
||
19 years ago
|
if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
|
||
|
die("'%s': not a regular file or symlink", path);
|
||
18 years ago
|
|
||
17 years ago
|
diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
|
||
|
|
||
18 years ago
|
if (blob[0].mode == S_IFINVALID)
|
||
|
blob[0].mode = canon_mode(st.st_mode);
|
||
|
|
||
19 years ago
|
stuff_change(&revs->diffopt,
|
||
18 years ago
|
blob[0].mode, canon_mode(st.st_mode),
|
||
19 years ago
|
blob[0].sha1, null_sha1,
|
||
19 years ago
|
path, path);
|
||
19 years ago
|
diffcore_std(&revs->diffopt);
|
||
|
diff_flush(&revs->diffopt);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static int builtin_diff_blobs(struct rev_info *revs,
|
||
|
int argc, const char **argv,
|
||
|
struct blobinfo *blob)
|
||
|
{
|
||
|
unsigned mode = canon_mode(S_IFREG | 0644);
|
||
|
|
||
19 years ago
|
if (argc > 1)
|
||
|
usage(builtin_diff_usage);
|
||
|
|
||
18 years ago
|
if (blob[0].mode == S_IFINVALID)
|
||
|
blob[0].mode = mode;
|
||
|
|
||
|
if (blob[1].mode == S_IFINVALID)
|
||
|
blob[1].mode = mode;
|
||
|
|
||
19 years ago
|
stuff_change(&revs->diffopt,
|
||
18 years ago
|
blob[0].mode, blob[1].mode,
|
||
19 years ago
|
blob[0].sha1, blob[1].sha1,
|
||
19 years ago
|
blob[0].name, blob[1].name);
|
||
19 years ago
|
diffcore_std(&revs->diffopt);
|
||
|
diff_flush(&revs->diffopt);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static int builtin_diff_index(struct rev_info *revs,
|
||
|
int argc, const char **argv)
|
||
|
{
|
||
|
int cached = 0;
|
||
|
while (1 < argc) {
|
||
|
const char *arg = argv[1];
|
||
16 years ago
|
if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
|
||
19 years ago
|
cached = 1;
|
||
|
else
|
||
|
usage(builtin_diff_usage);
|
||
|
argv++; argc--;
|
||
|
}
|
||
17 years ago
|
if (!cached)
|
||
|
setup_work_tree();
|
||
19 years ago
|
/*
|
||
|
* Make sure there is one revision (i.e. pending object),
|
||
|
* and there is no revision filtering parameters.
|
||
|
*/
|
||
19 years ago
|
if (revs->pending.nr != 1 ||
|
||
19 years ago
|
revs->max_count != -1 || revs->min_age != -1 ||
|
||
|
revs->max_age != -1)
|
||
|
usage(builtin_diff_usage);
|
||
16 years ago
|
if (read_cache_preload(revs->diffopt.paths) < 0) {
|
||
|
perror("read_cache_preload");
|
||
18 years ago
|
return -1;
|
||
|
}
|
||
19 years ago
|
return run_diff_index(revs, cached);
|
||
|
}
|
||
|
|
||
|
static int builtin_diff_tree(struct rev_info *revs,
|
||
|
int argc, const char **argv,
|
||
19 years ago
|
struct object_array_entry *ent)
|
||
19 years ago
|
{
|
||
|
const unsigned char *(sha1[2]);
|
||
19 years ago
|
int swap = 0;
|
||
19 years ago
|
|
||
|
if (argc > 1)
|
||
|
usage(builtin_diff_usage);
|
||
19 years ago
|
|
||
|
/* We saw two trees, ent[0] and ent[1].
|
||
19 years ago
|
* if ent[1] is uninteresting, they are swapped
|
||
19 years ago
|
*/
|
||
19 years ago
|
if (ent[1].item->flags & UNINTERESTING)
|
||
|
swap = 1;
|
||
19 years ago
|
sha1[swap] = ent[0].item->sha1;
|
||
|
sha1[1-swap] = ent[1].item->sha1;
|
||
|
diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
|
||
|
log_tree_diff_flush(revs);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
19 years ago
|
static int builtin_diff_combined(struct rev_info *revs,
|
||
|
int argc, const char **argv,
|
||
19 years ago
|
struct object_array_entry *ent,
|
||
19 years ago
|
int ents)
|
||
|
{
|
||
|
const unsigned char (*parent)[20];
|
||
|
int i;
|
||
|
|
||
19 years ago
|
if (argc > 1)
|
||
|
usage(builtin_diff_usage);
|
||
|
|
||
19 years ago
|
if (!revs->dense_combined_merges && !revs->combine_merges)
|
||
|
revs->dense_combined_merges = revs->combine_merges = 1;
|
||
|
parent = xmalloc(ents * sizeof(*parent));
|
||
|
for (i = 0; i < ents; i++)
|
||
16 years ago
|
hashcpy((unsigned char *)(parent + i), ent[i].item->sha1);
|
||
19 years ago
|
diff_tree_combined(parent[0], parent + 1, ents - 1,
|
||
|
revs->dense_combined_merges, revs);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
18 years ago
|
static void refresh_index_quietly(void)
|
||
|
{
|
||
|
struct lock_file *lock_file;
|
||
|
int fd;
|
||
|
|
||
|
lock_file = xcalloc(1, sizeof(struct lock_file));
|
||
|
fd = hold_locked_index(lock_file, 0);
|
||
|
if (fd < 0)
|
||
|
return;
|
||
|
discard_cache();
|
||
|
read_cache();
|
||
|
refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
|
||
17 years ago
|
|
||
|
if (active_cache_changed &&
|
||
17 years ago
|
!write_cache(fd, active_cache, active_nr))
|
||
17 years ago
|
commit_locked_index(lock_file);
|
||
|
|
||
18 years ago
|
rollback_lock_file(lock_file);
|
||
|
}
|
||
|
|
||
17 years ago
|
static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
|
||
|
{
|
||
|
int result;
|
||
|
unsigned int options = 0;
|
||
|
|
||
|
while (1 < argc && argv[1][0] == '-') {
|
||
|
if (!strcmp(argv[1], "--base"))
|
||
|
revs->max_count = 1;
|
||
|
else if (!strcmp(argv[1], "--ours"))
|
||
|
revs->max_count = 2;
|
||
|
else if (!strcmp(argv[1], "--theirs"))
|
||
|
revs->max_count = 3;
|
||
|
else if (!strcmp(argv[1], "-q"))
|
||
|
options |= DIFF_SILENT_ON_REMOVED;
|
||
16 years ago
|
else if (!strcmp(argv[1], "-h"))
|
||
|
usage(builtin_diff_usage);
|
||
17 years ago
|
else
|
||
|
return error("invalid option: %s", argv[1]);
|
||
|
argv++; argc--;
|
||
|
}
|
||
|
|
||
17 years ago
|
/*
|
||
|
* "diff --base" should not combine merges because it was not
|
||
|
* asked to. "diff -c" should not densify (if the user wants
|
||
|
* dense one, --cc can be explicitly asked for, or just rely
|
||
|
* on the default).
|
||
|
*/
|
||
|
if (revs->max_count == -1 && !revs->combine_merges &&
|
||
17 years ago
|
(revs->diffopt.output_format & DIFF_FORMAT_PATCH))
|
||
|
revs->combine_merges = revs->dense_combined_merges = 1;
|
||
|
|
||
17 years ago
|
setup_work_tree();
|
||
16 years ago
|
if (read_cache_preload(revs->diffopt.paths) < 0) {
|
||
|
perror("read_cache_preload");
|
||
17 years ago
|
return -1;
|
||
|
}
|
||
|
result = run_diff_files(revs, options);
|
||
|
return diff_result_code(&revs->diffopt, result);
|
||
|
}
|
||
|
|
||
19 years ago
|
int cmd_diff(int argc, const char **argv, const char *prefix)
|
||
19 years ago
|
{
|
||
19 years ago
|
int i;
|
||
19 years ago
|
struct rev_info rev;
|
||
19 years ago
|
struct object_array_entry ent[100];
|
||
19 years ago
|
int ents = 0, blobs = 0, paths = 0;
|
||
19 years ago
|
const char *path = NULL;
|
||
19 years ago
|
struct blobinfo blob[2];
|
||
17 years ago
|
int nongit;
|
||
18 years ago
|
int result = 0;
|
||
19 years ago
|
|
||
|
/*
|
||
|
* We could get N tree-ish in the rev.pending_objects list.
|
||
|
* Also there could be M blobs there, and P pathspecs.
|
||
|
*
|
||
|
* N=0, M=0:
|
||
|
* cache vs files (diff-files)
|
||
|
* N=0, M=2:
|
||
|
* compare two random blobs. P must be zero.
|
||
|
* N=0, M=1, P=1:
|
||
|
* compare a blob with a working tree file.
|
||
|
*
|
||
|
* N=1, M=0:
|
||
|
* tree vs cache (diff-index --cached)
|
||
|
*
|
||
|
* N=2, M=0:
|
||
|
* tree vs tree (diff-tree)
|
||
|
*
|
||
17 years ago
|
* N=0, M=0, P=2:
|
||
|
* compare two filesystem entities (aka --no-index).
|
||
|
*
|
||
19 years ago
|
* Other cases are errors.
|
||
|
*/
|
||
19 years ago
|
|
||
18 years ago
|
prefix = setup_git_directory_gently(&nongit);
|
||
17 years ago
|
git_config(git_diff_ui_config, NULL);
|
||
17 years ago
|
|
||
|
if (diff_use_color_default == -1)
|
||
|
diff_use_color_default = git_use_color_default;
|
||
|
|
||
19 years ago
|
init_revisions(&rev, prefix);
|
||
17 years ago
|
|
||
|
/* If this is a no-index diff, just run it and exit there. */
|
||
|
diff_no_index(&rev, argc, argv, nongit, prefix);
|
||
|
|
||
|
/* Otherwise, we are doing the usual "git" diff */
|
||
18 years ago
|
rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
|
||
19 years ago
|
|
||
16 years ago
|
/* Default to let external and textconv be used */
|
||
16 years ago
|
DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
|
||
16 years ago
|
DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
|
||
16 years ago
|
|
||
17 years ago
|
if (nongit)
|
||
|
die("Not a git repository");
|
||
|
argc = setup_revisions(argc, argv, &rev, NULL);
|
||
19 years ago
|
if (!rev.diffopt.output_format) {
|
||
19 years ago
|
rev.diffopt.output_format = DIFF_FORMAT_PATCH;
|
||
19 years ago
|
if (diff_setup_done(&rev.diffopt) < 0)
|
||
|
die("diff_setup_done failed");
|
||
19 years ago
|
}
|
||
16 years ago
|
|
||
17 years ago
|
DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
|
||
19 years ago
|
|
||
17 years ago
|
/*
|
||
|
* If the user asked for our exit code then don't start a
|
||
18 years ago
|
* pager or we would end up reporting its exit code instead.
|
||
|
*/
|
||
17 years ago
|
if (!DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS) &&
|
||
|
check_pager_config("diff") != 0)
|
||
18 years ago
|
setup_pager();
|
||
|
|
||
17 years ago
|
/*
|
||
|
* Do we have --cached and not have a pending object, then
|
||
19 years ago
|
* default to HEAD by hand. Eek.
|
||
|
*/
|
||
19 years ago
|
if (!rev.pending.nr) {
|
||
19 years ago
|
int i;
|
||
|
for (i = 1; i < argc; i++) {
|
||
|
const char *arg = argv[i];
|
||
|
if (!strcmp(arg, "--"))
|
||
|
break;
|
||
16 years ago
|
else if (!strcmp(arg, "--cached") ||
|
||
|
!strcmp(arg, "--staged")) {
|
||
17 years ago
|
add_head_to_pending(&rev);
|
||
18 years ago
|
if (!rev.pending.nr)
|
||
|
die("No HEAD commit to compare with (yet)");
|
||
19 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
19 years ago
|
for (i = 0; i < rev.pending.nr; i++) {
|
||
|
struct object_array_entry *list = rev.pending.objects+i;
|
||
19 years ago
|
struct object *obj = list->item;
|
||
|
const char *name = list->name;
|
||
|
int flags = (obj->flags & UNINTERESTING);
|
||
|
if (!obj->parsed)
|
||
|
obj = parse_object(obj->sha1);
|
||
|
obj = deref_tag(obj, NULL, 0);
|
||
|
if (!obj)
|
||
|
die("invalid object '%s' given.", name);
|
||
19 years ago
|
if (obj->type == OBJ_COMMIT)
|
||
19 years ago
|
obj = &((struct commit *)obj)->tree->object;
|
||
19 years ago
|
if (obj->type == OBJ_TREE) {
|
||
19 years ago
|
if (ARRAY_SIZE(ent) <= ents)
|
||
|
die("more than %d trees given: '%s'",
|
||
19 years ago
|
(int) ARRAY_SIZE(ent), name);
|
||
19 years ago
|
obj->flags |= flags;
|
||
|
ent[ents].item = obj;
|
||
|
ent[ents].name = name;
|
||
|
ents++;
|
||
|
continue;
|
||
|
}
|
||
19 years ago
|
if (obj->type == OBJ_BLOB) {
|
||
19 years ago
|
if (2 <= blobs)
|
||
|
die("more than two blobs given: '%s'", name);
|
||
19 years ago
|
hashcpy(blob[blobs].sha1, obj->sha1);
|
||
19 years ago
|
blob[blobs].name = name;
|
||
18 years ago
|
blob[blobs].mode = list->mode;
|
||
19 years ago
|
blobs++;
|
||
|
continue;
|
||
19 years ago
|
|
||
19 years ago
|
}
|
||
|
die("unhandled object '%s' given.", name);
|
||
|
}
|
||
|
if (rev.prune_data) {
|
||
|
const char **pathspec = rev.prune_data;
|
||
|
while (*pathspec) {
|
||
|
if (!path)
|
||
|
path = *pathspec;
|
||
|
paths++;
|
||
|
pathspec++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Now, do the arguments look reasonable?
|
||
|
*/
|
||
|
if (!ents) {
|
||
|
switch (blobs) {
|
||
|
case 0:
|
||
17 years ago
|
result = builtin_diff_files(&rev, argc, argv);
|
||
19 years ago
|
break;
|
||
|
case 1:
|
||
|
if (paths != 1)
|
||
|
usage(builtin_diff_usage);
|
||
18 years ago
|
result = builtin_diff_b_f(&rev, argc, argv, blob, path);
|
||
19 years ago
|
break;
|
||
|
case 2:
|
||
19 years ago
|
if (paths)
|
||
|
usage(builtin_diff_usage);
|
||
18 years ago
|
result = builtin_diff_blobs(&rev, argc, argv, blob);
|
||
19 years ago
|
break;
|
||
|
default:
|
||
|
usage(builtin_diff_usage);
|
||
|
}
|
||
|
}
|
||
|
else if (blobs)
|
||
|
usage(builtin_diff_usage);
|
||
|
else if (ents == 1)
|
||
18 years ago
|
result = builtin_diff_index(&rev, argc, argv);
|
||
19 years ago
|
else if (ents == 2)
|
||
18 years ago
|
result = builtin_diff_tree(&rev, argc, argv, ent);
|
||
19 years ago
|
else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) {
|
||
|
/* diff A...B where there is one sane merge base between
|
||
|
* A and B. We have ent[0] == merge-base, ent[1] == A,
|
||
|
* and ent[2] == B. Show diff between the base and B.
|
||
|
*/
|
||
19 years ago
|
ent[1] = ent[2];
|
||
18 years ago
|
result = builtin_diff_tree(&rev, argc, argv, ent);
|
||
19 years ago
|
}
|
||
19 years ago
|
else
|
||
18 years ago
|
result = builtin_diff_combined(&rev, argc, argv,
|
||
19 years ago
|
ent, ents);
|
||
17 years ago
|
result = diff_result_code(&rev.diffopt, result);
|
||
18 years ago
|
if (1 < rev.diffopt.skip_stat_unmatch)
|
||
|
refresh_index_quietly();
|
||
18 years ago
|
return result;
|
||
19 years ago
|
}
|