Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Rosenberg2011-09-05 15:03:36 +0000
committerMatthias Sohn2011-09-05 15:03:36 +0000
commit09f457f5465127053757a86073145d520d157124 (patch)
treed56c45d97623a1f7a8b2ec79406d15fc57449a55
parent6859aaf7b816d065ae28cf614dfb369d51b9edaa (diff)
downloadegit-09f457f5465127053757a86073145d520d157124.tar.gz
egit-09f457f5465127053757a86073145d520d157124.tar.xz
egit-09f457f5465127053757a86073145d520d157124.zip
Start the switch-to menu with recently used branch names first
This assures that you rarely need to use the Other... menu item. Branch names are still ordered alphabetically. If only a few recently used branch names exist, more entries are added as before this change. Depend on JGit change I91c7e08c4afd2562df2226887a933d93c78a0371 Change-Id: I5e666fb37d70c078eac816af8d66dcc000b16be8 Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/SwitchToMenu.java95
1 files changed, 69 insertions, 26 deletions
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/SwitchToMenu.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/SwitchToMenu.java
index 73a1c1c818..dfca6555e7 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/SwitchToMenu.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/SwitchToMenu.java
@@ -9,9 +9,10 @@
package org.eclipse.egit.ui.internal.actions;
import java.io.IOException;
+import java.util.List;
import java.util.Map;
-import java.util.TreeMap;
import java.util.Map.Entry;
+import java.util.TreeMap;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IAdaptable;
@@ -27,6 +28,9 @@ import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.storage.file.CheckoutEntry;
+import org.eclipse.jgit.storage.file.ReflogEntry;
+import org.eclipse.jgit.storage.file.ReflogReader;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
@@ -101,37 +105,57 @@ public class SwitchToMenu extends ContributionItem implements
}
});
new MenuItem(menu, SWT.SEPARATOR);
- String currentBranch;
- Map<String, Ref> localBranches;
try {
- currentBranch = mapping.getRepository().getFullBranch();
- localBranches = mapping.getRepository().getRefDatabase().getRefs(
+ String currentBranch = mapping.getRepository().getFullBranch();
+ Map<String, Ref> localBranches = mapping.getRepository().getRefDatabase().getRefs(
Constants.R_HEADS);
- // sort by name
TreeMap<String, Ref> sortedRefs = new TreeMap<String, Ref>();
- sortedRefs.putAll(localBranches);
+
+ // Add the MAX_NUM_MENU_ENTRIES most recently used branches first
+ List<ReflogEntry> reflogEntries = new ReflogReader(
+ mapping.getRepository(), Constants.HEAD)
+ .getReverseEntries();
+ for (ReflogEntry entry : reflogEntries) {
+ CheckoutEntry checkout = entry.parseCheckout();
+ if (checkout != null) {
+ Ref ref = localBranches.get(checkout.getFromBranch());
+ if (ref != null)
+ if (sortedRefs.size() < MAX_NUM_MENU_ENTRIES)
+ sortedRefs.put(checkout.getFromBranch(), ref);
+ ref = localBranches.get(checkout.getToBranch());
+ if (ref != null)
+ if (sortedRefs.size() < MAX_NUM_MENU_ENTRIES)
+ sortedRefs.put(checkout.getToBranch(), ref);
+ }
+ }
+
+ // Add the recently used branches to the menu, in alphabetical order
int itemCount = 0;
for (final Entry<String, Ref> entry : sortedRefs.entrySet()) {
itemCount++;
- // protect ourselves against a huge sub-menu
- if (itemCount > MAX_NUM_MENU_ENTRIES)
- break;
- MenuItem item = new MenuItem(menu, SWT.PUSH);
- item.setText(entry.getKey());
- boolean checkedOut = currentBranch.equals(entry.getValue()
- .getName());
- if (checkedOut)
- item.setImage(checkedOutImage);
- else
- item.setImage(branchImage);
- item.setEnabled(!checkedOut);
- item.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- BranchOperationUI.checkout(repository,
- entry.getValue().getName()).start();
- }
- });
+ final String shortName = entry.getKey();
+ final String fullName = entry.getValue().getName();
+ createMenuItem(menu, repository, currentBranch, fullName, shortName);
+ // Do not duplicate branch names
+ localBranches.remove(shortName);
+ }
+
+ if (itemCount < MAX_NUM_MENU_ENTRIES && itemCount > 0 && sortedRefs.size() > 0) {
+ // Now add more other branches if we have only a few branch switches
+ // Sort the remaining local branches
+ sortedRefs.clear();
+ sortedRefs.putAll(localBranches);
+ // A separator between recently used branches and other branches is nice
+ new MenuItem(menu, SWT.SEPARATOR);
+ for (final Entry<String, Ref> entry : sortedRefs.entrySet()) {
+ itemCount++;
+ // protect ourselves against a huge sub-menu
+ if (itemCount > MAX_NUM_MENU_ENTRIES)
+ break;
+ final String fullName = entry.getValue().getName();
+ final String shortName = entry.getKey();
+ createMenuItem(menu, repository, fullName, shortName, shortName);
+ }
}
if (localBranches.size() > 0)
new MenuItem(menu, SWT.SEPARATOR);
@@ -148,6 +172,25 @@ public class SwitchToMenu extends ContributionItem implements
}
}
+ private void createMenuItem(Menu menu, final Repository repository,
+ String currentBranch, final String fullName, String shortName) {
+ final MenuItem item = new MenuItem(menu, SWT.PUSH);
+ item.setText(shortName);
+ boolean checkedOut = currentBranch.equals(fullName);
+ if (checkedOut)
+ item.setImage(checkedOutImage);
+ else
+ item.setImage(branchImage);
+ item.setEnabled(!checkedOut);
+ item.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ BranchOperationUI.checkout(repository, fullName)
+ .start();
+ }
+ });
+ }
+
@Override
public boolean isDynamic() {
return true;

Back to the top