public boolean doBranchCreate()

in plugin/src/com/microsoft/alm/plugin/idea/git/ui/branch/CreateBranchModel.java [192:287]


    public boolean doBranchCreate(@NotNull final ServerContext context, final ProgressIndicator progressIndicator) {
        logger.info("CreateBranchModel.doBranchCreate");
        // call server to create branch
        boolean hasNotifiedUser = false; //keep track of notifications because of recursive call
        String errorMessage = StringUtils.EMPTY;
        try {
            // ref update will create a new ref when no existing ref is found (we check for existing)
            final GitRefUpdate gitRefUpdate = new GitRefUpdate();
            gitRefUpdate.setName(REFS_PREFIX + getBranchName().replaceFirst(ORIGIN_PREFIX, StringUtils.EMPTY));
            gitRefUpdate.setOldObjectId(BASE_HASH); // since branch is new the last commit hash is all 0's
            gitRefUpdate.setNewObjectId(GeneralGitHelper.getLastCommitHash(project, gitRepository, selectedRemoteBranch)); // TODO: get the latest commit from server b/c the latest local commit could be incorrect
            gitRefUpdate.setRepositoryId(context.getGitRepository().getId());

            logger.info("CreateBranchModel.createBranch sending create ref call to server");
            final List<GitRefUpdateResult> results = context.getGitHttpClient().updateRefs(Arrays.asList(gitRefUpdate),
                    context.getGitRepository().getId(), context.getTeamProjectReference().getId().toString());

            // check returned results
            if (results.size() < 1 || !results.get(0).getSuccess()) {
                errorMessage = results.size() > 0 ? results.get(0).getCustomMessage() : TfPluginBundle.KEY_CREATE_BRANCH_ERRORS_UNEXPECTED_SERVER_ERROR;
            } else {
                // Get the repository object for this project
                final GitRepository gitRepository = TfGitHelper.getTfGitRepository(project);
                if (gitRepository != null) {
                    // Create a progressIndicator if one was not passed in
                    // Fetch server changes so we can checkout here if we want to
                    logger.info("Fetching latest from server so that the new branch is available to checkout");
                    final GitFetcher fetcher = new GitFetcher(project, getProgressIndicator(progressIndicator), true);
                    final GitFetchResult fetchResult = fetcher.fetch(gitRepository);
                    if (fetchResult.isSuccess() && this.checkoutBranch) {
                        logger.info("Checking out new branch: " + branchName);
                        // Creating a branch using the brancher has to start on the UI thread (it will background the work itself)
                        IdeaHelper.runOnUIThread(new Runnable() {
                            @Override
                            public void run() {
                                logger.info("Finishing: Checking out new branch: " + branchName);
                                final String remoteBranchName = TfGitHelper.getRemoteBranchName(selectedRemoteBranch.getRemote(), branchName);
                                final GitBrancher brancher = ServiceManager.getService(project, GitBrancher.class);
                                // Checkout a new branch from the remote branch that was created on the server and fetched above
                                brancher.checkoutNewBranchStartingFrom(branchName,remoteBranchName,
                                        Collections.singletonList(gitRepository), null);
                            }
                        });
                    }
                } else {
                    logger.warn("Could not fetch branch from server. Unable to retrieve the git repo object from the project.");
                }

            }
        } catch (Throwable t) {
            if (AuthHelper.isNotAuthorizedError(t)) {
                final ServerContext newContext = ServerContextManager.getInstance().updateAuthenticationInfo(context.getGitRepository().getRemoteUrl());
                if (newContext != null) {
                    //retry creating the branch with new context and authentication info
                    hasNotifiedUser = doBranchCreate(newContext, progressIndicator);
                } else {
                    //user cancelled login, don't retry
                    errorMessage = t.getMessage();
                }
            } else {
                errorMessage = t.getMessage();
            }
        }

        if (!hasNotifiedUser) {
            // alert user to success or error in creating the branch
            if (StringUtils.isEmpty(errorMessage)) {
                logger.info("Create branch succeeded");
                setBranchWasCreated(true);

                // create user notification
                final String branchLink = String.format(UrlHelper.SHORT_HTTP_LINK_FORMATTER, UrlHelper.getBranchURI(context.getUri(), getBranchName()), getBranchName());
                if (!ApplicationManager.getApplication().isUnitTestMode()) {
                    VcsNotifier.getInstance(project).notifyImportantInfo(TfPluginBundle.message(TfPluginBundle.KEY_CREATE_BRANCH_DIALOG_SUCCESSFUL_TITLE),
                            TfPluginBundle.message(TfPluginBundle.KEY_CREATE_BRANCH_DIALOG_SUCCESSFUL_DESCRIPTION, branchLink), new NotificationListener() {
                                @Override
                                public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent hyperlinkEvent) {
                                    BrowserUtil.browse(hyperlinkEvent.getURL());
                                }
                            });
                }
            } else {
                logger.warn("Create branch failed: {}", errorMessage);
                if (!ApplicationManager.getApplication().isUnitTestMode()) {
                    VcsNotifier.getInstance(project).notifyError(
                            TfPluginBundle.message(TfPluginBundle.KEY_CREATE_BRANCH_DIALOG_FAILED_TITLE),
                            TfPluginBundle.message(
                                    TfPluginBundle.KEY_CREATE_BRANCH_ERRORS_BRANCH_CREATE_FAILED,
                                    errorMessage));
                }
            }

            hasNotifiedUser = true;
        }
        return hasNotifiedUser;
    }