Browse Source

DirCache: Add helper to read from a tree

Application code sometimes wants to read a DirCache from an ObjectId,
but its confusing how to do this because its buried inside the
DirCacheBuilder.

Use this utility in a few places within JGit that also want to read
a DirCache from a tree's ObjectId.

Change-Id: I578b7e18e58753d154937f4ab835012b09e5adca
stable-4.3
Shawn Pearce 9 years ago
parent
commit
93eca6dfe1
  1. 24
      org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java
  2. 31
      org.eclipse.jgit/src/org/eclipse/jgit/merge/RecursiveMerger.java
  3. 8
      org.eclipse.jgit/src/org/eclipse/jgit/transport/PushCertificateStore.java

24
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java vendored

@ -71,9 +71,11 @@ import org.eclipse.jgit.events.IndexChangedListener;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.internal.storage.file.FileSnapshot;
import org.eclipse.jgit.internal.storage.file.LockFile;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.treewalk.TreeWalk;
@ -146,6 +148,28 @@ public class DirCache {
return new DirCache(null, null);
}
/**
* Create a new in memory index read from the contents of a tree.
*
* @param reader
* reader to access the tree objects from a repository.
* @param treeId
* tree to read. Must identify a tree, not a tree-ish.
* @return a new cache which has no backing store file, but contains the
* contents of {@code treeId}.
* @throws IOException
* one or more trees not available from the ObjectReader.
* @since 4.2
*/
public static DirCache read(ObjectReader reader, AnyObjectId treeId)
throws IOException {
DirCache d = newInCore();
DirCacheBuilder b = d.builder();
b.addTree(null, DirCacheEntry.STAGE_0, reader, treeId);
b.finish();
return d;
}
/**
* Create a new in-core index representation and read an index from disk.
* <p>

31
org.eclipse.jgit/src/org/eclipse/jgit/merge/RecursiveMerger.java

@ -57,8 +57,6 @@ import java.util.List;
import java.util.TimeZone;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheBuilder;
import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.NoMergeBaseException;
import org.eclipse.jgit.internal.JGitText;
@ -70,7 +68,6 @@ import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.filter.RevFilter;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.EmptyTreeIterator;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.WorkingTreeIterator;
/**
@ -181,7 +178,7 @@ public class RecursiveMerger extends ResolveMerger {
WorkingTreeIterator oldWTreeIt = workingTreeIterator;
workingTreeIterator = null;
try {
dircache = dircacheFromTree(currentBase.getTree());
dircache = DirCache.read(reader, currentBase.getTree());
inCore = true;
List<RevCommit> parents = new ArrayList<RevCommit>();
@ -256,30 +253,4 @@ public class RecursiveMerger extends ResolveMerger {
new Date((time + 1) * 1000L),
TimeZone.getTimeZone("GMT+0000")); //$NON-NLS-1$
}
/**
* Create a new in memory dircache which has the same content as a given
* tree.
*
* @param treeId
* the tree which should be used to fill the dircache
* @return a new in memory dircache
* @throws IOException
*/
private DirCache dircacheFromTree(ObjectId treeId) throws IOException {
DirCache ret = DirCache.newInCore();
DirCacheBuilder aBuilder = ret.builder();
try (TreeWalk atw = new TreeWalk(reader)) {
atw.addTree(treeId);
atw.setRecursive(true);
while (atw.next()) {
DirCacheEntry e = new DirCacheEntry(atw.getRawPath());
e.setFileMode(atw.getFileMode(0));
e.setObjectId(atw.getObjectId(0));
aBuilder.add(e);
}
}
aBuilder.finish();
return ret;
}
}

8
org.eclipse.jgit/src/org/eclipse/jgit/transport/PushCertificateStore.java

@ -65,7 +65,6 @@ import java.util.Map;
import java.util.NoSuchElementException;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheBuilder;
import org.eclipse.jgit.dircache.DirCacheEditor;
import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
import org.eclipse.jgit.dircache.DirCacheEntry;
@ -448,13 +447,10 @@ public class PushCertificateStore implements AutoCloseable {
}
private DirCache newDirCache() throws IOException {
DirCache dc = DirCache.newInCore();
if (commit != null) {
DirCacheBuilder b = dc.builder();
b.addTree(new byte[0], DirCacheEntry.STAGE_0, reader, commit.getTree());
b.finish();
return DirCache.read(reader, commit.getTree());
}
return dc;
return DirCache.newInCore();
}
private ObjectId saveCert(ObjectInserter inserter, DirCache dc,

Loading…
Cancel
Save