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

get merge commits

parent 8dcab9bb
No related branches found
No related tags found
No related merge requests found
package de.uni_passau.fim.seibt.gitwrapper.repo;
import java.util.Objects;
import java.util.logging.Logger;
public class Commit {
private static final Logger LOG = Logger.getLogger(Commit.class.getCanonicalName());
private Repository repo;
private GitWrapper git;
private String id;
public Commit(Repository repo, String id) {
this.repo = repo;
this.git = repo.getGit();
this.id = id;
}
public String getId() {
return id;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Commit commit = (Commit) o;
return Objects.equals(repo, commit.repo) && Objects.equals(id, commit.id);
}
@Override
public int hashCode() {
return Objects.hash(repo, id);
}
@Override
public String toString() {
return String.format("Commit{id='%s'}", id);
}
}
...@@ -25,9 +25,12 @@ public class GitWrapper { ...@@ -25,9 +25,12 @@ public class GitWrapper {
private static final Pattern CLONING_INTO = Pattern.compile("Cloning into '(.*)'\\.\\.\\."); private static final Pattern CLONING_INTO = Pattern.compile("Cloning into '(.*)'\\.\\.\\.");
private static final Pattern ALREADY_EXISTS = Pattern.compile("fatal: destination path '(.*)' already exists and is not an empty directory\\."); private static final Pattern ALREADY_EXISTS = Pattern.compile("fatal: destination path '(.*)' already exists and is not an empty directory\\.");
private class ExecRes { /**
private int code; * The result of a command line execution.
private String output; */
static class ExecRes {
int code;
String output;
} }
private String git; private String git;
......
package de.uni_passau.fim.seibt.gitwrapper.repo; package de.uni_passau.fim.seibt.gitwrapper.repo;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import de.uni_passau.fim.seibt.gitwrapper.repo.GitWrapper.ExecRes;
/** /**
* A git {@link Repository}. * A git {@link Repository}.
*/ */
public class Repository { public class Repository {
private GitWrapper gitWrapper; private static final Logger LOG = Logger.getLogger(Repository.class.getCanonicalName());
private GitWrapper git;
private String url; private String url;
private File dir; private File dir;
...@@ -15,17 +29,72 @@ public class Repository { ...@@ -15,17 +29,72 @@ public class Repository {
/** /**
* Constructs a new {@link Repository}. * Constructs a new {@link Repository}.
* *
* @param gitWrapper * @param git
* the {@link GitWrapper} to use * the {@link GitWrapper} to use
* @param url * @param url
* the url the {@link Repository} was cloned from * the url the {@link Repository} was cloned from
* @param dir * @param dir
* the directory containing the {@link Repository} * the directory containing the {@link Repository}
*/ */
Repository(GitWrapper gitWrapper, String url, File dir) { Repository(GitWrapper git, String url, File dir) {
this.gitWrapper = gitWrapper; this.git = git;
this.url = url; this.url = url;
this.dir = dir;
try {
this.dir = dir.getCanonicalFile();
} catch (IOException e) {
LOG.log(Level.WARNING, e, () -> "Exception computing the canonical file for " + dir + ". Using the absolute file instead.");
this.dir = dir.getAbsoluteFile();
}
}
/**
* Returns the merge commits in this repository.
*
* @return the merge commits
*/
public List<Commit> getMergeCommits() {
Optional<ExecRes> revList = git.exec(dir, "rev-list", "--all", "--merges");
Function<ExecRes, List<Commit>> toCommitList = res -> {
String[] lines = res.output.split("[\\r?\\n]+");
LOG.fine(() -> String.format("Found %d merge commits in %s.", lines.length, this));
return Arrays.stream(lines).map(id -> new Commit(this, id)).collect(Collectors.toList());
};
return revList.map(toCommitList).orElse(Collections.emptyList());
}
public GitWrapper getGit() {
return git;
}
public String getUrl() {
return url;
}
public File getDir() {
return dir;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Repository that = (Repository) o;
return Objects.equals(url, that.url) && Objects.equals(dir, that.dir);
}
@Override
public int hashCode() {
return Objects.hash(url, dir);
} }
@Override @Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment