use of com.oxygenxml.git.view.event.GitEventInfo in project oxygen-git-client-addon by oxygenxml.
the class GitTestBase method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
// Create the unstaged resources panel
refreshSupport = new PanelRefresh(null) {
@Override
protected int getScheduleDelay() {
// Execute refresh events immediately from tests.
return 1;
}
};
gitInit();
ColorTheme colorTheme = Mockito.mock(ColorTheme.class);
Mockito.when(colorTheme.isDarkTheme()).thenReturn(false);
StandalonePluginWorkspace pluginWSMock = Mockito.mock(StandalonePluginWorkspace.class);
Mockito.when(pluginWSMock.getColorTheme()).thenReturn(colorTheme);
PluginWorkspaceProvider.setPluginWorkspace(pluginWSMock);
Mockito.doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
if (arguments.length == 2) {
urls2compare.add((URL) arguments[0]);
urls2compare.add((URL) arguments[1]);
}
return null;
}
}).when(pluginWSMock).openDiffFilesApplication(Mockito.any(), Mockito.any());
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
if (arguments != null && arguments.length > 0) {
toOpen.add((URL) invocation.getArguments()[0]);
}
return null;
}
}).when(pluginWSMock).open(Mockito.any());
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
if (invocation.getArguments().length > 0) {
WSEditorChangeListener listener = (WSEditorChangeListener) invocation.getArguments()[0];
editorChangeListeners.add(listener);
}
return null;
}
}).when(pluginWSMock).addEditorChangeListener((WSEditorChangeListener) Mockito.any(), Mockito.anyInt());
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WSEditorChangeListener listener = (WSEditorChangeListener) invocation.getArguments()[0];
editorChangeListeners.remove(listener);
return null;
}
}).when(pluginWSMock).removeEditorChangeListener((WSEditorChangeListener) Mockito.any(), Mockito.anyInt());
Mockito.when(pluginWSMock.getEditorAccess((URL) Mockito.any(), Mockito.anyInt())).then(new Answer<WSEditor>() {
@Override
public WSEditor answer(InvocationOnMock invocation) throws Throwable {
WSEditor wsEditorMock = createWSEditorMock((URL) invocation.getArguments()[0]);
return wsEditorMock;
}
});
XMLUtilAccess xmlUtilAccess = Mockito.mock(XMLUtilAccess.class);
Mockito.when(xmlUtilAccess.escapeTextValue(Mockito.anyString())).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
Object object = invocation.getArguments()[0];
return object != null ? (String) object : "";
}
});
Mockito.when(xmlUtilAccess.unescapeAttributeValue(Mockito.anyString())).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
Object object = invocation.getArguments()[0];
return object != null ? (String) object : "";
}
});
Mockito.doReturn(xmlUtilAccess).when(pluginWSMock).getXMLUtilAccess();
UtilAccess utilAccessMock = Mockito.mock(UtilAccess.class);
Mockito.when(pluginWSMock.getUtilAccess()).thenReturn(utilAccessMock);
Mockito.when(utilAccessMock.locateFile((URL) Mockito.any())).then(new Answer<File>() {
@Override
public File answer(InvocationOnMock invocation) throws Throwable {
URL url = (URL) invocation.getArguments()[0];
String path = url.getPath();
if (PlatformDetectionUtil.isWin() && path.startsWith("/")) {
path = path.substring(1, path.length());
}
return new File(url.getPath());
}
});
// PluginWorkspaceProvider.getPluginWorkspace().getUtilAccess().getFileName()
Mockito.when(utilAccessMock.getFileName(Mockito.anyString())).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
String file = (String) invocation.getArguments()[0];
file = file.replace('\\', '/');
int index = file.lastIndexOf("/");
return index != -1 ? file.substring(index + 1) : file;
}
});
Mockito.when(utilAccessMock.uncorrectURL(Mockito.anyString())).then(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArguments()[0].toString().replace("%20", " ");
}
});
ImageUtilities imgUtils = Mockito.mock(ImageUtilities.class);
Mockito.when(pluginWSMock.getImageUtilities()).thenReturn(imgUtils);
Mockito.when(imgUtils.loadIcon((URL) Mockito.any())).thenAnswer(new Answer<ImageIcon>() {
@Override
public ImageIcon answer(InvocationOnMock invocation) throws Throwable {
URL url = (URL) invocation.getArguments()[0];
return new ImageIcon(url);
}
});
ProjectController projectCtrlMock = Mockito.mock(ProjectController.class);
Mockito.when(pluginWSMock.getProjectManager()).thenReturn(projectCtrlMock);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(projectCtrlMock).refreshFolders(Mockito.any());
WSOptionsStorage wsOptions = new TestWsOptionsStorage();
Mockito.when(pluginWSMock.getOptionsStorage()).thenReturn(wsOptions);
installGitProtocol();
GitAccess gitAccess = GitAccess.getInstance();
GitController ctrl = new GitController(gitAccess);
ctrl.addGitListener(new GitEventAdapter() {
private Repository oldRepository;
@Override
public void operationAboutToStart(GitEventInfo info) {
if (info.getGitOperation() == GitOperation.OPEN_WORKING_COPY) {
try {
oldRepository = gitAccess.getRepository();
} catch (NoRepositorySelected e) {
// Ignore
}
}
}
@Override
public void operationSuccessfullyEnded(GitEventInfo info) {
if (info.getGitOperation() == GitOperation.OPEN_WORKING_COPY) {
if (oldRepository != null) {
loadedRepos.remove(oldRepository);
}
oldRepository = null;
}
}
@Override
public void operationFailed(GitEventInfo info, Throwable t) {
if (info.getGitOperation() == GitOperation.OPEN_WORKING_COPY) {
oldRepository = null;
}
}
});
OptionsManager.getInstance().loadOptions(wsOptions);
gitAccess.getStatusCache().installEditorsHook(pluginWSMock);
}
use of com.oxygenxml.git.view.event.GitEventInfo in project oxygen-git-client-addon by oxygenxml.
the class GitAccess method continueRebase.
/**
* Continue rebase after a conflict resolution.
*/
public void continueRebase() {
fireOperationAboutToStart(new GitEventInfo(GitOperation.CONTINUE_REBASE));
GitOperationScheduler.getInstance().schedule(() -> {
try {
RebaseResult result = git.rebase().setOperation(Operation.CONTINUE).call();
if (result.getStatus() == RebaseResult.Status.NOTHING_TO_COMMIT) {
skipCommit();
}
fireOperationSuccessfullyEnded(new GitEventInfo(GitOperation.CONTINUE_REBASE));
} catch (UnmergedPathsException e) {
fireOperationFailed(new GitEventInfo(GitOperation.CONTINUE_REBASE), e);
LOGGER.debug(e.getMessage(), e);
PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(TRANSLATOR.getTranslation(Tags.CANNOT_CONTINUE_REBASE_BECAUSE_OF_CONFLICTS));
} catch (GitAPIException e) {
fireOperationFailed(new GitEventInfo(GitOperation.CONTINUE_REBASE), e);
LOGGER.debug(e.getMessage(), e);
PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(e.getMessage());
}
});
}
use of com.oxygenxml.git.view.event.GitEventInfo in project oxygen-git-client-addon by oxygenxml.
the class GitAccess method dropStash.
/**
* Drops one stash item from the list of stashes.
*
* @param stashIndex The index of the stash item to be dropped.
*
* @throws GitAPIException
*/
public void dropStash(int stashIndex) throws GitAPIException {
fireOperationAboutToStart(new GitEventInfo(GitOperation.STASH_DROP));
try {
git.stashDrop().setStashRef(stashIndex).call();
fireOperationSuccessfullyEnded(new GitEventInfo(GitOperation.STASH_DROP));
} catch (GitAPIException e) {
LOGGER.error(e.getMessage(), e);
fireOperationFailed(new GitEventInfo(GitOperation.STASH_DROP), e);
throw e;
}
}
use of com.oxygenxml.git.view.event.GitEventInfo in project oxygen-git-client-addon by oxygenxml.
the class GitAccess method displayStashApplyFailedCauseMessage.
/**
* Display the stash apply failed cause message for user.
*
* @param isPop <code>true</code> if the pop method.
* @param status stash status operation.
* @param exception the exception that cause the fail in apply stash.
*/
private void displayStashApplyFailedCauseMessage(boolean isPop, StashApplyStatus status, Exception exception) {
List<String> conflictingList = new ArrayList<>(getConflictingFiles());
if (!conflictingList.isEmpty() && status != StashApplyStatus.CANNOT_START_APPLY_BECAUSE_CONFLICTS) {
status = StashApplyStatus.APPLIED_SUCCESSFULLY_WITH_CONFLICTS;
}
switch(status) {
case APPLIED_SUCCESSFULLY_WITH_CONFLICTS:
if (isPop) {
FileStatusDialog.showWarningMessage(TRANSLATOR.getTranslation(Tags.APPLY_STASH), conflictingList, TRANSLATOR.getTranslation(Tags.STASH_GENERATE_CONFLICTS) + " " + TRANSLATOR.getTranslation(Tags.STASH_WAS_KEPT));
} else {
FileStatusDialog.showWarningMessage(TRANSLATOR.getTranslation(Tags.APPLY_STASH), conflictingList, TRANSLATOR.getTranslation(Tags.STASH_GENERATE_CONFLICTS));
}
fireOperationSuccessfullyEnded(new GitEventInfo(GitOperation.STASH_APPLY));
break;
case CANNOT_START_APPLY_BECAUSE_CONFLICTS:
FileStatusDialog.showErrorMessage(TRANSLATOR.getTranslation(Tags.APPLY_STASH), new ArrayList<>(getConflictingFiles()), TRANSLATOR.getTranslation(Tags.UNABLE_TO_APPLY_STASH) + ". " + TRANSLATOR.getTranslation(Tags.RESOLVE_CONFLICTS_FIRST));
fireOperationFailed(new GitEventInfo(GitOperation.STASH_APPLY), exception);
LOGGER.error(exception.getMessage(), exception);
break;
case CANNOT_START_APPLY_BECAUSE_UNCOMMITTED_FILES:
FileStatusDialog.showErrorMessage(TRANSLATOR.getTranslation(Tags.APPLY_STASH), null, TRANSLATOR.getTranslation(Tags.UNABLE_TO_APPLY_STASH) + ". " + TRANSLATOR.getTranslation(Tags.STASH_SOLUTIONS_TO_APPLY));
fireOperationFailed(new GitEventInfo(GitOperation.STASH_APPLY), exception);
LOGGER.error(exception.getMessage(), exception);
break;
case CANNOT_START_BECAUSE_STAGED_FILES:
FileStatusDialog.showErrorMessage(TRANSLATOR.getTranslation(Tags.APPLY_STASH), null, TRANSLATOR.getTranslation(Tags.STASH_REMOVE_STAGED_CHANGES));
fireOperationFailed(new GitEventInfo(GitOperation.STASH_APPLY), exception);
LOGGER.error(exception.getMessage(), exception);
break;
default:
PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(TRANSLATOR.getTranslation(Tags.UNABLE_TO_APPLY_STASH) + ".", exception);
LOGGER.error(exception.getMessage(), exception);
fireOperationFailed(new GitEventInfo(GitOperation.STASH_APPLY), exception);
break;
}
}
use of com.oxygenxml.git.view.event.GitEventInfo in project oxygen-git-client-addon by oxygenxml.
the class GitAccess method revertCommit.
/**
* Reverts the given commit.
*
* @param commitId The commit id to which to reset.
*
* @throws IOException
* @throws NoRepositorySelected
* @throws GitAPIException
*/
public void revertCommit(String commitId) throws IOException, NoRepositorySelected, GitAPIException {
Status gitStatus = git.status().call();
if (!gitStatus.getConflicting().isEmpty()) {
PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(Translator.getInstance().getTranslation(Tags.RESOLVE_CONFLICTS_FIRST));
} else if (gitStatus.hasUncommittedChanges()) {
FileStatusDialog.showErrorMessage(Translator.getInstance().getTranslation(Tags.REVERT_COMMIT), new ArrayList<>(gitStatus.getUncommittedChanges()), Translator.getInstance().getTranslation(Tags.REVERT_COMMIT_FAILED_UNCOMMITTED_CHANGES_MESSAGE));
} else {
fireOperationAboutToStart(new GitEventInfo(GitOperation.REVERT_COMMIT));
Repository repo = git.getRepository();
try (RevWalk revWalk = new RevWalk(repo)) {
RevCommit revcom = revWalk.parseCommit(getRepository().resolve(commitId));
git.revert().include(revcom).call();
Set<String> conflictingFiles = getConflictingFiles();
if (!conflictingFiles.isEmpty()) {
FileStatusDialog.showWarningMessage(TRANSLATOR.getTranslation(Tags.REVERT_COMMIT), new ArrayList<>(conflictingFiles), TRANSLATOR.getTranslation(Tags.REVERT_COMMIT_RESULTED_IN_CONFLICTS));
}
fireOperationSuccessfullyEnded(new GitEventInfo(GitOperation.REVERT_COMMIT));
} catch (GitAPIException | RevisionSyntaxException e) {
fireOperationFailed(new GitEventInfo(GitOperation.REVERT_COMMIT), e);
PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(e.getMessage(), e);
}
}
}
Aggregations