protected void execute()

in src/org/jetbrains/tfsIntegration/actions/BranchAction.java [50:147]


  protected void execute(final @NotNull Project project,
                         final @NotNull WorkspaceInfo workspace,
                         final @NotNull FilePath sourceLocalPath,
                         final @NotNull ExtendedItem sourceExtendedItem) {
    try {
      final String sourceServerPath = sourceExtendedItem.getSitem();
      CreateBranchDialog d = new CreateBranchDialog(project, workspace, sourceServerPath, sourceExtendedItem.getType() == ItemType.Folder);
      if (!d.showAndGet()) {
        return;
      }

      VersionSpecBase version = d.getVersionSpec();
      if (version == null) {
        Messages.showErrorDialog(project, "Incorrect version specified", "Create Branch");
        return;
      }

      final String targetServerPath = d.getTargetPath();
      if (d.isCreateWorkingCopies()) {
        FilePath targetLocalPath = workspace.findLocalPathByServerPath(targetServerPath, true, project);
        if (targetLocalPath == null) {
          FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
          descriptor.setTitle("Select Local Folder");
          descriptor.setShowFileSystemRoots(true);
          final String message = MessageFormat
            .format("Branch target folder ''{0}'' is not mapped. Select a local folder to create a mapping in workspace ''{1}''",
                    targetServerPath, workspace.getName());
          descriptor.setDescription(message);

          VirtualFile selectedFile = FileChooser.chooseFile(descriptor, project, null);
          if (selectedFile == null) {
            return;
          }

          workspace.addWorkingFolderInfo(
            new WorkingFolderInfo(WorkingFolderInfo.Status.Active, TfsFileUtil.getFilePath(selectedFile), targetServerPath));
          workspace.saveToServer(project, workspace);
        }
      }

      final ResultWithFailures<GetOperation> createBranchResult = workspace.getServer().getVCS()
        .createBranch(workspace.getName(), workspace.getOwnerName(), sourceServerPath, version, targetServerPath, project,
                      TFSBundle.message("creating.branch"));
      if (!createBranchResult.getFailures().isEmpty()) {
        StringBuilder s = new StringBuilder("Failed to create branch:\n");
        for (Failure failure : createBranchResult.getFailures()) {
          s.append(failure.getMessage()).append("\n");
        }
        Messages.showErrorDialog(project, s.toString(), "Create Branch");
        return;
      }

      if (d.isCreateWorkingCopies()) {
        final Ref<Collection<VcsException>> downloadErrors = new Ref<>(Collections.emptyList());
        ProgressManager.getInstance().runProcessWithProgressSynchronously(
          () -> downloadErrors.set(ApplyGetOperations.execute(project, workspace, createBranchResult.getResult(),
                                                            new ApplyProgress.ProgressIndicatorWrapper(
                                                          ProgressManager.getInstance().getProgressIndicator()), null,
                                                            ApplyGetOperations.DownloadMode.ALLOW)), "Creating target working copies", false, project);

        if (!downloadErrors.get().isEmpty()) {
          AbstractVcsHelper.getInstance(project).showErrors(new ArrayList<>(downloadErrors.get()), "Create Branch");
        }
      }

      // TODO checkin requires proper configuration
      final Collection<PendingChange> pendingChanges = workspace.getServer().getVCS()
        .queryPendingSetsByServerItems(workspace.getName(), workspace.getOwnerName(), Collections.singletonList(targetServerPath),
                                       RecursionType.Full, project, TFSBundle.message("loading.changes"));
      Collection<String> checkin = new ArrayList<>();
      for (PendingChange change : pendingChanges) {
        if (new ChangeTypeMask(change.getChg()).contains(ChangeType_type0.Branch)) {
          checkin.add(change.getItem());
        }
      }
      final String comment = MessageFormat.format("Branched from {0}", sourceServerPath);
      final ResultWithFailures<CheckinResult> checkinResult = workspace.getServer().getVCS()
        .checkIn(workspace.getName(), workspace.getOwnerName(), checkin, comment, Collections.emptyMap(),
                 Collections.emptyList(), null, project, TFSBundle.message("checking.in"));

      if (!checkinResult.getFailures().isEmpty()) {
        final List<VcsException> checkinErrors = TfsUtil.getVcsExceptions(checkinResult.getFailures());
        AbstractVcsHelper.getInstance(project).showErrors(checkinErrors, "Create Branch");
      }

      final FilePath targetLocalPath = workspace.findLocalPathByServerPath(targetServerPath, true, project);
      if (targetLocalPath != null) {
        TfsFileUtil.markDirtyRecursively(project, targetLocalPath);
      }

      String message = MessageFormat.format("''{0}'' branched successfully to ''{1}''.", sourceServerPath, targetServerPath);
      Messages.showInfoMessage(project, message, "Create Branch");
    }
    catch (TfsException ex) {
      String message = "Failed to create branch: " + ex.getMessage();
      Messages.showErrorDialog(project, message, "Create Branch");
    }
  }