Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jgit-dev] Transient poopies after JGit commit

On Sat, Feb 20, 2010 at 8:41 PM, Bill Burdick <bill.burdick@xxxxxxxxx> wrote:
Cool, I'll try DirCache.  The most important thing to me is that I'm not using JGit in an incorrect way.


OK, I'm trying to use DirCache, but I must not be using it properly.  I have a boolean in my script to switch between using GitIndex and DirCache.  GitIndex behaves like I would expect, but when I use DirCache to do two commits (after edits), the second one adds a commit to the list and updates the head like you would expect, but it appears to change the tree in the first commit and leaves the new commit empty.  Here is the if-clause that chooses between GitIndex and DirCache.  After that, I'm attaching the entire (updated) script.

When I make a DirCacheEntry, do I need to initialize it further than setting its path and fileMode?

As an aside, it seems like if DirCacheBuilder had an add(path) method, creating new entries would be just as easy as it is with GitIndex.

Here's the if-clause:


if (useIndex) {
index = repository.getIndex
pathsDo(repoPath, _.getName != ".git") {index.add(repoPath, _)}
index.write
commit.setTree(repository.mapTree(index.writeTree))
} else {
dircache = DirCache.lock(repository.getIndexFile)
val builder = dircache.builder

pathsDo(repoPath, _.getName != ".git") {f =>
val entry = new DirCacheEntry(f.getAbsolutePath.substring(repoPathLen))

entry.setFileMode(FileMode.REGULAR_FILE)
builder.add(entry)
}
builder.commit
commit.setTree(repository.mapTree(dircache.writeTree(new ObjectWriter(repository))))
}


And here's the whole script...


import java.io.File
import java.io.IOException
import org.eclipse.jgit.lib.Commit
import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.lib.ObjectId
import org.eclipse.jgit.lib.ObjectWriter
import org.eclipse.jgit.lib.PersonIdent
import org.eclipse.jgit.lib.RefUpdate
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.lib.Tree
import org.eclipse.jgit.dircache.DirCache
import org.eclipse.jgit.dircache.DirCacheEntry
import org.eclipse.jgit.lib.RepositoryState
import org.eclipse.jgit.lib.FileMode
import org.eclipse.jgit.lib.GitIndex

val useIndex = true
val cache: File = new File("/tmp/xus/.git")
val repository = new Repository(cache)
val author: PersonIdent = new PersonIdent("Bubba", "gump@xxxxxxxxxx")
val repoPath = repository.getDirectory.getParentFile
val repoPathLen = repository.getDirectory.getParentFile.getAbsolutePath.length + 1
val currentHeadId = repository.resolve(Constants.HEAD)
var index: GitIndex = null
var dircache: DirCache = null

def pathsDo(dir: File, filter: (File) => Boolean)(block: (File) => Any) {
for (file <- dir.listFiles) {
if (filter(file)) {
if (file.isDirectory) {
pathsDo(file, filter)(block)
} else {
println("FILE: "+file.getName)
block(file)
}
}
}
}

val commit = new Commit(repository, if (currentHeadId != null) Array(currentHeadId) else Array[ObjectId]())
if (useIndex) {
index = repository.getIndex
pathsDo(repoPath, _.getName != ".git") {index.add(repoPath, _)}
index.write
commit.setTree(repository.mapTree(index.writeTree))
} else {
dircache = DirCache.lock(repository.getIndexFile)
val builder = dircache.builder

pathsDo(repoPath, _.getName != ".git") {f =>
val entry = new DirCacheEntry(f.getAbsolutePath.substring(repoPathLen))

entry.setFileMode(FileMode.REGULAR_FILE)
builder.add(entry)
}
builder.commit
commit.setTree(repository.mapTree(dircache.writeTree(new ObjectWriter(repository))))
}
commit.setMessage("Test commit")
commit.setAuthor(author)
commit.setCommitter(author)
commit.setCommitId(new ObjectWriter(repository).writeCommit(commit))
val refUpdate = repository.updateRef(Constants.HEAD)
refUpdate.setNewObjectId(commit.getCommitId())
refUpdate.setRefLogMessage("commit: Test commit", false);
if (refUpdate.forceUpdate() == RefUpdate.Result.LOCK_FAILURE) {
throw new IOException("Failed to update " + refUpdate.getName + " to commit " + commit.getCommitId + ".");
}


Back to the top