private Ref checkoutBranch(File projectDir, String branch) throws GitAPIException { Git git = this.gitFactory.open(projectDir); CheckoutCommand command = git.checkout().setName(branch); try { if (shouldTrack(git, branch)) { trackBranch(command, branch); } return command.call(); } catch (GitAPIException e) { deleteBaseDirIfExists(); throw e; } finally { git.close(); } }
protected void checkoutBranch(Git git, GitContext context) throws GitAPIException { String current = currentBranch(git); if (Objects.equals(current, branch)) { return; } System.out.println("Checking out branch: " + branch); // lets check if the branch exists CheckoutCommand command = git.checkout().setName(branch); boolean exists = localBranchExists(git, branch); if (!exists) { command = command.setCreateBranch(true).setForce(true). setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK). setStartPoint(getRemote() + "/" + branch); } Ref ref = command.call(); if (LOG.isDebugEnabled()) { LOG.debug("Checked out branch " + branch + " with results " + ref.getName()); } configureBranch(git, branch); }
/** * Checkout on provided repository - git checkout. * * @param repositoryName name of the repository * @param name name to checkout e.g.: branch name */ public void checkout(String repositoryName, String name) { RepositoryContext repositoryContext = repositoryByName.get(repositoryName); try { CheckoutCommand checkoutCommand = repositoryContext.git.checkout(); checkoutCommand.setName(name); checkoutCommand.call(); } catch (GitAPIException e) { throw new RuntimeException(e); } }
private void checkoutToBranch(String branch) throws GitAPIException { CheckoutCommand checkoutCommand = clonedRepo.checkout() .setCreateBranch(false) .setName(branch); List<Ref> refList = clonedRepo.branchList().call(); if (!anyRefMatches(refList, branch)) { checkoutCommand = checkoutCommand .setCreateBranch(true) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK) .setStartPoint("origin/" + branch); } checkoutCommand .call(); }
@Override public String checkout( String branchName, boolean useBranchNameAsStartPoint, boolean createBranch, boolean useForce) { CheckoutCommand command = _git.checkout(); command.setCreateBranch(createBranch); command.setForce(useForce); command.setName(branchName); if (useBranchNameAsStartPoint) { command.setStartPoint(REFS_REMOTES + branchName); } Ref ref = null; try { ref = command.call(); } catch (Throwable e) { throw new RuntimeException( String.format("Failed to checkout branch [%s]", branchName), e); } return ref.getName(); }
@Override public void synchroniseEnd() { validateChanges(); // deleted = 0; changed.clear(); // if (orderedCommits.size() > 0) { RevCommit first = orderedCommits.getFirst(); orderedCommits.remove(); System.out.println("changing repo to commit with time: " + first.getCommitTime()); // update git to this revision CheckoutCommand c = git.checkout(); c.setName(first.getId().getName()); try { c.call(); } catch (Exception e) { System.err.println(e.getMessage()); // e.printStackTrace(); } } else { System.out.println("done -- we are at latest marked commit"); } }
public Git call(final GitOperationsStep gitOperationsStep, Git git, CredentialsProvider cp, String gitRepoUrl, File gitRepoFolder) throws IllegalArgumentException, IOException, NoFilepatternException, GitAPIException { CheckoutCommand cc = git.checkout().setAllPaths(this.allPaths) .setForce(this.force); if (!Const.isEmpty(this.path)) { cc = cc.addPath(gitOperationsStep.environmentSubstitute(this.path)); } if (!Const.isEmpty(this.name)) { cc = cc.setName(gitOperationsStep.environmentSubstitute(this.name)); } if (!Const.isEmpty(this.startPoint)) { cc = cc.setStartPoint(gitOperationsStep .environmentSubstitute(this.startPoint)); } cc.call(); return git; }
/** * Restores the last commit file content to the local file at the given path. * Both files must have the same path, otherwise it will not work. * * @param file * - the path to the file you want to restore */ public void restoreLastCommitFile(List<String> paths) { try { CheckoutCommand checkoutCmd = git.checkout(); checkoutCmd.addPaths(paths); checkoutCmd.call(); fireFileStateChanged(new ChangeEvent(GitCommand.DISCARD, paths)); } catch (GitAPIException e) { if (logger.isDebugEnabled()) { logger.debug(e, e); } } }
/** * Reverts the selected files back to their previous state in the most recent commit * @return whatever {@link #createResult(Ref)} returns or null if a {@link GitAPIException} was thrown. */ public final R revertChanges() { CheckoutCommand checkout = getGitOrThrow().checkout(); List<String> selectedFiles = getDialogPane().getSelectedFiles(); selectedFiles.forEach(checkout::addPath); try { Ref ref = checkout.call(); return createResult(ref); } catch (GitAPIException e) { e.printStackTrace(); return null; } }
private void revertChanges() { CheckoutCommand checkout = getGitOrThrow().checkout(); List<String> selectedFiles = fileViewer.getSelectedFiles(); selectedFiles.forEach(checkout::addPath); result = new RevertChangesResult(selectedFiles); try { checkout.call(); } catch (GitAPIException e) { e.printStackTrace(); } }
private void trackBranch(CheckoutCommand checkout, String label) { checkout.setCreateBranch(true).setName(label) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK) .setStartPoint("origin/" + label); }
private void trackBranch(Git git, CheckoutCommand checkout, String label) { checkout.setCreateBranch(true).setName(label) .setUpstreamMode(SetupUpstreamMode.TRACK) .setStartPoint("origin/" + label); }
private String getCommitID(Git git, String label) throws GitAPIException { CheckoutCommand checkout = git.checkout(); checkout.setName(label); Ref localRef = checkout.call(); return localRef.getObjectId().getName(); }
private void createRepository() throws IOException, GitAPIException { File repo = new File(TestUtilsFactory.WORKDIR,"EnvVar - " + UUID.randomUUID().toString().substring(0, 6) + "/.git"); if (repo.getAbsoluteFile().exists()) { FileUtils.deleteDirectory(repo.getParentFile()); } FileRepositoryBuilder builder = new FileRepositoryBuilder(); repository = builder.setGitDir(repo.getAbsoluteFile()) .readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build(); if (!repository.isBare() && repository.getBranch() == null) { repository.create(); } Git git = new Git(repository); File readme = new File(repository.getDirectory().getParent().concat("/" + "readme")); FileUtils.writeStringToFile(readme, "commit 1\n"); git.add().addFilepattern(readme.getName()).call(); git.commit().setMessage("commit message 1").call(); CreateBranchCommand createBranchCommand = git.branchCreate(); createBranchCommand.setName(INTEGRATION_BRANCH); createBranchCommand.call(); createBranchCommand = git.branchCreate(); createBranchCommand.setName(READY_BRANCH); createBranchCommand.call(); CheckoutCommand checkout = git.checkout(); checkout.setName(READY_BRANCH); checkout.call(); FileUtils.writeStringToFile(readme, "commit 2\n"); git.add().addFilepattern(readme.getName()).call(); git.commit().setMessage("commit message 2").call(); checkout = git.checkout(); checkout.setName(INTEGRATION_BRANCH); checkout.call(); DeleteBranchCommand deleteBranchCommand = git.branchDelete(); deleteBranchCommand.setBranchNames("master"); deleteBranchCommand.call(); REPOSITORY = repository.getDirectory().getAbsolutePath(); }
/** * Checks out all projects of the given {@link TrainIteration}. * * @param train * @throws Exception */ public void checkout(Train train) { Assert.notNull(train, "Train must not be null!"); update(train); ExecutionUtils.run(train, module -> { Project project = module.getProject(); doWithGit(project, git -> { Branch branch = Branch.from(module); CheckoutCommand command = git.checkout().setName(branch.toString()); if (!branchExists(project, branch)) { logger.log(project, "git checkout -b %s --track origin/%s", branch, branch); command.setCreateBranch(true)// .setStartPoint("origin/".concat(branch.toString()))// .call(); } else { logger.log(project, "git checkout %s", branch); command.call(); } reset(project, branch); }); }); logger.log(train, "Successfully checked out projects."); }
/** * Reverts the given modified files back to their previous state in the most recent commit. Note: this does not * create a commit that reverts a previous commit. * @param git the git repository * @param relativePaths the list of files' paths relative to the parent directory (given "/home/user/dir/.git/", * the parent directory would be "/home/user/dir/") * @throws GitAPIException */ public static void revertChanges(Git git, List<String> relativePaths) throws GitAPIException { CheckoutCommand checkout = git.checkout(); relativePaths.forEach(checkout::addPath); checkout.call(); }