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.UIRepositoryUtils.java

License:Open Source License

/**
 * Checks the repository to see if there are uncommitted changes, and
 * prompts the user to clean them up./*  www .j a va2 s  .c o  m*/
 *
 * @param repo
 *            the repository
 * @param shell
 *            the parent shell for opening the dialog
 * @param dialogTitle
 *            the dialog title
 * @return true if the git status was clean or it was dirty and the user
 *         cleaned up the uncommitted changes and the previous action may
 *         continue
 * @throws GitAPIException
 *             if there was an error checking the repository
 */
public static boolean handleUncommittedFiles(Repository repo, Shell shell, String dialogTitle)
        throws GitAPIException {
    Status status = null;
    try (Git git = new Git(repo)) {
        status = git.status().call();
    }
    if (status != null && status.hasUncommittedChanges()) {
        List<String> files = new ArrayList<>(status.getModified());
        Collections.sort(files);
        CleanupUncomittedChangesDialog cleanupUncomittedChangesDialog = new CleanupUncomittedChangesDialog(
                shell, dialogTitle, UIText.AbstractRebaseCommandHandler_cleanupDialog_text, repo, files);
        cleanupUncomittedChangesDialog.open();
        return cleanupUncomittedChangesDialog.shouldContinue();
    } else
        return true;
}

From source file:org.eclipse.egit.ui.test.nonswt.decoration.DecoratableResourceHelperTest.java

License:Open Source License

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

    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    gitDir = new File(root.getLocation().toFile(), Constants.DOT_GIT);

    repository = new FileRepository(gitDir);
    repository.create();/*from  w w  w .j  av a2 s  .  c o m*/
    repository.close();
    repository = Activator.getDefault().getRepositoryCache().lookupRepository(gitDir);

    project = root.getProject(TEST_PROJECT);
    project.create(null);
    project.open(null);

    project.getFolder(TEST_FOLDER2).create(true, true, null);
    IFile testFile2 = project.getFile(TEST_FILE2);
    testFile2.create(new ByteArrayInputStream("content".getBytes()), true, null);

    RepositoryMapping mapping = new RepositoryMapping(project, gitDir);

    GitProjectData projectData = new GitProjectData(project);
    projectData.setRepositoryMappings(Collections.singleton(mapping));
    projectData.store();
    GitProjectData.add(project, projectData);

    RepositoryProvider.map(project, GitProvider.class.getName());

    git = new Git(repository);
    git.add().addFilepattern(".").call();
    git.commit().setMessage("Initial commit").call();

    indexDiffCacheEntry = Activator.getDefault().getIndexDiffCache().getIndexDiffCacheEntry(repository);
    waitForIndexDiffUpdate(false);
}

From source file:org.eclipse.egit.ui.test.stagview.StagingViewTest.java

License:Open Source License

@Test
public void testCommitSingleFile() throws Exception {
    setTestFileContent("I have changed this");
    new Git(repository).add().addFilepattern(".").call();
    StagingViewTester stagingViewTester = StagingViewTester.openStagingView();
    selectRepositoryNode();/*w  w  w. j  a  va  2  s  . com*/
    TestUtil.joinJobs(JobFamilies.INDEX_DIFF_CACHE_UPDATE);
    stagingViewTester.setAuthor(TestUtil.TESTAUTHOR);
    stagingViewTester.setCommitter(TestUtil.TESTCOMMITTER);
    stagingViewTester.setCommitMessage("The new commit");
    stagingViewTester.commit();
    TestUtil.checkHeadCommit(repository, TestUtil.TESTAUTHOR, TestUtil.TESTCOMMITTER, "The new commit");
}

From source file:org.eclipse.egit.ui.test.stagview.StagingViewTest.java

License:Open Source License

@Test
public void testAmend() throws Exception {
    RevCommit oldHeadCommit = TestUtil.getHeadCommit(repository);
    commitOneFileChange("Yet another Change");
    RevCommit headCommit = TestUtil.getHeadCommit(repository);
    ObjectId headCommitId = headCommit.getId();
    String changeId = CommitMessageUtil.extractChangeId(headCommit.getFullMessage());
    setTestFileContent("Changes over changes");
    new Git(repository).add().addFilepattern(".").call();
    StagingViewTester stagingViewTester = StagingViewTester.openStagingView();
    stagingViewTester.setAmend(true);//from w w w. j a  v a2 s .  c  o m
    assertTrue(stagingViewTester.getCommitMessage().indexOf("Change-Id") > 0);
    assertTrue(stagingViewTester.getCommitMessage().indexOf("Signed-off-by") > 0);
    assertTrue(stagingViewTester.getSignedOff());
    assertTrue(stagingViewTester.getInsertChangeId());
    stagingViewTester.commit();
    headCommit = TestUtil.getHeadCommit(repository);
    if (headCommitId.equals(headCommit.getId()))
        fail("There is no new commit");
    assertEquals(oldHeadCommit, headCommit.getParent(0));
    assertTrue(headCommit.getFullMessage().indexOf(changeId) > 0);
}

From source file:org.eclipse.egit.ui.test.stagview.StagingViewTest.java

License:Open Source License

private void commitOneFileChange(String fileContent) throws Exception {
    setTestFileContent(fileContent);/*from  w  ww  .j a  v a  2 s .c o m*/
    new Git(repository).add().addFilepattern(".").call();
    selectRepositoryNode(); // update staging view after add
    // TODO: adding should be done by using staging view.
    // Can be done when add is available via context menu
    StagingViewTester stagingViewTester = StagingViewTester.openStagingView();
    stagingViewTester.setAuthor(TestUtil.TESTAUTHOR);
    stagingViewTester.setCommitter(TestUtil.TESTCOMMITTER);
    stagingViewTester.setCommitMessage("Commit message");
    stagingViewTester.setInsertChangeId(true);
    stagingViewTester.setSignedOff(true);
    String commitMessage = stagingViewTester.getCommitMessage();
    assertTrue(commitMessage.indexOf("Change-Id") > 0);
    assertTrue(commitMessage.indexOf("Signed-off-by") > 0);
    stagingViewTester.commit();
}

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();/*from   w  w w .  ja v a  2s .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();
    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.CompareActionsTest.java

License:Open Source License

@Test
public void testCompareWithIndex() throws Exception {
    String compareWithIndexActionLabel = util.getPluginLocalizedValue("CompareWithIndexAction_label");
    clickCompareWith(compareWithIndexActionLabel);

    // compare with index should not have any changes
    assertEquals(0, bot.viewById(CompareTreeView.ID).bot().tree().getAllItems().length);
    // change test file -> should have one change
    setTestFileContent("Hello there");

    clickCompareWith(compareWithIndexActionLabel);

    waitUntilCompareTreeViewTreeHasNodeCount(1);

    // add to index -> no more changes
    new Git(lookupRepository(repositoryFile)).add().addFilepattern(PROJ1 + "/" + FOLDER + "/" + FILE1).call();

    clickCompareWith(compareWithIndexActionLabel);

    assertEquals(0, bot.viewById(CompareTreeView.ID).bot().tree().getAllItems().length);

    // reset -> there should be no more changes
    ResetOperation rop = new ResetOperation(lookupRepository(repositoryFile), "refs/heads/master",
            ResetType.HARD);/*from w w w .  j  ava2s  .com*/
    rop.execute(new NullProgressMonitor());

    clickCompareWith(compareWithIndexActionLabel);
    waitUntilCompareTreeViewTreeHasNodeCount(0);
}

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

License:Open Source License

@Test
public void testCompareWithHead() throws Exception {
    String compareWithHeadMenuLabel = util.getPluginLocalizedValue("CompareWithHeadAction_label");
    clickCompareWith(compareWithHeadMenuLabel);

    // compare with HEAD should not have any changes
    waitUntilCompareTreeViewTreeHasNodeCount(0);
    // change test file -> should have one change
    setTestFileContent("Hello there");

    clickCompareWith(compareWithHeadMenuLabel);

    waitUntilCompareTreeViewTreeHasNodeCount(1);
    // add to index -> should still show as change
    new Git(lookupRepository(repositoryFile)).add().addFilepattern(PROJ1 + "/" + FOLDER + "/" + FILE1).call();

    clickCompareWith(compareWithHeadMenuLabel);

    assertEquals(1, bot.viewById(CompareTreeView.ID).bot().tree().getAllItems().length);

    // reset -> there should be no more changes
    ResetOperation rop = new ResetOperation(lookupRepository(repositoryFile), "refs/heads/master",
            ResetType.HARD);/*from  ww  w. ja  v  a2 s  .  c om*/
    rop.execute(new NullProgressMonitor());

    clickCompareWith(compareWithHeadMenuLabel);
    waitUntilCompareTreeViewTreeHasNodeCount(0);
}

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  w w. j  a va  2  s  . c om*/
    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.egit.ui.variables.DynamicVariablesTest.java

License:Open Source License

@Before
public void setUp() throws Exception {

    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    FileUtils.mkdir(new File(root.getLocation().toFile(), "Sub"), true);
    gitDir = new File(new File(root.getLocation().toFile(), "Sub"), Constants.DOT_GIT);

    repository = new FileRepository(gitDir);
    repository.create();//from  w w  w .j  a v a2s . c  o m

    project = root.getProject(TEST_PROJECT);
    project.create(null);
    project.open(null);
    IProjectDescription description = project.getDescription();
    description.setLocation(root.getLocation().append(TEST_PROJECT_LOC));
    project.move(description, IResource.FORCE, null);

    project2 = root.getProject(TEST_PROJECT2);
    project2.create(null);
    project2.open(null);
    gitDir2 = new File(project2.getLocation().toFile().getAbsoluteFile(), Constants.DOT_GIT);
    repository2 = new FileRepository(gitDir2);
    repository2.create();

    RepositoryMapping mapping = new RepositoryMapping(project, gitDir);
    RepositoryMapping mapping2 = new RepositoryMapping(project2, gitDir2);

    GitProjectData projectData = new GitProjectData(project);
    GitProjectData projectData2 = new GitProjectData(project2);
    projectData.setRepositoryMappings(Collections.singletonList(mapping));
    projectData.store();
    projectData2.setRepositoryMappings(Collections.singletonList(mapping2));
    projectData2.store();
    GitProjectData.add(project, projectData);
    GitProjectData.add(project2, projectData2);

    RepositoryProvider.map(project, GitProvider.class.getName());
    RepositoryProvider.map(project2, GitProvider.class.getName());

    FileWriter fileWriter = new FileWriter(new File(repository.getWorkTree(), TEST_PROJECT + "/" + TEST_FILE));
    fileWriter.write("Some data");
    fileWriter.close();
    FileWriter fileWriter2 = new FileWriter(new File(repository2.getWorkTree(), TEST_FILE2));
    fileWriter2.write("Some other data");
    fileWriter2.close();
    project.refreshLocal(IResource.DEPTH_INFINITE, null);
    project2.refreshLocal(IResource.DEPTH_INFINITE, null);
    git = new Git(repository);
    git.add().addFilepattern(".").call();
    git.commit().setMessage("Initial commit").call();

    git = new Git(repository2);
    git.add().addFilepattern(".").call();
    git.commit().setMessage("Initial commit").call();
    git.branchRename().setNewName("main").call();
}