Skip to contentSkip to conversation

read-tree: loosen too strict index requirements

PATCH New Junio C Hamano 3 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

This patch teaches read-tree 3-way merge that, when only "the other tree" changed a path, and if the index file already has the same change, we are not in a situation that would clobber the index and the work tree, and lets the merge succeed; this is case #14ALT in t1000 test. It does not change the result of the merge, but prevents it from failing when it does not have to.

---

*** This is another leftover bits from the earlier 3-piece series. *** This is not necessary for the --emu23 two-way improvements, *** but rather help a case that would not be so uncommon in the *** 3-way merge case.

 read-tree.c                 |    6 ++++++
 t/t1000-read-tree-m-3way.sh |    9 +++++++++
 2 files changed, 15 insertions(+), 0 deletions(-)
read-tree.c +6 -0
@@ -280,6 +280,12 @@ static int threeway_merge(struct cache_e
 		}
 		/* otherwise we will apply the original rule */
 	}
+	/* #14ALT */
+	if (a && b && c && same(a, b) && !same(a, c)) {
+		if (old && same(old, c))
+			return merged_entry_allow_dirty(c, old, dst);
+		/* otherwise the regular rule applies */
+	}
 	/*
 	 * If we have an entry in the index cache ("old"), then we want
 	 * to make sure that it matches any entries in stage 2 ("first
t/t1000-read-tree-m-3way.sh +9 -0
@@ -464,6 +464,15 @@ test_expect_success \
      git-read-tree -m $tree_O $tree_A $tree_B &&
      check_result"
 
+test_expect_success \
+    '14 - may match B in O && A && B && O==A && O!=B case' \
+    "rm -f .git/index NM &&
+     cp .orig-B/NM NM &&
+     git-update-cache --add NM &&
+     echo extra >>NM &&
+     git-read-tree -m $tree_O $tree_A $tree_B &&
+     check_result"
+
 test_expect_failure \
     '14 (fail) - must match and be up-to-date in O && A && B && O==A && O!=B case' \
     "rm -f .git/index NM &&

------------

#1 lore raw

PATCH 1/1 Add --diff-filter= output restriction to diff-* family.

This is a halfway between debugging aid and a helper to write an ultra-smart merge scripts. The new option takes a string that consists of a list of "status" letters, and limits the diff output to only those classes of changes, with two exceptions:

- A broken pair (aka "complete rewrite"), does not match D (deleted) or N (created). Use B to look for them.

- The letter "A" in the diff-filter string does not match anything itself, but causes the entire diff that contains selected patches to be output (this behaviour is similar to that of --pickaxe-all for the -S option).

For example,

    $ git-rev-list HEAD |
      git-diff-tree --stdin -s -v -B -C --diff-filter=BCR

shows a list of commits that have complete rewrite, copy, or rename.

---

 diff.h        |    8 +++++--
 diff-cache.c  |    9 ++++++-
 diff-files.c  |    7 ++++--
 diff-helper.c |   15 +++++++-----
 diff-stages.c |    8 +++++--
 diff-tree.c   |   12 +++++++---
 diff.c        |   70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 7 files changed, 108 insertions(+), 21 deletions(-)
diff.h +6 -2
@@ -47,7 +47,11 @@ extern void diffcore_std(const char **pa
 			 int detect_rename, int rename_score,
 			 const char *pickaxe, int pickaxe_opts,
 			 int break_opt,
-			 const char *orderfile);
+			 const char *orderfile, const char *filter);
+
+extern void diffcore_std_no_resolve(const char **paths,
+				    const char *pickaxe, int pickaxe_opts,
+				    const char *orderfile, const char *filter);
 
 extern int diff_queue_is_empty(void);
 
@@ -56,6 +60,6 @@ extern int diff_queue_is_empty(void);
 #define DIFF_FORMAT_PATCH	2
 #define DIFF_FORMAT_NO_OUTPUT	3
 
-extern void diff_flush(int output_style, int resolve_rename_copy);
+extern void diff_flush(int output_style);
 
 #endif /* DIFF_H */
diff-cache.c +7 -2
@@ -11,6 +11,7 @@ static const char *pickaxe = NULL;
 static int pickaxe_opts = 0;
 static int diff_break_opt = -1;
 static const char *orderfile = NULL;
+static const char *diff_filter = NULL;
 
 /* A file entry went away or appeared */
 static void show_file(const char *prefix, struct cache_entry *ce, unsigned char *sha1, unsigned int mode)
@@ -224,6 +225,10 @@ int main(int argc, const char **argv)
 			pickaxe = arg + 2;
 			continue;
 		}
+		if (!strncmp(arg, "--diff-filter=", 14)) {
+			diff_filter = arg + 14;
+			continue;
+		}
 		if (!strncmp(arg, "-O", 2)) {
 			orderfile = arg + 2;
 			continue;
@@ -263,7 +268,7 @@ int main(int argc, const char **argv)
 		     detect_rename, diff_score_opt,
 		     pickaxe, pickaxe_opts,
 		     diff_break_opt,
-		     orderfile);
-	diff_flush(diff_output_format, 1);
+		     orderfile, diff_filter);
+	diff_flush(diff_output_format);
 	return ret;
 }
diff-files.c +5 -2
@@ -17,6 +17,7 @@ static const char *pickaxe = NULL;
 static int pickaxe_opts = 0;
 static int diff_break_opt = -1;
 static const char *orderfile = NULL;
+static const char *diff_filter = NULL;
 static int silent = 0;
 
 static void show_unmerge(const char *path)
@@ -59,6 +60,8 @@ int main(int argc, const char **argv)
 			pickaxe = argv[1] + 2;
 		else if (!strncmp(argv[1], "-O", 2))
 			orderfile = argv[1] + 2;
+		else if (!strncmp(argv[1], "--diff-filter=", 14))
+			diff_filter = argv[1] + 14;
 		else if (!strcmp(argv[1], "--pickaxe-all"))
 			pickaxe_opts = DIFF_PICKAXE_ALL;
 		else if (!strncmp(argv[1], "-B", 2)) {
@@ -131,7 +134,7 @@ int main(int argc, const char **argv)
 		     detect_rename, diff_score_opt,
 		     pickaxe, pickaxe_opts,
 		     diff_break_opt,
-		     orderfile);
-	diff_flush(diff_output_format, 1);
+		     orderfile, diff_filter);
+	diff_flush(diff_output_format);
 	return 0;
 }
diff-helper.c +9 -6
@@ -8,17 +8,16 @@
 static const char *pickaxe = NULL;
 static int pickaxe_opts = 0;
 static const char *orderfile = NULL;
+static const char *diff_filter = NULL;
 static int line_termination = '\n';
 static int inter_name_termination = '\t';
 
 static void flush_them(int ac, const char **av)
 {
-	diffcore_std(av + 1,
-		     0, 0, /* no renames */
-		     pickaxe, pickaxe_opts,
-		     -1, /* no breaks */
-		     orderfile);
-	diff_flush(DIFF_FORMAT_PATCH, 0);
+	diffcore_std_no_resolve(av + 1,
+				pickaxe, pickaxe_opts,
+				orderfile, diff_filter);
+	diff_flush(DIFF_FORMAT_PATCH);
 }
 
 static const char *diff_helper_usage =
@@ -38,6 +37,10 @@ int main(int ac, const char **av) {
 		}
 		else if (!strcmp(av[1], "--pickaxe-all"))
 			pickaxe_opts = DIFF_PICKAXE_ALL;
+		else if (!strncmp(av[1], "--diff-filter=", 14))
+			diff_filter = av[1] + 14;
+		else if (!strncmp(av[1], "-O", 2))
+			orderfile = av[1] + 2;
 		else
 			usage(diff_helper_usage);
 		ac--; av++;
diff-stages.c +6 -2
@@ -13,6 +13,7 @@ static const char *pickaxe = NULL;
 static int pickaxe_opts = 0;
 static int diff_break_opt = -1;
 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>...]";
@@ -88,6 +89,8 @@ int main(int ac, const char **av)
 			pickaxe = arg + 2;
 		else if (!strncmp(arg, "-O", 2))
 			orderfile = arg + 2;
+		else if (!strncmp(arg, "--diff-filter=", 14))
+			diff_filter = arg + 14;
 		else if (!strcmp(arg, "--pickaxe-all"))
 			pickaxe_opts = DIFF_PICKAXE_ALL;
 		else
@@ -111,7 +114,8 @@ int main(int ac, const char **av)
 		     detect_rename, diff_score_opt,
 		     pickaxe, pickaxe_opts,
 		     diff_break_opt,
-		     orderfile);
-	diff_flush(diff_output_format, 1);
+		     orderfile,
+		     diff_filter);
+	diff_flush(diff_output_format);
 	return 0;
 }
diff-tree.c +9 -3
@@ -18,6 +18,7 @@ static const char *pickaxe = NULL;
 static int pickaxe_opts = 0;
 static int diff_break_opt = -1;
 static const char *orderfile = NULL;
+static const char *diff_filter = NULL;
 static const char *header = NULL;
 static const char *header_prefix = "";
 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
@@ -272,9 +273,10 @@ static int call_diff_flush(void)
 		     detect_rename, diff_score_opt,
 		     pickaxe, pickaxe_opts,
 		     diff_break_opt,
-		     orderfile);
+		     orderfile,
+		     diff_filter);
 	if (diff_queue_is_empty()) {
-		diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
+		diff_flush(DIFF_FORMAT_NO_OUTPUT);
 		return 0;
 	}
 	if (header) {
@@ -285,7 +287,7 @@ static int call_diff_flush(void)
 		printf(fmt, header, 0);
 		header = NULL;
 	}
-	diff_flush(diff_output_format, 1);
+	diff_flush(diff_output_format);
 	return 1;
 }
 
@@ -455,6 +457,10 @@ int main(int argc, const char **argv)
 			orderfile = arg + 2;
 			continue;
 		}
+		if (!strncmp(arg, "--diff-filter=", 14)) {
+			diff_filter = arg + 14;
+			continue;
+		}
 		if (!strcmp(arg, "--pickaxe-all")) {
 			pickaxe_opts = DIFF_PICKAXE_ALL;
 			continue;
diff.c +66 -4

98 lines not on this page · raw message

------------

#2 lore raw

PATCH 1/1 Fix rename/copy when dealing with temporarily broken pairs.

In reply to Junio C Hamano

When rename/copy uses a file that was broken by diffcore-break as the source, and the broken filepair gets merged back later, the output was mislabeled as a rename. In this case, the source file ends up staying in the output, so we should label it as a copy instead.

---

 diff.c            |    1 +
 diffcore-rename.c |   10 ++++++++--
 2 files changed, 9 insertions(+), 2 deletions(-)
diff.c +1 -0
@@ -661,6 +661,7 @@ struct diff_filepair *diff_queue(struct 
 	dp->one = one;
 	dp->two = two;
 	dp->score = 0;
+	dp->status = 0;
 	dp->source_stays = 0;
 	dp->broken_pair = 0;
 	diff_q(queue, dp);
diffcore-rename.c +8 -2
@@ -249,8 +249,14 @@ void diffcore_rename(int detect_rename, 
 				continue; /* unmerged */
 			else
 				locate_rename_dst(p->two, 1);
-		else if (!DIFF_FILE_VALID(p->two))
-			register_rename_src(p->one, 0);
+		else if (!DIFF_FILE_VALID(p->two)) {
+			/* If the source is a broken "delete", and
+			 * they did not really want to get broken,
+			 * that means the source actually stays.
+			 */
+			int stays = (p->broken_pair && !p->score);
+			register_rename_src(p->one, stays);
+		}
 		else if (detect_rename == DIFF_DETECT_COPY)
 			register_rename_src(p->one, 1);
 	}

------------

#3 lore raw

Re: [PATCH] Fix rename/copy when dealing with temporarily broken pairs.

G'day Junio,

Quoted 5 lines · Junio C Hamano

When rename/copy uses a file that was broken by diffcore-break as the source, and the broken filepair gets merged back later, the output was mislabeled as a rename. In this case, the source file ends up staying in the output, so we should label it as a copy instead.

I've got a patch series that renames a file, but the patches generated by your git-format-patch-script fail to rename the file to the new name.

I wondered if this patch might fix it, but it doesn't seem to.

Here's a hunk from the patch:

traversal.c renamed from epoch.c +1 -1
similarity index 100%
rename from epoch.c
rename to traversal.c
@@ -14,7 +14,7 @@
 
 #include "cache.h"
 #include "commit.h"
-#include "epoch.h"
+#include "traversal.h"
 
 struct fraction {
        BIGNUM numerator;

What tool am I meant to be using to apply the patch?

I am currently using "patch -p1"

jon.

#4 lore raw

Re: [PATCH] Fix rename/copy when dealing with temporarily broken pairs.

Dear diary, on Mon, Jun 13, 2005 at 06:24:16PM CEST, I got a letter where Jon Seymour <[email protected]> told me that...

Quoted 7 lines
traversal.c renamed from epoch.c +0 -0
similarity index 100%
rename from epoch.c
rename to traversal.c

What tool am I meant to be using to apply the patch?

I am currently using "patch -p1"

Try git-apply.

Signature · 3 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
<Espy> be careful, some twit might quote you out of context..
#5 lore raw

Re: [PATCH] Fix rename/copy when dealing with temporarily broken pairs.

In reply to Jon Seymour
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"JS" == Jon Seymour <[email protected]> writes:

JS> I've got a patch series that renames a file, but the patches JS> generated by your git-format-patch-script fail to rename the JS> file to the new name.

JS> I wondered if this patch might fix it, but it doesn't seem to.

I doubt the problem you have is related to the problem this patch addresses. This is a fix for a case that the machinery sometimes says rename when it should say copy.

Regular "patch" cannot grok either copy or rename. If your recipient uses "patch", then you must not use -C nor -M when you are preparing a diff for his consumption.

JS> What tool am I meant to be using to apply the patch? JS> I am currently using "patch -p1"

As Petr already responded, the git-apply program from the King Penguin himself should work [*1*].

One thing I just want to make sure is this. You are not reporting that the patch you sent that said "rename" should have been "copy", are you?

I understand that your change creates traversal.c out of epoch.c. Say the output is from between commitA and commitB.

What I would like to know is if commitB still contains epoch.c, or it does not have epoch.c anymore [*2*]. If it is the former then I think you spotted a bug in the patch your message is a response to. I hope that is not the case.

P.S. I will be offline shortly and will not be able to offer further help until perhaps late next week.

[Footnote]

*1* Find the thread in the mailing list archive that contains this message, which I think would be of help too.

Date: Sun, 5 Jun 2005 16:17:56 -0700 (PDT)
From: Linus Torvalds <[email protected]>
To: Junio C Hamano <[email protected]>
cc: Daniel Barkalow <[email protected]>, [email protected]
Subject: Re: [PATCH-CAREFUL/RENAME] rename git-rpush and git-rpull to git-ssh-push and git-ssh-pull
Message-ID: <[email protected]>
References: <[email protected]>
<[email protected]> <[email protected]> <[email protected]> <[email protected]>

On Sun, 5 Jun 2005, Junio C Hamano wrote:
>
> Here is a rebase. To apply with git-apply, you need the "Yes, > sir" patch to grok "rename from/to".

Seems to have applied correctly. Very nice.

Linus

#6 lore raw

Re: [PATCH] Fix rename/copy when dealing with temporarily broken pairs.

Quoted 4 lines · Junio C Hamano

JS> I wondered if this patch might fix it, but it doesn't seem to. Regular "patch" cannot grok either copy or rename. If your recipient uses "patch", then you must not use -C nor -M when you are preparing a diff for his consumption.

Ok, there was no problem other than me not understanding what git-apply did - thank you Pasky for bringing light to the dark regions of my sleep-deprived brain!

jon.