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
tags/v3.6.0.201412230720-r
Shawn Pearce 9 years ago
parent
commit
75b4a23748

+ 2
- 2
org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java View File

@@ -281,7 +281,7 @@ public class FileTreeIteratorTest extends RepositoryTestCase {

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

FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
.getConfig().get(WorkingTreeOptions.KEY));

+ 1
- 6
org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java View File

@@ -43,7 +43,6 @@
*/
package org.eclipse.jgit.api;

import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
@@ -81,7 +80,6 @@ import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
import org.eclipse.jgit.util.FileUtils;

/**
* Checkout a branch to the working tree.
@@ -455,11 +453,8 @@ public class CheckoutCommand extends GitCommand<Ref> {
}

private void checkoutPath(DirCacheEntry entry, ObjectReader reader) {
File file = new File(repo.getWorkTree(), entry.getPathString());
File parentDir = file.getParentFile();
try {
FileUtils.mkdirs(parentDir, true);
DirCacheCheckout.checkoutEntry(repo, file, entry, reader);
DirCacheCheckout.checkoutEntry(repo, entry, reader);
} catch (IOException e) {
throw new JGitInternalException(MessageFormat.format(
JGitText.get().checkoutConflictWithFile,

+ 1
- 3
org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java View File

@@ -42,7 +42,6 @@
*/
package org.eclipse.jgit.api;

import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;

@@ -367,8 +366,7 @@ public class StashApplyCommand extends GitCommand<ObjectId> {

private void checkoutPath(DirCacheEntry entry, ObjectReader reader) {
try {
File file = new File(repo.getWorkTree(), entry.getPathString());
DirCacheCheckout.checkoutEntry(repo, file, entry, reader);
DirCacheCheckout.checkoutEntry(repo, entry, reader);
} catch (IOException e) {
throw new JGitInternalException(MessageFormat.format(
JGitText.get().checkoutConflictWithFile,

+ 38
- 17
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java View File

@@ -447,19 +447,9 @@ public class DirCacheCheckout {
removeEmptyParents(file);

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);

// submodules are handled with separate operations
if (FileMode.GITLINK.equals(entry.getRawMode()))
continue;

checkoutEntry(repo, file, entry, objectReader);
if (!FileMode.GITLINK.equals(entry.getRawMode()))
checkoutEntry(repo, entry, objectReader);
}

// commit the index builder - a new index is persisted
@@ -1158,8 +1148,7 @@ public class DirCacheCheckout {
*
* @param repository
* @param f
* the file to be modified. The parent directory for this file
* has to exist already
* this parameter is ignored.
* @param entry
* the entry containing new mode and content
* @throws IOException
@@ -1190,19 +1179,51 @@ public class DirCacheCheckout {
*
* @param repo
* @param f
* the file to be modified. The parent directory for this file
* has to exist already
* this parameter is ignored.
* @param entry
* the entry containing new mode and content
* @param or
* object reader to use for checkout
* @throws IOException
* @deprecated Do not pass File object.
*/
@Deprecated
public static void checkoutEntry(final Repository repo, File f,
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());
File f = new File(repo.getWorkTree(), entry.getPathString());
File parentDir = f.getParentFile();
parentDir.mkdirs();
FileUtils.mkdirs(parentDir, true);
FS fs = repo.getFS();
WorkingTreeOptions opt = repo.getConfig().get(WorkingTreeOptions.KEY);
if (entry.getFileMode() == FileMode.SYMLINK

+ 4
- 21
org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java View File

@@ -77,7 +77,6 @@ import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.IndexWriteException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.FileMode;
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.WorkingTreeIterator;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.TemporaryBuffer;

/**
@@ -309,13 +307,6 @@ public class ResolveMerger extends ThreeWayMerger {
}

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
// "folder". Otherwise this could result in a failing path because
// of a non-empty directory, for which delete() would fail.
@@ -328,18 +319,10 @@ public class ResolveMerger extends ThreeWayMerger {
MergeFailureReason.COULD_NOT_DELETE);
modifiedFiles.add(fileName);
}
}

private void createDir(File f) throws IOException {
if (!db.getFS().isDirectory(f) && !f.mkdirs()) {
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);
for (Map.Entry<String, DirCacheEntry> entry : toBeCheckedOut
.entrySet()) {
DirCacheCheckout.checkoutEntry(db, entry.getValue(), reader);
modifiedFiles.add(entry.getKey());
}
}


Loading…
Cancel
Save