Merge changes I39bfefee,I47795987,I70d120fb,I58cc5e01,I96bee7b9
[jgit.git] / org.eclipse.jgit / src / org / eclipse / jgit / api / AddCommand.java
blobf7d4da4d5a923db8119876aa45f3c182e4a9fb4e
1 /*
2 * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
3 * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com>
4 * and other copyright owners as documented in the project's IP log.
6 * This program and the accompanying materials are made available
7 * under the terms of the Eclipse Distribution License v1.0 which
8 * accompanies this distribution, is reproduced below, and is
9 * available at http://www.eclipse.org/org/documents/edl-v10.php
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
17 * - Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials provided
23 * with the distribution.
25 * - Neither the name of the Eclipse Foundation, Inc. nor the
26 * names of its contributors may be used to endorse or promote
27 * products derived from this software without specific prior
28 * written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 package org.eclipse.jgit.api;
46 import java.io.File;
47 import java.io.IOException;
48 import java.util.Collection;
49 import java.util.LinkedList;
51 import org.eclipse.jgit.JGitText;
52 import org.eclipse.jgit.dircache.DirCache;
53 import org.eclipse.jgit.dircache.DirCacheBuildIterator;
54 import org.eclipse.jgit.dircache.DirCacheBuilder;
55 import org.eclipse.jgit.dircache.DirCacheEntry;
56 import org.eclipse.jgit.dircache.DirCacheIterator;
57 import org.eclipse.jgit.lib.ObjectWriter;
58 import org.eclipse.jgit.lib.Repository;
59 import org.eclipse.jgit.treewalk.FileTreeIterator;
60 import org.eclipse.jgit.treewalk.TreeWalk;
61 import org.eclipse.jgit.treewalk.WorkingTreeIterator;
62 import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
64 /**
65 * A class used to execute a {@code Add} command. It has setters for all
66 * supported options and arguments of this command and a {@link #call()} method
67 * to finally execute the command. Each instance of this class should only be
68 * used for one invocation of the command (means: one call to {@link #call()})
70 * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-add.html"
71 * >Git documentation about Add</a>
73 public class AddCommand extends GitCommand<DirCache> {
75 private Collection<String> filepatterns;
77 private WorkingTreeIterator workingTreeIterator;
79 private boolean update = false;
81 /**
83 * @param repo
85 public AddCommand(Repository repo) {
86 super(repo);
87 filepatterns = new LinkedList<String>();
90 /**
91 * @param filepattern
92 * File to add content from. Also a leading directory name (e.g.
93 * dir to add dir/file1 and dir/file2) can be given to add all
94 * files in the directory, recursively. Fileglobs (e.g. *.c) are
95 * not yet supported.
96 * @return {@code this}
98 public AddCommand addFilepattern(String filepattern) {
99 checkCallable();
100 filepatterns.add(filepattern);
101 return this;
105 * Allow clients to provide their own implementation of a FileTreeIterator
106 * @param f
107 * @return {@code this}
109 public AddCommand setWorkingTreeIterator(WorkingTreeIterator f) {
110 workingTreeIterator = f;
111 return this;
115 * Executes the {@code Add} command. Each instance of this class should only
116 * be used for one invocation of the command. Don't call this method twice
117 * on an instance.
119 * @return the DirCache after Add
121 public DirCache call() throws NoFilepatternException {
123 if (filepatterns.isEmpty())
124 throw new NoFilepatternException(JGitText.get().atLeastOnePatternIsRequired);
125 checkCallable();
126 DirCache dc = null;
127 boolean addAll = false;
128 if (filepatterns.contains("."))
129 addAll = true;
131 try {
132 dc = repo.lockDirCache();
133 ObjectWriter ow = new ObjectWriter(repo);
134 DirCacheIterator c;
136 DirCacheBuilder builder = dc.builder();
137 final TreeWalk tw = new TreeWalk(repo);
138 tw.reset();
139 tw.addTree(new DirCacheBuildIterator(builder));
140 if (workingTreeIterator == null)
141 workingTreeIterator = new FileTreeIterator(repo);
142 tw.addTree(workingTreeIterator);
143 tw.setRecursive(true);
144 if (!addAll)
145 tw.setFilter(PathFilterGroup.createFromStrings(filepatterns));
147 String lastAddedFile = null;
149 while (tw.next()) {
150 String path = tw.getPathString();
152 final File file = new File(repo.getWorkTree(), path);
153 WorkingTreeIterator f = tw.getTree(1, WorkingTreeIterator.class);
154 if (tw.getTree(0, DirCacheIterator.class) == null &&
155 f != null && f.isEntryIgnored()) {
156 // file is not in index but is ignored, do nothing
158 // In case of an existing merge conflict the
159 // DirCacheBuildIterator iterates over all stages of
160 // this path, we however want to add only one
161 // new DirCacheEntry per path.
162 else if (!(path.equals(lastAddedFile))) {
163 if (!(update && tw.getTree(0, DirCacheIterator.class) == null)) {
164 if (f != null) { // the file exists
165 DirCacheEntry entry = new DirCacheEntry(path);
166 entry.setLength((int)f.getEntryLength());
167 entry.setLastModified(f.getEntryLastModified());
168 entry.setFileMode(f.getEntryFileMode());
169 entry.setObjectId(ow.writeBlob(file));
171 builder.add(entry);
172 lastAddedFile = path;
173 } else if (!update){
174 c = tw.getTree(0, DirCacheIterator.class);
175 builder.add(c.getDirCacheEntry());
180 builder.commit();
181 setCallable(false);
182 } catch (IOException e) {
183 throw new JGitInternalException(
184 JGitText.get().exceptionCaughtDuringExecutionOfAddCommand, e);
185 } finally {
186 if (dc != null)
187 dc.unlock();
190 return dc;
194 * @param update
195 * If set to true, the command only matches {@code filepattern}
196 * against already tracked files in the index rather than the
197 * working tree. That means that it will never stage new files,
198 * but that it will stage modified new contents of tracked files
199 * and that it will remove files from the index if the
200 * corresponding files in the working tree have been removed.
201 * In contrast to the git command line a {@code filepattern} must
202 * exist also if update is set to true as there is no
203 * concept of a working directory here.
205 * @return {@code this}
207 public AddCommand setUpdate(boolean update) {
208 this.update = update;
209 return this;
213 * @return is the parameter update is set
215 public boolean isUpdate() {
216 return update;