use of org.eclipse.che.api.git.exception.GitException in project che by eclipse.
the class JGitConnection method pull.
@Override
public PullResponse pull(PullParams params) throws GitException, UnauthorizedException {
String remoteName = params.getRemote();
String remoteUri;
try {
if (repository.getRepositoryState().equals(RepositoryState.MERGING)) {
throw new GitException(ERROR_PULL_MERGING);
}
String fullBranch = repository.getFullBranch();
if (!fullBranch.startsWith(Constants.R_HEADS)) {
throw new DetachedHeadException(ERROR_PULL_HEAD_DETACHED);
}
String branch = fullBranch.substring(Constants.R_HEADS.length());
StoredConfig config = repository.getConfig();
if (remoteName == null) {
remoteName = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants.CONFIG_KEY_REMOTE);
if (remoteName == null) {
remoteName = Constants.DEFAULT_REMOTE_NAME;
}
}
remoteUri = config.getString(ConfigConstants.CONFIG_REMOTE_SECTION, remoteName, ConfigConstants.CONFIG_KEY_URL);
String remoteBranch;
RefSpec fetchRefSpecs = null;
String refSpec = params.getRefSpec();
if (refSpec != null) {
fetchRefSpecs = //
(refSpec.indexOf(':') < 0) ? //
new RefSpec(Constants.R_HEADS + refSpec + ":" + fullBranch) : new RefSpec(refSpec);
remoteBranch = fetchRefSpecs.getSource();
} else {
remoteBranch = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants.CONFIG_KEY_MERGE);
}
if (remoteBranch == null) {
remoteBranch = fullBranch;
}
FetchCommand fetchCommand = getGit().fetch();
fetchCommand.setRemote(remoteName);
if (fetchRefSpecs != null) {
fetchCommand.setRefSpecs(fetchRefSpecs);
}
int timeout = params.getTimeout();
if (timeout > 0) {
fetchCommand.setTimeout(timeout);
}
FetchResult fetchResult = (FetchResult) executeRemoteCommand(remoteUri, fetchCommand, params.getUsername(), params.getPassword());
Ref remoteBranchRef = fetchResult.getAdvertisedRef(remoteBranch);
if (remoteBranchRef == null) {
remoteBranchRef = fetchResult.getAdvertisedRef(Constants.R_HEADS + remoteBranch);
}
if (remoteBranchRef == null) {
throw new GitException(format(ERROR_PULL_REF_MISSING, remoteBranch));
}
org.eclipse.jgit.api.MergeResult mergeResult = getGit().merge().include(remoteBranchRef).call();
if (mergeResult.getMergeStatus().equals(org.eclipse.jgit.api.MergeResult.MergeStatus.ALREADY_UP_TO_DATE)) {
return newDto(PullResponse.class).withCommandOutput("Already up-to-date");
}
if (mergeResult.getConflicts() != null) {
StringBuilder message = new StringBuilder(ERROR_PULL_MERGE_CONFLICT_IN_FILES);
message.append(lineSeparator());
Map<String, int[][]> allConflicts = mergeResult.getConflicts();
for (String path : allConflicts.keySet()) {
message.append(path).append(lineSeparator());
}
message.append(ERROR_PULL_AUTO_MERGE_FAILED);
throw new GitException(message.toString());
}
} catch (CheckoutConflictException exception) {
StringBuilder message = new StringBuilder(ERROR_CHECKOUT_CONFLICT);
message.append(lineSeparator());
for (String path : exception.getConflictingPaths()) {
message.append(path).append(lineSeparator());
}
message.append(ERROR_PULL_COMMIT_BEFORE_MERGE);
throw new GitException(message.toString(), exception);
} catch (IOException | GitAPIException exception) {
String errorMessage;
if (exception.getMessage().equals("Invalid remote: " + remoteName)) {
errorMessage = ERROR_NO_REMOTE_REPOSITORY;
} else {
errorMessage = generateExceptionMessage(exception);
}
throw new GitException(errorMessage, exception);
}
return newDto(PullResponse.class).withCommandOutput("Successfully pulled from " + remoteUri);
}
use of org.eclipse.che.api.git.exception.GitException in project che by eclipse.
the class JGitConnection method tagDelete.
@Override
public void tagDelete(String name) throws GitException {
try {
Ref tagRef = repository.findRef(name);
if (tagRef == null) {
throw new GitException("Tag " + name + " not found. ");
}
RefUpdate updateRef = repository.updateRef(tagRef.getName());
updateRef.setRefLogMessage("tag deleted", false);
updateRef.setForceUpdate(true);
Result deleteResult = updateRef.delete();
if (deleteResult != Result.FORCED && deleteResult != Result.FAST_FORWARD) {
throw new GitException(format(ERROR_TAG_DELETE, name, deleteResult));
}
} catch (IOException exception) {
throw new GitException(exception.getMessage(), exception);
}
}
use of org.eclipse.che.api.git.exception.GitException in project che by eclipse.
the class JGitConnection method log.
/** @see org.eclipse.che.api.git.GitConnection#log(LogParams) */
@Override
public LogPage log(LogParams params) throws GitException {
LogCommand logCommand = getGit().log();
try {
setRevisionRange(logCommand, params);
logCommand.setSkip(params.getSkip());
logCommand.setMaxCount(params.getMaxCount());
List<String> fileFilter = params.getFileFilter();
if (fileFilter != null) {
fileFilter.forEach(logCommand::addPath);
}
String filePath = params.getFilePath();
if (!isNullOrEmpty(filePath)) {
logCommand.addPath(filePath);
}
Iterator<RevCommit> revIterator = logCommand.call().iterator();
List<Revision> commits = new ArrayList<>();
while (revIterator.hasNext()) {
RevCommit commit = revIterator.next();
Revision revision = getRevision(commit, filePath);
commits.add(revision);
}
return new LogPage(commits);
} catch (GitAPIException | IOException exception) {
String errorMessage = exception.getMessage();
if (ERROR_LOG_NO_HEAD_EXISTS.equals(errorMessage)) {
throw new GitException(errorMessage, ErrorCodes.INIT_COMMIT_WAS_NOT_PERFORMED);
} else {
LOG.error("Failed to retrieve log. ", exception);
throw new GitException(exception);
}
}
}
use of org.eclipse.che.api.git.exception.GitException in project che by eclipse.
the class JGitConnection method add.
@Override
public void add(AddParams params) throws GitException {
AddCommand addCommand = getGit().add().setUpdate(params.isUpdate());
List<String> filePatterns = params.getFilePattern();
if (filePatterns.isEmpty()) {
filePatterns = AddRequest.DEFAULT_PATTERN;
}
filePatterns.forEach(addCommand::addFilepattern);
try {
addCommand.call();
addDeletedFilesToIndex(filePatterns);
} catch (GitAPIException exception) {
throw new GitException(exception.getMessage(), exception);
}
}
use of org.eclipse.che.api.git.exception.GitException in project che by eclipse.
the class JGitConnection method remoteAdd.
@Override
public void remoteAdd(RemoteAddParams params) throws GitException {
String remoteName = params.getName();
if (isNullOrEmpty(remoteName)) {
throw new GitException(ERROR_ADD_REMOTE_NAME_MISSING);
}
StoredConfig config = repository.getConfig();
Set<String> remoteNames = config.getSubsections("remote");
if (remoteNames.contains(remoteName)) {
throw new GitException(format(ERROR_ADD_REMOTE_NAME_ALREADY_EXISTS, remoteName));
}
String url = params.getUrl();
if (isNullOrEmpty(url)) {
throw new GitException(ERROR_ADD_REMOTE_URL_MISSING);
}
RemoteConfig remoteConfig;
try {
remoteConfig = new RemoteConfig(config, remoteName);
} catch (URISyntaxException exception) {
// Not happen since it is newly created remote.
throw new GitException(exception.getMessage(), exception);
}
try {
remoteConfig.addURI(new URIish(url));
} catch (URISyntaxException exception) {
throw new GitException("Remote url " + url + " is invalid. ");
}
List<String> branches = params.getBranches();
if (branches.isEmpty()) {
remoteConfig.addFetchRefSpec(new RefSpec(Constants.R_HEADS + "*" + ":" + Constants.R_REMOTES + remoteName + "/*").setForceUpdate(true));
} else {
for (String branch : branches) {
remoteConfig.addFetchRefSpec(new RefSpec(Constants.R_HEADS + branch + ":" + Constants.R_REMOTES + remoteName + "/" + branch).setForceUpdate(true));
}
}
remoteConfig.update(config);
try {
config.save();
} catch (IOException exception) {
throw new GitException(exception.getMessage(), exception);
}
}
Aggregations