Example usage for org.eclipse.jgit.lib Repository getConfig

List of usage examples for org.eclipse.jgit.lib Repository getConfig

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository getConfig.

Prototype

@NonNull
public abstract StoredConfig getConfig();

Source Link

Document

Get the configuration of this repository.

Usage

From source file:org.eclipse.egit.ui.internal.actions.CommitActionHandler.java

License:Open Source License

public Object execute(final ExecutionEvent event) throws ExecutionException {
    // let's see if there is any dirty editor around and
    // ask the user if they want to save or abort
    if (!PlatformUI.getWorkbench().saveAllEditors(true)) {
        return null;
    }/*w  w  w . j a  v a 2 s  .c  om*/

    resetState();
    final IProject[] projects = getProjectsInRepositoryOfSelectedResources(event);
    try {
        PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {

            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    buildIndexHeadDiffList(projects, monitor);
                } catch (IOException e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        Activator.handleError(UIText.CommitAction_errorComputingDiffs, e.getCause(), true);
        return null;
    } catch (InterruptedException e) {
        return null;
    }

    Repository[] repos = getRepositoriesFor(getProjectsForSelectedResources(event));
    Repository repository = null;
    Repository mergeRepository = null;
    amendAllowed = repos.length == 1;
    boolean isMergedResolved = false;
    for (Repository repo : repos) {
        repository = repo;
        RepositoryState state = repo.getRepositoryState();
        if (!state.canCommit()) {
            MessageDialog.openError(getShell(event), UIText.CommitAction_cannotCommit,
                    NLS.bind(UIText.CommitAction_repositoryState, state.getDescription()));
            return null;
        } else if (state.equals(RepositoryState.MERGING_RESOLVED)) {
            isMergedResolved = true;
            mergeRepository = repo;
        }
    }

    loadPreviousCommit(event);
    if (files.isEmpty()) {
        if (amendAllowed && previousCommit != null) {
            boolean result = MessageDialog.openQuestion(getShell(event), UIText.CommitAction_noFilesToCommit,
                    UIText.CommitAction_amendCommit);
            if (!result)
                return null;
            amending = true;
        } else {
            MessageDialog.openWarning(getShell(event), UIText.CommitAction_noFilesToCommit,
                    UIText.CommitAction_amendNotPossible);
            return null;
        }
    }

    String author = null;
    String committer = null;
    if (repository != null) {
        final UserConfig config = repository.getConfig().get(UserConfig.KEY);
        author = config.getAuthorName();
        final String authorEmail = config.getAuthorEmail();
        author = author + " <" + authorEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$

        committer = config.getCommitterName();
        final String committerEmail = config.getCommitterEmail();
        committer = committer + " <" + committerEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$
    }

    CommitDialog commitDialog = new CommitDialog(getShell(event));
    commitDialog.setAmending(amending);
    commitDialog.setAmendAllowed(amendAllowed);
    commitDialog.setFileList(files, indexDiffs);
    commitDialog.setPreselectedFiles(getSelectedFiles(event));
    commitDialog.setAuthor(author);
    commitDialog.setCommitter(committer);
    commitDialog.setAllowToChangeSelection(!isMergedResolved);

    if (previousCommit != null) {
        commitDialog.setPreviousCommitMessage(previousCommit.getFullMessage());
        PersonIdent previousAuthor = previousCommit.getAuthorIdent();
        commitDialog
                .setPreviousAuthor(previousAuthor.getName() + " <" + previousAuthor.getEmailAddress() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    if (isMergedResolved) {
        commitDialog.setCommitMessage(getMergeResolveMessage(mergeRepository, event));
    }

    if (commitDialog.open() != IDialogConstants.OK_ID)
        return null;

    final CommitOperation commitOperation = new CommitOperation(commitDialog.getSelectedFiles(), notIndexed,
            notTracked, commitDialog.getAuthor(), commitDialog.getCommitter(), commitDialog.getCommitMessage());
    if (commitDialog.isAmending()) {
        commitOperation.setAmending(true);
        commitOperation.setPreviousCommit(previousCommit);
        commitOperation.setRepos(repos);
    }
    commitOperation.setComputeChangeId(commitDialog.getCreateChangeId());
    commitOperation.setCommitAll(isMergedResolved);
    if (isMergedResolved)
        commitOperation.setRepos(repos);
    String jobname = UIText.CommitAction_CommittingChanges;
    Job job = new Job(jobname) {
        @Override
        protected IStatus run(IProgressMonitor monitor) {
            try {
                commitOperation.execute(monitor);

                for (IProject proj : getProjectsForSelectedResources(event)) {
                    RepositoryMapping.getMapping(proj).fireRepositoryChanged();
                }
            } catch (CoreException e) {
                return Activator.createErrorStatus(UIText.CommitAction_CommittingFailed, e);
            } catch (ExecutionException e) {
                return Activator.createErrorStatus(UIText.CommitAction_CommittingFailed, e);
            } finally {
                GitLightweightDecorator.refresh();
            }
            return Status.OK_STATUS;
        }

        @Override
        public boolean belongsTo(Object family) {
            if (family.equals(JobFamilies.COMMIT))
                return true;
            return super.belongsTo(family);
        }

    };
    job.setUser(true);
    job.schedule();
    return null;
}

From source file:org.eclipse.egit.ui.internal.actions.SynchronizeWithAction.java

License:Open Source License

private List<RemoteConfig> getRemoteConfigs(Repository repo) throws InvocationTargetException {
    try {//from www .j ava  2 s  .c om
        return RemoteConfig.getAllRemoteConfigs(repo.getConfig());
    } catch (URISyntaxException e) {
        throw new InvocationTargetException(e);
    }
}

From source file:org.eclipse.egit.ui.internal.actions.SynchronizeWithActionHandler.java

License:Open Source License

private List<RemoteConfig> getRemoteConfigs(Repository repo) throws URISyntaxException {
    return RemoteConfig.getAllRemoteConfigs(repo.getConfig());
}

From source file:org.eclipse.egit.ui.internal.actions.SynchronizeWorkspaceActionHandler.java

License:Open Source License

private String getDstRef(Repository repo, boolean launchFetch) {
    if (launchFetch) {
        String realName = getRealBranchName(repo);
        String name = BRANCH_NAME_PATTERN.matcher(realName).replaceAll(""); //$NON-NLS-1$
        StoredConfig config = repo.getConfig();
        String remote = config.getString(CONFIG_BRANCH_SECTION, name, CONFIG_KEY_REMOTE);
        String merge = config.getString(CONFIG_BRANCH_SECTION, name, CONFIG_KEY_MERGE);
        if (remote == null || merge == null)
            return HEAD;

        String mergeBranchName = merge.replace(R_HEADS, ""); //$NON-NLS-1$
        return R_REMOTES + remote + "/" + mergeBranchName; //$NON-NLS-1$
    } else/*  www .j  av a  2  s.c  om*/
        return HEAD;
}

From source file:org.eclipse.egit.ui.internal.dialogs.BasicConfigurationDialog.java

License:Open Source License

private static StoredConfig loadRepoScopedConfig(Repository repo) {
    StoredConfig c = repo.getConfig();
    try {//from  ww w .ja  v a 2 s .  co m
        c.load();
    } catch (IOException e) {
        Activator.handleError(e.getMessage(), e, true);
    } catch (ConfigInvalidException e) {
        Activator.handleError(e.getMessage(), e, true);
    }
    return c;
}

From source file:org.eclipse.egit.ui.internal.dialogs.NewRemoteDialog.java

License:Open Source License

/**
 * @param parentShell//from   w  w  w. j a va  2  s  . co m
 * @param repository
 */
public NewRemoteDialog(Shell parentShell, Repository repository) {
    super(parentShell);
    existingRemotes = repository.getConfig().getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION);
}

From source file:org.eclipse.egit.ui.internal.fetch.FetchWizard.java

License:Open Source License

/**
 * Create wizard for provided local repository.
 *
 * @param localDb//from   w  w  w  .ja  v a 2 s. c o m
 *            local repository to fetch to.
 * @throws URISyntaxException
 *             when configuration of this repository contains illegal URIs.
 */
public FetchWizard(final Repository localDb) throws URISyntaxException {
    this.localDb = localDb;
    final List<RemoteConfig> remotes = RemoteConfig.getAllRemoteConfigs(localDb.getConfig());
    repoPage = new RepositorySelectionPage(true, remotes, null);
    refSpecPage = new RefSpecPage(localDb, false) {
        @Override
        public void setVisible(boolean visible) {
            if (visible) {
                setSelection(repoPage.getSelection());
                setCredentials(repoPage.getCredentials());
            }
            super.setVisible(visible);
        }
    };
    // TODO use/create another cool icon
    setDefaultPageImageDescriptor(UIIcons.WIZBAN_IMPORT_REPO);
    setNeedsProgressMonitor(true);
}

From source file:org.eclipse.egit.ui.internal.fetch.SimpleConfigureFetchDialog.java

License:Open Source License

/**
 * @param shell/*from w  w  w  .j av  a2 s  .  co  m*/
 * @param repository
 * @param remoteName
 *            the remote name to use
 * @return the dialog to open, or null
 */
public static Dialog getDialog(Shell shell, Repository repository, String remoteName) {
    RemoteConfig configToUse;
    try {
        configToUse = new RemoteConfig(repository.getConfig(), remoteName);
    } catch (URISyntaxException e) {
        Activator.handleError(e.getMessage(), e, true);
        return null;
    }
    return new SimpleConfigureFetchDialog(shell, repository, configToUse, false);
}

From source file:org.eclipse.egit.ui.internal.fetch.SimpleConfigureFetchDialog.java

License:Open Source License

/**
 * @param repository/*from   ww  w .  ja  va2  s .c  o  m*/
 * @return the configured remote for the current branch, or the default
 *         remote; <code>null</code> if a local branch is checked out that
 *         points to "." as remote
 */
public static RemoteConfig getConfiguredRemote(Repository repository) {
    String branch;
    try {
        branch = repository.getBranch();
    } catch (IOException e) {
        Activator.handleError(e.getMessage(), e, true);
        return null;
    }
    if (branch == null)
        return null;

    String remoteName;
    if (ObjectId.isId(branch))
        remoteName = Constants.DEFAULT_REMOTE_NAME;
    else
        remoteName = repository.getConfig().getString(ConfigConstants.CONFIG_BRANCH_SECTION, branch,
                ConfigConstants.CONFIG_REMOTE_SECTION);

    // check if we find the configured and default Remotes
    List<RemoteConfig> allRemotes;
    try {
        allRemotes = RemoteConfig.getAllRemoteConfigs(repository.getConfig());
    } catch (URISyntaxException e) {
        allRemotes = new ArrayList<RemoteConfig>();
    }

    RemoteConfig defaultConfig = null;
    RemoteConfig configuredConfig = null;
    for (RemoteConfig config : allRemotes) {
        if (config.getName().equals(Constants.DEFAULT_REMOTE_NAME))
            defaultConfig = config;
        if (remoteName != null && config.getName().equals(remoteName))
            configuredConfig = config;
    }

    RemoteConfig configToUse = configuredConfig != null ? configuredConfig : defaultConfig;
    return configToUse;
}

From source file:org.eclipse.egit.ui.internal.fetch.SimpleConfigureFetchDialog.java

License:Open Source License

/**
 * @param shell/*from  w  w  w .  j  a v a2s . co  m*/
 * @param repository
 * @param config
 * @param showBranchInfo
 *            should be true if this is used for upstream configuration; if
 *            false, branch information will be hidden in the dialog
 */
private SimpleConfigureFetchDialog(Shell shell, Repository repository, RemoteConfig config,
        boolean showBranchInfo) {
    super(shell);
    setHelpAvailable(false);
    setShellStyle(getShellStyle() | SWT.SHELL_TRIM);
    this.repository = repository;
    this.config = config;
    this.showBranchInfo = showBranchInfo;

    // Add default fetch ref spec if this is a new remote config
    if (config.getFetchRefSpecs().isEmpty() && !repository.getConfig()
            .getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION).contains(config.getName())) {
        StringBuilder defaultRefSpec = new StringBuilder();
        defaultRefSpec.append('+');
        defaultRefSpec.append(Constants.R_HEADS);
        defaultRefSpec.append('*').append(':');
        defaultRefSpec.append(Constants.R_REMOTES);
        defaultRefSpec.append(config.getName());
        defaultRefSpec.append(RefSpec.WILDCARD_SUFFIX);
        config.addFetchRefSpec(new RefSpec(defaultRefSpec.toString()));
    }
}