Search in sources :

Example 41 with GitTreeNode

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

the class BranchManagementTest method testBranchesTreeStructure.

/**
 * <p><b>Description:</b> Tests the structure of a tree with both local and remote branches.</p>
 * <p><b>Bug ID:</b> EXM-41701</p>
 *
 * @author bogdan_draghici
 *
 * @throws Exception
 */
public void testBranchesTreeStructure() throws Exception {
    File file = new File(LOCAL_TEST_REPOSITORY + "local.txt");
    file.createNewFile();
    setFileContent(file, "local content");
    // Make the first commit for the local repository and create a branch for it.
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local.txt"));
    gitAccess.commit("First local commit.");
    gitAccess.createBranch(LOCAL_BRANCH_NAME1);
    gitAccess.createBranch(LOCAL_BRANCH_NAME2);
    gitAccess.setRepositorySynchronously(REMOTE_TEST_REPOSITORY);
    file = new File(REMOTE_TEST_REPOSITORY + "remote1.txt");
    file.createNewFile();
    setFileContent(file, "remote content");
    // Make the first commit for the remote repository and create a branch for it.
    gitAccess.add(new FileStatus(GitChangeType.ADD, "remote1.txt"));
    gitAccess.commit("First remote commit.");
    gitAccess.createBranch(REMOTE_BRANCH_NAME1);
    gitAccess.createBranch(REMOTE_BRANCH_NAME2);
    gitAccess.setRepositorySynchronously(LOCAL_TEST_REPOSITORY);
    gitAccess.fetch();
    BranchManagementPanel branchManagementPanel = new BranchManagementPanel(Mockito.mock(GitControllerBase.class));
    branchManagementPanel.refreshBranches();
    flushAWT();
    JTree tree = branchManagementPanel.getTree();
    GitTreeNode root = (GitTreeNode) tree.getModel().getRoot();
    StringBuilder actualTree = new StringBuilder();
    serializeTree(actualTree, root);
    assertEquals("localRepository\n" + "  refs/heads/\n" + "    refs/heads/LocalBranch\n" + "    refs/heads/LocalBranch2\n" + "    refs/heads/" + GitAccess.DEFAULT_BRANCH_NAME + "\n" + "  refs/remotes/\n" + "    refs/remotes/origin/\n" + "      refs/remotes/origin/" + GitAccess.DEFAULT_BRANCH_NAME + "\n" + "      refs/remotes/origin/RemoteBranch\n" + "      refs/remotes/origin/RemoteBranch2\n" + "", actualTree.toString());
}
Also used : GitControllerBase(com.oxygenxml.git.service.GitControllerBase) JTree(javax.swing.JTree) FileStatus(com.oxygenxml.git.service.entities.FileStatus) GitTreeNode(com.oxygenxml.git.view.GitTreeNode) File(java.io.File)

Example 42 with GitTreeNode

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

the class BranchMergingTest method testBranchMergingWithoutResolvingConflict.

/**
 *<p><b>Description:</b>Tests the merging after you have an ignored conflict.</p>
 * <p><b>Bug ID:</b> EXM-43410</p>
 *
 * @author gabriel_nedianu
 *
 * @throws Exception
 */
public void testBranchMergingWithoutResolvingConflict() throws Exception {
    JDialog conflictMergeDialog = null;
    JDialog mergeOkDialog = null;
    File file = new File(LOCAL_TEST_REPOSITORY, "local.txt");
    file.createNewFile();
    setFileContent(file, "local file 1 content");
    // Make the first commit for the local repository and create a new branch
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local.txt"));
    gitAccess.commit("1st commit on main.");
    gitAccess.createBranch(LOCAL_BRANCH_NAME1);
    GitControllerBase mock = new GitController(GitAccess.getInstance());
    BranchManagementPanel branchManagementPanel = new BranchManagementPanel(mock);
    branchManagementPanel.refreshBranches();
    flushAWT();
    // ------------- Checkout branch: LOCAL_BRANCH_NAME1  -------------
    gitAccess.setBranch(LOCAL_BRANCH_NAME1);
    // Commit on this branch
    setFileContent(file, "local file ... new branch");
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local.txt"));
    gitAccess.commit("Commit on secondary branch");
    // ------------- Move to the main branch and commit something there ---------------
    gitAccess.setBranch(GitAccess.DEFAULT_BRANCH_NAME);
    setFileContent(file, "file modifications");
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local.txt"));
    gitAccess.commit("2nd commit on main");
    // Merge secondary branch into main
    BranchTreeMenuActionsProvider branchTreeMenuActionsProvider = new BranchTreeMenuActionsProvider(mock);
    GitTreeNode root = (GitTreeNode) (branchManagementPanel.getTree().getModel().getRoot());
    GitTreeNode secondaryBranchNode = (GitTreeNode) root.getFirstLeaf();
    String secondaryBranchPath = (String) secondaryBranchNode.getUserObject();
    assertTrue(secondaryBranchPath.contains(Constants.R_HEADS));
    List<AbstractAction> actionsForSecondaryBranch = branchTreeMenuActionsProvider.getActionsForNode(secondaryBranchNode);
    for (AbstractAction action : actionsForSecondaryBranch) {
        if (action != null) {
            String actionName = action.getValue(AbstractAction.NAME).toString();
            if ("Merge_Branch1_Into_Branch2".equals(actionName)) {
                SwingUtilities.invokeLater(() -> action.actionPerformed(null));
                break;
            }
        }
    }
    flushAWT();
    sleep(300);
    // Confirm merge dialog
    mergeOkDialog = findDialog(translator.getTranslation(Tags.MERGE_BRANCHES));
    JButton mergeOkButton = findFirstButton(mergeOkDialog, translator.getTranslation(Tags.MERGE));
    mergeOkButton.doClick();
    sleep(200);
    conflictMergeDialog = findDialog(translator.getTranslation(Tags.MERGE_CONFLICTS_TITLE));
    assertNotNull(conflictMergeDialog);
    conflictMergeDialog.dispose();
    sleep(200);
    // Don't resolve merge conflicts and try to do the merge again and we will get an errMsg
    // Mock showErrorMessage
    StandalonePluginWorkspace pluginWSMock = Mockito.mock(StandalonePluginWorkspace.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            String message = (String) invocation.getArguments()[0];
            errMsg[0] = message;
            return null;
        }
    }).when(pluginWSMock).showErrorMessage(Mockito.anyString());
    PluginWorkspaceProvider.setPluginWorkspace(pluginWSMock);
    List<AbstractAction> actionsForSecondaryBranch2 = branchTreeMenuActionsProvider.getActionsForNode(secondaryBranchNode);
    for (AbstractAction action : actionsForSecondaryBranch2) {
        if (action != null) {
            String actionName = action.getValue(AbstractAction.NAME).toString();
            if ("Merge_Branch1_Into_Branch2".equals(actionName)) {
                SwingUtilities.invokeLater(() -> action.actionPerformed(null));
                break;
            }
        }
    }
    flushAWT();
    sleep(200);
    assertEquals("Resolve_conflicts_first", errMsg[0]);
}
Also used : FileStatus(com.oxygenxml.git.service.entities.FileStatus) JButton(javax.swing.JButton) GitController(com.oxygenxml.git.view.event.GitController) GitControllerBase(com.oxygenxml.git.service.GitControllerBase) GitTreeNode(com.oxygenxml.git.view.GitTreeNode) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StandalonePluginWorkspace(ro.sync.exml.workspace.api.standalone.StandalonePluginWorkspace) File(java.io.File) AbstractAction(javax.swing.AbstractAction) JDialog(javax.swing.JDialog)

Example 43 with GitTreeNode

use of com.oxygenxml.git.view.GitTreeNode 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)

Example 44 with GitTreeNode

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

the class TreeUtil method getTreePath.

/**
 * Converts a file path to the TreePath that indentifies the file in the tree model.
 *
 * @param model  The tree model.
 * @param path   File path from the working copy.
 *
 * @return A file path to the TreePath.
 */
public static TreePath getTreePath(TreeModel model, String path) {
    String[] strings = path.split("/");
    Object[] parts = new Object[1 + strings.length];
    parts[0] = model.getRoot();
    for (int i = 0; i < strings.length; i++) {
        parts[i + 1] = new GitTreeNode(strings[i]);
    }
    return new TreePath(parts);
}
Also used : GitTreeNode(com.oxygenxml.git.view.GitTreeNode) TreePath(javax.swing.tree.TreePath)

Example 45 with GitTreeNode

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

the class TreeUtil method childIndex.

/**
 * Returns the index of a child of a given node, provided its string value.
 *
 * @param node
 *          The node to search its children
 * @param childValue
 *          The value of the child to compare with
 * @return The index
 */
public static int childIndex(final GitTreeNode node, final String childValue) {
    Enumeration<TreeNode> children = node.children();
    GitTreeNode child = null;
    int index = -1;
    while (children.hasMoreElements() && index < 0) {
        child = (GitTreeNode) children.nextElement();
        if (child.getUserObject() != null && childValue.equals(child.getUserObject())) {
            index = node.getIndex(child);
        }
    }
    return index;
}
Also used : GitTreeNode(com.oxygenxml.git.view.GitTreeNode) TreeNode(javax.swing.tree.TreeNode) GitTreeNode(com.oxygenxml.git.view.GitTreeNode)

Aggregations

GitTreeNode (com.oxygenxml.git.view.GitTreeNode)49 FileStatus (com.oxygenxml.git.service.entities.FileStatus)33 File (java.io.File)30 GitControllerBase (com.oxygenxml.git.service.GitControllerBase)23 AbstractAction (javax.swing.AbstractAction)19 GitController (com.oxygenxml.git.view.event.GitController)18 JButton (javax.swing.JButton)15 TreePath (javax.swing.tree.TreePath)14 Test (org.junit.Test)14 JDialog (javax.swing.JDialog)13 JTree (javax.swing.JTree)8 BranchManagementPanel (com.oxygenxml.git.view.branches.BranchManagementPanel)7 BranchTreeMenuActionsProvider (com.oxygenxml.git.view.branches.BranchTreeMenuActionsProvider)7 JTextField (javax.swing.JTextField)7 JCheckBox (javax.swing.JCheckBox)6 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 JLabel (javax.swing.JLabel)4 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)4