public BranchImpl(org.eclipse.che.api.git.shared.Branch origin) { this.name = origin.getName(); this.displayName = origin.getDisplayName(); this.remote = origin.isRemote(); this.active = origin.isActive(); }
Writer writer = new OutputStreamWriter(entityStream); for (Branch branch : branches) { if (branch.isActive()) { writer.write('*'); } else { if (branch.getName().startsWith("refs/remotes")) { writer.write(branch.getName().substring(5)); // trim leading 'refs/' } else { writer.write(branch.getDisplayName());
@Override public void onSuccess(List<Branch> result) { List<String> localBranches = branchSearcher.getLocalBranchesToDisplay(result); view.setLocalBranches(localBranches); for (Branch branch : result) { if (branch.isActive()) { view.selectLocalBranch(branch.getDisplayName()); break; } } // getting remote branch only after selecting current local branch updateRemoteBranches(); }
/** * Checks that branch linked to remote. * * @param branch branch for checking * @return {@code true} if branch linked to remote and else returns {@code false} */ public boolean isLinkedTo(Branch branch) { return branch.getName().startsWith(refsForRemoteRepository); }
/** @return name of branch, e.g. 'origin/master' -> 'master' */ private String getSelectedBranchName() { String selectedBranchName = selectedBranch.getDisplayName(); String[] tokens = selectedBranchName.split("/"); return tokens.length > 0 ? tokens[tokens.length - 1] : selectedBranchName; }
dtoFactory .createDto(Branch.class) .withActive(false) .withRemote(true) .withDisplayName(displayName) .withName("refs/remotes/" + displayName);
@Override public void onCheckoutClicked() { final CheckoutRequest checkoutRequest = dtoFactory.createDto(CheckoutRequest.class); if (selectedBranch.isRemote()) { checkoutRequest.setTrackBranch(selectedBranch.getDisplayName()); } else { checkoutRequest.setName(selectedBranch.getDisplayName()); } service .checkout(project.getLocation(), checkoutRequest) .then( ignored -> { view.closeDialogIfShowing(); }) .catchError( error -> { handleError(error.getCause(), BRANCH_CHECKOUT_COMMAND_NAME); }); }
@Override public void onBranchSelected(@NotNull Branch branch) { selectedBranch = branch; boolean isActive = selectedBranch.isActive(); view.setEnableCheckoutButton(!isActive); view.setEnableDeleteButton(!isActive); view.setEnableRenameButton(true); }
@Override public void onRenameClicked() { if (selectedBranch.isRemote()) { dialogFactory .createConfirmDialog( constant.branchConfirmRenameTitle(), constant.branchConfirmRenameMessage(), this::renameBranch, null) .show(); } else { renameBranch(); } }
List<Reference> references = new ArrayList<>(); for (Branch branch : branches) { if (!branch.isActive()) { Reference reference = new Reference(branch.getName(), branch.getDisplayName(), LOCAL_BRANCH); references.add(reference); List<Reference> references = new ArrayList<>(); for (Branch branch : branches) { if (!branch.isActive()) { Reference reference = new Reference(branch.getName(), branch.getDisplayName(), REMOTE_BRANCH); references.add(reference);
/** * Update the list of branches. * * @param remoteMode is a remote mode */ private void updateBranches(@NotNull final BranchListMode remoteMode) { service .branchList(project.getLocation(), remoteMode) .then( branches -> { if (LIST_REMOTE.equals(remoteMode)) { view.setRemoteBranches( branchSearcher.getRemoteBranchesToDisplay(view.getRepositoryName(), branches)); updateBranches(LIST_LOCAL); } else { view.setLocalBranches(branchSearcher.getLocalBranchesToDisplay(branches)); for (Branch branch : branches) { if (branch.isActive()) { view.selectRemoteBranch(branch.getDisplayName()); break; } } } }) .catchError( error -> { handleError(error.getCause(), BRANCH_LIST_COMMAND_NAME); view.setEnablePullButton(false); }); }
/** * Determines simple name of branch. Example difference between name and simple name: name of * branch equals "refs/remotes/origin/master" and simple name of branch equals "master" * * @return simple name of branch */ public String getBranchNameWithoutRefs(Branch branch) { return branch .getName() .replaceFirst(refsForRemoteRepository, "") .replaceFirst("refs/heads/", ""); } }
/** * Get simple names of local branches. * * @param localBranches local branches */ @NotNull public List<String> getLocalBranchesToDisplay(@NotNull List<Branch> localBranches) { List<String> branches = new ArrayList<>(); if (localBranches.isEmpty()) { branches.add("master"); return branches; } for (Branch branch : localBranches) { branches.add(branch.getDisplayName()); } return branches; } }
view.setLocalBranches(branchSearcher.getLocalBranchesToDisplay(branches)); for (Branch branch : branches) { if (branch.isActive()) { view.selectRemoteBranch(branch.getDisplayName()); break;
/** * Returns simple name of the given branch. E.g.: for a branch with name * "refs/remotes/origin/master" returns "master". Returns full name of the given branch if there's * no correspondent remote branch. */ private String getRemoteBranchNameWithoutRefs(GitConnection git, Branch branch) throws GitException { String branchName = branch.getName().replaceFirst("refs/remotes/", ""); List<Remote> remotes = git.remoteList(null, false); for (Remote remote : remotes) { if (branchName.startsWith(remote.getName())) { return branchName.replaceFirst(remote.getName() + "/", ""); } } return branchName; } }
@Override public void setRemoteBranchesList(List<Branch> branches) { remoteBranches.clear(); branches.forEach(branch -> remoteBranches.addItem(branch.getDisplayName())); }
private String getRemoteBranch(GitConnection git, String branchName) throws GitException { final List<Branch> remotes = git.branchList(LIST_REMOTE); for (Branch branch : remotes) { if (branchName.equals(getRemoteBranchNameWithoutRefs(git, branch))) { return branch.getName(); } } throw new GitException("Failed to get remote branch name", FAILED_CHECKOUT); }
private void renameBranch(String newName) { service .branchRename(project.getLocation(), selectedBranch.getDisplayName(), newName) .then( ignored -> { getBranches(); view.setFocus(); }) .catchError( error -> { handleError(error.getCause(), BRANCH_RENAME_COMMAND_NAME); getBranches(); // rename of remote branch occurs in three stages, so needs update list // of branches on view }); }
private void checkoutBranch( GitConnection git, String projectName, String branchName, String startPoint) throws GitException { final CheckoutParams params = CheckoutParams.create(branchName); final boolean branchExist = git.branchList(LIST_ALL) .stream() .anyMatch(branch -> branch.getDisplayName().equals("origin/" + branchName)); final GitCheckoutEvent checkout = newDto(GitCheckoutEvent.class) .withWorkspaceId(WorkspaceIdProvider.getWorkspaceId()) .withProjectName(projectName); if (startPoint != null) { if (branchExist) { git.checkout(params); eventService.publish( checkout.withCheckoutOnly(true).withBranchRef(getRemoteBranch(git, branchName))); } else { checkoutAndRethrow( git, params.withCreateNew(true).withStartPoint(startPoint).withNoTrack(true), FAILED_CHECKOUT_WITH_START_POINT); eventService.publish(checkout.withCheckoutOnly(false)); } } else { checkoutAndRethrow(git, params, FAILED_CHECKOUT); eventService.publish( checkout.withCheckoutOnly(true).withBranchRef(getRemoteBranch(git, branchName))); } }