Commit a String with 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?
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).
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.
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.
链接地址: http://www.djcxy.com/p/45222.html上一篇: 如何让git默认使用ssh而不是https来访问新的软件仓库
下一篇: 用JGit提交一个字符串