List of usage examples for org.eclipse.jgit.api Git getRepository
public Repository getRepository()
From source file:org.jboss.forge.addon.git.GitUtilsTest.java
License:Open Source License
@Test public void shouldCherryPickChangesWithoutNewCommit() throws Exception { // git init//from www . java2s . co m // create new branch (b2) but stay on master // commit new file #1 // switch to other branch // commit new file #2 // switch to master // cherry pick (without committing) the latest commit from b2 // verify file #2 exists // verify number of commits (2 on master branch) String[] branchNames = { "master", "branch_two" }; String[] files = { "test1.txt", "test2.txt" }; Project project = projectFactory.createTempProject(); Git repo = gitUtils.init(project.getRoot().reify(DirectoryResource.class)); gitUtils.addAll(repo); gitUtils.commitAll(repo, "initial commit"); repo.branchCreate().setName(branchNames[1]).call(); FileResource<?> file0 = project.getRoot().getChild(files[0]).reify(FileResource.class); file0.createNewFile(); gitUtils.add(repo, files[0]); gitUtils.commit(repo, "file added on " + branchNames[0]); gitUtils.switchBranch(repo, branchNames[1]); FileResource<?> file1 = project.getRoot().getChild(files[1]).reify(FileResource.class); file1.createNewFile(); gitUtils.add(repo, files[1]); gitUtils.commit(repo, "file added on " + branchNames[1]); gitUtils.getLogForCurrentBranch(repo); gitUtils.switchBranch(repo, branchNames[0]); Ref branch2Ref = repo.getRepository().findRef(branchNames[1]); gitUtils.cherryPickNoMerge(repo, branch2Ref); // assert file2 exists Assert.assertTrue("file from cherry picked commit should exist", project.getRoot().getChild(files[1]).exists()); // assert number of commits (on master). Should be 2 (cherry pick produced no merge) List<String> log = gitUtils.getLogForCurrentBranch(repo); Assert.assertEquals("wrong number of commits", 2, log.size()); }
From source file:org.jboss.forge.addon.git.GitUtilsTest.java
License:Open Source License
@Test public void shouldNotCrashWhenCherryPickNoMergeIsCalledOnLastCommit() throws Exception { String[] branchNames = { "master" }; String[] files = { "test1.txt" }; List<String> commits = null; CherryPickResult cherryPickResult = null; Project project = projectFactory.createTempProject(); Git repo = gitUtils.init(project.getRoot().reify(DirectoryResource.class)); gitUtils.addAll(repo);//from w ww . j av a2s.c om gitUtils.commitAll(repo, "initial commit"); FileResource<?> file0 = project.getRoot().getChild(files[0]).reify(FileResource.class); file0.createNewFile(); gitUtils.add(repo, files[0]); gitUtils.commit(repo, "file added on " + branchNames[0]); commits = gitUtils.getLogForCurrentBranch(repo); Assert.assertEquals("Wrong number of commits in log", 2, commits.size()); cherryPickResult = gitUtils.cherryPickNoMerge(repo, repo.getRepository().findRef(branchNames[0])); Assert.assertEquals("Wrong cherrypick status", CherryPickResult.CherryPickStatus.OK, cherryPickResult.getStatus()); gitUtils.resetHard(repo, "HEAD^1"); commits = gitUtils.getLogForCurrentBranch(repo); Assert.assertEquals("Wrong number of commits in log", 1, commits.size()); try { gitUtils.cherryPickNoMerge(repo, repo.getRepository().findRef(branchNames[0])); Assert.fail("Expected exception: " + CantMergeCommitException.class); } catch (CantMergeCommitException cmce) { // Expected } }
From source file:org.jboss.forge.addon.manager.impl.ui.AddonGitBuildAndInstallCommand.java
License:Open Source License
private void cloneTo(GitUtils gitUtils, DirectoryResource projectRoot) throws GitAPIException, IOException { Git git = null; try {/*ww w . ja v a2 s. c om*/ git = gitUtils.clone(projectRoot, url.getValue().getFullyQualifiedName()); if (ref.hasValue()) { String refName = ref.getValue(); String currentBranch = git.getRepository().getBranch(); // No need to checkout if the branch name is the same if (!currentBranch.equals(refName)) { git.checkout().setCreateBranch(true).setName(refName).setUpstreamMode(SetupUpstreamMode.TRACK) .setStartPoint("origin/" + refName).call(); } } } finally { gitUtils.close(git); } }
From source file:org.jboss.forge.git.GitUtils.java
License:Open Source License
/** * Initialize a new git repository.//from w ww . j a v a 2 s.com * * @param dir The directory in which to create a new .git/ folder and repository. */ public static Git init(final DirectoryResource dir) throws IOException { FileResource<?> gitDir = dir.getChildDirectory(".git").reify(FileResource.class); gitDir.mkdirs(); RepositoryBuilder db = new RepositoryBuilder().setGitDir(gitDir.getUnderlyingResourceObject()).setup(); Git git = new Git(db.build()); git.getRepository().create(); return git; }
From source file:org.jboss.forge.git.GitUtils.java
License:Open Source License
public static String getCurrentBranchName(final Git repo) throws IOException { return repo.getRepository().getBranch(); }
From source file:org.jboss.forge.git.GitUtils.java
License:Open Source License
public static Iterable<RevCommit> getLogForBranch(final Git repo, String branchName) throws GitAPIException, IOException { String oldBranch = repo.getRepository().getBranch(); repo.checkout().setName(branchName).call(); Iterable<RevCommit> commits = repo.log().call(); repo.checkout().setName(oldBranch).call(); return commits; }
From source file:org.jboss.forge.git.GitUtils.java
License:Open Source License
public static CherryPickResult cherryPickNoMerge(final Git git, Ref src) throws GitAPIException, CantMergeCommitWithZeroParentsException { // Does the same as the original git-cherryPick // except commiting after running merger Repository repo = git.getRepository(); RevCommit newHead = null;/*from w w w . j a v a 2 s . c om*/ List<Ref> cherryPickedRefs = new LinkedList<Ref>(); RevWalk revWalk = new RevWalk(repo); try { // get the head commit Ref headRef = repo.getRef(Constants.HEAD); if (headRef == null) throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported); RevCommit headCommit = revWalk.parseCommit(headRef.getObjectId()); newHead = headCommit; // get the commit to be cherry-picked // handle annotated tags ObjectId srcObjectId = src.getPeeledObjectId(); if (srcObjectId == null) srcObjectId = src.getObjectId(); RevCommit srcCommit = revWalk.parseCommit(srcObjectId); // get the parent of the commit to cherry-pick if (srcCommit.getParentCount() == 0) throw new CantMergeCommitWithZeroParentsException("Commit with zero parents cannot be merged"); if (srcCommit.getParentCount() > 1) throw new MultipleParentsNotAllowedException( MessageFormat.format(JGitText.get().canOnlyCherryPickCommitsWithOneParent, srcCommit.name(), Integer.valueOf(srcCommit.getParentCount()))); RevCommit srcParent = srcCommit.getParent(0); revWalk.parseHeaders(srcParent); ResolveMerger merger = (ResolveMerger) MergeStrategy.RESOLVE.newMerger(repo); merger.setWorkingTreeIterator(new FileTreeIterator(repo)); merger.setBase(srcParent.getTree()); if (merger.merge(headCommit, srcCommit)) { DirCacheCheckout dco = new DirCacheCheckout(repo, headCommit.getTree(), repo.lockDirCache(), merger.getResultTreeId()); dco.setFailOnConflict(true); dco.checkout(); cherryPickedRefs.add(src); } else { if (merger.failed()) return new CherryPickResult(merger.getFailingPaths()); // there are merge conflicts String message = new MergeMessageFormatter().formatWithConflicts(srcCommit.getFullMessage(), merger.getUnmergedPaths()); repo.writeCherryPickHead(srcCommit.getId()); repo.writeMergeCommitMsg(message); return CherryPickResult.CONFLICT; } } catch (IOException e) { throw new JGitInternalException( MessageFormat.format(JGitText.get().exceptionCaughtDuringExecutionOfCherryPickCommand, e), e); } finally { revWalk.release(); } return new CherryPickResult(newHead, cherryPickedRefs); }
From source file:org.jboss.forge.git.GitUtilsTest.java
License:Open Source License
@Test public void testCreateRepo() throws Exception { Project project = initializeJavaProject(); Git repo = GitUtils.init(project.getProjectRoot()); Assert.assertTrue(repo.getRepository().getDirectory().exists()); }
From source file:org.jboss.forge.git.GitUtilsTest.java
License:Open Source License
@Test public void testGetTags() throws Exception { Project project = initializeJavaProject(); Git repo = GitUtils.init(project.getProjectRoot()); Map<String, Ref> tags = repo.getRepository().getTags(); Assert.assertTrue(tags.isEmpty());//from w w w . ja v a 2 s . c o m }
From source file:org.jboss.forge.git.GitUtilsTest.java
License:Open Source License
@Test public void shouldCherryPickChanges() throws Exception { // git init//from w ww . ja va 2 s .c o m // create new branch (b2) but stay on master // commit new file #1 // switch to other branch // commit new file #2 // switch to master // cherry pick the latest commit from b2 // verify file #2 exists // verify number of commits (3 on master branch) String[] branchNames = { "master", "branch_two" }; String[] files = { "test1.txt", "test2.txt" }; Project project = initializeJavaProject(); Git repo = GitUtils.init(project.getProjectRoot()); GitUtils.addAll(repo); GitUtils.commitAll(repo, "initial commit"); repo.branchCreate().setName(branchNames[1]).call(); FileResource<?> file0 = project.getProjectRoot().getChild(files[0]).reify(FileResource.class); file0.createNewFile(); GitUtils.add(repo, files[0]); GitUtils.commit(repo, "file added on " + branchNames[0]); GitUtils.switchBranch(repo, branchNames[1]); FileResource<?> file1 = project.getProjectRoot().getChild(files[1]).reify(FileResource.class); file1.createNewFile(); GitUtils.add(repo, files[1]); GitUtils.commit(repo, "file added on " + branchNames[1]); GitUtils.getLogForCurrentBranch(repo); GitUtils.switchBranch(repo, branchNames[0]); Ref branch2Ref = repo.getRepository().getRef(branchNames[1]); GitUtils.cherryPick(repo, branch2Ref); // assert file2 exists Assert.assertTrue("file from cherry picked commit should exist", project.getProjectRoot().getChild(files[1]).exists()); // assert number of commits (on master). Should be 3, latest created by the merge from cherry pick List<String> log = GitUtils.getLogForCurrentBranch(repo); Assert.assertEquals("wrong number of commits", 3, log.size()); }