use of org.eclipse.che.api.git.shared.Branch in project che by eclipse.
the class JGitConnection method branchList.
@Override
public List<Branch> branchList(BranchListMode listMode) throws GitException {
ListBranchCommand listBranchCommand = getGit().branchList();
if (LIST_ALL == listMode || listMode == null) {
listBranchCommand.setListMode(ListMode.ALL);
} else if (LIST_REMOTE == listMode) {
listBranchCommand.setListMode(ListMode.REMOTE);
}
List<Ref> refs;
String currentRef;
try {
refs = listBranchCommand.call();
String headBranch = getRepository().getBranch();
Optional<Ref> currentTag = getGit().tagList().call().stream().filter(tag -> tag.getObjectId().getName().equals(headBranch)).findFirst();
if (currentTag.isPresent()) {
currentRef = currentTag.get().getName();
} else {
currentRef = "refs/heads/" + headBranch;
}
} catch (GitAPIException | IOException exception) {
throw new GitException(exception.getMessage(), exception);
}
List<Branch> branches = new ArrayList<>();
for (Ref ref : refs) {
String refName = ref.getName();
boolean isCommitOrTag = Constants.HEAD.equals(refName);
String branchName = isCommitOrTag ? currentRef : refName;
String branchDisplayName;
if (isCommitOrTag) {
branchDisplayName = "(detached from " + Repository.shortenRefName(currentRef) + ")";
} else {
branchDisplayName = Repository.shortenRefName(refName);
}
Branch branch = newDto(Branch.class).withName(branchName).withActive(isCommitOrTag || refName.equals(currentRef)).withDisplayName(branchDisplayName).withRemote(refName.startsWith("refs/remotes"));
branches.add(branch);
}
return branches;
}
use of org.eclipse.che.api.git.shared.Branch in project che by eclipse.
the class JGitConnection method remoteDelete.
@Override
public void remoteDelete(String name) throws GitException {
StoredConfig config = repository.getConfig();
Set<String> remoteNames = config.getSubsections(ConfigConstants.CONFIG_KEY_REMOTE);
if (!remoteNames.contains(name)) {
throw new GitException("error: Could not remove config section 'remote." + name + "'");
}
config.unsetSection(ConfigConstants.CONFIG_REMOTE_SECTION, name);
Set<String> branches = config.getSubsections(ConfigConstants.CONFIG_BRANCH_SECTION);
for (String branch : branches) {
String r = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants.CONFIG_KEY_REMOTE);
if (name.equals(r)) {
config.unset(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants.CONFIG_KEY_REMOTE);
config.unset(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants.CONFIG_KEY_MERGE);
List<Branch> remoteBranches = branchList(LIST_REMOTE);
for (Branch remoteBranch : remoteBranches) {
if (remoteBranch.getDisplayName().startsWith(name)) {
branchDelete(remoteBranch.getName(), true);
}
}
}
}
try {
config.save();
} catch (IOException exception) {
throw new GitException(exception.getMessage(), exception);
}
}
use of org.eclipse.che.api.git.shared.Branch in project che by eclipse.
the class JGitConnection method checkout.
@Override
public void checkout(CheckoutParams params) throws GitException {
CheckoutCommand checkoutCommand = getGit().checkout();
String startPoint = params.getStartPoint();
String name = params.getName();
String trackBranch = params.getTrackBranch();
// checkout files?
List<String> files = params.getFiles();
boolean shouldCheckoutToFile = name != null && new File(getWorkingDir(), name).exists();
if (shouldCheckoutToFile || !files.isEmpty()) {
if (shouldCheckoutToFile) {
checkoutCommand.addPath(params.getName());
} else {
files.forEach(checkoutCommand::addPath);
}
} else {
// checkout branch
if (startPoint != null && trackBranch != null) {
throw new GitException("Start point and track branch can not be used together.");
}
if (params.isCreateNew() && name == null) {
throw new GitException("Branch name must be set when createNew equals to true.");
}
if (startPoint != null) {
checkoutCommand.setStartPoint(startPoint);
}
if (params.isCreateNew()) {
checkoutCommand.setCreateBranch(true);
checkoutCommand.setName(name);
} else if (name != null) {
checkoutCommand.setName(name);
List<String> localBranches = branchList(LIST_LOCAL).stream().map(Branch::getDisplayName).collect(Collectors.toList());
if (!localBranches.contains(name)) {
Optional<Branch> remoteBranch = branchList(LIST_REMOTE).stream().filter(branch -> branch.getName().contains(name)).findFirst();
if (remoteBranch.isPresent()) {
checkoutCommand.setCreateBranch(true);
checkoutCommand.setStartPoint(remoteBranch.get().getName());
}
}
}
if (trackBranch != null) {
if (name == null) {
checkoutCommand.setName(cleanRemoteName(trackBranch));
}
checkoutCommand.setCreateBranch(true);
checkoutCommand.setStartPoint(trackBranch);
}
checkoutCommand.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);
}
try {
checkoutCommand.call();
} catch (CheckoutConflictException exception) {
throw new GitConflictException(exception.getMessage(), exception.getConflictingPaths());
} catch (RefAlreadyExistsException exception) {
throw new GitRefAlreadyExistsException(exception.getMessage());
} catch (RefNotFoundException exception) {
throw new GitRefNotFoundException(exception.getMessage());
} catch (InvalidRefNameException exception) {
throw new GitInvalidRefNameException(exception.getMessage());
} catch (GitAPIException exception) {
if (exception.getMessage().endsWith("already exists")) {
throw new GitException(format(ERROR_CHECKOUT_BRANCH_NAME_EXISTS, name != null ? name : cleanRemoteName(trackBranch)));
}
throw new GitException(exception.getMessage(), exception);
}
}
use of org.eclipse.che.api.git.shared.Branch in project che by eclipse.
the class GitServiceClientImpl method branchCreate.
@Override
public Promise<Branch> branchCreate(DevMachine devMachine, Path project, String name, String startPoint) {
BranchCreateRequest branchCreateRequest = dtoFactory.createDto(BranchCreateRequest.class).withName(name).withStartPoint(startPoint);
String url = appContext.getDevMachine().getWsAgentBaseUrl() + BRANCH + "?projectPath=" + project.toString();
return asyncRequestFactory.createPostRequest(url, branchCreateRequest).loader(loader).header(ACCEPT, APPLICATION_JSON).send(dtoUnmarshallerFactory.newUnmarshaller(Branch.class));
}
use of org.eclipse.che.api.git.shared.Branch in project che by eclipse.
the class PushToRemotePresenter method updateRemoteBranches.
/**
* Update list of remote branches on view.
*/
void updateRemoteBranches() {
getBranchesForCurrentProject(LIST_REMOTE, new AsyncCallback<List<Branch>>() {
@Override
public void onSuccess(final List<Branch> result) {
// Need to add the upstream of local branch in the list of remote branches
// to be able to push changes to the remote upstream branch
getUpstreamBranch(new AsyncCallback<Branch>() {
@Override
public void onSuccess(Branch upstream) {
BranchFilterByRemote remoteRefsHandler = new BranchFilterByRemote(view.getRepository());
final List<String> remoteBranches = branchSearcher.getRemoteBranchesToDisplay(remoteRefsHandler, result);
String selectedRemoteBranch = null;
if (upstream != null && upstream.isRemote() && remoteRefsHandler.isLinkedTo(upstream)) {
String simpleUpstreamName = remoteRefsHandler.getBranchNameWithoutRefs(upstream);
if (!remoteBranches.contains(simpleUpstreamName)) {
remoteBranches.add(simpleUpstreamName);
}
selectedRemoteBranch = simpleUpstreamName;
}
// Need to add the current local branch in the list of remote branches
// to be able to push changes to the remote branch with same name
final String currentBranch = view.getLocalBranch();
if (!remoteBranches.contains(currentBranch)) {
remoteBranches.add(currentBranch);
}
if (selectedRemoteBranch == null) {
selectedRemoteBranch = currentBranch;
}
view.setRemoteBranches(remoteBranches);
view.selectRemoteBranch(selectedRemoteBranch);
}
@Override
public void onFailure(Throwable caught) {
GitOutputConsole console = gitOutputConsoleFactory.create(CONFIG_COMMAND_NAME);
console.printError(constant.failedGettingConfig());
processesPanelPresenter.addCommandOutput(appContext.getDevMachine().getId(), console);
notificationManager.notify(constant.failedGettingConfig(), FAIL, FLOAT_MODE);
}
});
}
@Override
public void onFailure(Throwable exception) {
String errorMessage = exception.getMessage() != null ? exception.getMessage() : constant.remoteBranchesListFailed();
GitOutputConsole console = gitOutputConsoleFactory.create(BRANCH_LIST_COMMAND_NAME);
console.printError(errorMessage);
processesPanelPresenter.addCommandOutput(appContext.getDevMachine().getId(), console);
notificationManager.notify(constant.remoteBranchesListFailed(), FAIL, FLOAT_MODE);
view.setEnablePushButton(false);
}
});
}
Aggregations