Issue 20: Make IndexBuilder optionally log operations
[nbgit.git] / src / org / nbgit / client / IndexBuilder.java
blob5b0bd3b40a6e7701a1de8dc5e3f90b8a554a5933
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.IOException;
40 import java.util.Collection;
41 import java.util.HashSet;
42 import org.nbgit.Git;
43 import org.nbgit.OutputLogger;
44 import org.spearce.jgit.lib.GitIndex;
45 import org.spearce.jgit.lib.Repository;
47 /**
48 * Wrapper for JGit's index API.
50 public class IndexBuilder {
52 private static String ADDING = "A"; // NOI18N
53 private static String DELETING = "D"; // NOI18N
54 private static String MOVING = "R"; // NOI18N
55 private final Repository repository;
56 private final HashSet<File> delete = new HashSet<File>();
57 private final HashSet<File> add = new HashSet<File>();
58 private OutputLogger logger;
60 private IndexBuilder(Repository repository) {
61 this.repository = repository;
64 public static IndexBuilder create(Repository repository) throws IOException {
65 return new IndexBuilder(repository);
68 public static IndexBuilder create(File workDir) throws IOException {
69 Repository repository = Git.getInstance().getRepository(workDir);
70 return create(repository);
73 public IndexBuilder add(File file) {
74 log(ADDING, file);
75 add.add(file);
76 return this;
79 public IndexBuilder addAll(Collection<File> files) {
80 for (File file : files)
81 add(file);
82 return this;
85 public IndexBuilder move(File src, File dst) {
86 log(MOVING, src, dst);
87 add.add(dst);
88 delete.add(src);
89 return this;
92 public IndexBuilder delete(File file) {
93 log(DELETING, file);
94 delete.add(file);
95 return this;
98 public IndexBuilder deleteAll(Collection<File> files) {
99 for (File file : files)
100 delete(file);
101 return this;
104 public void write() throws IOException {
105 GitIndex index = repository.getIndex();
106 for (File file : add) {
107 GitIndex.Entry entry = index.add(repository.getWorkDir(), file);
108 entry.setAssumeValid(false);
110 for (File file : delete)
111 index.remove(repository.getWorkDir(), file);
112 index.write();
113 add.clear();
114 delete.clear();
117 public IndexBuilder log(OutputLogger logger) {
118 this.logger = logger;
119 return this;
122 private void log(String string, File... files) {
123 if (logger != null && files.length > 0 &&
124 (add.size() + delete.size()) < OutputLogger.MAX_LINES_TO_PRINT) {
125 string += " " + toPath(files[0]); //NOI18N
126 if (files.length > 1)
127 string += " -> " + toPath(files[1]); //NOI18N
128 logger.output(string);
132 private String toPath(File file) {
133 return Repository.stripWorkDir(repository.getWorkDir(), file);