合并后 JGit 记录奇怪的行为
JGit log strange behavior after merge
在日志命令中发现了一个奇怪的行为(错误?)。
下面的测试创建一个 repo,创建一个分支,对创建的分支和 master 进行一些提交,然后将 master 合并到创建的分支。合并后,它会尝试计算分支和主分支之间的提交数。因为master已经合并了--分支不在master之后,即对应的commit count应该是0.
public class JGitBugTest {
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@Test
public void testJGitLogBug() throws Exception {
final String BRANCH_NAME = "TST-2";
final String MASTER_BRANCH_NAME = Constants.MASTER;
File folder = tempFolder.newFolder();
// Create a Git repository
Git api = Git.init().setBare( false ).setDirectory( folder ).call();
Repository repository = api.getRepository();
// Add an initial commit
api.commit().setMessage( "Initial commit" ).call();
// Create a new branch and add some commits to it
api.checkout().setCreateBranch( true ).setName( BRANCH_NAME ).call();
api.commit().setMessage( "TST-2 Added files 1" ).call();
// Add some commits to master branch too
api.checkout().setName( MASTER_BRANCH_NAME ).call();
api.commit().setMessage( "TST-1 Added files 1" ).call();
api.commit().setMessage( "TST-1 Added files 2" ).call();
// If this delay is commented out -- test fails and
// 'behind' is equal to "the number of commits to master - 1".
// Thread.sleep(1000);
// Checkout the branch and merge master to it
api.checkout().setName( BRANCH_NAME ).call();
api.merge()
.include( repository.resolve( MASTER_BRANCH_NAME ) )
.setStrategy( MergeStrategy.RECURSIVE )
.call()
.getNewHead()
.name();
// Calculate the number of commits the branch behind of the master
// It should be zero because we have merged master into the branch already.
Iterable<RevCommit> iterable = api.log()
.add( repository.resolve( MASTER_BRANCH_NAME ) )
.not( repository.resolve( BRANCH_NAME ) )
.call();
int behind = 0;
for( RevCommit commit : iterable ) {
behind++;
}
Assert.assertEquals( 0, behind );
}
}
上述测试失败,behind
得出 master 中的提交数减 1。
此外,如果第 43 行中的 'sleep' 未被注释——错误将消失,并且 'behind' 等于 0。
我做错了什么?是 JGit 库中的错误还是我的代码中的错误?
运行 Windows 上的代码,我可以重现你描述的内容。
在我看来,这像是 JGit 中的一个错误。我推荐给 open a bugzilla or post your findings to the mailing list.
在日志命令中发现了一个奇怪的行为(错误?)。
下面的测试创建一个 repo,创建一个分支,对创建的分支和 master 进行一些提交,然后将 master 合并到创建的分支。合并后,它会尝试计算分支和主分支之间的提交数。因为master已经合并了--分支不在master之后,即对应的commit count应该是0.
public class JGitBugTest {
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@Test
public void testJGitLogBug() throws Exception {
final String BRANCH_NAME = "TST-2";
final String MASTER_BRANCH_NAME = Constants.MASTER;
File folder = tempFolder.newFolder();
// Create a Git repository
Git api = Git.init().setBare( false ).setDirectory( folder ).call();
Repository repository = api.getRepository();
// Add an initial commit
api.commit().setMessage( "Initial commit" ).call();
// Create a new branch and add some commits to it
api.checkout().setCreateBranch( true ).setName( BRANCH_NAME ).call();
api.commit().setMessage( "TST-2 Added files 1" ).call();
// Add some commits to master branch too
api.checkout().setName( MASTER_BRANCH_NAME ).call();
api.commit().setMessage( "TST-1 Added files 1" ).call();
api.commit().setMessage( "TST-1 Added files 2" ).call();
// If this delay is commented out -- test fails and
// 'behind' is equal to "the number of commits to master - 1".
// Thread.sleep(1000);
// Checkout the branch and merge master to it
api.checkout().setName( BRANCH_NAME ).call();
api.merge()
.include( repository.resolve( MASTER_BRANCH_NAME ) )
.setStrategy( MergeStrategy.RECURSIVE )
.call()
.getNewHead()
.name();
// Calculate the number of commits the branch behind of the master
// It should be zero because we have merged master into the branch already.
Iterable<RevCommit> iterable = api.log()
.add( repository.resolve( MASTER_BRANCH_NAME ) )
.not( repository.resolve( BRANCH_NAME ) )
.call();
int behind = 0;
for( RevCommit commit : iterable ) {
behind++;
}
Assert.assertEquals( 0, behind );
}
}
上述测试失败,behind
得出 master 中的提交数减 1。
此外,如果第 43 行中的 'sleep' 未被注释——错误将消失,并且 'behind' 等于 0。
我做错了什么?是 JGit 库中的错误还是我的代码中的错误?
运行 Windows 上的代码,我可以重现你描述的内容。
在我看来,这像是 JGit 中的一个错误。我推荐给 open a bugzilla or post your findings to the mailing list.