Merge "AddCommand: Cleanup conditional logic"

This commit is contained in:
Christian Halstrick 2015-12-28 08:39:06 -05:00 committed by Gerrit Code Review @ Eclipse.org
commit 2fdce1ef8c
1 changed files with 52 additions and 47 deletions

View File

@ -43,6 +43,9 @@
*/ */
package org.eclipse.jgit.api; package org.eclipse.jgit.api;
import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
import static org.eclipse.jgit.lib.FileMode.GITLINK;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collection; import java.util.Collection;
@ -58,8 +61,8 @@
import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.treewalk.FileTreeIterator; import org.eclipse.jgit.treewalk.FileTreeIterator;
@ -135,15 +138,12 @@ public DirCache call() throws GitAPIException, NoFilepatternException {
throw new NoFilepatternException(JGitText.get().atLeastOnePatternIsRequired); throw new NoFilepatternException(JGitText.get().atLeastOnePatternIsRequired);
checkCallable(); checkCallable();
DirCache dc = null; DirCache dc = null;
boolean addAll = false; boolean addAll = filepatterns.contains("."); //$NON-NLS-1$
if (filepatterns.contains(".")) //$NON-NLS-1$
addAll = true;
try (ObjectInserter inserter = repo.newObjectInserter(); try (ObjectInserter inserter = repo.newObjectInserter();
final TreeWalk tw = new TreeWalk(repo)) { final TreeWalk tw = new TreeWalk(repo)) {
tw.setOperationType(OperationType.CHECKIN_OP); tw.setOperationType(OperationType.CHECKIN_OP);
dc = repo.lockDirCache(); dc = repo.lockDirCache();
DirCacheIterator c;
DirCacheBuilder builder = dc.builder(); DirCacheBuilder builder = dc.builder();
tw.addTree(new DirCacheBuildIterator(builder)); tw.addTree(new DirCacheBuildIterator(builder));
@ -158,55 +158,60 @@ public DirCache call() throws GitAPIException, NoFilepatternException {
String lastAddedFile = null; String lastAddedFile = null;
while (tw.next()) { while (tw.next()) {
String path = tw.getPathString(); DirCacheIterator c = tw.getTree(0, DirCacheIterator.class);
WorkingTreeIterator f = tw.getTree(1, WorkingTreeIterator.class); WorkingTreeIterator f = tw.getTree(1, WorkingTreeIterator.class);
if (tw.getTree(0, DirCacheIterator.class) == null && if (c == null && f != null && f.isEntryIgnored()) {
f != null && f.isEntryIgnored()) {
// file is not in index but is ignored, do nothing // file is not in index but is ignored, do nothing
continue;
} else if (c == null && update) {
// Only update of existing entries was requested.
continue;
} }
String path = tw.getPathString();
if (path.equals(lastAddedFile)) {
// In case of an existing merge conflict the // In case of an existing merge conflict the
// DirCacheBuildIterator iterates over all stages of // DirCacheBuildIterator iterates over all stages of
// this path, we however want to add only one // this path, we however want to add only one
// new DirCacheEntry per path. // new DirCacheEntry per path.
else if (!(path.equals(lastAddedFile))) { continue;
if (!(update && tw.getTree(0, DirCacheIterator.class) == null)) { }
c = tw.getTree(0, DirCacheIterator.class);
if (f != null) { // the file exists if (f == null) { // working tree file does not exist
if (c != null
&& (!update || GITLINK == c.getEntryFileMode())) {
builder.add(c.getDirCacheEntry());
}
continue;
}
if (c != null && c.getDirCacheEntry() != null
&& c.getDirCacheEntry().isAssumeValid()) {
// Index entry is marked assume valid. Even though
// the user specified the file to be added JGit does
// not consider the file for addition.
builder.add(c.getDirCacheEntry());
continue;
}
long sz = f.getEntryLength(); long sz = f.getEntryLength();
DirCacheEntry entry = new DirCacheEntry(path); DirCacheEntry entry = new DirCacheEntry(path);
if (c == null || c.getDirCacheEntry() == null
|| !c.getDirCacheEntry().isAssumeValid()) {
FileMode mode = f.getIndexFileMode(c); FileMode mode = f.getIndexFileMode(c);
entry.setFileMode(mode); entry.setFileMode(mode);
if (FileMode.GITLINK != mode) { if (GITLINK != mode) {
entry.setLength(sz); entry.setLength(sz);
entry.setLastModified(f entry.setLastModified(f.getEntryLastModified());
.getEntryLastModified()); long len = f.getEntryContentLength();
long contentSize = f try (InputStream in = f.openEntryStream()) {
.getEntryContentLength(); ObjectId id = inserter.insert(OBJ_BLOB, len, in);
InputStream in = f.openEntryStream(); entry.setObjectId(id);
try {
entry.setObjectId(inserter.insert(
Constants.OBJ_BLOB, contentSize, in));
} finally {
in.close();
} }
} else } else {
entry.setObjectId(f.getEntryObjectId()); entry.setObjectId(f.getEntryObjectId());
}
builder.add(entry); builder.add(entry);
lastAddedFile = path; lastAddedFile = path;
} else {
builder.add(c.getDirCacheEntry());
}
} else if (c != null
&& (!update || FileMode.GITLINK == c
.getEntryFileMode()))
builder.add(c.getDirCacheEntry());
}
}
} }
inserter.flush(); inserter.flush();
builder.commit(); builder.commit();