<commit_before>/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package git4idea.log

import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.vcs.VcsException
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.containers.ContainerUtil
import com.intellij.vcs.log.Hash
import com.intellij.vcs.log.TimedVcsCommit
import com.intellij.vcs.log.VcsLogFilterCollection
import com.intellij.vcs.log.VcsLogTextFilter
import com.intellij.vcs.log.impl.VcsLogFilterCollectionImpl.VcsLogFilterCollectionBuilder
import com.intellij.vcs.log.util.BekUtil

internal class GitBekParentFixer private constructor(private val incorrectCommits: Set<Hash>) {

  fun fixCommit(commit: TimedVcsCommit): TimedVcsCommit {
    return if (!incorrectCommits.contains(commit.id)) commit
    else reverseParents(commit)
  }

  companion object {
    @JvmStatic
    @Throws(VcsException::class)
    fun prepare(root: VirtualFile, provider: GitLogProvider): GitBekParentFixer {
      return if (!BekUtil.isBekEnabled() || !Registry.`is`("git.log.fix.merge.commits.parents.order")) {
        GitBekParentFixer(emptySet())
      }
      else GitBekParentFixer(getIncorrectCommits(provider, root))
    }
  }
}

private const val MAGIC_TEXT = "Merge remote"
private val MAGIC_FILTER = createVcsLogFilterCollection()

@Throws(VcsException::class)
private fun getIncorrectCommits(provider: GitLogProvider, root: VirtualFile): Set<Hash> {
  val commitsMatchingFilter = provider.getCommitsMatchingFilter(root, MAGIC_FILTER, -1)
  return ContainerUtil.map2Set(commitsMatchingFilter) { timedVcsCommit -> timedVcsCommit.id }
}

private fun reverseParents(commit: TimedVcsCommit): TimedVcsCommit {
  return object : TimedVcsCommit {
    override fun getTimestamp(): Long {
      return commit.timestamp
    }

    override fun getId(): Hash {
      return commit.id
    }

    override fun getParents(): List<Hash> {
      return ContainerUtil.reverse(commit.parents)
    }
  }
}

private fun createVcsLogFilterCollection(): VcsLogFilterCollection {
  val textFilter = object : VcsLogTextFilter {
    override fun matchesCase(): Boolean {
      return false
    }

    override fun isRegex(): Boolean {
      return false
    }

    override fun getText(): String {<commit_msg>[git] inline reverseParents and simplify TimedVcsCommit object using delegation<commit_after>/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package git4idea.log

import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.vcs.VcsException
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.containers.ContainerUtil
import com.intellij.vcs.log.Hash
import com.intellij.vcs.log.TimedVcsCommit
import com.intellij.vcs.log.VcsLogFilterCollection
import com.intellij.vcs.log.VcsLogTextFilter
import com.intellij.vcs.log.impl.VcsLogFilterCollectionImpl.VcsLogFilterCollectionBuilder
import com.intellij.vcs.log.util.BekUtil

internal class GitBekParentFixer private constructor(private val incorrectCommits: Set<Hash>) {

  fun fixCommit(commit: TimedVcsCommit): TimedVcsCommit {
    return if (!incorrectCommits.contains(commit.id)) commit
    else object : TimedVcsCommit by commit {
      override fun getParents(): List<Hash> = ContainerUtil.reverse(commit.parents)
    }
  }

  companion object {
    @JvmStatic
    @Throws(VcsException::class)
    fun prepare(root: VirtualFile, provider: GitLogProvider): GitBekParentFixer {
      return if (!BekUtil.isBekEnabled() || !Registry.`is`("git.log.fix.merge.commits.parents.order")) {
        GitBekParentFixer(emptySet())
      }
      else GitBekParentFixer(getIncorrectCommits(provider, root))
    }
  }
}

private const val MAGIC_TEXT = "Merge remote"
private val MAGIC_FILTER = createVcsLogFilterCollection()

@Throws(VcsException::class)
private fun getIncorrectCommits(provider: GitLogProvider, root: VirtualFile): Set<Hash> {
  val commitsMatchingFilter = provider.getCommitsMatchingFilter(root, MAGIC_FILTER, -1)
  return ContainerUtil.map2Set(commitsMatchingFilter) { timedVcsCommit -> timedVcsCommit.id }
}

private fun createVcsLogFilterCollection(): VcsLogFilterCollection {
  val textFilter = object : VcsLogTextFilter {
    override fun matchesCase(): Boolean {
      return false
    }

    override fun isRegex(): Boolean {
      return false
    }

    override fun getText(): String {
{'fraction_non_alphanumeric': 0.08914515833790958, 'fraction_numerical': 0.0054914881933003845, 'mean_word_length': 4.558494404883011, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3951323', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1643, 'n_words': 426}