Search in sources :

Example 6 with RenameTracker

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

the class GitAccessRevertMergingConflictsTest method testRevertCommitWithRepoInConflict.

/**
 * <p><b>Description:</b> test the "Revert commit" action when the repo has conflicts.</p>
 * <p><b>Bug ID:</b> EXM-47154</p>
 *
 * @author Tudosie Razvan
 *
 * @throws Exception
 */
@Test
public void testRevertCommitWithRepoInConflict() throws Exception {
    File file1 = new File(LOCAL_TEST_REPOSITORY, "local1.txt");
    File file2 = new File(LOCAL_TEST_REPOSITORY, "local2.txt");
    file1.createNewFile();
    file2.createNewFile();
    setFileContent(file1, "local file 1 content");
    setFileContent(file2, "local file 2 content");
    // Make the first commit for the local repository and create a new branch
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local1.txt"));
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local2.txt"));
    gitAccess.commit("First local commit on main.");
    gitAccess.createBranch(LOCAL_BRANCH_NAME1);
    GitControllerBase mock = new GitController();
    BranchManagementPanel branchManagementPanel = new BranchManagementPanel(mock);
    branchManagementPanel.refreshBranches();
    flushAWT();
    // ------------- Checkout branch: LOCAL_BRANCH_NAME1 -------------
    gitAccess.setBranch(LOCAL_BRANCH_NAME1);
    // Commit on this branch
    setFileContent(file1, "local file 1 on new branch");
    setFileContent(file2, "local file 2 on new branch");
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local1.txt"));
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local2.txt"));
    gitAccess.commit("Commit on secondary branch");
    // ------------- Move to the main branch and commit something there
    // ---------------
    gitAccess.setBranch(GitAccess.DEFAULT_BRANCH_NAME);
    setFileContent(file1, "local file 1 modifications");
    setFileContent(file2, "local file 2 modifications");
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local1.txt"));
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local2.txt"));
    gitAccess.commit("2nd commit on main branch");
    // 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();
    // Confirm merge dialog
    JDialog mergeOkDialog = findDialog(translator.getTranslation(Tags.MERGE_BRANCHES));
    JButton mergeOkButton = findFirstButton(mergeOkDialog, translator.getTranslation(Tags.YES));
    mergeOkButton.doClick();
    flushAWT();
    sleep(300);
    JDialog conflictMergeDialog = findDialog(translator.getTranslation(Tags.MERGE_CONFLICTS_TITLE));
    assertNotNull(conflictMergeDialog);
    conflictMergeDialog.setVisible(false);
    conflictMergeDialog.dispose();
    flushAWT();
    sleep(200);
    assertTrue(TestUtil.read(file1.toURI().toURL()).contains("<<<<<<< HEAD\n" + "local file 1 modifications\n" + "=======\n" + "local file 1 on new branch\n" + ">>>>>>>"));
    assertTrue(TestUtil.read(file2.toURI().toURL()).contains("<<<<<<< HEAD\n" + "local file 2 modifications\n" + "=======\n" + "local file 2 on new branch\n" + ">>>>>>>"));
    List<CommitCharacteristics> commitsCharacteristics = gitAccess.getCommitsCharacteristics(HistoryStrategy.CURRENT_BRANCH, null, new RenameTracker());
    String initialHistory = "[ Uncommitted_changes , DATE , * , * , null , null ]\n" + "[ 2nd commit on main branch , DATE , AlexJitianu <alex_jitianu@sync.ro> , 1 , AlexJitianu , [2] ]\n" + "[ First local commit on main. , DATE , AlexJitianu <alex_jitianu@sync.ro> , 2 , AlexJitianu , [3] ]\n" + "[ Added a new file , DATE , AlexJitianu <alex_jitianu@sync.ro> , 3 , AlexJitianu , [4] ]\n" + "[ Modified a file , DATE , AlexJitianu <alex_jitianu@sync.ro> , 4 , AlexJitianu , [5] ]\n" + "[ First commit. , DATE , AlexJitianu <alex_jitianu@sync.ro> , 5 , AlexJitianu , null ]\n" + "";
    String regex = "(([0-9])|([0-2][0-9])|([3][0-1]))\\ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\ \\d{4}";
    assertEquals(initialHistory, dumpHistory(commitsCharacteristics).replaceAll(regex, "DATE"));
    CommitCharacteristics commitToRevert = gitAccess.getCommitsCharacteristics(HistoryStrategy.CURRENT_BRANCH, null, new RenameTracker()).get(4);
    RevertCommitAction revertAction = new RevertCommitAction(commitToRevert);
    SwingUtilities.invokeLater(() -> revertAction.actionPerformed(null));
    flushAWT();
    JDialog revertConfirmationDlg = findDialog(Tags.REVERT_COMMIT);
    JTextArea confirmationTextArea = findFirstTextArea(revertConfirmationDlg);
    assertEquals(Tags.REVERT_COMMIT_CONFIRMATION, confirmationTextArea.getText().toString());
    JButton revertOkButton = findFirstButton(revertConfirmationDlg, Tags.YES);
    revertOkButton.doClick();
    flushAWT();
    sleep(300);
    assertEquals("", errMsg[0]);
}
Also used : RenameTracker(com.oxygenxml.git.view.history.RenameTracker) FileStatus(com.oxygenxml.git.service.entities.FileStatus) JTextArea(javax.swing.JTextArea) JButton(javax.swing.JButton) GitController(com.oxygenxml.git.view.event.GitController) CommitCharacteristics(com.oxygenxml.git.view.history.CommitCharacteristics) RevertCommitAction(com.oxygenxml.git.view.history.actions.RevertCommitAction) BranchTreeMenuActionsProvider(com.oxygenxml.git.view.branches.BranchTreeMenuActionsProvider) GitTreeNode(com.oxygenxml.git.view.GitTreeNode) File(java.io.File) BranchManagementPanel(com.oxygenxml.git.view.branches.BranchManagementPanel) AbstractAction(javax.swing.AbstractAction) JDialog(javax.swing.JDialog) Test(org.junit.Test)

Example 7 with RenameTracker

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

the class GitHistoryTest method testHistory_UpstreamBranchAhead.

/**
 * The upstream branch is ahead. In the history we should present the upstream branch as well.
 *
 * @throws Exception If it fails.
 */
@Test
public void testHistory_UpstreamBranchAhead() throws Exception {
    URL script = getClass().getClassLoader().getResource("scripts/history_script.txt");
    File wcTree = new File("target/gen/GitHistoryTest_testHistoryRemote");
    File remoteDir = new File("target/gen/GitHistoryTest_testHistoryRemote_RemoteRepo");
    FileUtils.deleteDirectory(wcTree);
    FileUtils.deleteDirectory(remoteDir);
    RepoGenerationScript.generateRepository(script, wcTree);
    Repository remoteRepository = null;
    try {
        Repository localRepository = GitAccess.getInstance().getRepository();
        remoteDir.mkdirs();
        GitAccess.getInstance().createNewRepository(remoteDir.getAbsolutePath());
        remoteRepository = GitAccess.getInstance().getRepository();
        bindLocalToRemote(localRepository, remoteRepository);
        GitAccess.getInstance().setRepositorySynchronously(localRepository.getWorkTree().getAbsolutePath());
        push("Alex", "");
        // Make the remote evolve.
        // Not sure why we need this sleep. Commit order is messed up with out it.
        Thread.sleep(1000);
        GitAccess.getInstance().setRepositorySynchronously(remoteRepository.getWorkTree().getAbsolutePath());
        FileUtils.writeStringToFile(new File(remoteRepository.getWorkTree(), "root.txt"), "changed on the remote", "UTF-8");
        GitAccess.getInstance().add(new FileStatus(GitChangeType.MODIFIED, "root.txt"));
        RepoGenerationScript.setUserCredentials();
        GitAccess.getInstance().commit("Change on the remote.");
        // Switch to local.
        GitAccess.getInstance().setRepositorySynchronously(localRepository.getWorkTree().getAbsolutePath());
        GitAccess.getInstance().fetch();
        List<CommitCharacteristics> commitsCharacteristics = GitAccess.getInstance().getCommitsCharacteristics(HistoryStrategy.CURRENT_BRANCH, null, new RenameTracker());
        String dump = dumpHistory(commitsCharacteristics);
        String expected = "[ Change on the remote. , {date} , AlexJitianu <alex_jitianu@sync.ro> , 1 , AlexJitianu , [2] ]\n" + "[ Root file changed. , {date} , Alex <alex_jitianu@sync.ro> , 2 , AlexJitianu , [3] ]\n" + "[ Root file. , {date} , Alex <alex_jitianu@sync.ro> , 3 , AlexJitianu , [4] ]\n" + "[ Changes. , {date} , Alex <alex_jitianu@sync.ro> , 4 , AlexJitianu , [5] ]\n" + "[ First commit. , {date} , Alex <alex_jitianu@sync.ro> , 5 , AlexJitianu , null ]\n" + "";
        expected = expected.replaceAll("\\{date\\}", DATE_FORMAT.format(new Date()));
        assertEquals("root.txt was created and changed in the last two commit", expected, dump);
    } finally {
        GitAccess.getInstance().closeRepo();
        remoteRepository.close();
    }
}
Also used : RenameTracker(com.oxygenxml.git.view.history.RenameTracker) Repository(org.eclipse.jgit.lib.Repository) FileStatus(com.oxygenxml.git.service.entities.FileStatus) File(java.io.File) URL(java.net.URL) CommitCharacteristics(com.oxygenxml.git.view.history.CommitCharacteristics) Date(java.util.Date) Test(org.junit.Test)

Example 8 with RenameTracker

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

the class GitHistoryTest method testHistoryBranchMerged.

/**
 * We have a repository with multiple branches. The history should present the history for the current branch only.
 * When a branch is merged into another, the common revisions appear in both branches.
 *
 * @throws Exception If it fails.
 */
@Test
public void testHistoryBranchMerged() throws Exception {
    URL script = getClass().getClassLoader().getResource("scripts/history_script_branches_merged.txt");
    File wcTree = new File("target/gen/GitHistoryTest_testHistoryBranchMerged");
    RepoGenerationScript.generateRepository(script, wcTree);
    try {
        GitAccess.getInstance().setRepositorySynchronously(wcTree.getAbsolutePath());
        GitAccess.getInstance().setBranch("main");
        List<CommitCharacteristics> commitsCharacteristics = GitAccess.getInstance().getCommitsCharacteristics(HistoryStrategy.CURRENT_BRANCH, null, new RenameTracker());
        String dump = dumpHistory(commitsCharacteristics);
        String expected = "[ Another commit on main. , {date} , Alex <alex_jitianu@sync.ro> , 1 , AlexJitianu , [2] ]\n" + "[ Merge branch 'feature' , {date} , AlexJitianu <alex_jitianu@sync.ro> , 2 , AlexJitianu , [3, 4] ]\n" + "[ Changed on main branch. , {date} , Alex <alex_jitianu@sync.ro> , 3 , AlexJitianu , [5] ]\n" + "[ Changed on feature branch. , {date} , Alex <alex_jitianu@sync.ro> , 4 , AlexJitianu , [6] ]\n" + "[ Feature branch commit. , {date} , Alex <alex_jitianu@sync.ro> , 6 , AlexJitianu , [5] ]\n" + "[ Root file changed. , {date} , Alex <alex_jitianu@sync.ro> , 5 , AlexJitianu , [7] ]\n" + "[ Root file. , {date} , Alex <alex_jitianu@sync.ro> , 7 , AlexJitianu , [8] ]\n" + "[ Changes. , {date} , Alex <alex_jitianu@sync.ro> , 8 , AlexJitianu , [9] ]\n" + "[ First commit. , {date} , Alex <alex_jitianu@sync.ro> , 9 , AlexJitianu , null ]\n" + "";
        expected = expected.replaceAll("\\{date\\}", DATE_FORMAT.format(new Date()));
        assertEquals(expected, dump);
        GitAccess.getInstance().setBranch("feature");
        commitsCharacteristics = GitAccess.getInstance().getCommitsCharacteristics(HistoryStrategy.CURRENT_BRANCH, null, new RenameTracker());
        dump = dumpHistory(commitsCharacteristics);
        expected = "[ Anotehr commit of feature branch. , {date} , Alex <alex_jitianu@sync.ro> , 10 , AlexJitianu , [4] ]\n" + "[ Changed on feature branch. , {date} , Alex <alex_jitianu@sync.ro> , 4 , AlexJitianu , [6] ]\n" + "[ Feature branch commit. , {date} , Alex <alex_jitianu@sync.ro> , 6 , AlexJitianu , [5] ]\n" + "[ Root file changed. , {date} , Alex <alex_jitianu@sync.ro> , 5 , AlexJitianu , [7] ]\n" + "[ Root file. , {date} , Alex <alex_jitianu@sync.ro> , 7 , AlexJitianu , [8] ]\n" + "[ Changes. , {date} , Alex <alex_jitianu@sync.ro> , 8 , AlexJitianu , [9] ]\n" + "[ First commit. , {date} , Alex <alex_jitianu@sync.ro> , 9 , AlexJitianu , null ]\n" + "";
        expected = expected.replaceAll("\\{date\\}", DATE_FORMAT.format(new Date()));
        assertEquals(expected, dump);
    } finally {
        GitAccess.getInstance().cleanUp();
        waitForScheduler();
        flushAWT();
        FileUtils.deleteDirectory(wcTree);
        flushAWT();
    }
}
Also used : RenameTracker(com.oxygenxml.git.view.history.RenameTracker) File(java.io.File) URL(java.net.URL) CommitCharacteristics(com.oxygenxml.git.view.history.CommitCharacteristics) Date(java.util.Date) Test(org.junit.Test)

Example 9 with RenameTracker

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

the class GitHistoryTest method testCommitFiles.

/**
 * Tests the files that are contained in each commit.
 *
 * @throws Exception
 */
@Test
public void testCommitFiles() throws Exception {
    URL script = getClass().getClassLoader().getResource("scripts/history_script.txt");
    File wcTree = new File("target/gen/GitHistoryTest_testHistory");
    RepoGenerationScript.generateRepository(script, wcTree);
    try {
        FileUtils.writeStringToFile(new File(wcTree, "root.txt"), "changed", "UTF-8");
        GitAccess.getInstance().setRepositorySynchronously(wcTree.getAbsolutePath());
        List<CommitCharacteristics> commitsCharacteristics = GitAccess.getInstance().getCommitsCharacteristics(HistoryStrategy.CURRENT_BRANCH, null, new RenameTracker());
        assertEquals(5, commitsCharacteristics.size());
        Map<String, String> fileLists = new LinkedHashMap<>();
        commitsCharacteristics.stream().forEach(t -> {
            try {
                fileLists.put(getAssertableID(t.getCommitAbbreviatedId()), dumpFS(RevCommitUtil.getChangedFiles(t.getCommitId())));
            } catch (IOException | GitAPIException e) {
                fileLists.put(getAssertableID(t.getCommitAbbreviatedId()), e.getMessage());
            }
        });
        // fileLists.keySet().stream().forEach(k -> {
        // System.out.println(k);
        // System.out.println(fileLists.get(k));
        // System.out.println();
        // });
        Map<String, String> expected = new LinkedHashMap<>();
        expected.put("*", "(changeType=MODIFIED, fileLocation=root.txt)\n");
        expected.put("1", "(changeType=CHANGED, fileLocation=root.txt)\n");
        expected.put("2", "(changeType=ADD, fileLocation=root.txt)\n");
        expected.put("3", "(changeType=ADD, fileLocation=f2/file1.txt)\n" + "(changeType=CHANGED, fileLocation=f2/file2.txt)\n" + "(changeType=RENAME, fileLocation=f2/file3_renamed.txt)\n" + "(changeType=REMOVED, fileLocation=f2/file4.txt)\n");
        expected.put("4", "(changeType=ADD, fileLocation=f1/file1.txt)\n" + "(changeType=ADD, fileLocation=f2/file2.txt)\n" + "(changeType=ADD, fileLocation=f2/file3.txt)\n" + "(changeType=ADD, fileLocation=f2/file4.txt)\n" + "(changeType=ADD, fileLocation=newProject.xpr)\n" + "");
        assertEquals(expected.keySet().toString(), fileLists.keySet().toString());
        Set<String> keySet = expected.keySet();
        for (String commitID : keySet) {
            assertEquals("Unnexpected file list for commit id " + commitID, expected.get(commitID), fileLists.get(commitID));
        }
    } finally {
        GitAccess.getInstance().closeRepo();
        flushAWT();
        waitForScheduler();
        flushAWT();
        FileUtils.deleteDirectory(wcTree);
    }
}
Also used : RenameTracker(com.oxygenxml.git.view.history.RenameTracker) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) CommitCharacteristics(com.oxygenxml.git.view.history.CommitCharacteristics) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

CommitCharacteristics (com.oxygenxml.git.view.history.CommitCharacteristics)9 RenameTracker (com.oxygenxml.git.view.history.RenameTracker)9 File (java.io.File)9 Test (org.junit.Test)9 URL (java.net.URL)8 Date (java.util.Date)5 FileStatus (com.oxygenxml.git.service.entities.FileStatus)4 HistoryViewContextualMenuPresenter (com.oxygenxml.git.view.history.HistoryViewContextualMenuPresenter)2 ArrayList (java.util.ArrayList)2 Action (javax.swing.Action)2 JPopupMenu (javax.swing.JPopupMenu)2 GitTreeNode (com.oxygenxml.git.view.GitTreeNode)1 BranchManagementPanel (com.oxygenxml.git.view.branches.BranchManagementPanel)1 BranchTreeMenuActionsProvider (com.oxygenxml.git.view.branches.BranchTreeMenuActionsProvider)1 GitController (com.oxygenxml.git.view.event.GitController)1 RevertCommitAction (com.oxygenxml.git.view.history.actions.RevertCommitAction)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1