Merge remote branch 'jonas/master'
[nbgit.git] / src / org / nbgit / client / CheckoutBuilder.java
blob57a5c8d152355d952fddfd6ef3299ba675b43460
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.eclipse.jgit.dircache.DirCache;
48 import org.eclipse.jgit.dircache.DirCacheEntry;
49 import org.eclipse.jgit.lib.FileMode;
50 import org.eclipse.jgit.lib.GitIndex;
51 import org.eclipse.jgit.lib.ObjectId;
52 import org.eclipse.jgit.lib.Repository;
53 import org.eclipse.jgit.lib.Tree;
54 import org.eclipse.jgit.lib.TreeEntry;
56 /**
57 * Build a checkout of files from a revision.
59 public class CheckoutBuilder extends ClientBuilder {
61 private static final String BACKUP_EXT = ".orig";
62 private final HashMap<RevisionEntry, File> fileMappings = new HashMap<RevisionEntry, File>();
63 private boolean backup;
64 private Tree tree;
65 private DirCache index;
67 private CheckoutBuilder(Repository repository) {
68 super(repository);
71 /**
72 * Create builder for repository.
74 * @param repository to use for the builder.
75 * @return the builder.
77 public static CheckoutBuilder create(Repository repository) {
78 return new CheckoutBuilder(repository);
81 /**
82 * Create builder from working directory.
84 * @param workDir of the repository.
85 * @return the builder.
86 * @throws IOException if loading the repository fails.
88 public static CheckoutBuilder create(File workDir) throws IOException {
89 return create(toRepository(workDir));
92 /**
93 * Set revision to check out.
95 * @param revision to checkout.
96 * @return the builder.
97 * @throws IOException if the builder fails to load the revision.
98 * @throws IllegalArgumentException if the builder fails to resolve
99 * the revision.
101 public CheckoutBuilder revision(String revision)
102 throws IOException, IllegalArgumentException {
103 tree = repository.mapTree(revision);
104 if (tree == null)
105 throw new IllegalArgumentException(revision);
106 return this;
110 * Set file to be checked out to a specific destination.
112 * @param file to be checked out.
113 * @param destination where the file should be checked out.
114 * @return the builder.
115 * @throws FileNotFoundException if the file cannot be resolved.
116 * @throws IOException if checking of file existance fails.
118 public CheckoutBuilder file(File file, File destination)
119 throws IOException, FileNotFoundException {
120 String path = toPath(file);
121 ObjectId blobId = null;
122 int modeBits = 0;
124 if (tree != null) {
125 TreeEntry entry = tree.findBlobMember(path);
126 if (entry != null) {
127 blobId = entry.getId();
128 modeBits = entry.getMode().getBits();
130 } else {
131 if (index == null)
132 index = DirCache.read(repository);
133 DirCacheEntry entry = index.getEntry(path);
134 if (entry != null) {
135 blobId = entry.getObjectId();
136 modeBits = entry.getRawMode();
139 if (blobId == null)
140 throw new FileNotFoundException(path);
141 fileMappings.put(RevisionEntry.create(path, blobId, modeBits), destination);
142 return this;
146 * Set files to be checked out. The destination of the files will be the
147 * path of the file, which means their path relative to the repository's
148 * working directory.
150 * @param files to be checked out.
151 * @return the builder.
152 * @throws FileNotFoundException if the file cannot be resolved.
153 * @throws IOException if checking of file existance fails.
155 public CheckoutBuilder files(Collection<File> files)
156 throws IOException, FileNotFoundException {
157 for (File file : files) {
158 file(file, file);
160 return this;
164 * Whether to backup existing files that would otherwise be overwritten.
166 * @param backup files?
167 * @return the builder.
169 public CheckoutBuilder backup(boolean backup) {
170 this.backup = backup;
171 return this;
175 * Perform the checkout. Non-existing files added before a revision
176 * was set will be ignored.
178 * @throws IOException if the checkout fails.
180 public void checkout() throws IOException {
181 for (Map.Entry<RevisionEntry, File> mapping : fileMappings.entrySet()) {
182 RevisionEntry entry = mapping.getKey();
183 File file = mapping.getValue();
184 if (backup)
185 backupFile(file);
186 checkoutEntry(entry.getObjectId(), entry.getModeBits(), file);
190 private void backupFile(File file) throws IOException {
191 String extension = BACKUP_EXT;
193 for (int i = 0; i < 1024; i++) {
194 File backupFile = new File(file.getAbsolutePath() + extension);
195 if (!backupFile.exists()) {
196 file.renameTo(backupFile);
197 break;
199 extension = "." + i + BACKUP_EXT;
204 * Code originally from GitIndex.
206 private void checkoutEntry(GitIndex.Entry e, File file) throws IOException {
207 file.delete();
208 file.getParentFile().mkdirs();
210 FileChannel channel = new FileOutputStream(file).getChannel();
211 try {
212 byte[] bytes = repository.openBlob(e.getObjectId()).getBytes();
213 ByteBuffer buffer = ByteBuffer.wrap(bytes);
214 if (channel.write(buffer) != bytes.length)
215 throw new IOException("Could not write file " + file);
216 } finally {
217 channel.close();
219 setExecutable(file, FileMode.EXECUTABLE_FILE.equals(e.getModeBits()));