Example usage for org.eclipse.jgit.api Git Git

List of usage examples for org.eclipse.jgit.api Git Git

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git Git.

Prototype

public Git(Repository repo) 

Source Link

Document

Construct a new org.eclipse.jgit.api.Git object which can interact with the specified git repository.

Usage

From source file:org.eclipse.egit.core.op.RebaseOperation.java

License:Open Source License

public void execute(IProgressMonitor m) throws CoreException {
    if (result != null)
        throw new CoreException(
                new Status(IStatus.ERROR, Activator.getPluginId(), CoreText.OperationAlreadyExecuted));
    IProgressMonitor monitor;//from w w w  . j a v a2s. c  om
    if (m == null)
        monitor = new NullProgressMonitor();
    else
        monitor = m;

    IWorkspaceRunnable action = new IWorkspaceRunnable() {
        public void run(IProgressMonitor actMonitor) throws CoreException {
            RebaseCommand cmd = new Git(repository).rebase()
                    .setProgressMonitor(new EclipseGitProgressTransformer(actMonitor));
            try {
                if (operation == Operation.BEGIN)
                    result = cmd.setUpstream(ref.getName()).call();
                else
                    result = cmd.setOperation(operation).call();

            } catch (NoHeadException e) {
                throw new CoreException(Activator.error(e.getMessage(), e));
            } catch (RefNotFoundException e) {
                throw new CoreException(Activator.error(e.getMessage(), e));
            } catch (JGitInternalException e) {
                throw new CoreException(Activator.error(e.getMessage(), e));
            } catch (GitAPIException e) {
                throw new CoreException(Activator.error(e.getMessage(), e));
            }
        }
    };
    ResourcesPlugin.getWorkspace().run(action, monitor);
}

From source file:org.eclipse.egit.core.op.RemoveFromIndexOperation.java

License:Open Source License

private static GitCommand<?> prepareCommand(Repository repository, Collection<String> paths) {
    Git git = new Git(repository);
    if (hasHead(repository)) {
        ResetCommand resetCommand = git.reset();
        resetCommand.setRef(HEAD);/*from  w  ww  .jav a  2  s. c om*/
        for (String path : paths)
            resetCommand.addPath(getCommandPath(path));
        return resetCommand;
    } else {
        RmCommand rmCommand = git.rm();
        rmCommand.setCached(true);
        for (String path : paths)
            rmCommand.addFilepattern(getCommandPath(path));
        return rmCommand;
    }
}

From source file:org.eclipse.egit.core.op.RenameBranchOperation.java

License:Open Source License

public void execute(IProgressMonitor m) throws CoreException {
    IProgressMonitor monitor;/*from   w w w .java  2 s. c o  m*/
    if (m == null)
        monitor = new NullProgressMonitor();
    else
        monitor = m;

    IWorkspaceRunnable action = new IWorkspaceRunnable() {
        public void run(IProgressMonitor actMonitor) throws CoreException {
            String taskName = NLS.bind(CoreText.RenameBranchOperation_TaskName, branch.getName(), newName);
            actMonitor.beginTask(taskName, 1);
            try {
                new Git(repository).branchRename().setOldName(branch.getName()).setNewName(newName).call();
            } catch (JGitInternalException e) {
                throw new CoreException(Activator.error(e.getMessage(), e));
            } catch (RefNotFoundException e) {
                throw new CoreException(Activator.error(e.getMessage(), e));
            } catch (InvalidRefNameException e) {
                throw new CoreException(Activator.error(e.getMessage(), e));
            } catch (RefAlreadyExistsException e) {
                throw new CoreException(Activator.error(e.getMessage(), e));
            } catch (DetachedHeadException e) {
                throw new CoreException(Activator.error(e.getMessage(), e));
            }
            actMonitor.worked(1);
            actMonitor.done();
        }
    };
    // lock workspace to protect working tree changes
    ResourcesPlugin.getWorkspace().run(action, monitor);
}

From source file:org.eclipse.egit.core.op.RevertCommitOperation.java

License:Open Source License

public void execute(IProgressMonitor m) throws CoreException {
    IProgressMonitor monitor = m != null ? m : new NullProgressMonitor();
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        public void run(IProgressMonitor pm) throws CoreException {
            pm.beginTask("", 2); //$NON-NLS-1$

            pm.subTask(MessageFormat.format(CoreText.RevertCommitOperation_reverting, commit.name()));
            RevertCommand command = new Git(repo).revert().include(commit);
            try {
                newHead = command.call();
                reverted = command.getRevertedRefs();
                result = command.getFailingResult();
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            }//  w w w  .j a  v a2s .c o  m
            pm.worked(1);

            ProjectUtil.refreshValidProjects(ProjectUtil.getValidOpenProjects(repo),
                    new SubProgressMonitor(pm, 1));

            pm.done();
        }
    };
    ResourcesPlugin.getWorkspace().run(action, monitor);
}

From source file:org.eclipse.egit.core.op.RewordCommitOperation.java

License:Open Source License

@Override
public void execute(IProgressMonitor m) throws CoreException {
    IWorkspaceRunnable action = new IWorkspaceRunnable() {
        @Override//w w  w  .  ja  v  a  2  s.c  o m
        public void run(IProgressMonitor pm) throws CoreException {
            SubMonitor progress = SubMonitor.convert(pm, 2);
            progress.subTask(MessageFormat.format(CoreText.RewordCommitOperation_rewording, commit.name()));

            InteractiveHandler handler = new InteractiveHandler() {
                @Override
                public void prepareSteps(List<RebaseTodoLine> steps) {
                    for (RebaseTodoLine step : steps) {
                        if (step.getCommit().prefixCompare(commit) == 0) {
                            try {
                                step.setAction(RebaseTodoLine.Action.REWORD);
                            } catch (IllegalTodoFileModification e) {
                                // shouldn't happen
                            }
                        }
                    }
                }

                @Override
                public String modifyCommitMessage(String oldMessage) {
                    return newMessage;
                }
            };
            try (Git git = new Git(repository)) {
                git.rebase().setUpstream(commit.getParent(0)).runInteractively(handler)
                        .setOperation(RebaseCommand.Operation.BEGIN).call();
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            }
            progress.worked(1);

            ProjectUtil.refreshValidProjects(ProjectUtil.getValidOpenProjects(repository),
                    progress.newChild(1));
        }
    };

    ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, m);
}

From source file:org.eclipse.egit.core.op.SquashCommitsOperation.java

License:Open Source License

@Override
public void execute(IProgressMonitor m) throws CoreException {

    IWorkspaceRunnable action = new IWorkspaceRunnable() {
        @Override/*  w  w w  .  j a  v  a  2 s .  c o m*/
        public void run(IProgressMonitor pm) throws CoreException {
            SubMonitor progress = SubMonitor.convert(pm, 2);

            progress.subTask(MessageFormat.format(CoreText.SquashCommitsOperation_squashing,
                    Integer.valueOf(commits.size())));

            InteractiveHandler handler = new InteractiveHandler() {
                @Override
                public void prepareSteps(List<RebaseTodoLine> steps) {
                    RevCommit firstCommit = commits.get(0);
                    for (RebaseTodoLine step : steps) {
                        if (isRelevant(step.getCommit())) {
                            try {
                                if (step.getCommit().prefixCompare(firstCommit) == 0)
                                    step.setAction(RebaseTodoLine.Action.PICK);
                                else
                                    step.setAction(RebaseTodoLine.Action.SQUASH);
                            } catch (IllegalTodoFileModification e) {
                                // shouldn't happen
                            }
                        }
                    }
                }

                private boolean isRelevant(AbbreviatedObjectId id) {
                    for (RevCommit commit : commits) {
                        if (id.prefixCompare(commit) == 0)
                            return true;
                    }
                    return false;
                }

                @Override
                public String modifyCommitMessage(String oldMessage) {
                    return messageHandler.modifyCommitMessage(oldMessage);
                }
            };
            try (Git git = new Git(repository)) {
                RebaseCommand command = git.rebase().setUpstream(commits.get(0).getParent(0))
                        .runInteractively(handler).setOperation(RebaseCommand.Operation.BEGIN);
                MergeStrategy strategy = Activator.getDefault().getPreferredMergeStrategy();
                if (strategy != null) {
                    command.setStrategy(strategy);
                }
                command.call();
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            }
            progress.worked(1);

            ProjectUtil.refreshValidProjects(ProjectUtil.getValidOpenProjects(repository),
                    progress.newChild(1));
        }
    };
    ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, m);
}

From source file:org.eclipse.egit.core.storage.GitBlobStorageTest.java

License:Open Source License

@Test
public void testGitFileHistorySingleProjectOk() throws Exception {
    IProgressMonitor progress = new NullProgressMonitor();
    TestProject singleRepoProject = new TestProject(true, "Project-2");
    IProject proj = singleRepoProject.getProject();
    File singleProjectGitDir = new File(proj.getLocation().toFile(), Constants.DOT_GIT);
    if (singleProjectGitDir.exists())
        FileUtils.delete(singleProjectGitDir, FileUtils.RECURSIVE | FileUtils.RETRY);

    Repository singleProjectRepo = FileRepositoryBuilder.create(singleProjectGitDir);
    singleProjectRepo.create();/*from   www.  j av  a  2 s.c om*/

    // Repository must be mapped in order to test the GitFileHistory
    Activator.getDefault().getRepositoryUtil().addConfiguredRepository(singleProjectGitDir);
    ConnectProviderOperation connectOp = new ConnectProviderOperation(proj, singleProjectGitDir);
    connectOp.execute(progress);

    try {
        IFile file = proj.getFile("file");
        file.create(new ByteArrayInputStream("data".getBytes("UTF-8")), 0, progress);
        Git git = new Git(singleProjectRepo);
        git.add().addFilepattern(".").call();
        RevCommit commit = git.commit().setAuthor("JUnit", "junit@jgit.org").setAll(true)
                .setMessage("First commit").call();

        GitFileHistoryProvider fhProvider = new GitFileHistoryProvider();
        IFileHistory fh = fhProvider.getFileHistoryFor(singleRepoProject.getProject(), 0, null);
        assertNotNull(fh);
        assertEquals(fh.getFileRevisions().length, 1);
        assertNotNull(fh.getFileRevision(commit.getId().getName()));
    } finally {
        DisconnectProviderOperation disconnectOp = new DisconnectProviderOperation(
                Collections.singletonList(proj));
        disconnectOp.execute(progress);
        Activator.getDefault().getRepositoryUtil().removeDir(singleProjectGitDir);
        singleProjectRepo.close();
        singleRepoProject.dispose();
    }
}

From source file:org.eclipse.egit.core.synchronize.AbstractCacheTest.java

License:Open Source License

@Before
@Override/*from  ww  w  .  ja  va 2 s.c  o m*/
// copied from org.eclipse.jgit.lib.RepositoryTestCase
public void setUp() throws Exception {
    super.setUp();
    db = createWorkRepository();
    Git git = new Git(db);
    git.commit().setMessage("initial commit").call();
    git.tag().setName(INITIAL_TAG).call();
}

From source file:org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataTest.java

License:Open Source License

@Before
public void setUp() throws Exception {
    super.setUp();

    TestRepository testRepo = new TestRepository(gitDir);
    testRepo.connect(project.project);//from  w ww. j  av  a2 s.c o m
    repo = RepositoryMapping.getMapping(project.project).getRepository();

    // make initial commit
    new Git(repo).commit().setAuthor("JUnit", "junit@jgit.org").setMessage("Initall commit").call();
}

From source file:org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataTest.java

License:Open Source License

@Test
public void shouldReturnSourceMergeForSymbolicRef() throws Exception {
    // given/*from w  w  w .j  a v a 2 s  . c  o m*/
    Git git = new Git(repo);
    git.branchCreate().setName("test").setStartPoint("refs/heads/master")
            .setUpstreamMode(SetupUpstreamMode.TRACK).call();
    git.checkout().setName("test").call();
    GitSynchronizeData gsd = new GitSynchronizeData(repo, HEAD, HEAD, false);

    // when
    String srcMerge = gsd.getSrcMerge();

    // then
    assertThat(srcMerge, is("refs/heads/master"));
}