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
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
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
;
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
.filter
.PathFilterGroup
;
64 * A class used to execute a {@code Add} command. It has setters for all
65 * supported options and arguments of this command and a {@link #call()} method
66 * to finally execute the command. Each instance of this class should only be
67 * used for one invocation of the command (means: one call to {@link #call()})
69 * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-add.html"
70 * >Git documentation about Add</a>
72 public class AddCommand
extends GitCommand
<DirCache
> {
74 private Collection
<String
> filepatterns
;
80 public AddCommand(Repository repo
) {
82 filepatterns
= new LinkedList
<String
>();
87 * File to add content from. Also a leading directory name (e.g.
88 * dir to add dir/file1 and dir/file2) can be given to add all
89 * files in the directory, recursively. Fileglobs (e.g. *.c) are
91 * @return {@code this}
93 public AddCommand
addFilepattern(String filepattern
) {
95 filepatterns
.add(filepattern
);
100 * Executes the {@code Add} command. Each instance of this class should only
101 * be used for one invocation of the command. Don't call this method twice
104 * @return the DirCache after Add
106 public DirCache
call() throws NoFilepatternException
{
108 if (filepatterns
.isEmpty())
109 throw new NoFilepatternException(JGitText
.get().atLeastOnePatternIsRequired
);
112 boolean addAll
= false;
113 if (filepatterns
.contains("."))
117 dc
= DirCache
.lock(repo
);
118 ObjectWriter ow
= new ObjectWriter(repo
);
121 DirCacheBuilder builder
= dc
.builder();
122 final TreeWalk tw
= new TreeWalk(repo
);
124 tw
.addTree(new DirCacheBuildIterator(builder
));
125 FileTreeIterator fileTreeIterator
= new FileTreeIterator(repo
);
126 tw
.addTree(fileTreeIterator
);
127 tw
.setRecursive(true);
129 tw
.setFilter(PathFilterGroup
.createFromStrings(filepatterns
));
131 String lastAddedFile
= null;
134 String path
= tw
.getPathString();
136 final File file
= new File(repo
.getWorkDir(), path
);
137 FileTreeIterator f
= tw
.getTree(1, FileTreeIterator
.class);
138 if (tw
.getTree(0, DirCacheIterator
.class) == null &&
139 f
!= null && f
.isEntryIgnored()) {
140 // file is not in index but is ignored, do nothing
142 // In case of an existing merge conflict the
143 // DirCacheBuildIterator iterates over all stages of
144 // this path, we however want to add only one
145 // new DirCacheEntry per path.
146 else if (!(path
.equals(lastAddedFile
))) {
147 if (f
!= null) { // the file exists
148 DirCacheEntry entry
= new DirCacheEntry(path
);
149 entry
.setLength((int)f
.getEntryLength());
150 entry
.setLastModified(f
.getEntryLastModified());
151 entry
.setFileMode(f
.getEntryFileMode());
152 entry
.setObjectId(ow
.writeBlob(file
));
155 lastAddedFile
= path
;
157 c
= tw
.getTree(0, DirCacheIterator
.class);
158 builder
.add(c
.getDirCacheEntry());
164 } catch (IOException e
) {
165 throw new JGitInternalException(
166 JGitText
.get().exceptionCaughtDuringExecutionOfAddCommand
, e
);