diff --git a/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java b/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java index 5e0049c78e5bc7491d281386c34a5ef993410729..27d0b12eb2202da6cb72bfa003f2d2f806026a7e 100644 --- a/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java +++ b/src/de/uni_passau/fim/seibt/gitwrapper/repo/GitWrapper.java @@ -6,11 +6,13 @@ import de.uni_passau.fim.seibt.gitwrapper.process.ToolNotWorkingException; import de.uni_passau.fim.seibt.gitwrapper.process.ToolWrapper; import java.io.File; +import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.Scanner; import java.util.function.Function; +import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -135,24 +137,39 @@ public class GitWrapper extends ToolWrapper { */ public Optional<Repository> importRepository(File directory) { if (!isGitDir(directory)) { + LOG.warning(() -> directory + " does not contain a git repository."); return Optional.empty(); } - // TODO add fallback method for determining URL - - Optional<ExecRes> result = exec(directory, "config", "--get", "remote.origin.url"); - Optional<String> url = result.map(res -> { + Optional<ExecRes> originURL = exec(directory, "config", "--get", "remote.origin.url"); + Function<ExecRes, Repository> toRepository = res -> { String repoUrl = null; - if (failed(res) || (repoUrl = res.getStdOutTrimmed()).isEmpty()) { - // there was no remote url - LOG.fine(String.format("Repository %s does not hab an origin url, using the local path", directory)); - repoUrl = directory.getAbsolutePath(); + if (failed(res)) { + LOG.warning(() -> String.format("Failed to get the origin url for the repository in %s. Using the " + + "canonical directory path.", directory)); + } else if (res.getStdOutTrimmed().isEmpty()) { + LOG.warning(() -> String.format("Repository in %s does not have an origin url. Using the canonical " + + "directory path.", directory)); + } else { + repoUrl = res.getStdOutTrimmed(); + } + + if (repoUrl == null) { + + try { + repoUrl = directory.getCanonicalPath(); + } catch (IOException e) { + LOG.log(Level.WARNING, e, () -> "Exception computing the canonical path for " + directory + + ". Using the absolute path instead."); + repoUrl = directory.getAbsolutePath(); + } } - return repoUrl; - }); - return Optional.of(new Repository(this, url.orElse(""), directory)); + return new Repository(this, repoUrl, directory); + }; + + return originURL.map(toRepository); } /**