public static File findGitFolder(File basedir) { File gitDir = new File(basedir, ".git"); if (gitDir.exists() && gitDir.isDirectory()) { return gitDir; } File parent = basedir.getParentFile(); if (parent != null) { return findGitFolder(parent); } return null; }
@Override public Map<String, String> getAnnotations(Kind kind) { final Map<String, String> annotations = new HashMap<>(); if (GitUtil.findGitFolder(getContext().getProjectDirectory()) != null) { Repository repository = null; try { if (kind.isController() || kind == Kind.SERVICE) { repository = GitUtil.getGitRepository(getContext().getProjectDirectory()); if (repository != null) { String branch = repository.getBranch(); annotations.put(Fabric8Annotations.GIT_BRANCH.value(), branch); String id = GitUtil.getGitCommitId(repository); if (id != null) { annotations.put(Fabric8Annotations.GIT_COMMIT.value(), id);
@Override public Map<String, String> getAnnotations(Kind kind) { if (isOnline() && (kind.isController() || kind == Kind.SERVICE)) { Map<String, String> annotations = new HashMap<>(); String repoName = getContext().getGav().getArtifactId(); try (Repository repository = GitUtil.getGitRepository(getContext().getProjectDirectory())) { // Git annotations (if git is used as SCM) if (repository != null) { String gitCommitId = GitUtil.getGitCommitId(repository); if (gitCommitId != null) { addGitServiceUrl(annotations, repoName, gitCommitId); } else { log.debug("No Git commit id found"); } } else { log.debug("No local Git repository found"); } } catch (IOException | GitAPIException e) { log.error("Cannot extract Git information for adding to annotations: " + e, e); } // Jenkins annotations addJenkinsServiceUrl(annotations, repoName); return annotations; } return null; }
public static Repository getGitRepository(File currentDir) throws IOException { if (currentDir == null) { // TODO: Why is this check needed ? currentDir = new File(System.getProperty("basedir", ".")); } File gitFolder = findGitFolder(currentDir); if (gitFolder == null) { // No git repository found return null; } FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder .readEnvironment() .setGitDir(gitFolder) .build(); return repository; }