Example usage for org.eclipse.jgit.lib ConfigConstants CONFIG_KEY_MERGE

List of usage examples for org.eclipse.jgit.lib ConfigConstants CONFIG_KEY_MERGE

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ConfigConstants CONFIG_KEY_MERGE.

Prototype

String CONFIG_KEY_MERGE

To view the source code for org.eclipse.jgit.lib ConfigConstants CONFIG_KEY_MERGE.

Click Source Link

Document

The "merge" key

Usage

From source file:com.rimerosolutions.ant.git.tasks.FetchTask.java

License:Apache License

@Override
public void doExecute() {
    try {/*from w  w w .  j a  v  a 2s.  c o m*/
        StoredConfig config = git.getRepository().getConfig();
        List<RemoteConfig> remoteConfigs = RemoteConfig.getAllRemoteConfigs(config);

        if (remoteConfigs.isEmpty()) {
            URIish uri = new URIish(getUri());

            RemoteConfig remoteConfig = new RemoteConfig(config, Constants.DEFAULT_REMOTE_NAME);
            remoteConfig.addURI(uri);
            remoteConfig.addFetchRefSpec(new RefSpec("+" + Constants.R_HEADS + "*:" + Constants.R_REMOTES
                    + Constants.DEFAULT_REMOTE_NAME + "/*"));

            config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
                    ConfigConstants.CONFIG_KEY_REMOTE, Constants.DEFAULT_REMOTE_NAME);
            config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
                    ConfigConstants.CONFIG_KEY_MERGE, Constants.R_HEADS + Constants.MASTER);

            remoteConfig.update(config);
            config.save();
        }

        List<RefSpec> specs = new ArrayList<RefSpec>(3);

        specs.add(new RefSpec(
                "+" + Constants.R_HEADS + "*:" + Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/*"));
        specs.add(new RefSpec("+" + Constants.R_NOTES + "*:" + Constants.R_NOTES + "*"));
        specs.add(new RefSpec("+" + Constants.R_TAGS + "*:" + Constants.R_TAGS + "*"));

        FetchCommand fetchCommand = git.fetch().setDryRun(dryRun).setThin(thinPack).setRemote(getUri())
                .setRefSpecs(specs).setRemoveDeletedRefs(removeDeletedRefs);

        setupCredentials(fetchCommand);

        if (getProgressMonitor() != null) {
            fetchCommand.setProgressMonitor(getProgressMonitor());
        }

        FetchResult fetchResult = fetchCommand.call();

        GitTaskUtils.validateTrackingRefUpdates(FETCH_FAILED_MESSAGE, fetchResult.getTrackingRefUpdates());

        log(fetchResult.getMessages());

    } catch (URISyntaxException e) {
        throw new GitBuildException("Invalid URI syntax: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new GitBuildException("Could not save or get repository configuration: " + e.getMessage(), e);
    } catch (InvalidRemoteException e) {
        throw new GitBuildException("Invalid remote URI: " + e.getMessage(), e);
    } catch (TransportException e) {
        throw new GitBuildException("Communication error: " + e.getMessage(), e);
    } catch (GitAPIException e) {
        throw new GitBuildException("Unexpected exception: " + e.getMessage(), e);
    }
}

From source file:net.mobid.codetraq.runnables.GitChecker.java

License:Open Source License

private void clone(String path) {
    LogService.writeMessage("GitChecker is trying to do a clone from " + _server.getServerAddress());
    System.out.printf("GitChecker is trying to do a clone from %s%n", _server.getServerAddress());
    try {// ww w .j a  va  2 s.c  o  m
        File gitDir = new File(path);
        CloneCommand cloner = new CloneCommand();
        cloner.setBare(false);
        cloner.setDirectory(gitDir);
        cloner.setProgressMonitor(new TextProgressMonitor());
        cloner.setRemote("origin");
        cloner.setURI(_server.getServerAddress());
        mGit = cloner.call();
        // for some reason, repository cloned with jgit always has HEAD detached.
        // we need to create a "temporary" branch, then create a "master" branch.
        // we then merge the two...
        if (!isMasterBranchDefined(mGit.getRepository())) {
            // save the remote and merge config values
            mGit.getRepository().getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
                    _server.getServerBranch(), ConfigConstants.CONFIG_KEY_REMOTE, "origin");
            mGit.getRepository().getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
                    _server.getServerBranch(), ConfigConstants.CONFIG_KEY_MERGE, _server.getServerBranch());
            mGit.getRepository().getConfig().save();
        }
        if (mGit.getRepository().getFullBranch() == null
                || Utilities.isHexString(mGit.getRepository().getFullBranch())) {
            // HEAD is detached and we need to reattach it
            attachHead(mGit, _server.getServerBranch());
        }
    } catch (Exception ex) {
        LogService.getLogger(GitChecker.class.getName()).log(Level.SEVERE, null, ex);
        LogService.writeLog(Level.SEVERE, ex);
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public PullResponse pull(PullRequest request) throws GitException, UnauthorizedException {
    String remoteName = request.getRemote();
    String remoteUri;//from   www .  ja  va 2s.c o  m
    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 = request.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 = request.getTimeout();
        if (timeout > 0) {
            fetchCommand.setTimeout(timeout);
        }

        FetchResult fetchResult = (FetchResult) executeRemoteCommand(remoteUri, fetchCommand);

        Ref remoteBranchRef = fetchResult.getAdvertisedRef(remoteBranch);
        if (remoteBranchRef == null) {
            remoteBranchRef = fetchResult.getAdvertisedRef(Constants.R_HEADS + remoteBranch);
        }
        if (remoteBranchRef == null) {
            throw new GitException(String.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 = exception.getMessage();
        }
        throw new GitException(errorMessage, exception);
    }
    return newDto(PullResponse.class).withCommandOutput("Successfully pulled from " + remoteUri);
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@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 + "'");
    }/*from  w w w .  j  av a 2s  .c  o  m*/

    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(newDto(BranchListRequest.class).withListMode("r"));
            for (Branch remoteBranch : remoteBranches) {
                if (remoteBranch.getDisplayName().startsWith(name)) {
                    branchDelete(
                            newDto(BranchDeleteRequest.class).withName(remoteBranch.getName()).withForce(true));
                }
            }
        }
    }

    try {
        config.save();
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.egit.core.test.op.MergeOperationTest.java

License:Open Source License

private void setMerge(FastForwardMode ffMode) throws IOException {
    StoredConfig config = testRepository.getRepository().getConfig();
    config.setEnum(ConfigConstants.CONFIG_KEY_MERGE, null, ConfigConstants.CONFIG_KEY_FF,
            FastForwardMode.Merge.valueOf(ffMode));
    config.save();/*from  w ww .  j a va  2 s .  c o m*/
}

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

License:Open Source License

@Override
protected Control createDialogArea(Composite parent) {
    Composite main = new Composite(parent, SWT.NONE);
    GridLayoutFactory.fillDefaults().numColumns(2).applyTo(main);
    GridDataFactory.fillDefaults().grab(true, false).indent(5, 5).applyTo(main);
    Label branchLabel = new Label(main, SWT.NONE);
    branchLabel.setText(UIText.BranchConfigurationDialog_UpstreamBranchLabel);
    branchText = new Combo(main, SWT.BORDER);
    GridDataFactory.fillDefaults().grab(true, false).applyTo(branchText);

    try {/* w w  w  .ja  v a  2  s .  c o  m*/
        for (Ref ref : myRepository.getRefDatabase().getRefs(Constants.R_HEADS).values())
            branchText.add(ref.getName());
        for (Ref ref : myRepository.getRefDatabase().getRefs(Constants.R_REMOTES).values())
            branchText.add(ref.getName());
    } catch (IOException e) {
        Activator.logError(UIText.BranchConfigurationDialog_ExceptionGettingRefs, e);
    }

    Label remoteLabel = new Label(main, SWT.NONE);
    remoteLabel.setText(UIText.BranchConfigurationDialog_RemoteLabel);
    remoteText = new Combo(main, SWT.BORDER);
    GridDataFactory.fillDefaults().grab(true, false).applyTo(remoteText);

    // TODO do we have a constant somewhere?
    remoteText.add("."); //$NON-NLS-1$
    for (String remote : myConfig.getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION))
        remoteText.add(remote);

    rebase = new Button(main, SWT.CHECK);
    GridDataFactory.fillDefaults().span(2, 1).applyTo(rebase);
    rebase.setText(UIText.BranchConfigurationDialog_RebaseLabel);

    String branch = myConfig.getString(ConfigConstants.CONFIG_BRANCH_SECTION, myBranchName,
            ConfigConstants.CONFIG_KEY_MERGE);
    if (branch == null)
        branch = ""; //$NON-NLS-1$
    branchText.setText(branch);

    String remote = myConfig.getString(ConfigConstants.CONFIG_BRANCH_SECTION, myBranchName,
            ConfigConstants.CONFIG_KEY_REMOTE);
    if (remote == null)
        remote = ""; //$NON-NLS-1$
    remoteText.setText(remote);

    boolean rebaseFlag = myConfig.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, myBranchName,
            ConfigConstants.CONFIG_KEY_REBASE, false);
    rebase.setSelection(rebaseFlag);

    applyDialogFont(main);
    // return result;
    return main;
}

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

License:Open Source License

@Override
protected void okPressed() {
    try {//from  w  w  w  . j  a  va2s.c  o m
        String merge = branchText.getText();
        if (merge.length() > 0)
            myConfig.setString(ConfigConstants.CONFIG_BRANCH_SECTION, myBranchName,
                    ConfigConstants.CONFIG_KEY_MERGE, merge);
        else
            myConfig.unset(ConfigConstants.CONFIG_BRANCH_SECTION, myBranchName,
                    ConfigConstants.CONFIG_KEY_MERGE);

        String remote = remoteText.getText();
        if (remote.length() > 0)
            myConfig.setString(ConfigConstants.CONFIG_BRANCH_SECTION, myBranchName,
                    ConfigConstants.CONFIG_KEY_REMOTE, remote);
        else
            myConfig.unset(ConfigConstants.CONFIG_BRANCH_SECTION, myBranchName,
                    ConfigConstants.CONFIG_KEY_REMOTE);

        boolean rebaseFlag = rebase.getSelection();
        if (rebaseFlag)
            myConfig.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, myBranchName,
                    ConfigConstants.CONFIG_KEY_REBASE, true);
        else
            myConfig.unset(ConfigConstants.CONFIG_BRANCH_SECTION, myBranchName,
                    ConfigConstants.CONFIG_KEY_REBASE);
        try {
            myConfig.save();
            super.okPressed();
        } catch (IOException e) {
            Activator.handleError(UIText.BranchConfigurationDialog_SaveBranchConfigFailed, e, true);
        }
    } catch (RuntimeException e) {
        Activator.handleError(e.getMessage(), e, true);
    }
}

From source file:org.eclipse.egit.ui.internal.pull.PullWizard.java

License:Open Source License

private void configureUpstream() throws IOException {
    String fullBranch = this.repository.getFullBranch();
    if (fullBranch == null || !fullBranch.startsWith(Constants.R_HEADS)) {
        // Don't configure upstream for detached HEAD
        return;//from ww  w .ja  v a 2 s .com
    }
    String remoteName = this.page.getRemoteConfig().getName();
    String fullRemoteBranchName = this.page.getFullRemoteReference();

    String localBranchName = this.repository.getBranch();
    StoredConfig config = repository.getConfig();
    config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName, ConfigConstants.CONFIG_KEY_REMOTE,
            remoteName);
    config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName, ConfigConstants.CONFIG_KEY_MERGE,
            fullRemoteBranchName);
    BranchRebaseMode rebaseMode = this.page.getUpstreamConfig();
    if (rebaseMode != null) {
        config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName,
                ConfigConstants.CONFIG_KEY_REBASE, rebaseMode);
    }

    config.save();
}

From source file:org.eclipse.egit.ui.internal.push.PushBranchWizard.java

License:Open Source License

private void configureUpstream() throws IOException {
    if (this.ref == null) {
        // Don't configure upstream for detached HEAD
        return;//w w  w. ja va2  s.  c  o m
    }
    String remoteName = getRemoteName();
    String fullRemoteBranchName = pushBranchPage.getFullRemoteReference();
    String localBranchName = Repository.shortenRefName(this.ref.getName());

    StoredConfig config = repository.getConfig();
    config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName, ConfigConstants.CONFIG_KEY_REMOTE,
            remoteName);
    config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName, ConfigConstants.CONFIG_KEY_MERGE,
            fullRemoteBranchName);
    BranchRebaseMode rebaseMode = pushBranchPage.getUpstreamConfig();
    if (rebaseMode != null) {
        config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName,
                ConfigConstants.CONFIG_KEY_REBASE, rebaseMode);
    }

    config.save();
}

From source file:org.eclipse.egit.ui.internal.push.PushBranchWizardTest.java

License:Open Source License

@Test
public void pushWithRemoteUpstreamConfiguration() throws Exception {
    checkoutNewLocalBranch("foo");
    // Existing configuration
    repository.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION, "foo",
            ConfigConstants.CONFIG_KEY_REMOTE, "fetch");
    repository.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION, "foo",
            ConfigConstants.CONFIG_KEY_MERGE, "refs/heads/foo-on-remote");
    repository.getConfig().setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, "foo",
            ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
    // Make sure the repository does not have autosetuprebase set
    repository.getConfig().setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, null,
            ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE, false);

    PushBranchWizardTester wizard = PushBranchWizardTester.startWizard(selectProject(), "foo");
    wizard.selectRemote("fetch");
    wizard.assertBranchName("foo-on-remote");
    wizard.assertRebaseSelected();//from   w ww. ja va2  s.com
    assertFalse(wizard.isUpstreamConfigOverwriteWarningShown());
    wizard.selectMerge();
    assertTrue(wizard.isUpstreamConfigOverwriteWarningShown());
    wizard.deselectConfigureUpstream();
    assertFalse(wizard.isUpstreamConfigOverwriteWarningShown());
    wizard.next();
    wizard.finish();

    ObjectId remoteId = remoteRepository.resolve("foo-on-remote");
    ObjectId localId = repository.resolve("foo");
    assertEquals(localId, remoteId);

    // Still configured
    assertBranchConfig("foo", "fetch", "refs/heads/foo-on-remote", "true");
}