me.qoomon.maven.extension.gitversioning.GitUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maven-git-versioning-extension Show documentation
Show all versions of maven-git-versioning-extension Show documentation
Maven Git Versioning Extension
package me.qoomon.maven.extension.gitversioning;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public final class GitUtil {
public static Status getStatus(Repository repository) {
try {
return Git.wrap(repository).status().call();
} catch (GitAPIException e) {
throw new RuntimeException(e);
}
}
public static String getHeadBranch(Repository repository) throws IOException {
ObjectId head = repository.resolve(Constants.HEAD);
if (head == null) {
return Constants.MASTER;
}
if (ObjectId.isId(repository.getBranch())) {
return null;
}
return repository.getBranch();
}
public static List getHeadTags(Repository repository) throws IOException {
ObjectId head = repository.resolve(Constants.HEAD);
if (head == null) {
return Collections.emptyList();
}
return repository.getRefDatabase().getRefsByPrefix(Constants.R_TAGS).stream()
.map(ref -> {
try {
return repository.getRefDatabase().peel(ref);
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.filter(ref -> {
ObjectId objectId;
if (ref.getPeeledObjectId() != null) {
objectId = ref.getPeeledObjectId();
} else {
objectId = ref.getObjectId();
}
return objectId.equals(head);
})
.map(ref -> ref.getName().replaceFirst("^" + Constants.R_TAGS, ""))
.collect(Collectors.toList());
}
public static String getHeadCommit(Repository repository) throws IOException {
ObjectId head = repository.resolve(Constants.HEAD);
if (head == null) {
return "0000000000000000000000000000000000000000";
}
return head.getName();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy