Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » Wrong file hash
Wrong file hash [message #1843440] Thu, 29 July 2021 19:42 Go to next message
Yuriy Mitrofanov is currently offline Yuriy MitrofanovFriend
Messages: 22
Registered: July 2021
Junior Member
C:\Projects\test>git ls-tree cd4efa48be9c2f2602f305484005c73be93622e4
100644 blob e40fd988170f1bace96b582db3fd80013db70614 README.md
100644 blob c63225d6c0bb40de9eb9309790997bd1de55a207 build.gradle
040000 tree f61a1cc06fce76eb5d1fc3f059807f3a978de58c gradle
100644 blob 4453ccea33d960069d9137ee65f6b21fc65e7e92 gradlew
100644 blob e95643d6a2ca62258464e83c72f5156dc941c609 gradlew.bat
100644 blob 2714e7495fa236940b889121af75b4479667cac2 settings.gradle
040000 tree 4038c9573e806a46c18f071f918672aa22e3c98b src

C:\Projects\test>git hash-object gradlew.bat
e95643d6a2ca62258464e83c72f5156dc941c609

C:\Projects\test>git cat-file -p e95643d6a2ca62258464e83c72f5156dc941c609 >12345

C:\Projects\test>git hash-object 12345
e95643d6a2ca62258464e83c72f5156dc941c609

Jgit calculates the wrong hash for the file
f9553162f122c71b34635112e717c3e733b5b212

How change this options git config core.autocrlf in jgit?
How to change this setting without changing git global options.

[Updated on: Thu, 29 July 2021 20:03]

Report message to a moderator

Re: Wrong file hash [message #1843443 is a reply to message #1843440] Thu, 29 July 2021 21:59 Go to previous messageGo to next message
Thomas Wolf is currently offline Thomas WolfFriend
Messages: 576
Registered: August 2016
Senior Member
How exactly did you determine that JGit "computed the wrong hash"? The core.autocrlf option is set in JGit just like in C git: set it in the config.

But for cases like this gradlew.bat/gradlew, you want to ensure that the Windows gradlew.bat file has CRLF as line endings, and the gradlew Unix shell script has LF. So you use a .gitattributes file:
*.bat        text eol=crlf
gradlew      text eol=lf

Re: Wrong file hash [message #1843447 is a reply to message #1843443] Fri, 30 July 2021 05:43 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, 29 July 2021 21:59
How exactly did you determine that JGit "computed the wrong hash"? The core.autocrlf option is set in JGit just like in C git: set it in the config.

But for cases like this gradlew.bat/gradlew, you want to ensure that the Windows gradlew.bat file has CRLF as line endings, and the gradlew Unix shell script has LF. So you use a .gitattributes file:
*.bat        text eol=crlf
gradlew      text eol=lf



I was calculating hashes for each file.
Re: Wrong file hash [message #1843448 is a reply to message #1843447] Fri, 30 July 2021 06:39 Go to previous messageGo to next message
Thomas Wolf is currently offline Thomas WolfFriend
Messages: 576
Registered: August 2016
Senior Member
How exactly? Show code.
Re: Wrong file hash [message #1843470 is a reply to message #1843448] Sat, 31 July 2021 12:43 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 Fri, 30 July 2021 06:39
How exactly? Show code.


try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
// find the HEAD
ObjectId lastCommitId = repository.resolve(Constants.HEAD);

// a RevWalk allows to walk over commits based on some filtering that is defined
try (RevWalk revWalk = new RevWalk(repository)) {
RevCommit commit = revWalk.parseCommit(lastCommitId);
// and using commit's tree find the path
RevTree tree = commit.getTree();
System.out.println("Having tree: " + tree);

// now try to find a specific file
try (TreeWalk treeWalk = new TreeWalk(repository)) {
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
treeWalk.setFilter(PathFilter.create("README.md"));
if (!treeWalk.next()) {
throw new IllegalStateException("Did not find expected file 'README.md'");
}

ObjectId objectId = treeWalk.getObjectId(0);
ObjectLoader loader = repository.open(objectId);

// and then one can the loader to read the file
loader.copyTo(System.out);
}

revWalk.dispose();
}
}
}
Re: Wrong file hash [message #1843475 is a reply to message #1843470] Sat, 31 July 2021 17:40 Go to previous messageGo to next message
Thomas Wolf is currently offline Thomas WolfFriend
Messages: 576
Registered: August 2016
Senior Member
That doesn't compute the hash, it copies the git blob for README.md to stdout, without applying any crlf or smudge filters.

I don't see the connection to your initial git ls-tree or to your claim JGit computed hashes wrongly.
Re: Wrong file hash [message #1843477 is a reply to message #1843475] Sat, 31 July 2021 18:06 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 Sat, 31 July 2021 17:40
That doesn't compute the hash, it copies the git blob for README.md to stdout, without applying any crlf or smudge filters.

I don't see the connection to your initial git ls-tree or to your claim JGit computed hashes wrongly.


Yes, I know, but before saving the file, it calculates its hash.
Re: Wrong file hash [message #1843577 is a reply to message #1843477] Sun, 08 August 2021 18:11 Go to previous messageGo to next message
Thomas Wolf is currently offline Thomas WolfFriend
Messages: 576
Registered: August 2016
Senior Member
No, it doesn't. It reads the hash from the tree, it does not calculate the hash. Besides, your code snippet is definitely not the one you used to read gradlew.bat. It's just the snippet shown at the Vogella web site.
Re: Wrong file hash [message #1843588 is a reply to message #1843577] Mon, 09 August 2021 23:45 Go to previous message
Yuriy Mitrofanov is currently offline Yuriy MitrofanovFriend
Messages: 22
Registered: July 2021
Junior Member
Thomas Wolf wrote on Sun, 08 August 2021 18:11
No, it doesn't. It reads the hash from the tree, it does not calculate the hash. Besides, your code snippet is definitely not the one you used to read gradlew.bat. It's just the snippet shown at the Vogella web site.


This was just an example, I can publish all the code for you to understand my problem
Previous Topic:Cannot revert commit
Next Topic:Transport Error :: Egit :: clone repository fail
Goto Forum:
  


Current Time: Wed Dec 11 09:51:44 GMT 2024

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

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

Back to the top