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.
297 lines
6.8 KiB
297 lines
6.8 KiB
20 years ago
|
#include "cache.h"
|
||
20 years ago
|
#include "diff.h"
|
||
20 years ago
|
|
||
|
static int cached_only = 0;
|
||
20 years ago
|
static int diff_output_format = DIFF_FORMAT_RAW;
|
||
|
static int diff_line_termination = '\n';
|
||
20 years ago
|
static int match_nonexisting = 0;
|
||
20 years ago
|
static int detect_rename = 0;
|
||
20 years ago
|
static int find_copies_harder = 0;
|
||
20 years ago
|
static int diff_setup_opt = 0;
|
||
20 years ago
|
static int diff_score_opt = 0;
|
||
20 years ago
|
static const char *pickaxe = NULL;
|
||
20 years ago
|
static int pickaxe_opts = 0;
|
||
20 years ago
|
static int diff_break_opt = -1;
|
||
20 years ago
|
static const char *orderfile = NULL;
|
||
20 years ago
|
static const char *diff_filter = NULL;
|
||
20 years ago
|
|
||
|
/* A file entry went away or appeared */
|
||
20 years ago
|
static void show_file(const char *prefix, struct cache_entry *ce, unsigned char *sha1, unsigned int mode)
|
||
20 years ago
|
{
|
||
20 years ago
|
diff_addremove(prefix[0], ntohl(mode), sha1, ce->name, NULL);
|
||
20 years ago
|
}
|
||
|
|
||
20 years ago
|
static int get_stat_data(struct cache_entry *ce, unsigned char **sha1p, unsigned int *modep)
|
||
20 years ago
|
{
|
||
20 years ago
|
unsigned char *sha1 = ce->sha1;
|
||
|
unsigned int mode = ce->ce_mode;
|
||
20 years ago
|
|
||
|
if (!cached_only) {
|
||
|
static unsigned char no_sha1[20];
|
||
20 years ago
|
int changed;
|
||
20 years ago
|
struct stat st;
|
||
20 years ago
|
if (lstat(ce->name, &st) < 0) {
|
||
|
if (errno == ENOENT && match_nonexisting) {
|
||
|
*sha1p = sha1;
|
||
|
*modep = mode;
|
||
|
return 0;
|
||
|
}
|
||
20 years ago
|
return -1;
|
||
20 years ago
|
}
|
||
20 years ago
|
changed = ce_match_stat(ce, &st);
|
||
20 years ago
|
if (changed) {
|
||
20 years ago
|
mode = create_ce_mode(st.st_mode);
|
||
20 years ago
|
sha1 = no_sha1;
|
||
20 years ago
|
}
|
||
|
}
|
||
|
|
||
20 years ago
|
*sha1p = sha1;
|
||
|
*modep = mode;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
20 years ago
|
static void show_new_file(struct cache_entry *new)
|
||
20 years ago
|
{
|
||
|
unsigned char *sha1;
|
||
|
unsigned int mode;
|
||
|
|
||
|
/* New file in the index: it might actually be different in the working copy */
|
||
|
if (get_stat_data(new, &sha1, &mode) < 0)
|
||
20 years ago
|
return;
|
||
20 years ago
|
|
||
|
show_file("+", new, sha1, mode);
|
||
|
}
|
||
|
|
||
20 years ago
|
static int show_modified(struct cache_entry *old,
|
||
|
struct cache_entry *new,
|
||
|
int report_missing)
|
||
20 years ago
|
{
|
||
|
unsigned int mode, oldmode;
|
||
|
unsigned char *sha1;
|
||
|
|
||
|
if (get_stat_data(new, &sha1, &mode) < 0) {
|
||
20 years ago
|
if (report_missing)
|
||
|
show_file("-", old, old->sha1, old->ce_mode);
|
||
20 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
|
oldmode = old->ce_mode;
|
||
20 years ago
|
if (mode == oldmode && !memcmp(sha1, old->sha1, 20) &&
|
||
20 years ago
|
!find_copies_harder)
|
||
20 years ago
|
return 0;
|
||
|
|
||
20 years ago
|
mode = ntohl(mode);
|
||
|
oldmode = ntohl(oldmode);
|
||
|
|
||
20 years ago
|
diff_change(oldmode, mode,
|
||
|
old->sha1, sha1, old->name, NULL);
|
||
20 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
20 years ago
|
static int diff_cache(struct cache_entry **ac, int entries, const char **pathspec)
|
||
20 years ago
|
{
|
||
20 years ago
|
while (entries) {
|
||
|
struct cache_entry *ce = *ac;
|
||
20 years ago
|
int same = (entries > 1) && ce_same_name(ce, ac[1]);
|
||
20 years ago
|
|
||
20 years ago
|
if (!ce_path_match(ce, pathspec))
|
||
|
goto skip_entry;
|
||
|
|
||
20 years ago
|
switch (ce_stage(ce)) {
|
||
|
case 0:
|
||
|
/* No stage 1 entry? That means it's a new file */
|
||
|
if (!same) {
|
||
20 years ago
|
show_new_file(ce);
|
||
20 years ago
|
break;
|
||
|
}
|
||
|
/* Show difference between old and new */
|
||
20 years ago
|
show_modified(ac[1], ce, 1);
|
||
20 years ago
|
break;
|
||
|
case 1:
|
||
|
/* No stage 3 (merge) entry? That means it's been deleted */
|
||
|
if (!same) {
|
||
20 years ago
|
show_file("-", ce, ce->sha1, ce->ce_mode);
|
||
20 years ago
|
break;
|
||
|
}
|
||
20 years ago
|
/* We come here with ce pointing at stage 1
|
||
|
* (original tree) and ac[1] pointing at stage
|
||
|
* 3 (unmerged). show-modified with
|
||
|
* report-mising set to false does not say the
|
||
|
* file is deleted but reports true if work
|
||
|
* tree does not have it, in which case we
|
||
|
* fall through to report the unmerged state.
|
||
|
* Otherwise, we show the differences between
|
||
|
* the original tree and the work tree.
|
||
|
*/
|
||
|
if (!cached_only && !show_modified(ce, ac[1], 0))
|
||
|
break;
|
||
|
/* fallthru */
|
||
20 years ago
|
case 3:
|
||
20 years ago
|
diff_unmerge(ce->name);
|
||
20 years ago
|
break;
|
||
|
|
||
|
default:
|
||
|
die("impossible cache entry stage");
|
||
20 years ago
|
}
|
||
20 years ago
|
|
||
20 years ago
|
skip_entry:
|
||
20 years ago
|
/*
|
||
|
* Ignore all the different stages for this file,
|
||
|
* we've handled the relevant cases now.
|
||
|
*/
|
||
|
do {
|
||
20 years ago
|
ac++;
|
||
|
entries--;
|
||
20 years ago
|
} while (entries && ce_same_name(ce, ac[0]));
|
||
20 years ago
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
20 years ago
|
/*
|
||
|
* This turns all merge entries into "stage 3". That guarantees that
|
||
|
* when we read in the new tree (into "stage 1"), we won't lose sight
|
||
|
* of the fact that we had unmerged entries.
|
||
|
*/
|
||
|
static void mark_merge_entries(void)
|
||
20 years ago
|
{
|
||
|
int i;
|
||
|
for (i = 0; i < active_nr; i++) {
|
||
|
struct cache_entry *ce = active_cache[i];
|
||
|
if (!ce_stage(ce))
|
||
20 years ago
|
continue;
|
||
20 years ago
|
ce->ce_flags |= htons(CE_STAGEMASK);
|
||
20 years ago
|
}
|
||
|
}
|
||
|
|
||
20 years ago
|
static const char diff_cache_usage[] =
|
||
19 years ago
|
"git-diff-index [-m] [--cached] "
|
||
20 years ago
|
"[<common diff options>] <tree-ish> [<path>...]"
|
||
|
COMMON_DIFF_OPTIONS_HELP;
|
||
20 years ago
|
|
||
20 years ago
|
int main(int argc, char **argv)
|
||
20 years ago
|
{
|
||
20 years ago
|
const char *tree_name = NULL;
|
||
|
unsigned char sha1[20];
|
||
20 years ago
|
const char *prefix = setup_git_directory();
|
||
20 years ago
|
const char **pathspec = NULL;
|
||
20 years ago
|
void *tree;
|
||
|
unsigned long size;
|
||
20 years ago
|
int ret;
|
||
20 years ago
|
int allow_options = 1;
|
||
20 years ago
|
int i;
|
||
20 years ago
|
|
||
20 years ago
|
for (i = 1; i < argc; i++) {
|
||
|
const char *arg = argv[i];
|
||
|
|
||
20 years ago
|
if (!allow_options || *arg != '-') {
|
||
20 years ago
|
if (tree_name)
|
||
20 years ago
|
break;
|
||
|
tree_name = arg;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
20 years ago
|
if (!strcmp(arg, "--")) {
|
||
|
allow_options = 0;
|
||
|
continue;
|
||
|
}
|
||
20 years ago
|
if (!strcmp(arg, "-r")) {
|
||
20 years ago
|
/* We accept the -r flag just to look like git-diff-tree */
|
||
20 years ago
|
continue;
|
||
|
}
|
||
20 years ago
|
/* We accept the -u flag as a synonym for "-p" */
|
||
|
if (!strcmp(arg, "-p") || !strcmp(arg, "-u")) {
|
||
20 years ago
|
diff_output_format = DIFF_FORMAT_PATCH;
|
||
20 years ago
|
continue;
|
||
|
}
|
||
20 years ago
|
if (!strncmp(arg, "-B", 2)) {
|
||
20 years ago
|
if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
|
||
|
usage(diff_cache_usage);
|
||
20 years ago
|
continue;
|
||
|
}
|
||
20 years ago
|
if (!strncmp(arg, "-M", 2)) {
|
||
20 years ago
|
detect_rename = DIFF_DETECT_RENAME;
|
||
20 years ago
|
if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
|
||
|
usage(diff_cache_usage);
|
||
20 years ago
|
continue;
|
||
|
}
|
||
20 years ago
|
if (!strncmp(arg, "-C", 2)) {
|
||
20 years ago
|
detect_rename = DIFF_DETECT_COPY;
|
||
20 years ago
|
if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
|
||
|
usage(diff_cache_usage);
|
||
20 years ago
|
continue;
|
||
|
}
|
||
20 years ago
|
if (!strcmp(arg, "--find-copies-harder")) {
|
||
|
find_copies_harder = 1;
|
||
|
continue;
|
||
|
}
|
||
20 years ago
|
if (!strcmp(arg, "-z")) {
|
||
20 years ago
|
diff_line_termination = 0;
|
||
20 years ago
|
continue;
|
||
|
}
|
||
20 years ago
|
if (!strcmp(arg, "--name-only")) {
|
||
|
diff_output_format = DIFF_FORMAT_NAME;
|
||
|
continue;
|
||
|
}
|
||
20 years ago
|
if (!strcmp(arg, "-R")) {
|
||
20 years ago
|
diff_setup_opt |= DIFF_SETUP_REVERSE;
|
||
20 years ago
|
continue;
|
||
|
}
|
||
20 years ago
|
if (!strncmp(arg, "-S", 2)) {
|
||
20 years ago
|
pickaxe = arg + 2;
|
||
|
continue;
|
||
|
}
|
||
20 years ago
|
if (!strncmp(arg, "--diff-filter=", 14)) {
|
||
|
diff_filter = arg + 14;
|
||
|
continue;
|
||
|
}
|
||
20 years ago
|
if (!strncmp(arg, "-O", 2)) {
|
||
|
orderfile = arg + 2;
|
||
|
continue;
|
||
|
}
|
||
20 years ago
|
if (!strcmp(arg, "--pickaxe-all")) {
|
||
|
pickaxe_opts = DIFF_PICKAXE_ALL;
|
||
|
continue;
|
||
|
}
|
||
20 years ago
|
if (!strcmp(arg, "-m")) {
|
||
|
match_nonexisting = 1;
|
||
|
continue;
|
||
|
}
|
||
20 years ago
|
if (!strcmp(arg, "--cached")) {
|
||
|
cached_only = 1;
|
||
|
continue;
|
||
|
}
|
||
20 years ago
|
usage(diff_cache_usage);
|
||
20 years ago
|
}
|
||
|
|
||
20 years ago
|
pathspec = get_pathspec(prefix, argv + i);
|
||
|
|
||
20 years ago
|
if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
|
||
|
usage(diff_cache_usage);
|
||
|
|
||
20 years ago
|
if (!tree_name || get_sha1(tree_name, sha1))
|
||
20 years ago
|
usage(diff_cache_usage);
|
||
20 years ago
|
|
||
20 years ago
|
read_cache();
|
||
|
|
||
20 years ago
|
/* The rest is for paths restriction. */
|
||
20 years ago
|
diff_setup(diff_setup_opt);
|
||
20 years ago
|
|
||
20 years ago
|
mark_merge_entries();
|
||
20 years ago
|
|
||
20 years ago
|
tree = read_object_with_reference(sha1, "tree", &size, NULL);
|
||
20 years ago
|
if (!tree)
|
||
20 years ago
|
die("bad tree object %s", tree_name);
|
||
20 years ago
|
if (read_tree(tree, size, 1, pathspec))
|
||
20 years ago
|
die("unable to read tree object %s", tree_name);
|
||
20 years ago
|
|
||
20 years ago
|
ret = diff_cache(active_cache, active_nr, pathspec);
|
||
20 years ago
|
|
||
20 years ago
|
diffcore_std(pathspec,
|
||
20 years ago
|
detect_rename, diff_score_opt,
|
||
20 years ago
|
pickaxe, pickaxe_opts,
|
||
20 years ago
|
diff_break_opt,
|
||
20 years ago
|
orderfile, diff_filter);
|
||
20 years ago
|
diff_flush(diff_output_format, diff_line_termination);
|
||
20 years ago
|
return ret;
|
||
20 years ago
|
}
|