diff --git a/src/de/uni_passau/fim/seibt/gitwrapper/repo/Commit.java b/src/de/uni_passau/fim/seibt/gitwrapper/repo/Commit.java index a26c0729f02d1db24d3fd1684b04f59775b14957..7c2ef1fe90400682e5e3b60003cb68099075a28e 100644 --- a/src/de/uni_passau/fim/seibt/gitwrapper/repo/Commit.java +++ b/src/de/uni_passau/fim/seibt/gitwrapper/repo/Commit.java @@ -3,6 +3,7 @@ package de.uni_passau.fim.seibt.gitwrapper.repo; import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneId; +import java.util.Optional; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -245,4 +246,17 @@ public class Commit extends Reference { } }); } + + /** + * Checks if the given {@link Commit} is an ancestor of this commit. + * + * @param ancestor + * the {@link Commit} to check + * @return optionally <code>true</code>, if the given commit is in the history before this commit, + * <code>false</code>, if not and an {@link Optional#EMPTY empty optional}, if an error occurred. + */ + public Optional<Boolean> checkAncestry(Commit ancestor) { + return git.exec(repo.getDir(), " merge-base", "--is-ancestor", ancestor.getId(), id).map(execRes -> + execRes.exitCode == 0 ? Boolean.TRUE : execRes.exitCode == 1 ? Boolean.FALSE : null); + } }