Skip to content
Snippets Groups Projects
Commit 6219ae01 authored by Georg Seibt's avatar Georg Seibt :nerd:
Browse files

add some more checks to getMergeBase, merge, and getParents

parent 7fa4df3e
No related branches found
No related tags found
No related merge requests found
package de.uni_passau.fim.seibt.gitwrapper.repo; package de.uni_passau.fim.seibt.gitwrapper.repo;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
...@@ -9,7 +8,7 @@ import java.util.function.Function; ...@@ -9,7 +8,7 @@ import java.util.function.Function;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.stream.Collectors; 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 { public abstract class Reference {
...@@ -46,8 +45,8 @@ public abstract class Reference { ...@@ -46,8 +45,8 @@ public abstract class Reference {
return Optional.empty(); return Optional.empty();
} }
Optional<ProcessExecutor.ExecRes> mergeBase = git.exec(repo.getDir(), "merge-base", getId(), other.getId()); Optional<ExecRes> mergeBase = git.exec(repo.getDir(), "merge-base", getId(), other.getId());
Function<ProcessExecutor.ExecRes, Commit> toCommit = res -> { Function<ExecRes, Commit> toCommit = res -> {
if (git.failed(res)) { if (git.failed(res)) {
LOG.warning(() -> String.format("Failed to obtain a merge base for %s and %s.", this, other)); LOG.warning(() -> String.format("Failed to obtain a merge base for %s and %s.", this, other));
...@@ -75,8 +74,18 @@ public abstract class Reference { ...@@ -75,8 +74,18 @@ public abstract class Reference {
if (!repo.checkout(this)) { if (!repo.checkout(this)) {
return Optional.empty(); 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 { ...@@ -85,9 +94,14 @@ public abstract class Reference {
* *
* @return the parent {@link Commit} objects * @return the parent {@link Commit} objects
*/ */
public List<Commit> getParents() { public Optional<List<Commit>> getParents() {
Optional<ProcessExecutor.ExecRes> revList = git.exec(repo.getDir(), "rev-list", "--parents", "-n", "1", id); Optional<ExecRes> revList = git.exec(repo.getDir(), "rev-list", "--parents", "-n", "1", id);
Function<ProcessExecutor.ExecRes, List<Commit>> toParentsList = res -> { 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+"); String[] ids = res.getStdOutTrimmed().split("\\s+");
LOG.fine(() -> String.format("Found %d parents for %s.", ids.length - 1, this)); LOG.fine(() -> String.format("Found %d parents for %s.", ids.length - 1, this));
...@@ -96,7 +110,7 @@ public abstract class Reference { ...@@ -96,7 +110,7 @@ public abstract class Reference {
return Arrays.stream(ids).skip(1).map(repo::getCommitUnchecked).collect(Collectors.toList()); return Arrays.stream(ids).skip(1).map(repo::getCommitUnchecked).collect(Collectors.toList());
}; };
return revList.map(toParentsList).orElse(Collections.emptyList()); return revList.map(toParentsList);
} }
@Override @Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment