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.ui.internal.fetch.FetchGerritChangePage.java

License:Open Source License

private void createBranch(final String textForBranch, RevCommit commit, IProgressMonitor monitor)
        throws CoreException, GitAPIException {
    monitor.setTaskName(UIText.FetchGerritChangePage_CreatingBranchTaskName);
    CreateLocalBranchOperation bop = new CreateLocalBranchOperation(repository, textForBranch, commit);
    bop.execute(monitor);//from  www  . j  a  va 2s. c  o  m
    CheckoutCommand co = new Git(repository).checkout();
    try {
        co.setName(textForBranch).call();
    } catch (CheckoutConflictException e) {
        final CheckoutResult result = co.getResult();

        if (result.getStatus() == Status.CONFLICTS) {
            final Shell shell = getWizard().getContainer().getShell();

            shell.getDisplay().asyncExec(new Runnable() {
                public void run() {
                    new CheckoutConflictDialog(shell, repository, result.getConflictList()).open();
                }
            });
        }
    }
    monitor.worked(1);
}

From source file:org.eclipse.egit.ui.internal.history.command.CherryPickHandler.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    RevCommit commit = (RevCommit) getSelection(getPage()).getFirstElement();
    RevCommit newHead;/*from w w w  . j a v a 2  s .c  o  m*/
    Repository repo = getRepository(event);

    CherryPickCommand cherryPick;
    Git git = new Git(repo);
    try {
        cherryPick = git.cherryPick().include(commit.getId());
        newHead = cherryPick.call();
    } catch (Exception e) {
        throw new ExecutionException(CoreText.CherryPickOperation_InternalError, e);
    }
    if (newHead == null)
        throw new ExecutionException(CoreText.CherryPickOperation_Failed);
    return null;
}

From source file:org.eclipse.egit.ui.internal.operations.GitScopeOperation.java

License:Open Source License

private boolean hasChanged(IResource resource) {
    boolean hasChanged = false;
    RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
    try {//from  ww w  .  j a  v  a 2  s  .  co m
        Repository repository = mapping.getRepository();
        Status repoStatus = new Git(repository).status().call();
        String path = resource.getFullPath().removeFirstSegments(1).toOSString();
        hasChanged = repoStatus.getAdded().contains(path) || repoStatus.getChanged().contains(path)
                || repoStatus.getModified().contains(path) || repoStatus.getRemoved().contains(path)
                || repoStatus.getUntracked().contains(path);
    } catch (Exception e) {
        Activator.logError(UIText.GitScopeOperation_couldNotDetermineState, e);
    }
    return hasChanged;
}

From source file:org.eclipse.egit.ui.internal.push.PushBranchWizardTest.java

License:Open Source License

@Test
public void pushHeadToExistingRemote() throws Exception {
    try (Git git = new Git(repository)) {
        AnyObjectId head = repository.resolve(Constants.HEAD);
        git.checkout().setName(head.name()).call();
    }/*from   w  ww.  j a  v  a 2 s .  co m*/

    PushBranchWizardTester wizard = PushBranchWizardTester.startWizard(selectProject(), Constants.HEAD);
    wizard.selectRemote("fetch");
    wizard.enterBranchName("foo");
    wizard.next();
    wizard.finish();

    assertBranchPushed("foo", remoteRepository);
}

From source file:org.eclipse.egit.ui.internal.push.PushTagsWizardTest.java

License:Open Source License

@Test
public void pushTag() throws Exception {
    Git git = new Git(repository);
    git.tag().setName("foo").setMessage("Foo tag").call();

    PushTagsWizardTester wizard = PushTagsWizardTester.startWizard(selectProject());
    wizard.selectRemote("push");
    wizard.assertNextDisabled();/*from   w ww.j  a v  a2 s . c o  m*/
    wizard.checkTag("foo");
    wizard.next();
    wizard.finish();

    assertTagPushed("foo", remoteRepository);
}

From source file:org.eclipse.egit.ui.internal.reflog.ReflogViewContentProvider.java

License:Open Source License

public Object[] getElements(Object inputElement) {
    if (inputElement instanceof ReflogInput) {
        ReflogInput input = (ReflogInput) inputElement;
        ReflogCommand command = new Git(input.repository).reflog();
        command.setRef(input.ref);//w w w  . j ava2 s.c  o  m
        try {
            return command.call().toArray();
        } catch (Exception e) {
            Activator.logError("Error running reflog command", e); //$NON-NLS-1$
        }
    }
    return new Object[0];
}

From source file:org.eclipse.egit.ui.internal.repository.tree.command.AddToIndexCommand.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    List<FileNode> selectedNodes = getSelectedNodes(event);
    if (selectedNodes.isEmpty() || selectedNodes.get(0).getRepository() == null)
        return null;

    Repository repository = selectedNodes.get(0).getRepository();
    IPath workTreePath = new Path(repository.getWorkTree().getAbsolutePath());

    AddCommand addCommand = new Git(repository).add();

    Collection<IPath> paths = getSelectedFileAndFolderPaths(event);
    for (IPath path : paths) {
        String repoRelativepath;//  w w w  .j  a v  a2  s .  c om
        if (path.equals(workTreePath))
            repoRelativepath = "."; //$NON-NLS-1$
        else
            repoRelativepath = path.removeFirstSegments(path.matchingFirstSegments(workTreePath))
                    .setDevice(null).toString();
        addCommand.addFilepattern(repoRelativepath);
    }
    try {
        addCommand.call();
    } catch (GitAPIException e) {
        Activator.logError(UIText.AddToIndexCommand_addingFilesFailed, e);
    }
    return null;
}

From source file:org.eclipse.egit.ui.internal.staging.StagingView.java

License:Open Source License

private void replaceWith(String[] files, boolean headRevision) {
    if (files == null || files.length == 0)
        return;// www  . j a  va2s .  com
    CheckoutCommand checkoutCommand = new Git(currentRepository).checkout();
    if (headRevision)
        checkoutCommand.setStartPoint(Constants.HEAD);
    for (String path : files)
        checkoutCommand.addPath(path);
    try {
        checkoutCommand.call();
    } catch (Exception e) {
        Activator.handleError(UIText.StagingView_checkoutFailed, e, true);
    }
}

From source file:org.eclipse.egit.ui.internal.staging.StagingView.java

License:Open Source License

private void stage(IStructuredSelection selection) {
    Git git = new Git(currentRepository);
    RmCommand rm = null;// w w  w.j av a2s  .  com
    Iterator iterator = selection.iterator();
    List<String> addPaths = new ArrayList<String>();
    while (iterator.hasNext()) {
        Object element = iterator.next();
        if (element instanceof StagingEntry) {
            StagingEntry entry = (StagingEntry) element;
            switch (entry.getState()) {
            case ADDED:
            case CHANGED:
            case REMOVED:
                // already staged
                break;
            case CONFLICTING:
            case MODIFIED:
            case PARTIALLY_MODIFIED:
            case UNTRACKED:
                addPaths.add(entry.getPath());
                break;
            case MISSING:
                if (rm == null)
                    rm = git.rm().setCached(true);
                rm.addFilepattern(entry.getPath());
                break;
            }
        } else {
            IResource resource = AdapterUtils.adapt(element, IResource.class);
            if (resource != null) {
                RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
                if (mapping != null && mapping.getRepository() == currentRepository) {
                    String path = mapping.getRepoRelativePath(resource);
                    // If resource corresponds to root of working directory
                    if ("".equals(path)) //$NON-NLS-1$
                        addPaths.add("."); //$NON-NLS-1$
                    else
                        addPaths.add(path);
                }
            }
        }
    }

    if (!addPaths.isEmpty())
        try {
            AddCommand add = git.add();
            for (String addPath : addPaths)
                add.addFilepattern(addPath);
            add.call();
        } catch (NoFilepatternException e1) {
            // cannot happen
        } catch (JGitInternalException e1) {
            Activator.handleError(e1.getCause().getMessage(), e1.getCause(), true);
        } catch (Exception e1) {
            Activator.handleError(e1.getMessage(), e1, true);
        }
    if (rm != null)
        try {
            rm.call();
        } catch (NoFilepatternException e) {
            // cannot happen
        } catch (JGitInternalException e) {
            Activator.handleError(e.getCause().getMessage(), e.getCause(), true);
        } catch (Exception e) {
            Activator.handleError(e.getMessage(), e, true);
        }
}

From source file:org.eclipse.egit.ui.internal.synchronize.model.GitModelCacheTest.java

License:Open Source License

@Test
public void shouldReturnChildren() throws Exception {
    FileRepository repo = lookupRepository(leftRepoFile);
    writeTrashFile(repo, "dir/a.txt", "trash");
    writeTrashFile(repo, "dir/b.txt", "trash");
    writeTrashFile(repo, "dir/c.txt", "trash");
    writeTrashFile(repo, "dir/d.txt", "trash");
    new Git(repo).add().addFilepattern("dir").call();

    Map<String, Change> changes = StagedChangeCache.build(repo);
    assertEquals(4, changes.size());//ww  w  . ja  va  2s  .  c  o  m

    GitModelCache cache = new GitModelCache(createModelRepository(), repo, changes);

    GitModelObject[] cacheChildren = cache.getChildren();
    assertEquals(1, cacheChildren.length);
    GitModelObject dir = cacheChildren[0];
    assertEquals("dir", dir.getName());

    GitModelObject[] dirChildren = dir.getChildren();
    assertEquals(4, dirChildren.length);
}