List of usage examples for org.eclipse.jgit.storage.file FileRepositoryBuilder FileRepositoryBuilder
FileRepositoryBuilder
From source file:info.debatty.sparkpackages.maven.plugin.PublishMojo.java
License:Open Source License
final String getGitCommit() throws MojoFailureException { Repository git_repository = null;// w ww . ja v a 2 s . c o m try { git_repository = new FileRepositoryBuilder().setMustExist(true) .findGitDir(new File(getProject().getBasedir().getAbsolutePath())).build(); } catch (IOException ex) { throw new MojoFailureException("GIT repo not found!", ex); } ObjectId resolve = null; try { resolve = git_repository.resolve("HEAD"); } catch (IncorrectObjectTypeException ex) { throw new MojoFailureException("GIT error!", ex); } catch (RevisionSyntaxException | IOException ex) { throw new MojoFailureException("GIT error!", ex); } return resolve.getName(); }
From source file:io.fabric8.collector.git.GitBuildConfigProcessor.java
License:Apache License
protected void doPull(File gitFolder, CredentialsProvider cp, String branch, PersonIdent personIdent, UserDetails userDetails) {//from ww w . jav a2s . com try { FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(gitFolder).readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build(); Git git = new Git(repository); File projectFolder = repository.getDirectory(); StoredConfig config = repository.getConfig(); String url = config.getString("remote", userDetails.getRemote(), "url"); if (Strings.isNullOrBlank(url)) { LOG.warn("No remote repository url for " + branch + " defined for the git repository at " + projectFolder.getCanonicalPath() + " so cannot pull"); //return; } String mergeUrl = config.getString("branch", branch, "merge"); if (Strings.isNullOrBlank(mergeUrl)) { LOG.warn("No merge spec for branch." + branch + ".merge in the git repository at " + projectFolder.getCanonicalPath() + " so not doing a pull"); //return; } LOG.debug("Performing a pull in git repository " + projectFolder.getCanonicalPath() + " on remote URL: " + url); PullCommand pull = git.pull(); GitHelpers.configureCommand(pull, userDetails); pull.setRebase(true).call(); } catch (Throwable e) { LOG.error("Failed to pull from the remote git repo with credentials " + cp + " due: " + e.getMessage() + ". This exception is ignored.", e); } }
From source file:io.fabric8.collector.git.GitHelpers.java
License:Apache License
public static Git gitFromGitFolder(File gitFolder) throws IOException { FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(gitFolder).readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build();/* w w w .ja v a2 s .c om*/ return new Git(repository); }
From source file:io.fabric8.forge.generator.pipeline.JenkinsPipelineLibrary.java
License:Apache License
protected void doPull(File gitFolder, CredentialsProvider cp, String branch, PersonIdent personIdent, UserDetails userDetails) {//from w ww . j a v a 2 s. c om StopWatch watch = new StopWatch(); try { FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(gitFolder).readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build(); Git git = new Git(repository); File projectFolder = repository.getDirectory(); StoredConfig config = repository.getConfig(); String url = config.getString("remote", remote, "url"); if (io.fabric8.utils.Strings.isNullOrBlank(url)) { LOG.warn("No remote repository url for " + branch + " defined for the git repository at " + projectFolder.getCanonicalPath() + " so cannot pull"); //return; } String mergeUrl = config.getString("branch", branch, "merge"); if (io.fabric8.utils.Strings.isNullOrBlank(mergeUrl)) { LOG.warn("No merge spec for branch." + branch + ".merge in the git repository at " + projectFolder.getCanonicalPath() + " so not doing a pull"); //return; } // lets trash any failed changes LOG.debug("Stashing local changes to the repo"); boolean hasHead = true; try { git.log().all().call(); hasHead = git.getRepository().getAllRefs().containsKey("HEAD"); } catch (NoHeadException e) { hasHead = false; } if (hasHead) { // lets stash any local changes just in case.. try { git.stashCreate().setPerson(personIdent).setWorkingDirectoryMessage("Stash before a write") .setRef("HEAD").call(); } catch (Throwable e) { LOG.error("Failed to stash changes: " + e, e); Throwable cause = e.getCause(); if (cause != null && cause != e) { LOG.error("Cause: " + cause, cause); } } } //LOG.debug("Resetting the repo"); //git.reset().setMode(ResetCommand.ResetType.HARD).call(); LOG.debug("Performing a pull in git repository " + projectFolder.getCanonicalPath() + " on remote URL: " + url); PullCommand pull = git.pull(); GitUtils.configureCommand(pull, userDetails); pull.setRebase(true).call(); } catch (Throwable e) { LOG.error("Failed to pull from the remote git repo with credentials " + cp + " due: " + e.getMessage() + ". This exception is ignored.", e); } finally { LOG.debug("doPull took " + watch.taken()); } }
From source file:io.fabric8.forge.rest.git.RepositoryResource.java
License:Apache License
protected <T> T gitOperation(final GitContext context, final GitOperation<T> operation) throws Exception { return lockManager.withLock(gitFolder, new Callable<T>() { @Override/*from www .j ava 2s.c o m*/ public T call() throws Exception { projectFileSystem.cloneRepoIfNotExist(userDetails, basedir, cloneUrl); FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(gitFolder).readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build(); Git git = new Git(repository); if (Strings.isNullOrBlank(origin)) { throw new IOException("Could not find remote git URL for folder " + gitFolder.getPath()); } CredentialsProvider credentials = userDetails.createCredentialsProvider(); createPersonIdent(); disableSslCertificateChecks(); LOG.info("Stashing local changes to the repo"); boolean hasHead = true; try { git.log().all().call(); hasHead = git.getRepository().getAllRefs().containsKey("HEAD"); } catch (NoHeadException e) { hasHead = false; } if (hasHead) { // lets stash any local changes just in case.. try { git.stashCreate().setPerson(personIdent).setWorkingDirectoryMessage("Stash before a write") .setRef("HEAD").call(); } catch (Throwable e) { LOG.error("Failed to stash changes: " + e, e); Throwable cause = e.getCause(); if (cause != null && cause != e) { LOG.error("Cause: " + cause, cause); } } } checkoutBranch(git, context); if (context.isRequirePull()) { doPull(git, context); } T result = operation.call(git, context); if (Strings.isNullOrBlank(message)) { message = ""; } if (context.isRequireCommit() && hasGitChanges(git)) { doAddCommitAndPushFiles(git, userDetails, personIdent, branch, origin, message, isPushOnCommit()); } return result; } }); }
From source file:io.fabric8.forge.rest.main.GitCommandCompletePostProcessor.java
License:Apache License
@Override public void firePostCompleteActions(String name, ExecutionRequest executionRequest, RestUIContext context, CommandController controller, ExecutionResult results, HttpServletRequest request) { UserDetails userDetails = gitUserHelper.createUserDetails(request); String user = userDetails.getUser(); String password = userDetails.getPassword(); String authorEmail = userDetails.getEmail(); String address = userDetails.getAddress(); String branch = userDetails.getBranch(); String origin = projectFileSystem.getRemote(); try {/*from w w w . j a va 2 s . c o m*/ CredentialsProvider credentials = userDetails.createCredentialsProvider(); PersonIdent personIdent = new PersonIdent(user, authorEmail); if (name.equals(PROJECT_NEW_COMMAND)) { String targetLocation = null; String named = null; List<Map<String, String>> inputList = executionRequest.getInputList(); for (Map<String, String> map : inputList) { if (Strings.isNullOrEmpty(targetLocation)) { targetLocation = map.get("targetLocation"); } if (Strings.isNullOrEmpty(named)) { named = map.get("named"); } } if (Strings.isNullOrEmpty(targetLocation)) { LOG.warn("No targetLocation could be found!"); } else if (Strings.isNullOrEmpty(named)) { LOG.warn("No named could be found!"); } else { File basedir = new File(targetLocation, named); if (!basedir.isDirectory() || !basedir.exists()) { LOG.warn("Generated project folder does not exist: " + basedir.getAbsolutePath()); } else { InitCommand initCommand = Git.init(); initCommand.setDirectory(basedir); Git git = initCommand.call(); LOG.info("Initialised an empty git configuration repo at {}", basedir.getAbsolutePath()); // lets create the repository GitRepoClient repoClient = userDetails.createRepoClient(); CreateRepositoryDTO createRepository = new CreateRepositoryDTO(); createRepository.setName(named); String fullName = null; RepositoryDTO repository = repoClient.createRepository(createRepository); if (repository != null) { if (LOG.isDebugEnabled()) { LOG.debug("Got repository: " + toJson(repository)); } fullName = repository.getFullName(); } if (Strings.isNullOrEmpty(fullName)) { fullName = user + "/" + named; } String htmlUrl = address + user + "/" + named; String remoteUrl = address + user + "/" + named + ".git"; //results.appendOut("Created git repository " + fullName + " at: " + htmlUrl); results.setOutputProperty("fullName", fullName); results.setOutputProperty("cloneUrl", remoteUrl); results.setOutputProperty("htmlUrl", htmlUrl); // now lets import the code and publish LOG.info("Using remoteUrl: " + remoteUrl + " and remote name " + origin); configureBranch(git, branch, origin, remoteUrl); createKubernetesResources(user, named, remoteUrl, branch, repoClient, address); String message = createCommitMessage(name, executionRequest); doAddCommitAndPushFiles(git, credentials, personIdent, remoteUrl, branch, origin, message); } } } else { File basedir = context.getInitialSelectionFile(); String absolutePath = basedir != null ? basedir.getAbsolutePath() : null; if (basedir != null) { File gitFolder = new File(basedir, ".git"); if (gitFolder.exists() && gitFolder.isDirectory()) { FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(gitFolder).readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build(); Git git = new Git(repository); String remoteUrl = getRemoteURL(git, branch); if (origin == null) { LOG.warn("Could not find remote git URL for folder " + absolutePath); } else { String message = createCommitMessage(name, executionRequest); doAddCommitAndPushFiles(git, credentials, personIdent, remoteUrl, branch, origin, message); } } } } } catch (Exception e) { handleException(e); } }
From source file:io.fabric8.forge.rest.main.ProjectFileSystem.java
License:Apache License
protected void doPull(File gitFolder, CredentialsProvider cp, String branch) { try {//from w ww . j ava 2 s. c o m FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(gitFolder).readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build(); Git git = new Git(repository); File projectFolder = repository.getDirectory(); StoredConfig config = repository.getConfig(); String url = config.getString("remote", remote, "url"); if (Strings.isNullOrBlank(url)) { LOG.warn("No remote repository url for " + branch + " defined for the git repository at " + projectFolder.getCanonicalPath() + " so cannot pull"); //return; } String mergeUrl = config.getString("branch", branch, "merge"); if (Strings.isNullOrBlank(mergeUrl)) { LOG.warn("No merge spec for branch." + branch + ".merge in the git repository at " + projectFolder.getCanonicalPath() + " so not doing a pull"); //return; } // lets trash any failed changes LOG.info("Resetting the repo"); git.reset().setMode(ResetCommand.ResetType.HARD).call(); LOG.info("Performing a pull in git repository " + projectFolder.getCanonicalPath() + " on remote URL: " + url); git.pull().setCredentialsProvider(cp).setRebase(true).call(); } catch (Throwable e) { LOG.error("Failed to pull from the remote git repo with credentials " + cp + " due: " + e.getMessage() + ". This exception is ignored.", e); } }
From source file:io.fabric8.maven.AbstractFabric8Mojo.java
License:Apache License
protected Repository getGitRepository(File basedir, String envVarName) { try {//from ww w. j a va 2 s.c om File gitFolder = GitHelpers.findGitFolder(basedir); if (gitFolder == null) { warnIfInCDBuild("Could not find .git folder based on the current basedir of " + basedir); return null; } FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.readEnvironment().setGitDir(gitFolder).build(); if (repository == null) { warnIfInCDBuild("Cannot create default value for $" + envVarName + " as no .git/config file could be found"); } return repository; } catch (Exception e) { warnIfInCDBuild("Failed to initialise Git Repository: " + e, e); return null; } }
From source file:io.fabric8.maven.core.util.GitUtil.java
License:Apache License
public static Repository getGitRepository(MavenProject project) throws IOException { MavenProject rootProject = MavenUtil.getRootProject(project); File baseDir = rootProject.getBasedir(); if (baseDir == null) { baseDir = project.getBasedir();/*from ww w . jav a2s .c o m*/ } if (baseDir == null) { // TODO: Why is this check needed ? baseDir = new File(System.getProperty("basedir", ".")); } File gitFolder = findGitFolder(baseDir); if (gitFolder == null) { // No git repository found return null; } FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.readEnvironment().setGitDir(gitFolder).build(); return repository; }
From source file:io.fabric8.maven.CreateBranchMojo.java
License:Apache License
protected void initGitRepo() throws MojoExecutionException, IOException, GitAPIException { buildDir.mkdirs();// w w w. java2 s . c om File gitDir = new File(buildDir, ".git"); if (!gitDir.exists()) { String repo = gitUrl; if (Strings.isNotBlank(repo)) { getLog().info("Cloning git repo " + repo + " into directory " + getGitBuildPathDescription() + " cloneAllBranches: " + cloneAll); CloneCommand command = Git.cloneRepository().setCloneAllBranches(cloneAll).setURI(repo) .setDirectory(buildDir).setRemote(remoteName); // .setCredentialsProvider(getCredentials()). try { git = command.call(); return; } catch (Throwable e) { getLog().error("Failed to command remote repo " + repo + " due: " + e.getMessage(), e); // lets just use an empty repo instead } } else { InitCommand initCommand = Git.init(); initCommand.setDirectory(buildDir); git = initCommand.call(); getLog().info("Initialised an empty git configuration repo at " + getGitBuildPathDescription()); // lets add a dummy file File readMe = new File(buildDir, "ReadMe.md"); getLog().info("Generating " + readMe); Files.writeToFile(readMe, "fabric8 git repository created by fabric8-maven-plugin at " + new Date(), Charset.forName("UTF-8")); git.add().addFilepattern("ReadMe.md").call(); commit("Initial commit"); } String branch = git.getRepository().getBranch(); configureBranch(branch); } else { getLog().info("Reusing existing git repository at " + getGitBuildPathDescription()); FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(gitDir).readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build(); git = new Git(repository); if (pullOnStartup) { doPull(); } else { getLog().info("git pull from remote config repo on startup is disabled"); } } }