Search in sources :

Example 1 with MergeBranchesDialog

use of com.oxygenxml.git.view.dialog.MergeBranchesDialog in project oxygen-git-client-addon by oxygenxml.

the class BranchTreeMenuActionsProvider method createMergeAction.

/**
 * Create merge action for [selected_branch] into [current_branch].
 *
 * @param nodePath The node path of the selected branch.
 *
 * @return The merge action.
 */
private AbstractAction createMergeAction(String nodePath) {
    String selectedBranch = BranchesUtil.createBranchPath(nodePath, BranchManagementConstants.LOCAL_BRANCH_NODE_TREE_LEVEL);
    String currentBranch = GitAccess.getInstance().getBranchInfo().getBranchName();
    String mergeActionName = MessageFormat.format(Translator.getInstance().getTranslation(Tags.MERGE_BRANCH1_INTO_BRANCH2), TextFormatUtil.shortenText(selectedBranch, UIConstants.BRANCH_NAME_MAXIMUM_LENGTH, 0, "..."), TextFormatUtil.shortenText(currentBranch, UIConstants.BRANCH_NAME_MAXIMUM_LENGTH, 0, "...")) + "...";
    return new AbstractAction(mergeActionName) {

        @Override
        public void actionPerformed(ActionEvent e) {
            ctrl.asyncTask(() -> {
                if (RepoUtil.isUnfinishedConflictState(ctrl.getGitAccess().getRepository().getRepositoryState())) {
                    PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(TRANSLATOR.getTranslation(Tags.RESOLVE_CONFLICTS_FIRST));
                } else {
                    final MergeBranchesDialog mergeDialog = new MergeBranchesDialog(currentBranch, selectedBranch);
                    if (mergeDialog.getResult() == OKCancelDialog.RESULT_OK) {
                        if (mergeDialog.isSquashSelected()) {
                            ctrl.getGitAccess().squashAndMergeBranch(nodePath);
                        } else {
                            ctrl.getGitAccess().mergeBranch(nodePath);
                        }
                    }
                }
                return null;
            }, ex -> PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(ex.getMessage(), ex));
        }
    };
}
Also used : MergeBranchesDialog(com.oxygenxml.git.view.dialog.MergeBranchesDialog) ActionEvent(java.awt.event.ActionEvent) AbstractAction(javax.swing.AbstractAction)

Example 2 with MergeBranchesDialog

use of com.oxygenxml.git.view.dialog.MergeBranchesDialog in project oxygen-git-client-addon by oxygenxml.

the class BranchMergingTest method testBranchSquashMerging.

/**
 * <p><b>Description:</b>Tests the branch squash and merge.</p>
 * <p><b>Bug ID:</b> EXM-49976</p>
 *
 * @author alex_smarandache
 *
 * @throws Exception
 */
public void testBranchSquashMerging() throws Exception {
    final File file = new File(LOCAL_TEST_REPOSITORY, "local.txt");
    file.createNewFile();
    setFileContent(file, "local content");
    final String initialBranchName = gitAccess.getBranchInfo().getBranchName();
    assertEquals(GitAccess.DEFAULT_BRANCH_NAME, initialBranchName);
    // Make the first commit for the local repository on the main branch
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local.txt"));
    gitAccess.commit("First local commit.");
    gitAccess.createBranch(LOCAL_BRANCH_NAME1);
    // ------------- Checkout the other branch in the tree: LOCAL_BRANCH_NAME1 -------------
    GitControllerBase mock = new GitController();
    final BranchManagementPanel branchManagementPanel = new BranchManagementPanel(mock);
    branchManagementPanel.refreshBranches();
    flushAWT();
    refreshSupport.call();
    final GitTreeNode root = (GitTreeNode) (branchManagementPanel.getTree().getModel().getRoot());
    final GitTreeNode firstLeaf = (GitTreeNode) root.getFirstLeaf();
    final String firstLeafPath = (String) firstLeaf.getUserObject();
    assertTrue(firstLeafPath.contains(Constants.R_HEADS));
    String[] branchPath = firstLeafPath.split("/");
    assertEquals(LOCAL_BRANCH_NAME1, branchPath[branchPath.length - 1]);
    refreshSupport.call();
    BranchTreeMenuActionsProvider branchTreeMenuActionsProvider = new BranchTreeMenuActionsProvider(mock);
    List<AbstractAction> actionsForNode = branchTreeMenuActionsProvider.getActionsForNode(firstLeaf);
    for (AbstractAction abstractAction : actionsForNode) {
        if (abstractAction.getValue(AbstractAction.NAME).equals(translator.getTranslation(Tags.CHECKOUT))) {
            abstractAction.actionPerformed(null);
            break;
        }
    }
    refreshSupport.call();
    flushAWT();
    sleep(300);
    assertEquals(LOCAL_BRANCH_NAME1, gitAccess.getRepository().getBranch());
    // Change file on the secondary branch
    setFileContent(file, "local content for merging");
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local.txt"));
    gitAccess.commit("Branch commit");
    refreshSupport.call();
    final File file2 = new File(LOCAL_TEST_REPOSITORY, "local2.txt");
    file2.createNewFile();
    setFileContent(file2, "squash content");
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local2.txt"));
    gitAccess.commit("Branch commit 2");
    refreshSupport.call();
    // Move back to the main branch
    gitAccess.setBranch(GitAccess.DEFAULT_BRANCH_NAME);
    assertEquals(GitAccess.DEFAULT_BRANCH_NAME, gitAccess.getBranchInfo().getBranchName());
    refreshSupport.call();
    // Merge LocalBranch into main
    final List<AbstractAction> actionsForSecondaryBranch = branchTreeMenuActionsProvider.getActionsForNode(firstLeaf);
    for (AbstractAction action : actionsForSecondaryBranch) {
        if (action != null) {
            String actionName = action.getValue(AbstractAction.NAME).toString();
            if ("Merge_Branch1_Into_Branch2".equals(actionName)) {
                action.actionPerformed(null);
                break;
            }
        }
    }
    flushAWT();
    sleep(300);
    // Confirm merge dialog
    final MergeBranchesDialog mergeBranchesDialog = (MergeBranchesDialog) findDialog(translator.getTranslation(Tags.MERGE_BRANCHES));
    mergeBranchesDialog.getSquashOption().setSelected(true);
    JButton mergeOkButton = findFirstButton(mergeBranchesDialog, translator.getTranslation(Tags.MERGE));
    mergeOkButton.doClick();
    sleep(200);
    final List<CommitCharacteristics> commits = GitAccess.getInstance().getCommitsCharacteristics(HistoryStrategy.CURRENT_LOCAL_BRANCH, null, null);
    assertEquals(2, commits.size());
    for (CommitCharacteristics commit : commits) {
        // test if every commit has maximum one parent.
        assertTrue(commit.getPlotCommit().getParentCount() < 2);
    }
    assertEquals("local content for merging", TestUtil.read(file.toURI().toURL()));
    assertEquals("squash content", TestUtil.read(file2.toURI().toURL()));
}
Also used : FileStatus(com.oxygenxml.git.service.entities.FileStatus) JButton(javax.swing.JButton) GitController(com.oxygenxml.git.view.event.GitController) CommitCharacteristics(com.oxygenxml.git.view.history.CommitCharacteristics) GitControllerBase(com.oxygenxml.git.service.GitControllerBase) GitTreeNode(com.oxygenxml.git.view.GitTreeNode) MergeBranchesDialog(com.oxygenxml.git.view.dialog.MergeBranchesDialog) File(java.io.File) AbstractAction(javax.swing.AbstractAction)

Aggregations

MergeBranchesDialog (com.oxygenxml.git.view.dialog.MergeBranchesDialog)2 AbstractAction (javax.swing.AbstractAction)2 GitControllerBase (com.oxygenxml.git.service.GitControllerBase)1 FileStatus (com.oxygenxml.git.service.entities.FileStatus)1 GitTreeNode (com.oxygenxml.git.view.GitTreeNode)1 GitController (com.oxygenxml.git.view.event.GitController)1 CommitCharacteristics (com.oxygenxml.git.view.history.CommitCharacteristics)1 ActionEvent (java.awt.event.ActionEvent)1 File (java.io.File)1 JButton (javax.swing.JButton)1