aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit
diff options
context:
space:
mode:
authorRobin Rosenberg <robin.rosenberg@dewire.com>2012-05-30 15:56:30 -0400
committerGerrit Code Review @ Eclipse.org <gerrit@eclipse.org>2012-05-30 15:56:30 -0400
commit875247e24b3b5cfa97cad9be3ab2766c0506c1c4 (patch)
tree1e074ad6668f9c1853c49f5f2f2965a4fc3ffef8 /org.eclipse.jgit/src/org/eclipse/jgit
parent59d2ef94709ba29395c58fec454bf846f920e99a (diff)
parentdac66672df0535f61a13273524d46e1e0012ca69 (diff)
downloadjgit-875247e24b3b5cfa97cad9be3ab2766c0506c1c4.tar.gz
jgit-875247e24b3b5cfa97cad9be3ab2766c0506c1c4.zip
Merge "Update smudged entries when writing index" into stable-2.0
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java101
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java4
2 files changed, 102 insertions, 3 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java
index 0d5952df24..9108d9235d 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java
@@ -69,8 +69,11 @@ import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
+import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileSnapshot;
import org.eclipse.jgit.storage.file.LockFile;
+import org.eclipse.jgit.treewalk.FileTreeIterator;
+import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.MutableInteger;
@@ -145,6 +148,30 @@ public class DirCache {
* failures are reported as exceptions and therefore prevent the method from
* returning a partially populated index.
*
+ * @param repository
+ * repository containing the index to read
+ * @return a cache representing the contents of the specified index file (if
+ * it exists) or an empty cache if the file does not exist.
+ * @throws IOException
+ * the index file is present but could not be read.
+ * @throws CorruptObjectException
+ * the index file is using a format or extension that this
+ * library does not support.
+ */
+ public static DirCache read(final Repository repository)
+ throws CorruptObjectException, IOException {
+ final DirCache c = read(repository.getIndexFile(), repository.getFS());
+ c.repository = repository;
+ return c;
+ }
+
+ /**
+ * Create a new in-core index representation and read an index from disk.
+ * <p>
+ * The new index will be read before it is returned to the caller. Read
+ * failures are reported as exceptions and therefore prevent the method from
+ * returning a partially populated index.
+ *
* @param indexLocation
* location of the index file on disk.
* @param fs
@@ -217,6 +244,37 @@ public class DirCache {
* the method from returning a partially populated index. On read failure,
* the lock is released.
*
+ * @param repository
+ * repository containing the index to lock and read
+ * @param indexChangedListener
+ * listener to be informed when DirCache is committed
+ * @return a cache representing the contents of the specified index file (if
+ * it exists) or an empty cache if the file does not exist.
+ * @throws IOException
+ * the index file is present but could not be read, or the lock
+ * could not be obtained.
+ * @throws CorruptObjectException
+ * the index file is using a format or extension that this
+ * library does not support.
+ * @since 2.0
+ */
+ public static DirCache lock(final Repository repository,
+ final IndexChangedListener indexChangedListener)
+ throws CorruptObjectException, IOException {
+ DirCache c = lock(repository.getIndexFile(), repository.getFS(),
+ indexChangedListener);
+ c.repository = repository;
+ return c;
+ }
+
+ /**
+ * Create a new in-core index representation, lock it, and read from disk.
+ * <p>
+ * The new index will be locked and then read before it is returned to the
+ * caller. Read failures are reported as exceptions and therefore prevent
+ * the method from returning a partially populated index. On read failure,
+ * the lock is released.
+ *
* @param indexLocation
* location of the index file on disk.
* @param fs
@@ -272,6 +330,9 @@ public class DirCache {
/** listener to be informed on commit */
private IndexChangedListener indexChangedListener;
+ /** Repository containing this index */
+ private Repository repository;
+
/**
* Create a new in-core index representation.
* <p>
@@ -591,6 +652,13 @@ public class DirCache {
smudge_s = 0;
}
+ // Check if tree is non-null here since calling updateSmudgedEntries
+ // will automatically build it via creating a DirCacheIterator
+ final boolean writeTree = tree != null;
+
+ if (repository != null && entryCnt > 0)
+ updateSmudgedEntries();
+
for (int i = 0; i < entryCnt; i++) {
final DirCacheEntry e = sortedEntries[i];
if (e.mightBeRacilyClean(smudge_s, smudge_ns))
@@ -598,7 +666,7 @@ public class DirCache {
e.write(dos);
}
- if (tree != null) {
+ if (writeTree) {
final TemporaryBuffer bb = new TemporaryBuffer.LocalFile();
tree.write(tmp, bb);
bb.close();
@@ -865,4 +933,35 @@ public class DirCache {
private void registerIndexChangedListener(IndexChangedListener listener) {
this.indexChangedListener = listener;
}
+
+ /**
+ * Update any smudged entries with information from the working tree.
+ *
+ * @throws IOException
+ */
+ private void updateSmudgedEntries() throws IOException {
+ TreeWalk walk = new TreeWalk(repository);
+ try {
+ DirCacheIterator iIter = new DirCacheIterator(this);
+ FileTreeIterator fIter = new FileTreeIterator(repository);
+ walk.addTree(iIter);
+ walk.addTree(fIter);
+ walk.setRecursive(true);
+ while (walk.next()) {
+ iIter = walk.getTree(0, DirCacheIterator.class);
+ if (iIter == null)
+ continue;
+ fIter = walk.getTree(1, FileTreeIterator.class);
+ if (fIter == null)
+ continue;
+ DirCacheEntry entry = iIter.getDirCacheEntry();
+ if (entry.isSmudged() && iIter.idEqual(fIter)) {
+ entry.setLength(fIter.getEntryLength());
+ entry.setLastModified(fIter.getEntryLastModified());
+ }
+ }
+ } finally {
+ walk.release();
+ }
+ }
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
index c70c9b0f85..5d9488ae95 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
@@ -871,7 +871,7 @@ public abstract class Repository {
*/
public DirCache readDirCache() throws NoWorkTreeException,
CorruptObjectException, IOException {
- return DirCache.read(getIndexFile(), getFS());
+ return DirCache.read(this);
}
/**
@@ -903,7 +903,7 @@ public abstract class Repository {
notifyIndexChanged();
}
};
- return DirCache.lock(getIndexFile(), getFS(), l);
+ return DirCache.lock(this, l);
}
static byte[] gitInternalSlash(byte[] bytes) {