package jitstatic;

/*-
 * #%L
 * jitstatic
 * %%
 * Copyright (C) 2017 H.Hegardt
 * %%
 * 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.
 * #L%
 */

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;

import jitstatic.hosted.FileObjectIdStore;
import jitstatic.util.Pair;

public class CorruptedSourceException extends Exception {

	private static final long serialVersionUID = 5606961605803953513L;

	public CorruptedSourceException(final List<Pair<Set<Ref>, List<Pair<FileObjectIdStore, Exception>>>> errors) {
		super(compileMessage(errors));
	}

	public static String compileMessage(final List<Pair<Set<Ref>, List<Pair<FileObjectIdStore, Exception>>>> errors) {
		final StringBuilder sb = new StringBuilder();
		for (final Pair<Set<Ref>, List<Pair<FileObjectIdStore, Exception>>> p : errors) {
			sb.append("Error in branch");
			if (p.getLeft().size() > 1) {
				sb.append("es");
			}
			sb.append(" ");
			sb.append(p.getLeft().stream().map(r -> r.getName()).collect(Collectors.joining(", ")))
					.append(System.lineSeparator());
			for (final Pair<FileObjectIdStore, Exception> pe : p.getRight()) {
				final FileObjectIdStore fileInfo = pe.getLeft();
				sb.append("ID: ").append(fileInfo == null ? "null" : ObjectId.toString(fileInfo.getObjectId())).append(" Name: ")
						.append((fileInfo == null ? "FILE_NAME_MISSING" : fileInfo.getFileName())).append(" ")
						.append(" Reason: ")
						.append((pe.getRight() == null ? "null" : pe.getRight().getMessage())).append(System.lineSeparator());
			}
		}
		return sb.toString();
	}

	@Override
	public Throwable fillInStackTrace() {
		return this;
	}
}

@@ -1252,33 +1252,42 @@
 , Exception%3E%3E%3E%3E 
-e
+allBranchE
 rrors) %7B%0A%09%09final
@@ -1401,13 +1401,33 @@
 %3E%3E%3E 
-p : e
+branchErrors : allBranchE
 rror
@@ -1471,23 +1471,89 @@
 %0A%09%09%09
-if (p.getLeft()
+final Set%3CRef%3E branchFileErrors = branchErrors.getLeft();%0A%09%09%09if (branchFileErrors
 .siz
@@ -1625,19 +1625,24 @@
 end(
-p.getLeft()
+branchFileErrors
 .str
@@ -1795,14 +1795,32 @@
 on%3E 
-pe : p
+fileError : branchErrors
 .get
@@ -1869,18 +1869,25 @@
 eInfo = 
-pe
+fileError
 .getLeft
@@ -2143,18 +2143,25 @@
 append((
-pe
+fileError
 .getRigh
@@ -2183,18 +2183,25 @@
 null%22 : 
-pe
+fileError
 .getRigh

Clean up