List of usage examples for org.eclipse.jgit.api Git branchCreate
public CreateBranchCommand branchCreate()
From source file:playRepository.GitRepositoryTest.java
License:Apache License
@Test public void getMetaDataFromPath() throws Exception { // Given// w ww .j av a 2 s .c o m final String userName = "yobi"; final String projectName = "mytest"; final String branchName = "branch"; final String lightWeightTagName = "tag1"; final String annotatedTagName = "tag2"; String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName; Repository repository = GitRepository.buildGitRepository(userName, projectName + "/"); repository.create(); Git git = new Git(repository); FileUtils.touch(new File(wcPath + "/hello")); FileUtils.touch(new File(wcPath + "/dir/world")); git.add().addFilepattern("hello").call(); git.add().addFilepattern("dir").call(); git.commit().setAuthor("yobi", "yobi@yobi.io").setMessage("test").call(); git.branchCreate().setName(branchName).call(); git.tag().setName(lightWeightTagName).setAnnotated(false).call(); git.tag().setName(annotatedTagName).setAnnotated(true).setMessage("annotated tag").call(); repository.close(); running(support.Helpers.makeTestApplication(), new Runnable() { @Override public void run() { try { // When GitRepository gitRepository = new GitRepository(userName, projectName + "/"); ObjectNode notExistBranch = gitRepository.getMetaDataFromPath("not_exist_branch", ""); ObjectNode root = gitRepository.getMetaDataFromPath(""); ObjectNode dir = gitRepository.getMetaDataFromPath("dir"); ObjectNode file = gitRepository.getMetaDataFromPath("hello"); ObjectNode branch = gitRepository.getMetaDataFromPath(branchName, ""); ObjectNode lightWeightTag = gitRepository.getMetaDataFromPath(lightWeightTagName, ""); ObjectNode annotatedTag = gitRepository.getMetaDataFromPath(annotatedTagName, ""); // Then assertThat(notExistBranch).isNull(); assertThat(root.get("type").textValue()).isEqualTo("folder"); assertThat(root.get("data").get("hello").get("type").textValue()).isEqualTo("file"); assertThat(root.get("data").get("dir").get("type").textValue()).isEqualTo("folder"); assertThat(dir.get("type").textValue()).isEqualTo("folder"); assertThat(dir.get("data").get("world").get("type").textValue()).isEqualTo("file"); assertThat(file.get("type").textValue()).isEqualTo("file"); assertThat(branch.toString()).isEqualTo(root.toString()); assertThat(lightWeightTag.toString()).isEqualTo(root.toString()); assertThat(annotatedTag.toString()).isEqualTo(root.toString()); } catch (Exception e) { throw new RuntimeException(e); } } }); }
From source file:playRepository.GitRepositoryTest.java
License:Apache License
@Test public void deleteBranch() throws IOException, GitAPIException { // given//from ww w. jav a 2s . co m Repository repository = support.Git.createRepository("keesun", "test", false); Git git = new Git(repository); String fileName = "readme.md"; String contents = "Hello World"; String commitMessage = "Initial commit"; addCommit(git, fileName, contents, commitMessage, null); git.branchCreate().setName("develop").setForce(true).call(); GitRepository.checkout(repository, "develop"); // When String branchName = "refs/heads/master"; GitRepository.deleteBranch(repository, branchName); // Then List<Ref> refs = git.branchList().call(); for (Ref ref : refs) { if (ref.getName().equals(branchName)) { fail("deleting branch was failed"); } } }
From source file:playRepository.GitRepositoryTest.java
License:Apache License
@Test public void testGetAllBranches() throws IOException, GitAPIException { FakeApplication app = support.Helpers.makeTestApplication(); Helpers.start(app);/*from w ww .j a v a 2 s .c o m*/ // Given String userName = "wansoon"; String projectName = "test"; Project project = createProject(userName, projectName); project.save(); String email = "test@email.com"; String wcPath = GitRepository.getRepoPrefix() + userName + "/" + "clone-" + projectName + ".git"; String repoPath = wcPath + "/.git"; String dirName = "dir"; String fileName = "file"; GitRepository gitRepository = new GitRepository(userName, projectName); gitRepository.create(); Repository repository = new RepositoryBuilder().setGitDir(new File(repoPath)).build(); repository.create(false); Git git = new Git(repository); FileUtils.forceMkdir(new File(wcPath + "/" + dirName)); FileUtils.touch(new File(wcPath + "/" + fileName)); git.add().addFilepattern(dirName).call(); git.add().addFilepattern(fileName).call(); git.commit().setMessage("test").setAuthor(userName, email).call(); String branchName = "testBranch"; git.branchCreate().setName(branchName).setForce(true).call(); git.push().setRemote(GitRepository.getGitDirectoryURL(project)) .setRefSpecs(new RefSpec(branchName + ":" + branchName)).setForce(true).call(); repository.close(); // When List<GitBranch> gitBranches = gitRepository.getBranches(); gitRepository.close(); // Then assertThat(gitBranches.size()).isEqualTo(1); assertThat(gitBranches.get(0).getShortName()).isEqualTo(branchName); Helpers.stop(app); }
From source file:util.ChkoutCmd.java
License:Eclipse Distribution License
/** * @throws RefAlreadyExistsException/*from w w w .j av a 2s . c om*/ * when trying to create (without force) a branch with a name * that already exists * @throws RefNotFoundException * if the start point or branch can not be found * @throws InvalidRefNameException * if the provided name is <code>null</code> or otherwise * invalid * @throws CheckoutConflictException * if the checkout results in a conflict * @return the newly created branch */ public Ref call() throws GitAPIException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, CheckoutConflictException { checkCallable(); processOptions(); try { if (checkoutAllPaths || !paths.isEmpty()) { checkoutPaths(); status = new CheckoutResult(Status.OK, paths); setCallable(false); return null; } if (createBranch) { Git git = new Git(repo); CreateBranchCommand command = git.branchCreate(); command.setName(name); command.setStartPoint(getStartPoint().name()); if (upstreamMode != null) command.setUpstreamMode(upstreamMode); command.call(); } Ref headRef = repo.getRef(Constants.HEAD); String shortHeadRef = getShortBranchName(headRef); String refLogMessage = "checkout: moving from " + shortHeadRef; //$NON-NLS-1$ ObjectId branch = repo.resolve(name); if (branch == null) throw new RefNotFoundException(MessageFormat.format(JGitText.get().refNotResolved, name)); RevWalk revWalk = new RevWalk(repo); AnyObjectId headId = headRef.getObjectId(); RevCommit headCommit = headId == null ? null : revWalk.parseCommit(headId); RevCommit newCommit = revWalk.parseCommit(branch); RevTree headTree = headCommit == null ? null : headCommit.getTree(); DirCacheCheckout dco; DirCache dc = repo.lockDirCache(); try { dco = new DirCacheCheckout(repo, headTree, dc, newCommit.getTree()); dco.setFailOnConflict(false); try { dco.checkout(); } catch (org.eclipse.jgit.errors.CheckoutConflictException e) { status = new CheckoutResult(Status.CONFLICTS, dco.getConflicts()); throw new CheckoutConflictException(dco.getConflicts(), e); } } finally { dc.unlock(); } Ref ref = repo.getRef(name); if (ref != null && !ref.getName().startsWith(Constants.R_HEADS)) ref = null; String toName = Repository.shortenRefName(name); RefUpdate refUpdate = repo.updateRef(Constants.HEAD, ref == null); refUpdate.setForceUpdate(force); refUpdate.setRefLogMessage(refLogMessage + " to " + toName, false); //$NON-NLS-1$ Result updateResult; if (ref != null) updateResult = refUpdate.link(ref.getName()); else { refUpdate.setNewObjectId(newCommit); updateResult = refUpdate.forceUpdate(); } setCallable(false); boolean ok = false; switch (updateResult) { case NEW: ok = true; break; case NO_CHANGE: case FAST_FORWARD: case FORCED: ok = true; break; default: break; } if (!ok) throw new JGitInternalException( MessageFormat.format(JGitText.get().checkoutUnexpectedResult, updateResult.name())); if (!dco.getToBeDeleted().isEmpty()) { status = new CheckoutResult(Status.NONDELETED, dco.getToBeDeleted()); } else status = new CheckoutResult(new ArrayList<String>(dco.getUpdated().keySet()), dco.getRemoved()); return ref; } catch (IOException ioe) { throw new JGitInternalException(ioe.getMessage(), ioe); } finally { if (status == null) status = CheckoutResult.ERROR_RESULT; } }