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



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

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

*/ */
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;
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.
} }


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, file, entry, reader);
DirCacheCheckout.checkoutEntry(repo, entry, reader);
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException(MessageFormat.format( throw new JGitInternalException(MessageFormat.format(
JGitText.get().checkoutConflictWithFile, JGitText.get().checkoutConflictWithFile,

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

*/ */
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;




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, file, entry, reader);
DirCacheCheckout.checkoutEntry(repo, entry, reader);
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException(MessageFormat.format( throw new JGitInternalException(MessageFormat.format(
JGitText.get().checkoutConflictWithFile, JGitText.get().checkoutConflictWithFile,

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

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

// 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 // commit the index builder - a new index is persisted
* *
* @param repository * @param repository
* @param f * @param f
* the file to be modified. The parent directory for this file
* has to exist already
* this parameter is ignored.
* @param entry * @param entry
* the entry containing new mode and content * the entry containing new mode and content
* @throws IOException * @throws IOException
* *
* @param repo * @param repo
* @param f * @param f
* the file to be modified. The parent directory for this file
* has to exist already
* this parameter is ignored.
* @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

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

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


/** /**
} }


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.
MergeFailureReason.COULD_NOT_DELETE); MergeFailureReason.COULD_NOT_DELETE);
modifiedFiles.add(fileName); 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