Example usage for org.eclipse.jgit.lib StoredConfig get

List of usage examples for org.eclipse.jgit.lib StoredConfig get

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib StoredConfig get.

Prototype

@SuppressWarnings("unchecked")
public <T> T get(SectionParser<T> parser) 

Source Link

Document

Obtain a handle to a parsed set of configuration values.

Usage

From source file:me.cmoz.gradle.snapshot.GitSCMCommand.java

License:Apache License

@Override
@SneakyThrows(IOException.class)
public Commit getLatestCommit(@NonNull final String dateFormat) {
    if (repoDir == null) {
        throw new IllegalStateException("'.git' folder could not be found.");
    }/*  w w  w . ja v  a2 s. co  m*/

    final FileRepositoryBuilder builder = new FileRepositoryBuilder();

    final Repository repo = builder.setGitDir(repoDir).readEnvironment().build();
    final StoredConfig conf = repo.getConfig();

    int abbrev = Commit.ABBREV_LENGTH;
    if (conf != null) {
        abbrev = conf.getInt("core", "abbrev", abbrev);
    }

    final SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

    final Ref HEAD = repo.getRef(Constants.HEAD);
    if (HEAD.getObjectId() == null) {
        throw new RuntimeException("Could not find any commits from HEAD ref.");
    }

    final RevWalk revWalk = new RevWalk(repo);
    if (HEAD.getObjectId() == null) {
        throw new RuntimeException("Could not find any commits from HEAD ref.");
    }
    final RevCommit revCommit = revWalk.parseCommit(HEAD.getObjectId());
    revWalk.markStart(revCommit);

    try {
        // git commit time in sec and java datetime is in ms
        final Date commitTime = new Date(revCommit.getCommitTime() * 1000L);
        final PersonIdent ident = revCommit.getAuthorIdent();
        final UserConfig userConf = conf.get(UserConfig.KEY);

        return Commit.builder().buildTime(sdf.format(new Date())).buildAuthorName(userConf.getAuthorName())
                .buildAuthorEmail(userConf.getAuthorEmail()).branchName(repo.getBranch())
                .commitId(revCommit.getName()).commitTime(sdf.format(commitTime))
                .commitUserName(ident.getName()).commitUserEmail(ident.getEmailAddress())
                .commitMessage(revCommit.getFullMessage().trim()).build();
    } finally {
        revWalk.dispose();
        repo.close();
    }
}