/*
 * Copyright 2000-2013 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 org.hanuna.gitalk.data;

import com.intellij.openapi.util.Computable;
import com.intellij.vcs.log.CommitParents;
import com.intellij.vcs.log.Ref;
import com.intellij.vcs.log.VcsCommitDetails;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.List;

/**
 * Attaches the block of latest commits, which was read from the VCS, to the existing log structure.
 *
 * @author Kirill Likhodedov
 */
public class VcsLogJoiner {

  /**
   *
   * @param savedLog       currently available part of the log.
   * @param firstBlock     the first n commits read from the VCS.
   * @param refs           all references (branches) of the repository.
   * @param wholeLogGetter the function which will read the whole log from the file, if the part stored in {@code savedLog} is not enough.
   * @return New commits attached to the existing log structure.
   */
  @NotNull
  public List<? extends CommitParents> addCommits(@NotNull List<CommitParents> savedLog,
                                                  @NotNull List<? extends CommitParents> firstBlock, @NotNull Collection<Ref> refs,
                                                  @NotNull Computable<List<CommitParents>> wholeLogGetter) {
    // TODO
    return firstBlock;
  }

}

@@ -696,29 +696,108 @@
 lij.
-vcs.log.CommitParents
+util.containers.HashSet;%0Aimport com.intellij.vcs.log.CommitParents;%0Aimport com.intellij.vcs.log.Hash
 ;%0Aim
@@ -964,17 +964,68 @@
 .util.Li
-s
+nkedList;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0A/**%0A
@@ -1962,37 +1962,1489 @@
     
-// TODO%0A    return firstBlock
+int unsafeBlockSize = getFirstSafeIndex(savedLog, firstBlock, refs);%0A    if (unsafeBlockSize == -1) %7B // firstBlock not enough%0A      //TODO%0A    %7D%0A%0A    // TODO%0A    return firstBlock;%0A  %7D%0A%0A  /**%0A   *%0A   * @param savedLog       currently available part of the log.%0A   * @param firstBlock     the first n commits read from the VCS.%0A   * @param refs           all references (branches) of the repository.%0A   * @return -1 if not enough commits in firstBlock%0A   */%0A  private static int getFirstSafeIndex(@NotNull List%3CCommitParents%3E savedLog,%0A                                       @NotNull List%3C? extends CommitParents%3E firstBlock,%0A                                       @NotNull Collection%3CRef%3E refs) %7B%0A    Set%3CHash%3E allUnresolvedLinkedHashes = new HashSet%3CHash%3E();%0A    for (Ref ref: refs) %7B%0A      allUnresolvedLinkedHashes.add(ref.getCommitHash());%0A    %7D%0A    for (CommitParents commit : firstBlock) %7B%0A      allUnresolvedLinkedHashes.addAll(commit.getParents());%0A    %7D%0A    for (CommitParents commit : firstBlock) %7B%0A      allUnresolvedLinkedHashes.remove(commit.getHash());%0A    %7D%0A    return getLastSearchIndex(savedLog, allUnresolvedLinkedHashes);%0A  %7D%0A%0A  private static int getLastSearchIndex(@NotNull List%3CCommitParents%3E commits, @NotNull Set%3CHash%3E searchHashes) %7B%0A    int lastIndex = 0;%0A    for (CommitParents commit : commits) %7B%0A      if (searchHashes.size() == 0) %7B%0A        return lastIndex;%0A      %7D%0A      searchHashes.remove(commit.getHash());%0A      lastIndex++;%0A    %7D%0A    return -1
 ;%0A  

add check what all new commits were loaded