Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » Wrong file after jgit apply command
Wrong file after jgit apply command [message #1856545] Wed, 14 December 2022 17:21 Go to next message
Yuriy Mitrofanov is currently offline Yuriy MitrofanovFriend
Messages: 22
Registered: July 2021
Junior Member
Here is a brief description of the problem:

I have a repository with only 3 commits.
1 - init commit
2 - add new pom.xml file
3 - change pom.xml file

after second commit
pom.xml 100644 78e2a3b63c2a0bb3257f3148882afe863306087d

after third commit
pom.xml 100644 e94a2b41013a61f0707955cf2c197f9c0c689e32

After that I create an empty repository, I add the first commit to it.

Then I apply patch 1, create commit 2, apply patch 2, create commit 3 using Git C
I get the identical state of the file.

after second commit
pom.xml 100644 78e2a3b63c2a0bb3257f3148882afe863306087d

after third commit
pom.xml 100644 e94a2b41013a61f0707955cf2c197f9c0c689e32

Let's do the same operations through jgit

Create an empty repository, I add the first commit to it.

Apply patch 1 and create commit 2
after second commit
pom.xml 100644 78e2a3b63c2a0bb3257f3148882afe863306087d

Apply patch 2 and create commit 3
after third commit (Wrong)
pom.xml 100644 99dd0fe04c12084105636eaa1582588036ac5ffd

Now let's compare the files
As you can see in the attached picture, the files are different.
Attached files for both patches.
Re: Wrong file after jgit apply command [message #1856552 is a reply to message #1856545] Wed, 14 December 2022 21:24 Go to previous messageGo to next message
Yuriy Mitrofanov is currently offline Yuriy MitrofanovFriend
Messages: 22
Registered: July 2021
Junior Member
https://git.eclipse.org/r/plugins/gitiles/jgit/jgit/+/refs/heads/master/org.eclipse.jgit/src/org/eclipse/jgit/patch/PatchApplier.java#891
For now, the problem is that if the last line in the source file doesn't end with a line break (which is a very common case), then it inserts a line break anyway, after which the files are no longer
Re: Wrong file after jgit apply command [message #1856579 is a reply to message #1856552] Thu, 15 December 2022 22:22 Go to previous messageGo to next message
Thomas Wolf is currently offline Thomas WolfFriend
Messages: 576
Registered: August 2016
Senior Member
Please open a bug for this. Indeed the PatchApplier handles this wrongly.

The correction is probably at PatchApplier.java, line 873-878, where it should probably do (pseudo code)
if (last line in newLines was from the patch) {
  if (!isNoNewlineAtEndOfFile(fh)) {
     newLines.add(null);
  }
} else if (!rt.isMissingNewlineAtEnd()) {
  newLines.add(null);
}
Re: Wrong file after jgit apply command [message #1856583 is a reply to message #1856579] Thu, 15 December 2022 23:49 Go to previous messageGo to next message
Yuriy Mitrofanov is currently offline Yuriy MitrofanovFriend
Messages: 22
Registered: July 2021
Junior Member
Thomas Wolf wrote on Thu, 15 December 2022 22:22
Please open a bug for this. Indeed the PatchApplier handles this wrongly.

The correction is probably at PatchApplier.java, line 873-878, where it should probably do (pseudo code)
if (last line in newLines was from the patch) {
  if (!isNoNewlineAtEndOfFile(fh)) {
     newLines.add(null);
  }
} else if (!rt.isMissingNewlineAtEnd()) {
  newLines.add(null);
}



https://bugs.eclipse.org/bugs/show_bug.cgi?id=581234
Re: Wrong file after jgit apply command [message #1856605 is a reply to message #1856579] Fri, 16 December 2022 23:15 Go to previous messageGo to next message
Yuriy Mitrofanov is currently offline Yuriy MitrofanovFriend
Messages: 22
Registered: July 2021
Junior Member
Thomas Wolf wrote on Thu, 15 December 2022 22:22
Please open a bug for this. Indeed the PatchApplier handles this wrongly.

The correction is probably at PatchApplier.java, line 873-878, where it should probably do (pseudo code)
if (last line in newLines was from the patch) {
  if (!isNoNewlineAtEndOfFile(fh)) {
     newLines.add(null);
  }
} else if (!rt.isMissingNewlineAtEnd()) {
  newLines.add(null);
}


I managed to find another problem in PatchApplier.

When the file is already in the index, but the "delta" size for the file is larger than the "literal" size for the entire new version of the file.
It is added to the patch with a literal label and a modification label.

But file can't pass that test:
https://git.eclipse.org/r/plugins/gitiles/jgit/jgit/+/refs/heads/master/org.eclipse.jgit/src/org/eclipse/jgit/patch/PatchApplier.java#645
Re: Wrong file after jgit apply command [message #1856606 is a reply to message #1856605] Fri, 16 December 2022 23:18 Go to previous messageGo to next message
Yuriy Mitrofanov is currently offline Yuriy MitrofanovFriend
Messages: 22
Registered: July 2021
Junior Member
I fixed it but haven't written all the tests yet.

	private void checkOid(ObjectId baseId, ObjectId id, ChangeType type, File f,
			String path) throws PatchApplyException, IOException {
		boolean hashOk = false;
		if (id != null) {
			hashOk = baseId.equals(id);
			if (!hashOk && ChangeType.ADD.equals(type)
					&& ObjectId.zeroId().equals(baseId)) {
				// We create a new file. The OID of an empty file is not the
				// zero id!
				hashOk = Constants.EMPTY_BLOB_ID.equals(id);
			} else if (!hashOk && ChangeType.MODIFY.equals(type)
					&& !ObjectId.zeroId().equals(baseId)) {
				hashOk = true;
			}
		} else if (!inCore()) {
			if (ObjectId.zeroId().equals(baseId)) {
				// File empty is OK.
				hashOk = !f.exists() || f.length() == 0;
			} else {
				hashOk = baseId.equals(hash(f));
			}
		}
		if (!hashOk) {
			throw new PatchApplyException(MessageFormat
					.format(JGitText.get().applyBinaryBaseOidWrong, path));
		}
	}
Re: Wrong file after jgit apply command [message #1856615 is a reply to message #1856606] Sat, 17 December 2022 15:30 Go to previous message
Thomas Wolf is currently offline Thomas WolfFriend
Messages: 576
Registered: August 2016
Senior Member
if (!hashOk && ChangeType.MODIFY.equals(type) && !ObjectId.zeroId().equals(baseId)) doesn't look right. It defeats the whole purpose of the check. But let's see a failing test case first on Gerrit, then we can discuss code changes there much better than here.
Previous Topic:Java code to create new github repo and push some code
Next Topic:Discussion on default values of "Configure upstream for push and pull"
Goto Forum:
  


Current Time: Sat Apr 27 03:11:15 GMT 2024

Powered by FUDForum. Page generated in 0.03630 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top