git4idea: Added detection of branch configurations

This commit is contained in:
Constantine Plotnikov
2010-08-04 15:27:09 +04:00
parent 6e85c5de45
commit 06ff0a5283
4 changed files with 147 additions and 38 deletions
@@ -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();
@@ -503,33 +503,27 @@ public class GitBranchConfigurations implements PersistentStateComponent<GitBran
currents.put(root, current == null ? "" : current.getName());
}
if (myConfigurations.isEmpty()) {
List<String> 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<GitBran
}
}
/**
* Detect local configurations
*
* @param complete if complete configurations should be detected
* @throws VcsException
*/
void detectLocalConfigurations(boolean complete) throws VcsException {
synchronized (myStateLock) {
if (complete) {
List<String> 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<String> detected = new HashSet<String>();
HashSet<String> forRoot = new HashSet<String>();
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
*/
@@ -29,27 +29,9 @@
</constraints>
<properties>
<labelFor value="1ca7c"/>
<text value="&amp;Branches"/>
<text value="&amp;Branch Configurations"/>
</properties>
</component>
<grid id="eb1ed" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="87360" class="javax.swing.JButton" binding="myDeleteButton" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&amp;Delete"/>
</properties>
</component>
</children>
</grid>
<grid id="f0a1f" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
@@ -116,6 +98,48 @@
</component>
</children>
</grid>
<grid id="e0ed5" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="fe707" class="javax.swing.JButton" binding="myDetectConfigurationsButton" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Dete&amp;ct Configurations"/>
<toolTipText value="Detect configurations"/>
</properties>
</component>
<component id="87360" class="javax.swing.JButton" binding="myDeleteButton" default-binding="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&amp;Delete"/>
<toolTipText value="Delete selected configurations"/>
</properties>
</component>
<hspacer id="b475">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="afcc2" class="javax.swing.JCheckBox" binding="myIncludeIncompleteCheckBox" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&amp;Include incomplete"/>
<toolTipText value="If selected, the incomplete configurations are also detected"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>
@@ -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
*/