From 06ff0a528375c8e7b46e81467bdeda6fed433e55 Mon Sep 17 00:00:00 2001 From: Constantine Plotnikov Date: Wed, 4 Aug 2010 15:27:09 +0400 Subject: [PATCH] git4idea: Added detection of branch configurations --- .../branches/GitBranchConfiguration.java | 20 +++++- .../branches/GitBranchConfigurations.java | 69 +++++++++++++++---- .../GitManageConfigurationsDialog.form | 62 ++++++++++++----- .../GitManageConfigurationsDialog.java | 34 +++++++-- 4 files changed, 147 insertions(+), 38 deletions(-) diff --git a/plugins/git4idea/src/git4idea/checkout/branches/GitBranchConfiguration.java b/plugins/git4idea/src/git4idea/checkout/branches/GitBranchConfiguration.java index b6ac0cda1cec..1ac1e66b5327 100644 --- a/plugins/git4idea/src/git4idea/checkout/branches/GitBranchConfiguration.java +++ b/plugins/git4idea/src/git4idea/checkout/branches/GitBranchConfiguration.java @@ -16,6 +16,7 @@ package git4idea.checkout.branches; import com.intellij.util.xmlb.XmlSerializerUtil; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.HashMap; @@ -90,18 +91,33 @@ public class GitBranchConfiguration { } } - public void setBranch(String root, String reference) { + /** + * Set the mapping for the existing branch + * + * @param root the root + * @param reference the reference + */ + public void setBranch(@NotNull String root, @NotNull String reference) { synchronized (myConfig.getStateLock()) { myReferences.put(root, reference); } } - public String getReference(String root) { + /** + * Get reference in the mapping + * + * @param root the root to get reference for + * @return the branch mapping + */ + public String getReference(@NotNull String root) { synchronized (myConfig.getStateLock()) { return myReferences.get(root); } } + /** + * Clear all references in the mapping + */ public void clearReferences() { synchronized (myConfig.getStateLock()) { myReferences.clear(); diff --git a/plugins/git4idea/src/git4idea/checkout/branches/GitBranchConfigurations.java b/plugins/git4idea/src/git4idea/checkout/branches/GitBranchConfigurations.java index d6e4d5d6acf3..8cfaeded7b3a 100644 --- a/plugins/git4idea/src/git4idea/checkout/branches/GitBranchConfigurations.java +++ b/plugins/git4idea/src/git4idea/checkout/branches/GitBranchConfigurations.java @@ -503,33 +503,27 @@ public class GitBranchConfigurations implements PersistentStateComponent locals = detectConfigurations(true, myGitRoots); - if (locals.isEmpty()) { - // no commits - locals.add("master"); - } - for (String localName : locals) { - GitBranchConfiguration c = createConfiguration(localName); - c.setAutoDetected(true); + detectLocalConfigurations(true); + for (GitBranchConfiguration configuration : myConfigurations.values()) { boolean currentsMatched = true; for (VirtualFile root : myGitRoots) { - c.setBranch(root.getPath(), localName); - currentsMatched &= currents.get(root).equals(localName); + currentsMatched &= currents.get(root).equals(configuration.getReference(root.getPath())); } if (currentsMatched) { - myCurrentConfiguration = c; + myCurrentConfiguration = configuration; + break; } } if (myCurrentConfiguration == null) { // the configuration does not matches any standard, there could be no configurations with spaces at this point // since it is not allowed branch name. String name = "untitled"; - if (locals.contains(name)) { + if (myConfigurations.containsKey(name)) { String p = name; name = null; for (int i = 0; i < Integer.MAX_VALUE; i++) { final String c = p + i; - if (!locals.contains(c)) { + if (!myConfigurations.containsKey(c)) { name = c; break; } @@ -549,6 +543,55 @@ public class GitBranchConfigurations implements PersistentStateComponent locals = detectConfigurations(true, myGitRoots); + if (locals.isEmpty()) { + // no commits + locals.add("master"); + } + locals.removeAll(myConfigurations.keySet()); + for (String localName : locals) { + GitBranchConfiguration c = createConfiguration(localName); + c.setAutoDetected(true); + for (VirtualFile root : myGitRoots) { + c.setBranch(root.getPath(), localName); + } + } + } + else { + HashSet detected = new HashSet(); + HashSet forRoot = new HashSet(); + for (VirtualFile root : myGitRoots) { + forRoot.clear(); + GitBranch.listAsStrings(myProject, root, false, true, forRoot, null); + for (String b : forRoot) { + GitBranchConfiguration c; + if (detected.contains(b)) { + c = myConfigurations.get(b); + } + else if (!myConfigurations.containsKey(b)) { + detected.add(b); + c = createConfiguration(b); + c.setAutoDetected(true); + } + else { + continue; + } + c.setBranch(root.getPath(), b); + } + } + } + } + } + /** * The configurations changed */ diff --git a/plugins/git4idea/src/git4idea/checkout/branches/GitManageConfigurationsDialog.form b/plugins/git4idea/src/git4idea/checkout/branches/GitManageConfigurationsDialog.form index 16bcbe702c4b..8f3042dfc3f1 100644 --- a/plugins/git4idea/src/git4idea/checkout/branches/GitManageConfigurationsDialog.form +++ b/plugins/git4idea/src/git4idea/checkout/branches/GitManageConfigurationsDialog.form @@ -29,27 +29,9 @@ - + - - - - - - - - - - - - - - - - - - @@ -116,6 +98,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plugins/git4idea/src/git4idea/checkout/branches/GitManageConfigurationsDialog.java b/plugins/git4idea/src/git4idea/checkout/branches/GitManageConfigurationsDialog.java index 20bad4ea7882..fb766dd1ffaa 100644 --- a/plugins/git4idea/src/git4idea/checkout/branches/GitManageConfigurationsDialog.java +++ b/plugins/git4idea/src/git4idea/checkout/branches/GitManageConfigurationsDialog.java @@ -25,6 +25,7 @@ import com.intellij.openapi.vcs.changes.shelf.ShelvedChangeList; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.ui.components.JBList; import com.intellij.ui.table.JBTable; +import git4idea.ui.GitUIUtil; import org.jetbrains.annotations.Nullable; import javax.swing.*; @@ -72,6 +73,14 @@ public class GitManageConfigurationsDialog extends DialogWrapper { * The name label */ private JLabel myShelveNameLabel; + /** + * Detect branch configurations button + */ + private JButton myDetectConfigurationsButton; + /** + * The include incomplete checkbox + */ + private JCheckBox myIncludeIncompleteCheckBox; /** * The project to use */ @@ -117,10 +126,7 @@ public class GitManageConfigurationsDialog extends DialogWrapper { } }); myNamesModel = new DefaultListModel(); - myNamesList.setModel(myNamesModel); - for (String n : myConfigurations.getConfigurationNames()) { - myNamesModel.addElement(n); - } + refreshNames(); myDeleteButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -138,6 +144,18 @@ public class GitManageConfigurationsDialog extends DialogWrapper { myNamesList.setSelectedIndex(i); } }); + myDetectConfigurationsButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + myConfigurations.detectLocalConfigurations(myIncludeIncompleteCheckBox.isSelected()); + refreshNames(); + } + catch (VcsException e1) { + GitUIUtil.showOperationError(myProject, "Branch configuration detection failed", e1.getMessage()); + } + } + }); init(); if (myNamesModel.size() > 0) { myNamesList.setSelectedIndex(0); @@ -145,6 +163,14 @@ public class GitManageConfigurationsDialog extends DialogWrapper { updateOnSelection(); } + private void refreshNames() { + myNamesModel.clear(); + myNamesList.setModel(myNamesModel); + for (String n : myConfigurations.getConfigurationNames()) { + myNamesModel.addElement(n); + } + } + /** * Update dialog on selection */