Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » Want to enforce git repository commit of files before publishing them
Want to enforce git repository commit of files before publishing them [message #1751610] Wed, 11 January 2017 21:10 Go to next message
Baback Nemazie is currently offline Baback NemazieFriend
Messages: 8
Registered: January 2017
Junior Member
Hi,

I am developing an eclipse plugin for publishing some source files to a server. Before the source files are published I want to ensure/enforce that the source files are committed to remote git repository, so that if the user forgets to do so the system enforces it.

Assuming I have Egit installed on my eclipse instance, how do I go about doing this from my plugin?

Thanks for any pointers.
Re: Want to enforce git repository commit of files before publishing them [message #1751624 is a reply to message #1751610] Thu, 12 January 2017 00:57 Go to previous messageGo to next message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 962
Registered: July 2009
Senior Member
I think you need an up-to-date clone of the repository, then you can use JGit
to check if a blob matching the file's content hash can be found in the cloned repository

calculate SHA1 of file content:
- use Constants.newMessageDigest() to get a MessageDigest md
- open a buffered FileInputStream for the source file
- wrap this stream with a DigestInputStream and setMessageDigest(md)
- when file is completely read, close the stream and compute ObjectId for file content: ObjectId.fromRaw(md.digest())

- use CloneCommand to clone the repository (if already cloned run FetchCommand to ensure the clone is in sync with the server-side repo)
- try to load the blob object matching the file's content hash:
Repository.open(objectId, Constants.OBJ_BLOB)
if the object exists in the repository you'll get an ObjectLoader otherwise a MissingObjectException will be thrown
- if you need to do this for many files first get an ObjectReader (Repository.newObjectReader()) and then try to open the object
ObjectReader.open(objectId, Constants.OBJ_BLOB)
Re: Want to enforce git repository commit of files before publishing them [message #1751654 is a reply to message #1751610] Thu, 12 January 2017 09:51 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 241
Registered: July 2009
Senior Member
If the project where the user is modifying files is already under EGit control then you can leave all the working of computing sha1's to JGit' status command. Git. Git.status().call().isClean() would tell you if there are files the user has "forgot" to commit.
If this is a project which is potentially not yet under EGit control then you would have to go the hard way as Matthias describes.


Ciao
Chris
Re: Want to enforce git repository commit of files before publishing them [message #1751781 is a reply to message #1751624] Fri, 13 January 2017 18:33 Go to previous messageGo to next message
Baback Nemazie is currently offline Baback NemazieFriend
Messages: 8
Registered: January 2017
Junior Member
Thank you veru much for your response. I am able to do as you suggested however the SHA sum I get does not match the git one and so even though file has not changed the SHA sum do not match

This is the git reported hssh

$ git hash-object Rules.drl
87f63341793023cbcfdf9f9d90ff3fbebb7357f1

This is the code reported hash:

//46b61130d0d195afa9d14e063eca0e2526ff5364

Per sample code code in:

https://github.com/eclipse/jgit/blob/57a263f1823d164142235a72072154f0568cb61c/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java

and per directions in:

http://stackoverflow.com/questions/460297/git-finding-the-sha1-of-an-individual-file-in-the-index

I form the hash as follows:

public ObjectId hash(String content)
throws FileNotFoundException, IOException {
MessageDigest md = Constants.newMessageDigest();

md.update(Constants.encodeASCII(Constants.TYPE_BLOB));
md.update((byte) ' ');
md.update(Constants.encodeASCII(content.length()));
md.update((byte) 0);
md.update(Constants.encode(content));

return ObjectId.fromRaw(md.digest());
}

Is there something wrong with what I am doing?
Re: Want to enforce git repository commit of files before publishing them [message #1751782 is a reply to message #1751654] Fri, 13 January 2017 18:35 Go to previous messageGo to next message
Baback Nemazie is currently offline Baback NemazieFriend
Messages: 8
Registered: January 2017
Junior Member
I posted my comment before seeing this response. I will try it as well. Thanks a mil.
Re: Want to enforce git repository commit of files before publishing them [message #1751790 is a reply to message #1751782] Fri, 13 January 2017 22:07 Go to previous message
Baback Nemazie is currently offline Baback NemazieFriend
Messages: 8
Registered: January 2017
Junior Member
Update. My egit was not set up properly, hence the issue with hash mismatch. Both approaches work fine. Thanks!!
Previous Topic:Team - Pull often ignored in Project Explorer
Next Topic:git ssh push hangs with eclipse and tfs
Goto Forum:
  


Current Time: Fri Nov 24 09:39:53 GMT 2017

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

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