Imports System Imports System.Collections Imports System.Collections.Generic Imports System.Text Imports NUnit.Framework Imports Codice.Test Namespace cmtest Friend Class RepositoryChecker Friend Sub New(plasticWkPath As String, gitWkPath As String, wkserverName As String) mPlasticWkPath = plasticWkPath mGitWkPath = gitWkPath mServerName = wkserverName End Sub Public Sub CheckFullRepository() CheckBranchHeads() CheckChangesets() End Sub Private Sub CheckChangesets() Dim plasticChangesets As List(Of Long) = MetadataChecker.GetListOfChangesets(mPlasticWkPath) For Each plasticChangeset As Long In plasticChangesets CheckData(plasticChangeset) MetadataChecker.Check(plasticChangeset, mPlasticWkPath, mGitWkPath) Next End Sub Private Sub CheckBranchHeads() Dim branches As List(Of Branch) = MetadataChecker.GetListOfBranches(mPlasticWkPath) For Each branch As Branch In branches Dim attribute As String = GitHelper.GetGitAttribute(branch.Id, mPlasticWkPath) Dim fields As String() = attribute.Split(":"c) Dim refName As String = fields(0) Dim csetId As Long = Long.Parse(fields(1)) Assert.AreEqual(branch.Changeset, csetId, "The branch is out of sync") Dim foreignRev As String = GitHelper.GetGitEquivalentChangeset(csetId, mPlasticWkPath) Dim gitSha As String = GitHelper.GetReferenceCommit(refName, mGitWkPath) Assert.AreEqual(gitSha, foreignRev, "The branch is out of sync") Next End Sub Private Sub CheckMergeLinks() Dim plasticMergeLinks As List(Of Merge) = MetadataChecker.GetListOfMergeLinks(mPlasticWkPath) Dim mergeLinks As New Dictionary(Of Long, IList(Of String))() For Each plasticMergeLink As Merge In plasticMergeLinks Dim ids As IList(Of String) If Not mergeLinks.TryGetValue(plasticMergeLink.dstId, ids) Then ids = New List(Of String)() mergeLinks(plasticMergeLink.dstId) = ids End If ids.Add(GitHelper.GetGitEquivalentChangeset(plasticMergeLink.srcId, mPlasticWkPath)) Next CheckParentsFromDstMergeLink(mergeLinks) End Sub Private Sub CheckParentsFromDstMergeLink(mergeLinks As Dictionary(Of Long, IList(Of String))) Dim sha As String = Nothing Dim gitMergeLinks As IList(Of String) = Nothing For Each dstId As Long In mergeLinks.Keys sha = GitHelper.GetGitEquivalentChangeset(dstId, mPlasticWkPath) gitMergeLinks = GitHelper.GetMergeLinks(sha, mGitWkPath) CollectionAssert.AreEquivalent(mergeLinks(dstId), gitMergeLinks) Next End Sub Private Sub CheckBranches() Dim branches As List(Of Branch) = MetadataChecker.GetListOfBranches(mPlasticWkPath) ' check branch heads For Each branch As Branch In branches Dim attribute As String = GitHelper.GetGitAttribute(branch.Id, mPlasticWkPath) Dim fields As String() = attribute.Split(":"c) Dim refName As String = fields(0) Dim csetId As Long = Long.Parse(fields(1)) Assert.AreEqual(branch.Changeset, csetId, "The branch is out of sync") Dim foreignRev As String = GitHelper.GetGitEquivalentChangeset(csetId, mPlasticWkPath) Dim gitSha As String = GitHelper.GetReferenceCommit(refName, mGitWkPath) Assert.AreEqual(gitSha, foreignRev, "The branch is out of sync") Next ' check all branches are sync Assert.AreEqual(branches.Count, GitHelper.GetBranches(mGitWkPath).Count, "There are branches that are not synchronized.") End Sub Public Sub CheckFullRepositoryFromDate(dateTime As DateTime) Dim plasticChangesets As List(Of Cs) = MetadataChecker.GetListOfChangesetsFromDate(mPlasticWkPath, dateTime) For Each plasticChangeset As Cs In plasticChangesets CheckData(plasticChangeset.Id) MetadataChecker.Check(plasticChangeset, mPlasticWkPath, mGitWkPath) Next End Sub Private Sub CheckData(plasticChangeset As Long) Dim sha As String = GitHelper.GetGitEquivalentChangeset(plasticChangeset, mPlasticWkPath) If sha Is Nothing Then NUnit.Framework.Assert.Fail("The changeset cs:{0} has not a git mapping", plasticChangeset) End If ' update plastic changeset CmdRunner.ExecuteCommand(String.Format( "cm update . --changeset={0} -wks={1}", plasticChangeset, mServerName), mPlasticWkPath) ' update git changeset GitHelper.Switch(sha, mGitWkPath) ' compare workspace contents CompareWks(mPlasticWkPath, mGitWkPath) End Sub Private Function GetBranches() As IList Dim cmdres As String = CmdRunner.ExecuteCommandWithStringResult(String.Format( "cm find branches on repository '{0}' --format={{name}} --nototal -wks={1}", "imported", mServerName), mPlasticWkPath) Return ResultParser.GetListResults(cmdres, False) End Function Friend Shared Sub CompareWks(plasticwk As String, gitWk As String) Dim comparator As New FolderComparator(plasticwk, gitWk, New String() {Config.PLASTIC_WK_DIR, ".git"}, New String() {".private."}) Dim result As IList = comparator.Compare() If result IsNot Nothing AndAlso result.Count > 0 Then Dim builder As New StringBuilder() builder.AppendLine("Differences comparing Plastic and Git workspaces!!!!!!") For Each folderDiff As FolderComparationResult In result builder.AppendLine(String.Format("{0} ({1})-> {2}", folderDiff.ItemName, folderDiff.Type.ToString(), folderDiff.DifferenceMsg)) Next Throw New Exception(builder.ToString()) End If End Sub Private mPlasticWkPath As String Private mGitWkPath As String Private mServerName As String End Class End Namespace