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

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

Introduction

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

Prototype

public AddCommand add() 

Source Link

Document

Return a command object to execute a Add command

Usage

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;/*from   w  w  w  .  j a v a2  s . co  m*/
    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.emf.compare.diagram.papyrus.tests.egit.fixture.GitTestRepository.java

License:Open Source License

/**
 * Adds all changes and creates a commit.
 * //from   w  ww  . j ava2s  .c  om
 * @param commitMessage
 *            The commit message.
 * @param addDeleted
 *            Specifies whether missing or deleted files should added to
 *            index, too.
 * @return The reference to the created commit.
 * @throws Exception
 *             if anything goes wrong.
 */
public RevCommit addAllAndCommit(String commitMessage, boolean addDeleted) throws Exception {
    Git git = new Git(repository);
    try {
        git.add().addFilepattern(".").call();
        if (addDeleted) {
            addDeletedFiles();
        }
        return commit(commitMessage);
    } finally {
        git.close();
    }
}

From source file:org.eclipse.emf.compare.diagram.papyrus.tests.egit.fixture.GitTestRepository.java

License:Open Source License

/**
 * Adds all changes and amends the latest commit, also changing its message to the given message.
 * /*  ww w . ja v a 2s. c  o  m*/
 * @param message
 *            the amended commit message, must not be null
 * @return The RevCommit of the amended commit.
 * @throws Exception
 *             if anything goes wrong.
 */
public RevCommit addAllAndAmend(String message) throws Exception {
    Git git = new Git(repository);
    try {
        git.add().addFilepattern(".").call();
        return git.commit().setAmend(true).setMessage(message).call();
    } finally {
        git.close();
    }
}

From source file:org.eclipse.emf.compare.diagram.papyrus.tests.egit.fixture.GitTestRepository.java

License:Open Source License

/**
 * Adds the given resource to the index/*from  w w w  .j  a  va 2 s.  c o m*/
 *
 * @param resource
 * @throws CoreException
 * @throws IOException
 * @throws GitAPIException
 * @throws NoFilepatternException
 */
public void addToIndex(IResource resource)
        throws CoreException, IOException, NoFilepatternException, GitAPIException {
    String repoPath = getRepoRelativePath(resource.getLocation().toString());
    Git git = new Git(repository);
    try {
        git.add().addFilepattern(repoPath).call();
    } finally {
        git.close();
    }
}

From source file:org.eclipse.emf.compare.diagram.papyrus.tests.egit.fixture.GitTestRepository.java

License:Open Source License

/**
 * Adds the given resources to the index
 * //from w  w w.  j  a v a2s  .  c om
 * @param resources
 *            Resources to add to the index.
 */
public void addToIndex(IResource... resources)
        throws CoreException, IOException, NoFilepatternException, GitAPIException {
    Git git = new Git(repository);
    try {
        for (IResource resource : resources) {
            String repoPath = getRepoRelativePath(resource.getLocation().toString());
            git.add().addFilepattern(repoPath).call();
        }
    } finally {
        git.close();
    }
}

From source file:org.eclipse.emf.compare.diagram.papyrus.tests.egit.fixture.GitTestRepository.java

License:Open Source License

/**
 * Adds file to version control/*from w  w  w.j  a v  a  2s .  co  m*/
 *
 * @param file
 * @throws IOException
 * @throws GitAPIException
 * @throws NoFilepatternException
 */
public void track(File file) throws IOException, NoFilepatternException, GitAPIException {
    String repoPath = getRepoRelativePath(new Path(file.getPath()).toString());
    Git git = new Git(repository);
    try {
        git.add().addFilepattern(repoPath).call();
    } finally {
        git.close();
    }
}

From source file:org.eclipse.emf.compare.ide.ui.tests.egit.fixture.GitTestRepository.java

License:Open Source License

public RevCommit addAllAndCommit(String commitMessage) throws Exception {
    Git git = new Git(repository);
    try {/*from   w w w.  j  av a2  s  . c om*/
        git.add().addFilepattern(".").call();
        return commit(commitMessage);
    } finally {
        git.close();
    }
}

From source file:org.eclipse.emf.compare.ide.ui.tests.merge.DirCacheResourceVariantTreeProviderTest.java

License:Open Source License

private void addToIndex(Repository repository, IFile file)
        throws CoreException, IOException, NoFilepatternException, GitAPIException {
    String filePath = file.getProject().getName() + "/" + file.getProjectRelativePath(); //$NON-NLS-1$
    Git git = new Git(repository);
    try {//from ww  w .j a  va 2 s . co  m
        git.add().addFilepattern(filePath).call();
    } finally {
        git.close();
    }
}

From source file:org.eclipse.mylyn.gerrit.tests.support.GerritProject.java

License:Open Source License

public void addFile(String fileName, String text) throws Exception {
    Git gitProject = getGitProject();
    CommonTestUtil.write(new File(getFolder(), fileName).getAbsolutePath(), new StringBuffer(text));
    gitProject.add().addFilepattern(fileName).call();
}

From source file:org.eclipse.mylyn.gerrit.tests.support.GerritProject.java

License:Open Source License

public void addFile(String fileName, File file) throws Exception {
    Git gitProject = getGitProject();
    CommonTestUtil.copy(file, new File(getFolder(), fileName));
    gitProject.add().addFilepattern(fileName).call();
}