All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.intellij.vcs.log.graph.impl.permanent.DuplicateParentFixer Maven / Gradle / Ivy

/*
 * 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 com.intellij.vcs.log.graph.impl.permanent;


import com.intellij.util.containers.ContainerUtil;
import com.intellij.vcs.log.graph.GraphCommit;
import org.jetbrains.annotations.NotNull;

import java.util.*;

public class DuplicateParentFixer {

  public static  AbstractList> fixDuplicateParentCommits(final List> finalCommits) {
    return new AbstractList>() {
      @Override
      public GraphCommit get(int index) {
        return fixParentsDuplicate(finalCommits.get(index));
      }

      @Override
      public int size() {
        return finalCommits.size();
      }
    };
  }

  private static class DelegateGraphCommit implements GraphCommit {
    @NotNull private final GraphCommit myDelegate;

    @NotNull private final List myParents;

    private DelegateGraphCommit(@NotNull GraphCommit delegate, @NotNull List parents) {
      myDelegate = delegate;
      myParents = parents;
    }

    @NotNull
    @Override
    public CommitId getId() {
      return myDelegate.getId();
    }

    @NotNull
    @Override
    public List getParents() {
      return myParents;
    }

    @Override
    public long getTimestamp() {
      return myDelegate.getTimestamp();
    }
  }

  @NotNull
  private static  GraphCommit fixParentsDuplicate(@NotNull GraphCommit commit) {
    List parents = commit.getParents();
    if (parents.size() <= 1) return commit;

    if (parents.size() == 2) {
      CommitId commitId0 = parents.get(0);
      if (!commitId0.equals(parents.get(1))) {
        return commit;
      }
      else {
        return new DelegateGraphCommit(commit, Collections.singletonList(commitId0));
      }
    }

    Set allParents = new HashSet(parents);
    if (parents.size() == allParents.size()) return commit;

    List correctParents = ContainerUtil.newArrayList();
    for (CommitId commitId : parents) {
      if (allParents.remove(commitId)) {
        correctParents.add(commitId);
      }
    }

    return new DelegateGraphCommit(commit, correctParents);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy