All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.yoomoney.gradle.plugins.library.git.GitManager Maven / Gradle / Ivy

Go to download

Gradle plugin by YooMoney. See README: https://github.com/yoomoney-gradle-plugins/library-project-plugin

There is a newer version: 7.3.1
Show newest version
package ru.yoomoney.gradle.plugins.library.git;


import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.Optional;

/**
 * Работа с git
 */
public class GitManager implements Closeable {

    private final Logger log = Logging.getLogger(GitManager.class);
    private final Git git;

    /**
     * Конструктор
     *
     * @param projectDirectory директория с git
     */
    public GitManager(File projectDirectory) {
        try {
            this.git = new Git(new FileRepositoryBuilder()
                    .readEnvironment()
                    .findGitDir(projectDirectory)
                    .build());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Возвращает текущую ветку
     *
     * @return текущую ветку
     */
    public Optional getCurrentBranch() {
        try {
            return Optional.ofNullable(git.getRepository().getBranch());
        } catch (IOException e) {
            log.error("Can't get current branch ", e);
            return Optional.empty();
        }
    }

    /**
     * Проверяем, является ли текущая ветка подходящей для релиза
     *
     * @return true если из текущей ветки можно зарелизить библиотеку
     */
    public boolean isCurrentBranchForRelease() {
        return getCurrentBranch()
                .filter(branchName -> "master".equals(branchName) ||
                        branchName.startsWith("release/")).isPresent();
    }

    @Override
    public void close() {
        git.close();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy