/** * Gets org.eclipse.jgit.lib.Repository object for existing Git Repository. * * @param repositoryPath the path to an existing Git Repository * @return {@link org.eclipse.jgit.lib.Repository} object * @throws IOException */ public static Repository getRepository(String repositoryPath) throws IOException { RepositoryBuilder repositoryBuilder = new RepositoryBuilder(); repositoryBuilder.findGitDir(new File(repositoryPath)); Repository repository = repositoryBuilder.build(); repository.getConfig().setString(BRANCH, MASTER, MERGE, REFS_HEADS_MASTER); return repository; }
/** * Adds properties to the specified {@code transformer} for the current Git commit hash. The * following properties are added to {@code transformer}: * * <ul> * <li>{@code repository.commit}: The full commit hash, in lowercase hexadecimal form. * <li>{@code repository.commit.short}: The abbreviated commit hash, which is the first {@code * abbrevLen} hexadecimal characters of the full commit hash. * </ul> * * <p>If {@code baseDir} is not currently stored in a Git repository, or if the current Git commit * hash could not be determined, this method logs a warning and returns {@code false}. * * @param transformer The transformer. * @param baseDir The base directory where versioned files are contained. * @param abbrevLen The length of the abbreviated commit hash to create, in number of hexadecimal * characters. * @param log The Maven log instance. * @return {@code true} if the commit hash was identified and the properties added to the {@code * transformer}; otherwise, {@code false}. */ public static boolean addCommitProperties( Transformer transformer, File baseDir, int abbrevLen, Log log) { try { RepositoryBuilder builder = new RepositoryBuilder(); Repository repository = builder.findGitDir(baseDir).readEnvironment().build(); ObjectId objectId = repository.resolve(Constants.HEAD); if (objectId != null) { transformer.setParameter("repository.commit", objectId.getName()); transformer.setParameter("repository.commit.short", objectId.abbreviate(abbrevLen).name()); return true; } else { log.warn("Could not determine current repository commit hash."); return false; } } catch (IOException ex) { log.warn("Could not determine current repository commit hash.", ex); return false; } }