Issue 20: Optimize CheckoutBuilder's final checkout by caching blob info
[nbgit.git] / src / org / nbgit / client / CheckoutBuilder.java
blob311504677b7a781fc06889a36fa1a750e9c5a4dd
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2009 Jonas Fonseca <fonseca@diku.dk>
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html. See the License for the
12 * specific language governing permissions and limitations under the
13 * License. When distributing the software, include this License Header
14 * Notice in each file.
16 * This particular file is subject to the "Classpath" exception as provided
17 * by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the
19 * License Header, with the fields enclosed by brackets [] replaced by
20 * your own identifying information:
21 * "Portions Copyrighted [year] [name of copyright owner]"
23 * Contributor(s):
25 * If you wish your version of this file to be governed by only the CDDL
26 * or only the GPL Version 2, indicate your decision by adding
27 * "[Contributor] elects to include this software in this distribution
28 * under the [CDDL or GPL Version 2] license." If you do not indicate a
29 * single choice of license, a recipient has the option to distribute
30 * your version of this file under either the CDDL, the GPL Version 2 or
31 * to extend the choice of license to its licensees as provided above.
32 * However, if you add GPL Version 2 code and therefore, elected the GPL
33 * Version 2 license, then the option applies only if the new code is
34 * made subject to such option by the copyright holder.
36 package org.nbgit.client;
38 import java.io.File;
39 import java.io.FileNotFoundException;
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42 import java.nio.ByteBuffer;
43 import java.nio.channels.FileChannel;
44 import java.util.Collection;
45 import java.util.HashMap;
46 import java.util.Map;
47 import org.spearce.jgit.lib.FileMode;
48 import org.spearce.jgit.lib.GitIndex;
49 import org.spearce.jgit.lib.ObjectId;
50 import org.spearce.jgit.lib.Repository;
51 import org.spearce.jgit.lib.Tree;
52 import org.spearce.jgit.lib.TreeEntry;
54 /**
55 * Build a checkout of files from a revision.
57 public class CheckoutBuilder extends ClientBuilder {
59 private static final String BACKUP_EXT = ".orig";
60 private final HashMap<RevisionEntry, File> fileMappings = new HashMap<RevisionEntry, File>();
61 private boolean backup;
62 private Tree tree;
63 private GitIndex index;
65 private CheckoutBuilder(Repository repository) {
66 super(repository);
69 /**
70 * Create builder for repository.
72 * @param repository to use for the builder.
73 * @return the builder.
75 public static CheckoutBuilder create(Repository repository) {
76 return new CheckoutBuilder(repository);
79 /**
80 * Create builder from working directory.
82 * @param workDir of the repository.
83 * @return the builder.
84 * @throws IOException if loading the repository fails.
86 public static CheckoutBuilder create(File workDir) throws IOException {
87 return create(toRepository(workDir));
90 /**
91 * Set revision to check out.
93 * @param revision to checkout.
94 * @return the builder.
95 * @throws IOException if the builder fails to load the revision.
96 * @throws IllegalArgumentException if the builder fails to resolve
97 * the revision.
99 public CheckoutBuilder revision(String revision)
100 throws IOException, IllegalArgumentException {
101 tree = repository.mapTree(revision);
102 if (tree == null)
103 throw new IllegalArgumentException(revision);
104 return this;
108 * Set file to be checked out to a specific destination.
110 * @param file to be checked out.
111 * @param destination where the file should be checked out.
112 * @return the builder.
113 * @throws FileNotFoundException if the file cannot be resolved.
114 * @throws IOException if checking of file existance fails.
116 public CheckoutBuilder file(File file, File destination)
117 throws IOException, FileNotFoundException {
118 String path = toPath(file);
119 ObjectId blobId = null;
120 int modeBits = 0;
122 if (tree != null) {
123 TreeEntry entry = tree.findBlobMember(path);
124 if (entry != null) {
125 blobId = entry.getId();
126 modeBits = entry.getMode().getBits();
128 } else {
129 if (index == null)
130 index = repository.getIndex();
131 GitIndex.Entry entry = index.getEntry(path);
132 if (entry != null) {
133 blobId = entry.getObjectId();
134 modeBits = entry.getModeBits();
137 if (blobId == null)
138 throw new FileNotFoundException(path);
139 fileMappings.put(RevisionEntry.create(path, blobId, modeBits), destination);
140 return this;
144 * Set files to be checked out. The destination of the files will be the
145 * path of the file, which means their path relative to the repository's
146 * working directory.
148 * @param files to be checked out.
149 * @return the builder.
150 * @throws FileNotFoundException if the file cannot be resolved.
151 * @throws IOException if checking of file existance fails.
153 public CheckoutBuilder files(Collection<File> files)
154 throws IOException, FileNotFoundException {
155 for (File file : files) {
156 file(file, file);
158 return this;
162 * Whether to backup existing files that would otherwise be overwritten.
164 * @param backup files?
165 * @return the builder.
167 public CheckoutBuilder backup(boolean backup) {
168 this.backup = backup;
169 return this;
173 * Perform the checkout. Non-existing files added before a revision
174 * was set will be ignored.
176 * @throws IOException if the checkout fails.
178 public void checkout() throws IOException {
179 for (Map.Entry<RevisionEntry, File> mapping : fileMappings.entrySet()) {
180 RevisionEntry entry = mapping.getKey();
181 File file = mapping.getValue();
182 if (backup)
183 backupFile(file);
184 checkoutEntry(entry.getObjectId(), entry.getModeBits(), file);
188 private void backupFile(File file) throws IOException {
189 String extension = BACKUP_EXT;
191 for (int i = 0; i < 1024; i++) {
192 File backupFile = new File(file.getAbsolutePath() + extension);
193 if (!backupFile.exists()) {
194 file.renameTo(backupFile);
195 break;
197 extension = "." + i + BACKUP_EXT;
202 * Code originally from GitIndex.
204 private void checkoutEntry(ObjectId blobId, int modeBits, File file) throws IOException {
205 file.delete();
206 file.getParentFile().mkdirs();
208 FileChannel channel = new FileOutputStream(file).getChannel();
209 try {
210 byte[] bytes = repository.openBlob(blobId).getBytes();
211 ByteBuffer buffer = ByteBuffer.wrap(bytes);
212 if (channel.write(buffer) != bytes.length)
213 throw new IOException("Could not write file " + file);
214 } finally {
215 channel.close();
217 setExecutable(file, FileMode.EXECUTABLE_FILE.equals(modeBits));