/**
  * Prepare branch descriptors for existing configuration
  *
  * @param target the target
  * @param roots the vcs root
  * @return the list of branch descriptors
  * @throws VcsException in case of error
  */
 private List<BranchDescriptor> prepareBranchDescriptors(
     BranchConfiguration target, List<VirtualFile> roots) throws VcsException {
   Map<String, String> map =
       target == null ? Collections.<String, String>emptyMap() : target.getReferences();
   List<BranchDescriptor> rc = new ArrayList<BranchDescriptor>();
   for (VirtualFile root : roots) {
     BranchDescriptor d = new BranchDescriptor();
     d.root = root;
     d.storedReference = map.remove(root.getPath());
     if (d.storedReference != null) {
       d.storedRoot = d.root.getPath();
     }
     d.currentReference = myConfig.describeRoot(root);
     if (d.storedReference != null && !myModify) {
       d.referenceToCheckout = d.storedReference;
     } else {
       d.referenceToCheckout = d.currentReference;
     }
     Branch.listAsStrings(myProject, root, false, true, d.existingBranches, null);
     Branch.listAsStrings(myProject, root, true, true, d.referencesToSelect, null);
     d.updateStatus();
     rc.add(d);
   }
   for (Map.Entry<String, String> m : map.entrySet()) {
     String root = m.getKey();
     String ref = m.getValue();
     BranchDescriptor d = new BranchDescriptor();
     d.storedReference = ref;
     d.storedRoot = root;
     d.referenceToCheckout = ref;
     d.updateStatus();
     rc.add(d);
   }
   return rc;
 }
 /** {@inheritDoc} */
 @Override
 public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
   String t = (String) aValue;
   BranchDescriptor d = myBranches.get(rowIndex);
   if (d.root == null) {
     return;
   }
   if (columnIndex == REVISION_COLUMN) {
     String currentCandidate = getCandidateLocal(d.referenceToCheckout, d);
     boolean isCurrentMatchCandidate =
         currentCandidate != null && currentCandidate.equals(d.newBranchName);
     d.referenceToCheckout = t;
     if ((StringUtil.isEmpty(d.newBranchName) || isCurrentMatchCandidate)
         && t.startsWith(REMOTES_PREFIX)
         && d.referencesToSelect.contains(t)) {
       String candidate = getCandidateLocal(t, d);
       if (candidate != null) {
         d.newBranchName = candidate;
       }
     }
   } else if (columnIndex == NEW_BRANCH_COLUMN) {
     d.newBranchName = t;
   }
   d.updateStatus();
   fireTableRowsUpdated(rowIndex, rowIndex);
 }
 /**
  * Prepare branches for the case of remote checkout
  *
  * @param remoteBranch the remote branch to checkout
  * @param roots the collection of vcs roots
  * @return the list of descriptors for the remote
  * @throws VcsException if failed
  */
 private List<BranchDescriptor> prepareBranchesForRemote(
     String remoteBranch, List<VirtualFile> roots) throws VcsException {
   assert roots.size() > 0;
   List<BranchDescriptor> rc = new ArrayList<BranchDescriptor>();
   HashSet<String> allBranches = new HashSet<String>();
   allBranches.addAll(myConfig.getConfigurationNames());
   final String qualifiedBranch = "remotes/" + remoteBranch;
   String firstRemote = remoteBranch.endsWith("/HEAD") ? null : qualifiedBranch;
   for (VirtualFile root : roots) {
     BranchDescriptor d = new BranchDescriptor();
     d.root = root;
     d.currentReference = myConfig.describeRoot(root);
     if (firstRemote == null) {
       firstRemote = resolveHead(qualifiedBranch, d.root.getPath());
     }
     d.referenceToCheckout = qualifiedBranch;
     Branch.listAsStrings(myProject, root, false, true, d.existingBranches, null);
     Branch.listAsStrings(myProject, root, true, true, d.referencesToSelect, null);
     allBranches.addAll(d.existingBranches);
     rc.add(d);
   }
   String candidate;
   if (firstRemote == null) {
     candidate = "untitled";
   } else {
     int p = firstRemote.indexOf('/', REMOTES_PREFIX.length() + 1);
     assert p > 0 && p < firstRemote.length() - 1
         : "Unexpected format for remote branch: " + firstRemote;
     candidate = firstRemote.substring(p + 1);
   }
   String actual = null;
   if (!allBranches.contains(candidate)) {
     actual = candidate;
   } else {
     for (int i = 2; i < Integer.MAX_VALUE; i++) {
       String t = candidate + i;
       if (!allBranches.contains(t)) {
         actual = t;
         break;
       }
     }
     assert actual != null : "Unexpected number of branches: " + remoteBranch;
   }
   for (BranchDescriptor d : rc) {
     d.newBranchName = actual;
     d.updateStatus();
   }
   return rc;
 }