@ -58,10 +58,8 @@ import org.eclipse.jgit.api.errors.NoMessageException;
import org.eclipse.jgit.api.errors.UnmergedPathsException ;
import org.eclipse.jgit.api.errors.UnmergedPathsException ;
import org.eclipse.jgit.api.errors.WrongRepositoryStateException ;
import org.eclipse.jgit.api.errors.WrongRepositoryStateException ;
import org.eclipse.jgit.dircache.DirCache ;
import org.eclipse.jgit.dircache.DirCache ;
import org.eclipse.jgit.dircache.DirCacheBuildIterator ;
import org.eclipse.jgit.dircache.DirCacheBuilder ;
import org.eclipse.jgit.dircache.DirCacheBuilder ;
import org.eclipse.jgit.dircache.DirCacheEditor ;
import org.eclipse.jgit.dircache.DirCacheEditor.DeletePath ;
import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit ;
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.errors.UnmergedPathException ;
import org.eclipse.jgit.errors.UnmergedPathException ;
@ -298,25 +296,26 @@ public class CommitCommand extends GitCommand<RevCommit> {
throws IOException {
throws IOException {
ObjectInserter inserter = null ;
ObjectInserter inserter = null ;
// get DirCacheEditor to modify the index if required
// get DirCacheBuilder for existing index
DirCacheEditor dcEdito r = index . edito r ( ) ;
DirCacheBuilder existingBuilde r = index . build er( ) ;
// get DirCacheBuilder for newly created in-core index to build a
// get DirCacheBuilder for newly created in-core index to build a
// temporary index for this commit
// temporary index for this commit
DirCache inCoreIndex = DirCache . newInCore ( ) ;
DirCache inCoreIndex = DirCache . newInCore ( ) ;
DirCacheBuilder dc Builder = inCoreIndex . builder ( ) ;
DirCacheBuilder temp Builder = inCoreIndex . builder ( ) ;
onlyProcessed = new boolean [ only . size ( ) ] ;
onlyProcessed = new boolean [ only . size ( ) ] ;
boolean emptyCommit = true ;
boolean emptyCommit = true ;
TreeWalk treeWalk = new TreeWalk ( repo ) ;
TreeWalk treeWalk = new TreeWalk ( repo ) ;
int dcIdx = treeWalk . addTree ( new DirCacheIterator ( index ) ) ;
int dcIdx = treeWalk . addTree ( new DirCacheBuild Iterator ( existingBuilder ) ) ;
int fIdx = treeWalk . addTree ( new FileTreeIterator ( repo ) ) ;
int fIdx = treeWalk . addTree ( new FileTreeIterator ( repo ) ) ;
int hIdx = - 1 ;
int hIdx = - 1 ;
if ( headId ! = null )
if ( headId ! = null )
hIdx = treeWalk . addTree ( new RevWalk ( repo ) . parseTree ( headId ) ) ;
hIdx = treeWalk . addTree ( new RevWalk ( repo ) . parseTree ( headId ) ) ;
treeWalk . setRecursive ( true ) ;
treeWalk . setRecursive ( true ) ;
String lastAddedFile = null ;
while ( treeWalk . next ( ) ) {
while ( treeWalk . next ( ) ) {
String path = treeWalk . getPathString ( ) ;
String path = treeWalk . getPathString ( ) ;
// check if current entry's path matches a specified path
// check if current entry's path matches a specified path
@ -326,11 +325,12 @@ public class CommitCommand extends GitCommand<RevCommit> {
if ( hIdx ! = - 1 )
if ( hIdx ! = - 1 )
hTree = treeWalk . getTree ( hIdx , CanonicalTreeParser . class ) ;
hTree = treeWalk . getTree ( hIdx , CanonicalTreeParser . class ) ;
DirCacheIterator dcTree = treeWalk . getTree ( dcIdx ,
DirCacheIterator . class ) ;
if ( pos > = 0 ) {
if ( pos > = 0 ) {
// include entry in commit
// include entry in commit
DirCacheIterator dcTree = treeWalk . getTree ( dcIdx ,
DirCacheIterator . class ) ;
FileTreeIterator fTree = treeWalk . getTree ( fIdx ,
FileTreeIterator fTree = treeWalk . getTree ( fIdx ,
FileTreeIterator . class ) ;
FileTreeIterator . class ) ;
@ -339,6 +339,13 @@ public class CommitCommand extends GitCommand<RevCommit> {
if ( ! tracked )
if ( ! tracked )
break ;
break ;
// for an unmerged path, DirCacheBuildIterator will yield 3
// entries, we only want to add one
if ( path . equals ( lastAddedFile ) )
continue ;
lastAddedFile = path ;
if ( fTree ! = null ) {
if ( fTree ! = null ) {
// create a new DirCacheEntry with data retrieved from disk
// create a new DirCacheEntry with data retrieved from disk
final DirCacheEntry dcEntry = new DirCacheEntry ( path ) ;
final DirCacheEntry dcEntry = new DirCacheEntry ( path ) ;
@ -371,15 +378,10 @@ public class CommitCommand extends GitCommand<RevCommit> {
}
}
}
}
// update index
// add to existing index
dcEditor . add ( new PathEdit ( path ) {
existingBuilder . add ( dcEntry ) ;
@Override
public void apply ( DirCacheEntry ent ) {
ent . copyMetaData ( dcEntry ) ;
}
} ) ;
// add to temporary in-core index
// add to temporary in-core index
dc Builder. add ( dcEntry ) ;
tempBuilder . add ( dcEntry ) ;
if ( emptyCommit
if ( emptyCommit
& & ( hTree = = null | | ! hTree . idEqual ( fTree ) | | hTree
& & ( hTree = = null | | ! hTree . idEqual ( fTree ) | | hTree
@ -388,9 +390,8 @@ public class CommitCommand extends GitCommand<RevCommit> {
// this is a change
// this is a change
emptyCommit = false ;
emptyCommit = false ;
} else {
} else {
// if no file exists on disk, remove entry from index and
// if no file exists on disk, neither add it to
// don't add it to temporary in-core index
// index nor to temporary in-core index
dcEditor . add ( new DeletePath ( path ) ) ;
if ( emptyCommit & & hTree ! = null )
if ( emptyCommit & & hTree ! = null )
// this is a change
// this is a change
@ -408,8 +409,12 @@ public class CommitCommand extends GitCommand<RevCommit> {
dcEntry . setFileMode ( hTree . getEntryFileMode ( ) ) ;
dcEntry . setFileMode ( hTree . getEntryFileMode ( ) ) ;
// add to temporary in-core index
// add to temporary in-core index
dc Builder. add ( dcEntry ) ;
temp Builder. add ( dcEntry ) ;
}
}
// preserve existing entry in index
if ( dcTree ! = null )
existingBuilder . add ( dcTree . getDirCacheEntry ( ) ) ;
}
}
}
}
@ -425,9 +430,9 @@ public class CommitCommand extends GitCommand<RevCommit> {
throw new JGitInternalException ( JGitText . get ( ) . emptyCommit ) ;
throw new JGitInternalException ( JGitText . get ( ) . emptyCommit ) ;
// update index
// update index
dcEdito r. commit ( ) ;
existingBuilde r. commit ( ) ;
// finish temporary in-core index used for this commit
// finish temporary in-core index used for this commit
dc Builder. finish ( ) ;
temp Builder. finish ( ) ;
return inCoreIndex ;
return inCoreIndex ;
}
}