/** * Does not do anything if already on target branch. */ public void checkout(String branch) throws Exception { if (!branch.equals(getBranch())) { createBranchIfNeeded(branch); git.checkout().setName(branch).setStartPoint("origin/" + branch) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).call(); // for some reason jgit needs this when branch is switched git.checkout().setName(branch).call(); } }
/** * Create a local branch for remote tracking if it doesn't exist already. */ protected void createBranchIfNeeded(String branch) throws Exception { fetch(); // in case the branch is not known locally if (localRepo.findRef(branch) == null) { git.branchCreate() .setName(branch) .setUpstreamMode(SetupUpstreamMode.TRACK) .setStartPoint("origin/" + branch) .call(); } }
public LocalBranch createLocalBranch() throws GitAPIException { GitRepository gitRepository = getGitRepository(); Repository repository = gitRepository.getRepository(); Git git = gitRepository.getGit(); CreateBranchCommand branchCreate = git.branchCreate(); String name = getName(); String shortenRefName = repository.shortenRemoteBranchName(name); branchCreate.setName(shortenRefName); branchCreate.setStartPoint(name); branchCreate.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM); Ref call = branchCreate.call(); return new LocalBranch(gitRepository, call); }
@And("^I checkout branch \"([^\"]*)\"$") public void iCheckoutBranch(String name) throws Throwable { git.checkout() .setCreateBranch(!name.equals("master")) .setName(name) .setUpstreamMode(SetupUpstreamMode.TRACK) .setStartPoint("origin/" + name) .call(); }
public void testTrackMaster() throws IOException, JGitInternalException, GitAPIException { git.branchCreate() .setName("master") .setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM) .setStartPoint("origin/master") .setForce(true) .call(); }
protected void execute(ExecutionEvent event, final Repository repository, final String branchRef) throws ExecutionException { //check if local branch exists final String branchName = branchRef.substring("refs/heads/".length()); //$NON-NLS-1$ try { if (repository.getRef(branchRef) == null) { //create local branch String remoteBranchRef = "refs/remotes/origin/" + branchName; //$NON-NLS-1$ Ref remoteRef = repository.getRef(remoteBranchRef); if (remoteRef == null) { throw new RuntimeException(MessageFormat.format( "Remote branch {0} doesn't exist", remoteBranchRef)); } new Git(repository).branchCreate(). setName(branchName). setStartPoint(remoteBranchRef). setUpstreamMode(SetupUpstreamMode.TRACK). call(); repository.getConfig().setBoolean("branch", branchName, "rebase", true); //$NON-NLS-1$//$NON-NLS-2$ } } catch (Exception e1) { RepositoryUtils.handleException(e1); return; } BranchOperationUI op = BranchOperationUI.checkout(repository, branchRef) .doneCallback( new DoneCallback() { public void done(CheckoutResult result) { if (result.getStatus() == CheckoutResult.Status.OK) { String newUpstreamRef = branchRef + ":refs/for/" + branchName; //$NON-NLS-1$ repository.getConfig().setString("remote", "origin", "push", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ newUpstreamRef); //ensure in rebase mode repository.getConfig().setBoolean("branch", branchName, "rebase", true); //$NON-NLS-1$//$NON-NLS-2$ try { repository.getConfig().save(); } catch (IOException e) { RepositoryUtils.handleException(e); } } } }); op.start(); }
private void trackBranch(Git git, CheckoutCommand checkout, String label) { checkout.setCreateBranch(true).setName(label) .setUpstreamMode(SetupUpstreamMode.TRACK) .setStartPoint("origin/" + label); }
/** * Git Plugin * * Test that show that a ready/feature_1 branch get integrated into master * * Pretested integration: * - 'Integration branch' : master (default) * - 'Repository name' : origin (default) * - 'Strategy' : Squash Commit * * GitSCM: * - 'Name' : (empty) * * Workflow * - Create a repository containing a 'ready' branch. * - The build is triggered. * * Results * - We expect that the plugin triggers, and that the commits on ready branch * is merged into our integration branch master and build result becomes SUCCESS. * * @throws Exception */ @Test public void oneValidFeatureBranch_1BuildIsTriggeredTheBranchGetsIntegratedBuildMarkedAsSUCCESS() throws Exception { repository = TestUtilsFactory.createValidRepository("test-repo"); File workDir = new File(TestUtilsFactory.WORKDIR,"test-repo"); Git.cloneRepository().setURI("file:///" + repository.getDirectory().getAbsolutePath()).setDirectory(workDir) .setBare(false) .setCloneAllBranches(true) .setNoCheckout(false) .call().close(); Git git = Git.open(workDir); System.out.println("Opening git repository in: " + workDir.getAbsolutePath()); String readmeFromIntegration = FileUtils.readFileToString(new File(workDir,"readme")); git.checkout().setName(FEATURE_BRANCH_NAME).setUpstreamMode(SetupUpstreamMode.TRACK).setCreateBranch(true).call(); final int COMMIT_COUNT_ON_FEATURE_BEFORE_EXECUTION = TestUtilsFactory.countCommits(git); git.checkout().setName("master").setUpstreamMode(SetupUpstreamMode.TRACK).call(); FreeStyleProject project = TestUtilsFactory.configurePretestedIntegrationPlugin(jenkinsRule, TestUtilsFactory.STRATEGY_TYPE.ACCUMULATED, repository); TestUtilsFactory.triggerProject(project); jenkinsRule.waitUntilNoActivityUpTo(60000); RunList<FreeStyleBuild> builds = project.getBuilds(); for (FreeStyleBuild b : builds) { String console = jenkinsRule.createWebClient().getPage(b, "console").asText(); System.out.println(console); } String readmeFileContents = FileUtils.readFileToString(new File(workDir,"readme")); assertEquals(readmeFromIntegration, readmeFileContents); git.pull().call(); final int COMMIT_COUNT_ON_MASTER_AFTER_EXECUTION = TestUtilsFactory.countCommits(git); git.close(); //We assert that 2 commits from branch gets merged + 1 combined merge commit since we do --no-ff assertEquals(COMMIT_COUNT_ON_FEATURE_BEFORE_EXECUTION + 3, COMMIT_COUNT_ON_MASTER_AFTER_EXECUTION); }
/** * Test that show that a ready/feature_1 branch get integrated into master * * Pretested integration: * - 'Integration branch' : master (default) * - 'Repository name' : origin (default) * - 'Strategy' : Squash Commit * * GitSCM: * - 'Name' : (empty) * * Workflow * - Create a repository containing a 'ready' branch. * - The build is triggered. * * Results * - We expect that the plugin triggers, and that the commits on ready branch * is merged into our integration branch master and build result becomes SUCCESS. * * @throws Exception */ @Test public void oneValidFeatureBranchRunningOnSlave_1BuildIsTriggeredTheBranchGetsIntegratedBuildMarkedAsSUCCESS() throws Exception { repository = TestUtilsFactory.createValidRepository("test-repo"); File workDir = new File(TestUtilsFactory.WORKDIR,"test-repo"); Git.cloneRepository().setURI("file:///" + repository.getDirectory().getAbsolutePath()).setDirectory(workDir) .setBare(false) .setCloneAllBranches(true) .setNoCheckout(false) .call().close(); Git git = Git.open(workDir); String fromIntegration = FileUtils.readFileToString(new File(workDir, "readme")); git.checkout().setName(FEATURE_BRANCH_NAME).setUpstreamMode(SetupUpstreamMode.TRACK).setCreateBranch(true).call(); final int COMMIT_COUNT_ON_FEATURE_BEFORE_EXECUTION = TestUtilsFactory.countCommits(git); git.checkout().setName("master").setUpstreamMode(SetupUpstreamMode.TRACK).call(); FreeStyleProject project = TestUtilsFactory.configurePretestedIntegrationPlugin(jenkinsRule, STRATEGY_TYPE.ACCUMULATED, repository); TestUtilsFactory.triggerProject(project); jenkinsRule.waitUntilNoActivityUpTo(60000); int nextBuildNumber = project.getNextBuildNumber(); FreeStyleBuild build = project.getBuildByNumber(nextBuildNumber - 1); Result result = build.getResult(); assertTrue(result.isBetterOrEqualTo(Result.SUCCESS)); String readmeFileContents = FileUtils.readFileToString(new File(workDir, "readme")); assertEquals(fromIntegration, readmeFileContents); git.pull().call(); final int COMMIT_COUNT_ON_MASTER_AFTER_EXECUTION = TestUtilsFactory.countCommits(git); git.close(); assertTrue(COMMIT_COUNT_ON_MASTER_AFTER_EXECUTION == COMMIT_COUNT_ON_FEATURE_BEFORE_EXECUTION + 3); }