Skip to content

Commit ba7b2ad

Browse files
Switch branch Action (#286)
* Contributed an action to Switch Branch for a linked repository * Available as a context menu option in the abapGit repositories view.
1 parent 40865d5 commit ba7b2ad

16 files changed

+765
-27
lines changed
708 Bytes
Loading

org.abapgit.adt.ui/src/org/abapgit/adt/ui/internal/i18n/Messages.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
public class Messages extends NLS {
66
private static final String BUNDLE_NAME = "org.abapgit.adt.ui.internal.i18n.messages"; //$NON-NLS-1$
7+
public static String AbapGitView_action_switch_branch;
78
public static String AbapGitDialogPageObjLog_default_filename;
89
public static String AbapGitView_action_clone;
910
public static String AbapGitView_action_refresh;
@@ -138,6 +139,12 @@ public class Messages extends NLS {
138139
public static String AbapGitDialogPageObjLog_pull_title;
139140
public static String AbapGitDialogPageObjLog_push_description;
140141
public static String AbapGitDialogPageObjLog_push_title;
142+
public static String AbapGitWizardPageSwitch_branch_selection_title;
143+
public static String AbapGitWizardPageSwitch_branch_credentials_title;
144+
public static String AbapGitWizardPageSwitch_select_branch_description;
145+
public static String AbapGitWizardPageSwitch_credentials_description;
146+
public static String AbapGitWizardSwitch_branch_wizard_title;
147+
public static String AbapGitWizardSwitch_branch_package_ref_not_found_error;
141148

142149
//ABAPGIT STAGING MESSAGES
143150
public static String AbapGitStaging_no_repository_selected;

org.abapgit.adt.ui/src/org/abapgit/adt/ui/internal/i18n/messages.properties

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ AbapGitView_action_open_repo=Open Repository in Browser
88
AbapGitView_action_open_repo_tooltip=Open Repository in Browser
99
AbapGitView_action_open_repo_error_dialog_title=Error while opening repository in browser
1010
AbapGitView_action_open_xtol=Open Linked Package
11+
AbapGitView_action_switch_branch=Switch Branch...
12+
AbapGitWizardSwitch_branch_wizard_title=Switch Branch
13+
AbapGitWizardPageSwitch_branch_selection_title=Branch Selection
14+
AbapGitWizardPageSwitch_branch_credentials_title=User Authentication
15+
AbapGitWizardPageSwitch_select_branch_description=Select the branch.
16+
AbapGitWizardPageSwitch_credentials_description=Specify credentials for git repository.
1117
AbapGitView_column_branch=Branch
1218
AbapGitView_column_firstcommitat=Last Changed (UTC)
1319
AbapGitView_column_folder_logic=Folder Logic
@@ -132,6 +138,7 @@ AbapGitWizardPageRepositoryAndCredentials_title=Link abapGit Repository
132138
AbapGitWizardPageRepositoryAndCredentials_validate_password_error=Specify password or personal access token
133139
AbapGitWizardPageRepositoryAndCredentials_validate_url_error=Enter a valid Git URL
134140
AbapGitWizardPageRepositoryAndCredentials_validate_user_error=Specify user
141+
AbapGitWizardSwitch_branch_package_ref_not_found_error=Could not find package ref for {0}
135142

136143
AbapGitStaging_no_repository_selected=No Repository Selected
137144
AbapGitStaging_check_job_title=Repository checks

org.abapgit.adt.ui/src/org/abapgit/adt/ui/internal/repositories/AbapGitView.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.abapgit.adt.ui.internal.dialogs.AbapGitDialogObjLog;
2020
import org.abapgit.adt.ui.internal.i18n.Messages;
2121
import org.abapgit.adt.ui.internal.repositories.actions.OpenRepositoryAction;
22+
import org.abapgit.adt.ui.internal.repositories.actions.SwitchbranchAction;
2223
import org.abapgit.adt.ui.internal.staging.AbapGitStagingView;
2324
import org.abapgit.adt.ui.internal.staging.IAbapGitStagingView;
2425
import org.abapgit.adt.ui.internal.util.AbapGitUIServiceFactory;
@@ -101,7 +102,8 @@ public class AbapGitView extends ViewPart implements IAbapGitRepositoriesView {
101102
public static final String ID = "org.abapgit.adt.ui.views.AbapGitView"; //$NON-NLS-1$
102103

103104
protected TableViewer viewer;
104-
protected Action actionRefresh, actionWizard, actionCopy, actionOpen, actionShowMyRepos, actionPullWizard, actionOpenRepository;
105+
protected Action actionRefresh, actionWizard, actionCopy, actionOpen, actionShowMyRepos, actionPullWizard, actionOpenRepository,
106+
actionSwitchBranch;
105107
private ISelection lastSelection;
106108
protected IProject lastProject;
107109
private ViewerFilter searchFilter;
@@ -113,6 +115,10 @@ public class AbapGitView extends ViewPart implements IAbapGitRepositoriesView {
113115
//key binding for copy text
114116
private static final KeyStroke KEY_STROKE_COPY = KeyStroke.getInstance(SWT.MOD1, 'C' | 'c');
115117

118+
public void refresh() {
119+
updateView(true);
120+
}
121+
116122
private final ISelectionListener selectionListener = new ISelectionListener() {
117123
private boolean isUpdatingSelection = false;
118124
@Override
@@ -403,6 +409,10 @@ public void menuAboutToShow(IMenuManager manager) {
403409
}
404410
//separator
405411
manager.add(new Separator());
412+
//switch Branch Action
413+
manager.add(AbapGitView.this.actionSwitchBranch);
414+
//separator
415+
manager.add(new Separator());
406416
//copy to clip-board action
407417
manager.add(AbapGitView.this.actionCopy);
408418
//unlink action
@@ -564,6 +574,9 @@ public void run() {
564574

565575
//Open repository in external browser
566576
this.actionOpenRepository = new OpenRepositoryAction(this);
577+
578+
//Switch Branches
579+
this.actionSwitchBranch = new SwitchbranchAction(this);
567580
}
568581

569582
private List<IRepository> getRepositories(String destinationId, Boolean byCurrUser) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.abapgit.adt.ui.internal.repositories.actions;
2+
3+
import org.abapgit.adt.backend.IExternalRepositoryInfoService;
4+
import org.abapgit.adt.backend.IRepositoryService;
5+
import org.abapgit.adt.backend.RepositoryServiceFactory;
6+
import org.abapgit.adt.backend.model.abapgitrepositories.IRepository;
7+
import org.abapgit.adt.ui.AbapGitUIPlugin;
8+
import org.abapgit.adt.ui.internal.i18n.Messages;
9+
import org.abapgit.adt.ui.internal.repositories.AbapGitView;
10+
import org.abapgit.adt.ui.internal.repositories.exceptions.PackageRefNotFoundException;
11+
import org.abapgit.adt.ui.internal.wizards.AbapGitWizardSwitchBranch;
12+
import org.eclipse.core.resources.IProject;
13+
import org.eclipse.core.runtime.NullProgressMonitor;
14+
import org.eclipse.jface.action.Action;
15+
import org.eclipse.jface.dialogs.MessageDialog;
16+
import org.eclipse.jface.wizard.WizardDialog;
17+
import org.eclipse.ui.plugin.AbstractUIPlugin;
18+
19+
import com.sap.adt.communication.resources.ResourceException;
20+
import com.sap.adt.tools.core.project.AdtProjectServiceFactory;
21+
import com.sap.adt.tools.core.ui.packages.AdtPackageServiceUIFactory;
22+
import com.sap.adt.tools.core.ui.packages.IAdtPackageServiceUI;
23+
24+
public class SwitchbranchAction extends Action {
25+
26+
private IRepository selRepo;
27+
private final AbapGitView AbapGitView;
28+
private IProject project;
29+
30+
public SwitchbranchAction(AbapGitView view) {
31+
super(Messages.AbapGitView_action_switch_branch);
32+
setToolTipText(Messages.AbapGitView_action_switch_branch);
33+
setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(AbapGitUIPlugin.PLUGIN_ID, "/icons/etool/branches_obj.png")); //$NON-NLS-1$
34+
this.AbapGitView = view;
35+
}
36+
37+
@Override
38+
public void run() {
39+
this.project = getProject();
40+
this.selRepo = getRepository();
41+
if (this.selRepo != null) {
42+
String destination = AdtProjectServiceFactory.createProjectService().getDestinationId(this.project);
43+
try {
44+
IAdtPackageServiceUI packageServiceUI = AdtPackageServiceUIFactory.getOrCreateAdtPackageServiceUI();
45+
IExternalRepositoryInfoService externalRepoInfoService = RepositoryServiceFactory
46+
.createExternalRepositoryInfoService(destination, null);
47+
IRepositoryService repoService = RepositoryServiceFactory.createRepositoryService(destination, new NullProgressMonitor());
48+
WizardDialog dialog = new WizardDialog(this.AbapGitView.getViewSite().getShell(),
49+
new AbapGitWizardSwitchBranch(this.project, this.selRepo, destination, packageServiceUI, externalRepoInfoService,
50+
repoService));
51+
dialog.open();
52+
} catch (PackageRefNotFoundException | ResourceException e) {
53+
MessageDialog.openError(this.AbapGitView.getViewSite().getShell(), Messages.AbapGitWizardSwitch_branch_wizard_title,
54+
e.getLocalizedMessage());
55+
}
56+
}
57+
this.AbapGitView.refresh();
58+
59+
}
60+
61+
private IRepository getRepository() {
62+
return this.AbapGitView.getRepositorySelection();
63+
}
64+
65+
private IProject getProject() {
66+
return this.AbapGitView.getProject();
67+
}
68+
69+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.abapgit.adt.ui.internal.repositories.exceptions;
2+
3+
public class PackageRefNotFoundException extends RuntimeException {
4+
5+
public PackageRefNotFoundException(String message) {
6+
super(message);
7+
}
8+
9+
public PackageRefNotFoundException(String message, Throwable cause) {
10+
super(message, cause);
11+
}
12+
13+
public PackageRefNotFoundException(Throwable cause) {
14+
super(cause);
15+
}
16+
}

org.abapgit.adt.ui/src/org/abapgit/adt/ui/internal/util/GitCredentialsService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public static String getUrlForNodePath(String uri) {
195195
}
196196

197197
//Open a popup asking if user wants to store the credentials in secure store
198-
public static boolean showPopUpAskingToStoreCredentials(Shell shell, String url, String user, String pass) {
198+
public boolean showPopUpAskingToStoreCredentials(Shell shell, String url, String user, String pass) {
199199
//since the credentials are proper ask if they should be stored in secure store
200200
MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
201201
messageBox.setText(Messages.AbapGitWizardPageRepositoryAndCredentials_credentials_manager_popup_title);

org.abapgit.adt.ui/src/org/abapgit/adt/ui/internal/wizards/AbapGitWizardPageBranchAndPackage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ public class AbapGitWizardPageBranchAndPackage extends WizardPage {
6969
private final CloneData cloneData;
7070
private final String pullBranch;
7171

72-
private Button checkbox_lnp;
72+
protected Button checkbox_lnp;
7373
private Boolean chboxLinkAndPull;
74-
private TextViewer txtPackage;
75-
private ComboViewer comboBranches;
74+
protected TextViewer txtPackage;
75+
protected ComboViewer comboBranches;
7676

7777
private final Boolean pullAction;
7878
private boolean backButtonEnabled = true;

org.abapgit.adt.ui/src/org/abapgit/adt/ui/internal/wizards/AbapGitWizardPageRepositoryAndCredentials.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.eclipse.core.resources.IProject;
1919
import org.eclipse.core.runtime.IProgressMonitor;
2020
import org.eclipse.core.runtime.IStatus;
21+
import org.eclipse.core.runtime.NullProgressMonitor;
2122
import org.eclipse.core.runtime.Status;
2223
import org.eclipse.equinox.security.storage.ISecurePreferences;
2324
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
@@ -44,14 +45,16 @@ public class AbapGitWizardPageRepositoryAndCredentials extends WizardPage {
4445
private final CloneData cloneData;
4546

4647
private Text txtURL;
47-
private Text txtUser;
48-
private Text txtPwd;
48+
protected Text txtUser;
49+
protected Text txtPwd;
4950
private Label lblUser;
5051
private Label lblPwd;
5152
private Pattern r;
5253
private String ptrn;
5354
private String newUrl;
54-
55+
protected IRepositoryService repoService;
56+
protected IExternalRepositoryInfoService externalRepoService;
57+
protected GitCredentialsService gitCredentialsService;
5558
private final Boolean pullAction;
5659
private boolean wasVisibleBefore;
5760
private IExternalRepositoryInfoRequest repositoryCredentials;
@@ -62,6 +65,9 @@ public AbapGitWizardPageRepositoryAndCredentials(IProject project, String destin
6265
this.destination = destination;
6366
this.cloneData = cloneData;
6467
this.pullAction = pullAction;
68+
this.gitCredentialsService = new GitCredentialsService();
69+
this.repoService = RepositoryServiceFactory.createRepositoryService(destination, new NullProgressMonitor());
70+
this.externalRepoService = RepositoryServiceFactory.createExternalRepositoryInfoService(destination, new NullProgressMonitor());
6571
setTitle(Messages.AbapGitWizardPageRepositoryAndCredentials_title);
6672
setDescription(Messages.AbapGitWizardPageRepositoryAndCredentials_description);
6773

@@ -168,8 +174,7 @@ public void setVisible(boolean visible) {
168174
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
169175
monitor.beginTask(Messages.AbapGitWizardPageRepositoryAndCredentials_task_check_compatibility,
170176
IProgressMonitor.UNKNOWN);
171-
isSupported[0] = RepositoryServiceFactory
172-
.createRepositoryService(AbapGitWizardPageRepositoryAndCredentials.this.destination, monitor) != null;
177+
isSupported[0] = AbapGitWizardPageRepositoryAndCredentials.this.repoService != null;
173178
}
174179
});
175180
if (!isSupported[0]) {
@@ -189,7 +194,7 @@ public void run(IProgressMonitor monitor) throws InvocationTargetException, Inte
189194

190195
}
191196

192-
private boolean validateClientOnly() {
197+
protected boolean validateClientOnly() {
193198
setMessage(null);
194199
setPageComplete(true);
195200

@@ -279,17 +284,15 @@ public boolean validateAll() {
279284
return true;
280285
}
281286

282-
private void fetchRepositories() {
287+
protected void fetchRepositories() {
283288
try {
284289
getContainer().run(true, true, new IRunnableWithProgress() {
285290

286291
@Override
287292
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
288293
monitor.beginTask(Messages.AbapGitWizardPageRepositoryAndCredentials_task_repo_fetch, IProgressMonitor.UNKNOWN);
289-
IRepositoryService repoService = RepositoryServiceFactory.createRepositoryService(AbapGitWizardPageRepositoryAndCredentials.this.destination,
290-
monitor);
291-
292-
AbapGitWizardPageRepositoryAndCredentials.this.cloneData.repositories = repoService.getRepositories(monitor);
294+
AbapGitWizardPageRepositoryAndCredentials.this.cloneData.repositories = AbapGitWizardPageRepositoryAndCredentials.this.repoService
295+
.getRepositories(monitor);
293296
}
294297
});
295298
} catch (InvocationTargetException e) {
@@ -300,16 +303,15 @@ public void run(IProgressMonitor monitor) throws InvocationTargetException, Inte
300303
}
301304
}
302305

303-
private boolean fetchExternalRepoInfo() {
306+
protected boolean fetchExternalRepoInfo() {
304307
try {
305308
getContainer().run(true, true, new IRunnableWithProgress() {
306309

307310
@Override
308311
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
309312
monitor.beginTask(Messages.AbapGitWizardPageRepositoryAndCredentials_task_fetch_repo_info, IProgressMonitor.UNKNOWN);
310-
IExternalRepositoryInfoService externalRepoInfoService = RepositoryServiceFactory
311-
.createExternalRepositoryInfoService(AbapGitWizardPageRepositoryAndCredentials.this.destination, monitor);
312-
AbapGitWizardPageRepositoryAndCredentials.this.cloneData.externalRepoInfo = externalRepoInfoService.getExternalRepositoryInfo(AbapGitWizardPageRepositoryAndCredentials.this.cloneData.url,
313+
AbapGitWizardPageRepositoryAndCredentials.this.cloneData.externalRepoInfo = AbapGitWizardPageRepositoryAndCredentials.this.externalRepoService
314+
.getExternalRepositoryInfo(AbapGitWizardPageRepositoryAndCredentials.this.cloneData.url,
313315
AbapGitWizardPageRepositoryAndCredentials.this.cloneData.user, AbapGitWizardPageRepositoryAndCredentials.this.cloneData.pass, monitor);
314316
}
315317
});
@@ -318,11 +320,12 @@ public void run(IProgressMonitor monitor) throws InvocationTargetException, Inte
318320

319321
//if credentials are provided, ask user if the credentials should be stored in the secure storage
320322
if (this.cloneData.user != null && this.cloneData.pass != null) {
321-
if (this.repositoryCredentials == null
323+
if ((this.repositoryCredentials == null
322324
|| (this.repositoryCredentials != null && (!this.cloneData.pass.equals(this.repositoryCredentials.getPassword())
323-
|| !this.cloneData.user.equals(this.repositoryCredentials.getUser())))) {
324-
GitCredentialsService.showPopUpAskingToStoreCredentials(getShell(), this.cloneData.url, this.cloneData.user,
325-
this.cloneData.pass);
325+
|| !this.cloneData.user.equals(this.repositoryCredentials.getUser()))))
326+
&& (this.gitCredentialsService != null)) {
327+
this.gitCredentialsService.showPopUpAskingToStoreCredentials(getShell(), this.cloneData.url, this.cloneData.user,
328+
this.cloneData.pass);
326329
}
327330
}
328331
return true;
@@ -337,7 +340,7 @@ public void run(IProgressMonitor monitor) throws InvocationTargetException, Inte
337340
}
338341
}
339342

340-
private void setUserAndPassControlsVisible(boolean visible) {
343+
protected void setUserAndPassControlsVisible(boolean visible) {
341344
this.txtUser.setVisible(visible);
342345
this.txtPwd.setVisible(visible);
343346
this.lblUser.setVisible(visible);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.abapgit.adt.ui.internal.wizards;
2+
3+
import org.abapgit.adt.ui.internal.i18n.Messages;
4+
import org.abapgit.adt.ui.internal.wizards.AbapGitWizard.CloneData;
5+
import org.eclipse.core.resources.IProject;
6+
7+
public class AbapGitWizardPageSwitchBranchAndPackage extends AbapGitWizardPageBranchAndPackage {
8+
9+
public AbapGitWizardPageSwitchBranchAndPackage(IProject project, String destination, CloneData cloneData) {
10+
super(project, destination, cloneData, false);
11+
setTitle(Messages.AbapGitWizardPageSwitch_branch_selection_title);
12+
setDescription(Messages.AbapGitWizardPageSwitch_select_branch_description);
13+
}
14+
15+
@Override
16+
public void setVisible(boolean visible) {
17+
super.setVisible(visible);
18+
19+
if (visible) {
20+
this.comboBranches.getCombo().setEnabled(true);
21+
// hiding the pull after link checkbox
22+
this.checkbox_lnp.setVisible(false);
23+
}
24+
}
25+
26+
@Override
27+
public boolean canFlipToNextPage() {
28+
return false;
29+
}
30+
31+
}

0 commit comments

Comments
 (0)