diff options
author | Shawn Pearce <sop@google.com> | 2014-11-25 13:49:22 -0800 |
---|---|---|
committer | Shawn Pearce <spearce@spearce.org> | 2014-11-27 13:37:12 -0800 |
commit | 75b4a23748772be2e689db12e2d1e7d1aa43e06f (patch) | |
tree | 2473cbf1e70c602815f7c3e44f1db1830afbf841 /org.eclipse.jgit | |
parent | 6814728a821144617e5fe81d54bad8df12ce2586 (diff) | |
download | jgit-75b4a23748772be2e689db12e2d1e7d1aa43e06f.tar.gz jgit-75b4a23748772be2e689db12e2d1e7d1aa43e06f.zip |
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
Diffstat (limited to 'org.eclipse.jgit')
4 files changed, 44 insertions, 47 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java index 0eb25cf11d..1820932286 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java @@ -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, diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java index 5d0ac2f2da..356723db4b 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java @@ -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, diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java index c52833127c..b1c75f263f 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java @@ -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 diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java b/org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java index fb9abf8097..dba89709aa 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java +++ b/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.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()); } } |