/*
 * Copyright 2018 the original author or authors.
 *
 * 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.gradle.internal.execution.steps;

import com.google.common.collect.ImmutableSortedMap;
import org.gradle.internal.change.ChangeDetectorVisitor;
import org.gradle.internal.execution.CurrentSnapshotResult;
import org.gradle.internal.execution.IncrementalContext;
import org.gradle.internal.execution.Step;
import org.gradle.internal.execution.UnitOfWork;
import org.gradle.internal.execution.history.AfterPreviousExecutionState;
import org.gradle.internal.execution.history.changes.OutputFileChanges;
import org.gradle.internal.fingerprint.CurrentFileCollectionFingerprint;
import org.gradle.internal.fingerprint.FileCollectionFingerprint;

import java.util.Optional;

public class StoreSnapshotsStep<C extends IncrementalContext> implements Step<C, CurrentSnapshotResult> {
    private final Step<? super C, ? extends CurrentSnapshotResult> delegate;

    public StoreSnapshotsStep(
        Step<? super C, ? extends CurrentSnapshotResult> delegate
    ) {
        this.delegate = delegate;
    }

    @Override
    public CurrentSnapshotResult execute(C context) {
        CurrentSnapshotResult result = delegate.execute(context);
        ImmutableSortedMap<String, CurrentFileCollectionFingerprint> finalOutputs = result.getFinalOutputs();
        context.getBeforeExecutionState().ifPresent(beforeExecutionState -> {
            boolean successful = result.getOutcome().isSuccessful();
            // Do not store snapshots if there was a failure, but the outputs didn't change
            if (successful
                || didChangeOutput(context.getAfterPreviousExecutionState(), finalOutputs)) {
                UnitOfWork work = context.getWork();
                work.getExecutionHistoryStore().store(
                    work.getIdentity(),
                    result.getOriginMetadata(),
                    beforeExecutionState.getImplementation(),
                    beforeExecutionState.getAdditionalImplementations(),
                    beforeExecutionState.getInputProperties(),
                    beforeExecutionState.getInputFileProperties(),
                    finalOutputs,
                    successful
                );
            }
        });
        return result;
    }

    @SuppressWarnings("OptionalUsedAsFieldOrParameterType")
    private static boolean didChangeOutput(Optional<AfterPreviousExecutionState> afterPreviousExecutionState, ImmutableSortedMap<String, CurrentFileCollectionFingerprint> current) {
        // If there is no previous state, then we do have output changes
        if (!afterPreviousExecutionState.isPresent()) {
            return true;
        }

        // If there are different output properties compared to the previous execution, then we do have output changes
        ImmutableSortedMap<String, FileCollectionFingerprint> previous = afterPreviousExecutionState.get().getOutputFileProperties();
        if (!previous.keySet().equals(current.keySet())) {
            return true;
        }

        // Otherwise do deep compare of outputs
        ChangeDetectorVisitor visitor = new ChangeDetectorVisitor();
        OutputFileChanges changes = new OutputFileChanges(previous, current, true);
        changes.accept(visitor);
        return visitor.hasAnyChanges();
    }
}

@@ -2025,17 +2025,20 @@
      // 
-D
+We d
 o not st
@@ -2045,17 +2045,19 @@
 ore 
-snapshots
+the history
  if 
@@ -2079,39 +2079,285 @@
 lure
-, but the outputs didn't change
+ and the outputs did not change, since then the next execution can be incremental.%0A            // For example the current execution fails because of a compile failure and for the next execution the source file is fixed, so only the one changed source file needs to be compiled.
 %0A   

Update subprojects/execution/src/main/java/org/gradle/internal/execution/steps/StoreSnapshotsStep.java