Browse Source

Merge "AddCommand: Cleanup conditional logic"

stable-4.3
Christian Halstrick 9 years ago committed by Gerrit Code Review @ Eclipse.org
parent
commit
2fdce1ef8c
  1. 103
      org.eclipse.jgit/src/org/eclipse/jgit/api/AddCommand.java

103
org.eclipse.jgit/src/org/eclipse/jgit/api/AddCommand.java

@ -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.DirCacheBuilder;
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 class AddCommand extends GitCommand<DirCache> {
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 class AddCommand extends GitCommand<DirCache> {
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;
} }
// In case of an existing merge conflict the
// DirCacheBuildIterator iterates over all stages of String path = tw.getPathString();
// this path, we however want to add only one if (path.equals(lastAddedFile)) {
// new DirCacheEntry per path. // In case of an existing merge conflict the
else if (!(path.equals(lastAddedFile))) { // DirCacheBuildIterator iterates over all stages of
if (!(update && tw.getTree(0, DirCacheIterator.class) == null)) { // this path, we however want to add only one
c = tw.getTree(0, DirCacheIterator.class); // new DirCacheEntry per path.
if (f != null) { // the file exists continue;
long sz = f.getEntryLength(); }
DirCacheEntry entry = new DirCacheEntry(path);
if (c == null || c.getDirCacheEntry() == null if (f == null) { // working tree file does not exist
|| !c.getDirCacheEntry().isAssumeValid()) { if (c != null
FileMode mode = f.getIndexFileMode(c); && (!update || GITLINK == c.getEntryFileMode())) {
entry.setFileMode(mode); builder.add(c.getDirCacheEntry());
}
if (FileMode.GITLINK != mode) { continue;
entry.setLength(sz); }
entry.setLastModified(f
.getEntryLastModified()); if (c != null && c.getDirCacheEntry() != null
long contentSize = f && c.getDirCacheEntry().isAssumeValid()) {
.getEntryContentLength(); // Index entry is marked assume valid. Even though
InputStream in = f.openEntryStream(); // the user specified the file to be added JGit does
try { // not consider the file for addition.
entry.setObjectId(inserter.insert( builder.add(c.getDirCacheEntry());
Constants.OBJ_BLOB, contentSize, in)); continue;
} finally { }
in.close();
} long sz = f.getEntryLength();
} else DirCacheEntry entry = new DirCacheEntry(path);
entry.setObjectId(f.getEntryObjectId()); FileMode mode = f.getIndexFileMode(c);
builder.add(entry); entry.setFileMode(mode);
lastAddedFile = path;
} else { if (GITLINK != mode) {
builder.add(c.getDirCacheEntry()); entry.setLength(sz);
} entry.setLastModified(f.getEntryLastModified());
long len = f.getEntryContentLength();
} else if (c != null try (InputStream in = f.openEntryStream()) {
&& (!update || FileMode.GITLINK == c ObjectId id = inserter.insert(OBJ_BLOB, len, in);
.getEntryFileMode())) entry.setObjectId(id);
builder.add(c.getDirCacheEntry());
} }
} else {
entry.setObjectId(f.getEntryObjectId());
} }
builder.add(entry);
lastAddedFile = path;
} }
inserter.flush(); inserter.flush();
builder.commit(); builder.commit();

Loading…
Cancel
Save