Browse Source

Deprecate checkoutEntry variant that accepts File

Entries should only be written to the working tree managed by the
Repository. Simplify callers by passing only the entry and computing
the work tree location inside of the checkoutEntry method.

Change-Id: I574e41280d0407f1853fda12f4bd0d30f75d74e7
stable-3.6
Shawn Pearce 10 years ago committed by Shawn Pearce
parent
commit
75b4a23748
  1. 4
      org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java
  2. 7
      org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java
  3. 4
      org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java
  4. 55
      org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java
  5. 25
      org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java

4
org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java

@ -281,7 +281,7 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
@Test @Test
public void testIsModifiedSymlinkAsFile() throws Exception { public void testIsModifiedSymlinkAsFile() throws Exception {
File f = writeTrashFile("symlink", "content"); writeTrashFile("symlink", "content");
Git git = new Git(db); Git git = new Git(db);
db.getConfig().setString(ConfigConstants.CONFIG_CORE_SECTION, null, db.getConfig().setString(ConfigConstants.CONFIG_CORE_SECTION, null,
ConfigConstants.CONFIG_KEY_SYMLINKS, "false"); ConfigConstants.CONFIG_KEY_SYMLINKS, "false");
@ -292,7 +292,7 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
DirCacheEntry dce = db.readDirCache().getEntry("symlink"); DirCacheEntry dce = db.readDirCache().getEntry("symlink");
dce.setFileMode(FileMode.SYMLINK); dce.setFileMode(FileMode.SYMLINK);
ObjectReader objectReader = db.newObjectReader(); ObjectReader objectReader = db.newObjectReader();
DirCacheCheckout.checkoutEntry(db, f, dce, objectReader); DirCacheCheckout.checkoutEntry(db, dce, objectReader);
FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
.getConfig().get(WorkingTreeOptions.KEY)); .getConfig().get(WorkingTreeOptions.KEY));

7
org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java

@ -43,7 +43,6 @@
*/ */
package org.eclipse.jgit.api; package org.eclipse.jgit.api;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -81,7 +80,6 @@ import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup; import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
import org.eclipse.jgit.util.FileUtils;
/** /**
* Checkout a branch to the working tree. * Checkout a branch to the working tree.
@ -455,11 +453,8 @@ public class CheckoutCommand extends GitCommand<Ref> {
} }
private void checkoutPath(DirCacheEntry entry, ObjectReader reader) { private void checkoutPath(DirCacheEntry entry, ObjectReader reader) {
File file = new File(repo.getWorkTree(), entry.getPathString());
File parentDir = file.getParentFile();
try { try {
FileUtils.mkdirs(parentDir, true); DirCacheCheckout.checkoutEntry(repo, entry, reader);
DirCacheCheckout.checkoutEntry(repo, file, entry, reader);
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException(MessageFormat.format( throw new JGitInternalException(MessageFormat.format(
JGitText.get().checkoutConflictWithFile, JGitText.get().checkoutConflictWithFile,

4
org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java

@ -42,7 +42,6 @@
*/ */
package org.eclipse.jgit.api; package org.eclipse.jgit.api;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.text.MessageFormat; import java.text.MessageFormat;
@ -367,8 +366,7 @@ public class StashApplyCommand extends GitCommand<ObjectId> {
private void checkoutPath(DirCacheEntry entry, ObjectReader reader) { private void checkoutPath(DirCacheEntry entry, ObjectReader reader) {
try { try {
File file = new File(repo.getWorkTree(), entry.getPathString()); DirCacheCheckout.checkoutEntry(repo, entry, reader);
DirCacheCheckout.checkoutEntry(repo, file, entry, reader);
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException(MessageFormat.format( throw new JGitInternalException(MessageFormat.format(
JGitText.get().checkoutConflictWithFile, JGitText.get().checkoutConflictWithFile,

55
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java vendored

@ -447,19 +447,9 @@ public class DirCacheCheckout {
removeEmptyParents(file); removeEmptyParents(file);
for (String path : updated.keySet()) { for (String path : updated.keySet()) {
// ... create/overwrite this file ...
file = new File(repo.getWorkTree(), path);
if (!file.getParentFile().mkdirs()) {
// ignore
}
DirCacheEntry entry = dc.getEntry(path); DirCacheEntry entry = dc.getEntry(path);
if (!FileMode.GITLINK.equals(entry.getRawMode()))
// submodules are handled with separate operations checkoutEntry(repo, entry, objectReader);
if (FileMode.GITLINK.equals(entry.getRawMode()))
continue;
checkoutEntry(repo, file, entry, objectReader);
} }
// commit the index builder - a new index is persisted // commit the index builder - a new index is persisted
@ -1158,8 +1148,7 @@ public class DirCacheCheckout {
* *
* @param repository * @param repository
* @param f * @param f
* the file to be modified. The parent directory for this file * this parameter is ignored.
* has to exist already
* @param entry * @param entry
* the entry containing new mode and content * the entry containing new mode and content
* @throws IOException * @throws IOException
@ -1190,19 +1179,51 @@ public class DirCacheCheckout {
* *
* @param repo * @param repo
* @param f * @param f
* the file to be modified. The parent directory for this file * this parameter is ignored.
* has to exist already
* @param entry * @param entry
* the entry containing new mode and content * the entry containing new mode and content
* @param or * @param or
* object reader to use for checkout * object reader to use for checkout
* @throws IOException * @throws IOException
* @deprecated Do not pass File object.
*/ */
@Deprecated
public static void checkoutEntry(final Repository repo, File f, public static void checkoutEntry(final Repository repo, File f,
DirCacheEntry entry, ObjectReader or) throws IOException { DirCacheEntry entry, ObjectReader or) throws IOException {
if (f == null || repo.getWorkTree() == null)
throw new IllegalArgumentException();
if (!f.equals(new File(repo.getWorkTree(), entry.getPathString())))
throw new IllegalArgumentException();
checkoutEntry(repo, entry, or);
}
/**
* Updates the file in the working tree with content and mode from an entry
* in the index. The new content is first written to a new temporary file in
* the same directory as the real file. Then that new file is renamed to the
* final filename.
*
* <p>
* TODO: this method works directly on File IO, we may need another
* abstraction (like WorkingTreeIterator). This way we could tell e.g.
* Eclipse that Files in the workspace got changed
* </p>
*
* @param repo
* repository managing the destination work tree.
* @param entry
* the entry containing new mode and content
* @param or
* object reader to use for checkout
* @throws IOException
* @since 3.6
*/
public static void checkoutEntry(Repository repo, DirCacheEntry entry,
ObjectReader or) throws IOException {
ObjectLoader ol = or.open(entry.getObjectId()); ObjectLoader ol = or.open(entry.getObjectId());
File f = new File(repo.getWorkTree(), entry.getPathString());
File parentDir = f.getParentFile(); File parentDir = f.getParentFile();
parentDir.mkdirs(); FileUtils.mkdirs(parentDir, true);
FS fs = repo.getFS(); FS fs = repo.getFS();
WorkingTreeOptions opt = repo.getConfig().get(WorkingTreeOptions.KEY); WorkingTreeOptions opt = repo.getConfig().get(WorkingTreeOptions.KEY);
if (entry.getFileMode() == FileMode.SYMLINK if (entry.getFileMode() == FileMode.SYMLINK

25
org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java

@ -77,7 +77,6 @@ import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.IndexWriteException; import org.eclipse.jgit.errors.IndexWriteException;
import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.NoWorkTreeException; import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
@ -90,7 +89,6 @@ import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.WorkingTreeIterator; import org.eclipse.jgit.treewalk.WorkingTreeIterator;
import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.TemporaryBuffer; import org.eclipse.jgit.util.TemporaryBuffer;
/** /**
@ -309,13 +307,6 @@ public class ResolveMerger extends ThreeWayMerger {
} }
private void checkout() throws NoWorkTreeException, IOException { private void checkout() throws NoWorkTreeException, IOException {
for (Map.Entry<String, DirCacheEntry> entry : toBeCheckedOut
.entrySet()) {
File f = new File(db.getWorkTree(), entry.getKey());
createDir(f.getParentFile());
DirCacheCheckout.checkoutEntry(db, f, entry.getValue(), reader);
modifiedFiles.add(entry.getKey());
}
// Iterate in reverse so that "folder/file" is deleted before // Iterate in reverse so that "folder/file" is deleted before
// "folder". Otherwise this could result in a failing path because // "folder". Otherwise this could result in a failing path because
// of a non-empty directory, for which delete() would fail. // of a non-empty directory, for which delete() would fail.
@ -328,18 +319,10 @@ public class ResolveMerger extends ThreeWayMerger {
MergeFailureReason.COULD_NOT_DELETE); MergeFailureReason.COULD_NOT_DELETE);
modifiedFiles.add(fileName); modifiedFiles.add(fileName);
} }
} for (Map.Entry<String, DirCacheEntry> entry : toBeCheckedOut
.entrySet()) {
private void createDir(File f) throws IOException { DirCacheCheckout.checkoutEntry(db, entry.getValue(), reader);
if (!db.getFS().isDirectory(f) && !f.mkdirs()) { modifiedFiles.add(entry.getKey());
File p = f;
while (p != null && !db.getFS().exists(p))
p = p.getParentFile();
if (p == null || db.getFS().isDirectory(p))
throw new IOException(JGitText.get().cannotCreateDirectory);
FileUtils.delete(p);
if (!f.mkdirs())
throw new IOException(JGitText.get().cannotCreateDirectory);
} }
} }

Loading…
Cancel
Save