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

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

Introduction

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

Prototype

public CommitCommand commit() 

Source Link

Document

Return a command object to execute a Commit command

Usage

From source file:org.eclipse.egit.ui.test.team.actions.CompareActionsTest.java

License:Open Source License

@Test
public void testCompareWithPreviousWithMerge() throws Exception {
    Repository repo = lookupRepository(repositoryFile);

    Git git = new Git(repo);
    ObjectId masterId = repo.resolve("refs/heads/master");
    Ref newBranch = git.checkout().setCreateBranch(true).setStartPoint(commitOfTag.name()).setName("toMerge")
            .call();/*  ww w  . j a v a  2s .com*/
    ByteArrayInputStream bis = new ByteArrayInputStream("Modified".getBytes());
    ResourcesPlugin.getWorkspace().getRoot().getProject(PROJ1).getFolder(FOLDER).getFile(FILE2).setContents(bis,
            false, false, null);
    bis.close();
    git.commit().setAll(true).setMessage("To be merged").call();
    git.merge().include(masterId).call();
    String menuLabel = util.getPluginLocalizedValue("CompareWithPreviousAction.label");
    SWTBotShell selectDialog = openCompareWithDialog(menuLabel, UIText.CommitSelectDialog_WindowTitle);
    assertEquals(2, selectDialog.bot().table().rowCount());
    selectDialog.close();
    // cleanup: checkout again master and delete merged branch
    git.checkout().setName("refs/heads/master").call();
    git.branchDelete().setBranchNames(newBranch.getName()).setForce(true).call();
}

From source file:org.eclipse.egit.ui.test.team.actions.ReplaceActionsTest.java

License:Open Source License

@Test
public void testReplaceWithPreviousWithMerge() throws Exception {
    Repository repo = lookupRepository(repositoryFile);
    Git git = new Git(repo);
    ObjectId masterId = repo.resolve("refs/heads/master");
    Ref newBranch = git.checkout().setCreateBranch(true).setStartPoint(commitOfTag.name()).setName("toMerge")
            .call();/*w  ww .ja  v a2 s.c  o  m*/
    ByteArrayInputStream bis = new ByteArrayInputStream("Modified".getBytes());
    ResourcesPlugin.getWorkspace().getRoot().getProject(PROJ1).getFolder(FOLDER).getFile(FILE2).setContents(bis,
            false, false, null);
    bis.close();
    PersonIdent committer = new PersonIdent("COMMITTER", "a.c@d", new Date(),
            TimeZone.getTimeZone("GMT-03:30"));
    git.commit().setAll(true).setCommitter(committer).setMessage("To be merged").call();
    git.merge().include(masterId).call();
    String newContent = getTestFileContent();
    String menuLabel = util.getPluginLocalizedValue("replaceWithPreviousVersionAction.label");
    clickReplaceWith(menuLabel);
    bot.shell(UIText.DiscardChangesAction_confirmActionTitle).bot().button(IDialogConstants.OK_LABEL).click();
    SWTBotShell selectDialog = bot.shell(UIText.CommitSelectDialog_WindowTitle);
    assertEquals(2, selectDialog.bot().table().rowCount());
    selectDialog.close();
    // we have closed, so nothing should have changed
    String oldContent = getTestFileContent();
    assertTrue(newContent.equals(oldContent));

    clickReplaceWith(menuLabel);
    bot.shell(UIText.DiscardChangesAction_confirmActionTitle).bot().button(IDialogConstants.OK_LABEL).click();
    selectDialog = bot.shell(UIText.CommitSelectDialog_WindowTitle);
    selectDialog.bot().table().select(1);
    selectDialog.bot().button(IDialogConstants.OK_LABEL).click();
    TestUtil.joinJobs(org.eclipse.egit.ui.JobFamilies.DISCARD_CHANGES);
    oldContent = getTestFileContent();
    assertFalse(newContent.equals(oldContent));
    // cleanup: checkout again master and delete merged branch
    git.checkout().setName("refs/heads/master").call();
    git.branchDelete().setBranchNames(newBranch.getName()).setForce(true).call();
}

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.
 * /*w ww  . java 2 s.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

/**
 * Commits the current index./*from  ww w .j  ava  2  s  .  co m*/
 * 
 * @param message
 *            commit message
 * @return commit object
 */
public RevCommit commit(String message) throws Exception {
    Git git = new Git(repository);
    try {
        CommitCommand commitCommand = git.commit();
        commitCommand.setAuthor("J. Git", "j.git@egit.org");
        commitCommand.setCommitter(commitCommand.getAuthor());
        commitCommand.setMessage(message);
        return commitCommand.call();
    } finally {
        git.close();
    }
}

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

License:Open Source License

@Override
@Before//from  w  w  w.  j av  a 2 s  . c om
public void setUp() throws Exception {
    super.setUp();

    iProject = project.getProject();
    repo = RepositoryMapping.getMapping(iProject).getRepository();

    // make initial commit
    Git git = new Git(repo);
    try {
        git.commit().setAuthor("JUnit", "junit@jgit.org").setMessage("Initial commit").call(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    } finally {
        git.close();
    }

    // Select strategy recursive as preferred strategy
    InstanceScope.INSTANCE.getNode(Activator.getPluginId()).put(GitCorePreferences.core_preferredMergeStrategy,
            "model recursive"); //$NON-NLS-1$
}

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

License:Open Source License

@Override
@Before/*from www.j  a v  a 2 s. c  o  m*/
public void setUp() throws Exception {
    super.setUp();
    repository.disconnect(iProject);

    project = new TestProject("a/b/deepProject");
    iProject = project.getProject();
    repository.connect(iProject);
    repo = RepositoryMapping.getMapping(iProject).getRepository();

    // make initial commit
    Git git = new Git(repo);
    try {
        git.commit().setAuthor("JUnit", "junit@jgit.org").setMessage("Initial commit").call();
    } finally {
        git.close();
    }
}

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

License:Open Source License

@Before
@Override/*  w w  w . j  a  va2 s  . c o m*/
public void setUp() throws Exception {
    super.setUp();

    iProject = project.getProject();
    repo = RepositoryMapping.getMapping(iProject).getRepository();

    // make initial commit
    Git git = new Git(repo);
    try {
        git.commit().setAuthor("JUnit", "junit@jgit.org").setMessage("Initial commit").call();
    } finally {
        git.close();
    }
}

From source file:org.eclipse.orion.server.filesystem.git.GitFileStore.java

License:Open Source License

private void rm() throws CoreException {
    // TODO: use org.eclipse.jgit.api.RmCommand, see Enhancement 379
    if (!isRoot()) {
        try {//  w w  w. j  ava2  s  .c om
            Repository local = getLocalRepo();
            Git git = new Git(local);
            CommitCommand commit = git.commit();
            commit.setAll(true);
            commit.setMessage("auto-commit of " + toString());
            commit.call();
        } catch (Exception e) {
            throw new CoreException(
                    new Status(IStatus.ERROR, Activator.PI_GIT, IStatus.ERROR, e.getMessage(), e));
        }
        push();
    } // else {cannot commit/push root removal}
}

From source file:org.eclipse.orion.server.filesystem.git.GitFileStore.java

License:Open Source License

private void commit(boolean dir) throws CoreException {
    try {//from w ww .  j  a  v a2s.c o  m
        Repository local = getLocalRepo();
        Git git = new Git(local);
        String folderPattern = null;
        String filePattern = null;
        if (dir) {
            // add empty dir - http://stackoverflow.com/questions/115983/how-do-i-add-an-empty-directory-to-a-git-repository
            File folder = getLocalFile();
            File gitignoreFile = new File(folder.getPath() + "/" + Constants.DOT_GIT_IGNORE);
            // /<folder>/.gitignore
            gitignoreFile.createNewFile();
            String query = getUrl().getQuery();
            if (query.equals("/")) { // root
                filePattern = Constants.DOT_GIT_IGNORE;
            } else {
                folderPattern = new Path(query).toString().substring(1);
                // <folder>/
                filePattern = folderPattern + "/" + Constants.DOT_GIT_IGNORE;
                // <folder>/.gitignore
            }
        } else {
            // /<folder>/<file>
            IPath f = new Path(getUrl().getQuery()).removeLastSegments(1);
            // /<folder>/
            String s = f.toString().substring(1);
            // <folder>/
            folderPattern = s.equals("") ? null : s;
            // /<folder>/<file>
            s = getUrl().getQuery().substring(1);
            filePattern = s;
        }

        // TODO: folder may already exist, no need to add it again
        AddCommand add = git.add();
        if (folderPattern != null) {
            add.addFilepattern(folderPattern);
        }
        add.addFilepattern(filePattern);
        add.call();

        CommitCommand commit = git.commit();
        commit.setMessage("auto-commit of " + this);
        commit.call();
        LogHelper.log(new Status(IStatus.INFO, Activator.PI_GIT, 1, "Auto-commit of " + this + " done.", null));
    } catch (Exception e) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.PI_GIT, IStatus.ERROR, e.getMessage(), e));
    }
}

From source file:org.eclipse.orion.server.git.GitFileDecorator.java

License:Open Source License

/**
 * If this  server is configured to use git by default, then each project creation that is not
 * already in a repository needs to have a git -init performed to initialize the repository.
 *//*from   ww  w .  j  a v a 2s .  c  o  m*/
private void initGitRepository(HttpServletRequest request, IPath targetPath, JSONObject representation) {
    //project creation URL is of the form POST /workspace/<id>
    if (!(targetPath.segmentCount() == 2 && "workspace".equals(targetPath.segment(0)) //$NON-NLS-1$
            && Method.POST.equals(Method.fromString(request.getMethod()))))
        return;
    String scm = PreferenceHelper.getString(ServerConstants.CONFIG_FILE_DEFAULT_SCM, "").toLowerCase(); //$NON-NLS-1$
    if (!"git".equals(scm)) //$NON-NLS-1$
        return;
    try {
        IFileStore store = WebProject.fromId(representation.optString(ProtocolConstants.KEY_ID))
                .getProjectStore();
        //create repository in each project if it doesn't already exist
        File localFile = store.toLocalFile(EFS.NONE, null);
        File gitDir = GitUtils.getGitDir(localFile);
        if (gitDir == null) {
            gitDir = new File(localFile, Constants.DOT_GIT);
            FileRepository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
            repo.create();
            //we need to perform an initial commit to workaround JGit bug 339610.
            Git git = new Git(repo);
            git.add().addFilepattern(".").call(); //$NON-NLS-1$
            git.commit().setMessage("Initial commit").call();
        }
    } catch (Exception e) {
        //just log it - this is not the purpose of the file decorator
        LogHelper.log(e);
    }
}