Browse Source

Add support for reseting on directories

Reset command should works recursively and allows reset all changed
files in given directory.

Bug: 348524
Change-Id: I441db34f226be36548c61cef77958995971498de
Signed-off-by: Dariusz Luksza <dariusz@luksza.org>
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
stable-1.1
Dariusz Luksza 14 years ago committed by Chris Aniszczyk
parent
commit
1d1f572771
  1. 46
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java
  2. 15
      org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java

46
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java

@ -93,6 +93,16 @@ public class ResetCommandTest extends RepositoryTestCase {
git = new Git(db); git = new Git(db);
initialCommit = git.commit().setMessage("initial commit").call(); initialCommit = git.commit().setMessage("initial commit").call();
// create nested file
File dir = new File(db.getWorkTree(), "dir");
FileUtils.mkdir(dir);
File nestedFile = new File(dir, "b.txt");
FileUtils.createNewFile(nestedFile);
PrintWriter nesterFileWriter = new PrintWriter(nestedFile);
nesterFileWriter.print("content");
nesterFileWriter.flush();
// create file // create file
indexFile = new File(db.getWorkTree(), "a.txt"); indexFile = new File(db.getWorkTree(), "a.txt");
FileUtils.createNewFile(indexFile); FileUtils.createNewFile(indexFile);
@ -101,8 +111,9 @@ public class ResetCommandTest extends RepositoryTestCase {
writer.flush(); writer.flush();
// add file and commit it // add file and commit it
git.add().addFilepattern("a.txt").call(); git.add().addFilepattern("dir").addFilepattern("a.txt").call();
secondCommit = git.commit().setMessage("adding a.txt").call(); secondCommit = git.commit().setMessage("adding a.txt and dir/b.txt")
.call();
prestage = DirCache.read(db.getIndexFile(), db.getFS()).getEntry( prestage = DirCache.read(db.getIndexFile(), db.getFS()).getEntry(
indexFile.getName()); indexFile.getName());
@ -110,7 +121,9 @@ public class ResetCommandTest extends RepositoryTestCase {
// modify file and add to index // modify file and add to index
writer.print("new content"); writer.print("new content");
writer.close(); writer.close();
git.add().addFilepattern("a.txt").call(); nesterFileWriter.print("new content");
nesterFileWriter.close();
git.add().addFilepattern("a.txt").addFilepattern("dir").call();
// create a file not added to the index // create a file not added to the index
untrackedFile = new File(db.getWorkTree(), untrackedFile = new File(db.getWorkTree(),
@ -220,6 +233,33 @@ public class ResetCommandTest extends RepositoryTestCase {
assertFalse(inIndex(untrackedFile.getName())); assertFalse(inIndex(untrackedFile.getName()));
} }
@Test
public void testPathsResetOnDirs() throws Exception {
setupRepository();
DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS())
.getEntry("dir/b.txt");
assertNotNull(preReset);
git.add().addFilepattern(untrackedFile.getName()).call();
// 'dir/b.txt' has already been modified in setupRepository
git.reset().addPath("dir").call();
DirCacheEntry postReset = DirCache.read(db.getIndexFile(), db.getFS())
.getEntry("dir/b.txt");
assertNotNull(postReset);
Assert.assertNotSame(preReset.getObjectId(), postReset.getObjectId());
// check that HEAD hasn't moved
ObjectId head = db.resolve(Constants.HEAD);
assertTrue(head.equals(secondCommit));
// check if files still exist
assertTrue(untrackedFile.exists());
assertTrue(inHead("dir/b.txt"));
assertTrue(inIndex("dir/b.txt"));
}
@Test @Test
public void testPathsResetWithRef() throws Exception { public void testPathsResetWithRef() throws Exception {
setupRepository(); setupRepository();

15
org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java

@ -56,6 +56,7 @@ import org.eclipse.jgit.dircache.DirCacheEditor;
import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.RefUpdate;
@ -267,6 +268,7 @@ public class ResetCommand extends GitCommand<Ref> {
tw.addTree(new DirCacheIterator(dc)); tw.addTree(new DirCacheIterator(dc));
tw.addTree(commit.getTree()); tw.addTree(commit.getTree());
tw.setFilter(PathFilterGroup.createFromStrings(filepaths)); tw.setFilter(PathFilterGroup.createFromStrings(filepaths));
tw.setRecursive(true);
while (tw.next()) { while (tw.next()) {
final String path = tw.getPathString(); final String path = tw.getPathString();
@ -276,13 +278,18 @@ public class ResetCommand extends GitCommand<Ref> {
if (tree == null) if (tree == null)
// file is not in the commit, remove from index // file is not in the commit, remove from index
edit.add(new DirCacheEditor.DeletePath(path)); edit.add(new DirCacheEditor.DeletePath(path));
else { else { // revert index to commit
// revert index to commit // it seams that there is concurrent access to tree
// variable, therefore we need to keep references to
// entryFileMode and entryObjectId in local
// variables
final FileMode entryFileMode = tree.getEntryFileMode();
final ObjectId entryObjectId = tree.getEntryObjectId();
edit.add(new DirCacheEditor.PathEdit(path) { edit.add(new DirCacheEditor.PathEdit(path) {
@Override @Override
public void apply(DirCacheEntry ent) { public void apply(DirCacheEntry ent) {
ent.setFileMode(tree.getEntryFileMode()); ent.setFileMode(entryFileMode);
ent.setObjectId(tree.getEntryObjectId()); ent.setObjectId(entryObjectId);
ent.setLastModified(0); ent.setLastModified(0);
} }
}); });

Loading…
Cancel
Save