use of org.eclipse.jgit.lib.StoredConfig in project che by eclipse.
the class JGitConnection method remoteList.
@Override
public List<Remote> remoteList(String remoteName, boolean verbose) throws GitException {
StoredConfig config = repository.getConfig();
Set<String> remoteNames = new HashSet<>(config.getSubsections(ConfigConstants.CONFIG_KEY_REMOTE));
if (remoteName != null && remoteNames.contains(remoteName)) {
remoteNames.clear();
remoteNames.add(remoteName);
}
List<Remote> result = new ArrayList<>(remoteNames.size());
for (String remote : remoteNames) {
try {
List<URIish> uris = new RemoteConfig(config, remote).getURIs();
result.add(newDto(Remote.class).withName(remote).withUrl(uris.isEmpty() ? null : uris.get(0).toString()));
} catch (URISyntaxException exception) {
throw new GitException(exception.getMessage(), exception);
}
}
return result;
}
use of org.eclipse.jgit.lib.StoredConfig in project che by eclipse.
the class JGitConfigImpl method getList.
@Override
public List<String> getList() throws GitException {
List<String> results = new ArrayList<>();
// Iterate all sections and subsections, printing all values
StoredConfig config = repository.getConfig();
for (String section : config.getSections()) {
for (String subsection : config.getSubsections(section)) {
Set<String> names = config.getNames(section, subsection);
addConfigValues(section, subsection, names, results);
}
Set<String> names = config.getNames(section);
addConfigValues(section, null, names, results);
}
return results;
}
use of org.eclipse.jgit.lib.StoredConfig 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.jgit.lib.StoredConfig 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.jgit.lib.StoredConfig 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