Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commit a String with JGit

Tags:

java

jgit

A JGit-beginner-question:

I use JGit to read a file (BLOB) from a repository and manipulate its content. After that, I want to write the new content with the same filename back to the repository as a new commit. But how can I commit the new content with JGit?

My pseudo-code:

String gitUrl = "path/to/repository/.git";
Repository repository = new FileRepository(gitUrl);
String filename = "test/seppl.txt";
blobId = getIdOf(filename);
ObjectLoader object = repository.open(blobId, Constants.OBJ_BLOB);
ObjectStream is = object.openStream();
String newContent = processStream(is);
// How to commit the newContent in filename?

Do I have to write the newContent into a file and commit this file with the AddCommand and CommitCommand? Or can I write the String "on-the-fly" into the repository under the same filename?

Is there anywhere in the web an example how to make a commit with JGit?

like image 997
Sonson123 Avatar asked Apr 12 '12 16:04

Sonson123


3 Answers

I think there is no other way to commit any data than using the CommitCommand (except merging or such operations that are very specific).

So, yeah, you should make any change in your file and then add it and commit it (using the AddCommand and CommitCommand from the API).

like image 120
Vince Avatar answered Nov 12 '22 17:11

Vince


Yes, of course you can do what you want, but just not using Add/Commit as they are part of the high-level Porcelein API which is just a convenience API built on top of the low level API. Thus they just implement the most common use cases.

What you need to do is to look at the implementation of the AddCommand and CommitCommand to see how to use the lower-level API to create BLOB objects and then tree objects and than commit objects.

I'd second the recommendation that you thoroughly read chapter 9 of the Pro Git book so that you properly understand how Git works on a low level.

like image 28
Maks Avatar answered Nov 12 '22 17:11

Maks


You might want to look into using Blob's in Git. This technique is used to store the public PGP key's when signing Tags. It looks like the content you want still has to be put into a file, but it can be a temp file. The file won't exist in the main directory when someone does a pull. It'll be an entry in the tree as a blob.

http://book.git-scm.com/7_raw_git.html

The -w tells it to write the entry along with returning the hash.

git hash-object -w myfile.txt
6ff87c4664981e4397625791c8ea3bbb5f2279a3

Update

I skimmed through this entry on my phone and thus was less then attentive to the detail you provided. Yes you'd need to write the string to a file, but no you shouldn't have to add it like a normal file. I would suspect that JGit has the ability to do hash-object. It looks like you already has some code to deal with a BLOB entry. Perhaps there is a higher level call for hash-object in which you don't deal with the BLOB directly.

Considering everything in Git relies on hashes of content, I would say that, even if you did find a way to write the string directly you shouldn't. You should recommit the object with the same filename such that you get a new hash and updated entry.

like image 3
Andrew T Finnell Avatar answered Nov 12 '22 16:11

Andrew T Finnell