diff options
author | Robin Rosenberg <robin.rosenberg@dewire.com> | 2012-05-30 15:57:04 -0400 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org <gerrit@eclipse.org> | 2012-05-30 15:57:04 -0400 |
commit | 629e7cd926779871e9c0d6306e4c184ac67da864 (patch) | |
tree | 08e0b106f0ebb818ee68f5cbbfbbfe93f6ee0cfb /org.eclipse.jgit | |
parent | 875247e24b3b5cfa97cad9be3ab2766c0506c1c4 (diff) | |
parent | 531db82f38c307ee4ade9ac5c61229bc539f7d87 (diff) | |
download | jgit-629e7cd926779871e9c0d6306e4c184ac67da864.tar.gz jgit-629e7cd926779871e9c0d6306e4c184ac67da864.zip |
Merge "Retain file length and mod time when doing a mixed reset" into stable-2.0
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java | 49 |
1 files changed, 43 insertions, 6 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java index 422056bd6d..0af684e7eb 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java @@ -49,9 +49,10 @@ import java.util.LinkedList; import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.dircache.DirCache; -import org.eclipse.jgit.dircache.DirCacheBuilder; import org.eclipse.jgit.dircache.DirCacheCheckout; import org.eclipse.jgit.dircache.DirCacheEditor; +import org.eclipse.jgit.dircache.DirCacheEditor.DeletePath; +import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit; import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.internal.JGitText; @@ -64,6 +65,7 @@ import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryState; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.treewalk.AbstractTreeIterator; import org.eclipse.jgit.treewalk.CanonicalTreeParser; import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.filter.PathFilterGroup; @@ -313,14 +315,49 @@ public class ResetCommand extends GitCommand<Ref> { private void resetIndex(RevCommit commit) throws IOException { DirCache dc = repo.lockDirCache(); + TreeWalk walk = null; try { - dc.clear(); - DirCacheBuilder dcb = dc.builder(); - dcb.addTree(new byte[0], 0, repo.newObjectReader(), - commit.getTree()); - dcb.commit(); + DirCacheEditor editor = dc.editor(); + + walk = new TreeWalk(repo); + walk.addTree(commit.getTree()); + walk.addTree(new DirCacheIterator(dc)); + walk.setRecursive(true); + + while (walk.next()) { + AbstractTreeIterator cIter = walk.getTree(0, + AbstractTreeIterator.class); + if (cIter == null) { + editor.add(new DeletePath(walk.getPathString())); + continue; + } + + final DirCacheEntry entry = new DirCacheEntry(walk.getRawPath()); + entry.setFileMode(cIter.getEntryFileMode()); + entry.setObjectIdFromRaw(cIter.idBuffer(), cIter.idOffset()); + + DirCacheIterator dcIter = walk.getTree(1, + DirCacheIterator.class); + if (dcIter != null && dcIter.idEqual(cIter)) { + DirCacheEntry indexEntry = dcIter.getDirCacheEntry(); + entry.setLastModified(indexEntry.getLastModified()); + entry.setLength(indexEntry.getLength()); + } + + editor.add(new PathEdit(entry) { + + @Override + public void apply(DirCacheEntry ent) { + ent.copyMetaData(entry); + } + }); + } + + editor.commit(); } finally { dc.unlock(); + if (walk != null) + walk.release(); } } |