Skip to content

Commit

Permalink
[PATCH] Make -C less eager.
Browse files Browse the repository at this point in the history
Like diff-tree, this patch makes -C option for diff-* brothers
to use only pre-image of modified files as rename/copy detection
by default.  Give --find-copies-harder to use unmodified files
to find copies from as well.

This also fixes "diff-files -C" problem earlier noticed by
Linus.  It was feeding the null sha1 even when the file in the
work tree was known to match what is in the index file.  This
resulted in diff-files showing everything in the project.

Signed-off-by: Junio C Hamano <junkio@cox.net>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
  • Loading branch information
Junio C Hamano authored and Linus Torvalds committed Jun 20, 2005
1 parent 170241b commit 4727f64
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 18 deletions.
12 changes: 10 additions & 2 deletions diff-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ static int cached_only = 0;
static int diff_output_format = DIFF_FORMAT_HUMAN;
static int match_nonexisting = 0;
static int detect_rename = 0;
static int find_copies_harder = 0;
static int diff_setup_opt = 0;
static int diff_score_opt = 0;
static const char *pickaxe = NULL;
Expand Down Expand Up @@ -75,7 +76,7 @@ static int show_modified(struct cache_entry *old,

oldmode = old->ce_mode;
if (mode == oldmode && !memcmp(sha1, old->sha1, 20) &&
detect_rename < DIFF_DETECT_COPY)
!find_copies_harder)
return 0;

mode = ntohl(mode);
Expand Down Expand Up @@ -158,7 +159,7 @@ static void mark_merge_entries(void)
}

static char *diff_cache_usage =
"git-diff-cache [-p] [-r] [-z] [-m] [-M] [-C] [-R] [-S<string>] [-O<orderfile>] [--cached] <tree-ish> [<path>...]";
"git-diff-cache [-p] [-r] [-z] [-m] [--cached] [-R] [-B] [-M] [-C] [--find-copies-harder] [-O<orderfile>] [-S<string>] [--pickaxe-all] <tree-ish> [<path>...]";

int main(int argc, const char **argv)
{
Expand Down Expand Up @@ -213,6 +214,10 @@ int main(int argc, const char **argv)
usage(diff_cache_usage);
continue;
}
if (!strcmp(arg, "--find-copies-harder")) {
find_copies_harder = 1;
continue;
}
if (!strcmp(arg, "-z")) {
diff_output_format = DIFF_FORMAT_MACHINE;
continue;
Expand Down Expand Up @@ -248,6 +253,9 @@ int main(int argc, const char **argv)
usage(diff_cache_usage);
}

if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
usage(diff_cache_usage);

if (!tree_name || get_sha1(tree_name, sha1))
usage(diff_cache_usage);

Expand Down
13 changes: 9 additions & 4 deletions diff-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
#include "diff.h"

static const char *diff_files_usage =
"git-diff-files [-p] [-q] [-r] [-z] [-M] [-C] [-R] [-S<string>] [-O<orderfile>] [paths...]";
"git-diff-files [-p] [-q] [-r] [-z] [-R] [-B] [-M] [-C] [--find-copies-harder] [-O<orderfile>] [-S<string>] [--pickaxe-all] [<path>...]";

static int diff_output_format = DIFF_FORMAT_HUMAN;
static int detect_rename = 0;
static int find_copies_harder = 0;
static int diff_setup_opt = 0;
static int diff_score_opt = 0;
static const char *pickaxe = NULL;
Expand Down Expand Up @@ -81,11 +82,16 @@ int main(int argc, const char **argv)
usage(diff_files_usage);
detect_rename = DIFF_DETECT_COPY;
}
else if (!strcmp(argv[1], "--find-copies-harder"))
find_copies_harder = 1;
else
usage(diff_files_usage);
argv++; argc--;
}

if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
usage(diff_files_usage);

/* At this point, if argc == 1, then we are doing everything.
* Otherwise argv[1] .. argv[argc-1] have the explicit paths.
*/
Expand Down Expand Up @@ -122,12 +128,11 @@ int main(int argc, const char **argv)
continue;
}
changed = ce_match_stat(ce, &st);
if (!changed && detect_rename < DIFF_DETECT_COPY)
if (!changed && !find_copies_harder)
continue;

oldmode = ntohl(ce->ce_mode);
show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
ce->sha1, null_sha1,
ce->sha1, (changed ? null_sha1 : ce->sha1),
ce->name);
}
diffcore_std((1 < argc) ? argv + 1 : NULL,
Expand Down
15 changes: 10 additions & 5 deletions diff-stages.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

static int diff_output_format = DIFF_FORMAT_HUMAN;
static int detect_rename = 0;
static int find_copies_harder = 0;
static int diff_setup_opt = 0;
static int diff_score_opt = 0;
static const char *pickaxe = NULL;
Expand All @@ -16,7 +17,7 @@ static const char *orderfile = NULL;
static const char *diff_filter = NULL;

static char *diff_stages_usage =
"git-diff-stages [-p] [-r] [-z] [-M] [-C] [-R] [-S<string>] [-O<orderfile>] <stage1> <stage2> [<path>...]";
"git-diff-stages [-p] [-r] [-z] [-R] [-B] [-M] [-C] [--find-copies-harder] [-O<orderfile>] [-S<string>] [--pickaxe-all] <stage1> <stage2> [<path>...]";

static void diff_stages(int stage1, int stage2)
{
Expand Down Expand Up @@ -50,9 +51,10 @@ static void diff_stages(int stage1, int stage2)
diff_addremove('-', ntohl(one->ce_mode),
one->sha1, name, NULL);
else if (memcmp(one->sha1, two->sha1, 20) ||
(one->ce_mode != two->ce_mode))
diff_change(ntohl(one->ce_mode), ntohl(two->ce_mode),
one->sha1, two->sha1, name, NULL);
(one->ce_mode != two->ce_mode) ||
find_copies_harder)
diff_change(ntohl(one->ce_mode), ntohl(two->ce_mode),
one->sha1, two->sha1, name, NULL);
}
}

Expand Down Expand Up @@ -81,6 +83,8 @@ int main(int ac, const char **av)
if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
usage(diff_stages_usage);
}
else if (!strcmp(arg, "--find-copies-harder"))
find_copies_harder = 1;
else if (!strcmp(arg, "-z"))
diff_output_format = DIFF_FORMAT_MACHINE;
else if (!strcmp(arg, "-R"))
Expand All @@ -102,7 +106,8 @@ int main(int ac, const char **av)
sscanf(av[1], "%d", &stage1) != 1 ||
! (0 <= stage1 && stage1 <= 3) ||
sscanf(av[2], "%d", &stage2) != 1 ||
! (0 <= stage2 && stage2 <= 3))
! (0 <= stage2 && stage2 <= 3) ||
find_copies_harder && detect_rename != DIFF_DETECT_COPY)
usage(diff_stages_usage);

av += 3; /* The rest from av[0] are for paths restriction. */
Expand Down
6 changes: 3 additions & 3 deletions diff-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ static int interesting(void *tree, unsigned long size, const char *base)
static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
{
while (size) {
if (find_copies_harder || interesting(tree, size, base))
if (interesting(tree, size, base))
show_file(prefix, tree, size, base);
update_tree_entry(&tree, &size);
}
Expand Down Expand Up @@ -269,7 +269,7 @@ static void call_diff_setup(void)

static int call_diff_flush(void)
{
diffcore_std(find_copies_harder ? paths : 0,
diffcore_std(NULL,
detect_rename, diff_score_opt,
pickaxe, pickaxe_opts,
diff_break_opt,
Expand Down Expand Up @@ -401,7 +401,7 @@ static int diff_tree_stdin(char *line)
}

static char *diff_tree_usage =
"git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-O<orderfile>] [-m] [-s] [-v] [--pretty] [-t] <tree-ish> <tree-ish>";
"git-diff-tree [-p] [-r] [-z] [--stdin] [-m] [-s] [-v] [--pretty] [-t] [-R] [-B] [-M] [-C] [--find-copies-header] [-O<orderfile>] [-S<string>] [--pickaxe-all] <tree-ish> <tree-ish>";

static enum cmit_fmt get_commit_format(const char *arg)
{
Expand Down
3 changes: 2 additions & 1 deletion t/t4003-diff-rename-1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ test_expect_success \
# the diff-core. Unchanged rezrov, although being fed to
# git-diff-cache as well, should not be mentioned.

GIT_DIFF_OPTS=--unified=0 git-diff-cache -C -p $tree >current
GIT_DIFF_OPTS=--unified=0 \
git-diff-cache -C --find-copies-harder -p $tree >current
cat >expected <<\EOF
diff --git a/COPYING b/COPYING.1
copy from COPYING
Expand Down
2 changes: 1 addition & 1 deletion t/t4005-diff-rename-2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ test_expect_success \
'cat ../../COPYING >COPYING &&
git-update-cache --add --remove COPYING COPYING.1'

git-diff-cache -C $tree >current
git-diff-cache -C --find-copies-harder $tree >current
cat >expected <<\EOF
:100644 100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0603b3238a076dc6c8022aedc6648fa523a17178 C1234 COPYING COPYING.1
EOF
Expand Down
2 changes: 1 addition & 1 deletion t/t4007-rename-3.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test_expect_success \
# path1 both have COPYING and the latter is a copy of path0/COPYING.
# Comparing the full tree with cache should tell us so.

git-diff-cache -C $tree >current
git-diff-cache -C --find-copies-harder $tree >current

cat >expected <<\EOF
:100644 100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 6ff87c4664981e4397625791c8ea3bbb5f2279a3 C100 path0/COPYING path1/COPYING
Expand Down
2 changes: 1 addition & 1 deletion t/t4009-diff-rename-4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ test_expect_success \
'cat ../../COPYING >COPYING &&
git-update-cache --add --remove COPYING COPYING.1'

git-diff-cache -z -C $tree >current
git-diff-cache -z -C --find-copies-harder $tree >current
cat >expected <<\EOF
:100644 100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0603b3238a076dc6c8022aedc6648fa523a17178 C1234
COPYING
Expand Down

0 comments on commit 4727f64

Please sign in to comment.