From 6219ae014ec832e33a755c38c9e732b85fd9b8f1 Mon Sep 17 00:00:00 2001
From: Georg Seibt <seibt@fim.uni-passau.de>
Date: Thu, 20 Oct 2016 16:55:10 +0200
Subject: [PATCH] add some more checks to getMergeBase, merge, and getParents

---
 .../fim/seibt/gitwrapper/repo/Reference.java  | 34 +++++++++++++------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/src/de/uni_passau/fim/seibt/gitwrapper/repo/Reference.java b/src/de/uni_passau/fim/seibt/gitwrapper/repo/Reference.java
index 150ae85..e75abed 100644
--- a/src/de/uni_passau/fim/seibt/gitwrapper/repo/Reference.java
+++ b/src/de/uni_passau/fim/seibt/gitwrapper/repo/Reference.java
@@ -1,7 +1,6 @@
 package de.uni_passau.fim.seibt.gitwrapper.repo;
 
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
@@ -9,7 +8,7 @@ import java.util.function.Function;
 import java.util.logging.Logger;
 import java.util.stream.Collectors;
 
-import de.uni_passau.fim.seibt.gitwrapper.process.ProcessExecutor;
+import de.uni_passau.fim.seibt.gitwrapper.process.ProcessExecutor.ExecRes;
 
 public abstract class Reference {
 
@@ -46,8 +45,8 @@ public abstract class Reference {
             return Optional.empty();
         }
 
-        Optional<ProcessExecutor.ExecRes> mergeBase = git.exec(repo.getDir(), "merge-base", getId(), other.getId());
-        Function<ProcessExecutor.ExecRes, Commit> toCommit = res -> {
+        Optional<ExecRes> mergeBase = git.exec(repo.getDir(), "merge-base", getId(), other.getId());
+        Function<ExecRes, Commit> toCommit = res -> {
 
             if (git.failed(res)) {
                 LOG.warning(() -> String.format("Failed to obtain a merge base for %s and %s.", this, other));
@@ -75,8 +74,18 @@ public abstract class Reference {
         if (!repo.checkout(this)) {
             return Optional.empty();
         }
-        Optional<ProcessExecutor.ExecRes> mergeBase = git.exec(repo.getDir(), "merge", "-n", "-q", other.getId());
-        return mergeBase.map(res -> repo.getStatus().orElse(null));
+
+        Optional<ExecRes> mergeBase = git.exec(repo.getDir(), "merge", "-n", "-q", other.getId());
+        Function<ExecRes, Optional<Status>> toStatus = res -> {
+            if (git.failed(res)) {
+                LOG.warning(() -> String.format("Could not execute the merge between %s and %s.", this, other));
+                return Optional.empty();
+            }
+
+            return repo.getStatus();
+        };
+
+        return mergeBase.flatMap(toStatus);
     }
 
 
@@ -85,9 +94,14 @@ public abstract class Reference {
      *
      * @return the parent {@link Commit} objects
      */
-    public List<Commit> getParents() {
-        Optional<ProcessExecutor.ExecRes> revList = git.exec(repo.getDir(), "rev-list", "--parents", "-n", "1", id);
-        Function<ProcessExecutor.ExecRes, List<Commit>> toParentsList = res -> {
+    public Optional<List<Commit>> getParents() {
+        Optional<ExecRes> revList = git.exec(repo.getDir(), "rev-list", "--parents", "-n", "1", id);
+        Function<ExecRes, List<Commit>> toParentsList = res -> {
+            if (git.failed(res)) {
+                LOG.warning(() -> String.format("Could not find the parents of %s.", this));
+                return null;
+            }
+
             String[] ids = res.getStdOutTrimmed().split("\\s+");
 
             LOG.fine(() -> String.format("Found %d parents for %s.", ids.length - 1, this));
@@ -96,7 +110,7 @@ public abstract class Reference {
             return Arrays.stream(ids).skip(1).map(repo::getCommitUnchecked).collect(Collectors.toList());
         };
 
-        return revList.map(toParentsList).orElse(Collections.emptyList());
+        return revList.map(toParentsList);
     }
 
     @Override
-- 
GitLab