Example usage for org.eclipse.jgit.transport RefSpec setForceUpdate

List of usage examples for org.eclipse.jgit.transport RefSpec setForceUpdate

Introduction

In this page you can find the example usage for org.eclipse.jgit.transport RefSpec setForceUpdate.

Prototype

public RefSpec setForceUpdate(boolean forceUpdate) 

Source Link

Document

Create a new RefSpec with a different force update setting.

Usage

From source file:com.google.gerrit.server.git.PushReplication.java

License:Apache License

private List<ReplicationConfig> allConfigs(final SitePaths site) throws ConfigInvalidException, IOException {
    final FileBasedConfig cfg = new FileBasedConfig(site.replication_config, FS.DETECTED);

    if (!cfg.getFile().exists()) {
        log.warn("No " + cfg.getFile() + "; not replicating");
        return Collections.emptyList();
    }//ww w  .  ja  v  a  2s.c om
    if (cfg.getFile().length() == 0) {
        log.info("Empty " + cfg.getFile() + "; not replicating");
        return Collections.emptyList();
    }

    try {
        cfg.load();
    } catch (ConfigInvalidException e) {
        throw new ConfigInvalidException("Config file " + cfg.getFile() + " is invalid: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new IOException("Cannot read " + cfg.getFile() + ": " + e.getMessage(), e);
    }

    final List<ReplicationConfig> r = new ArrayList<ReplicationConfig>();
    for (final RemoteConfig c : allRemotes(cfg)) {
        if (c.getURIs().isEmpty()) {
            continue;
        }

        for (final URIish u : c.getURIs()) {
            if (u.getPath() == null || !u.getPath().contains("${name}")) {
                throw new ConfigInvalidException("remote." + c.getName() + ".url" + " \"" + u
                        + "\" lacks ${name} placeholder in " + cfg.getFile());
            }
        }

        // In case if refspec destination for push is not set then we assume it is
        // equal to source
        for (RefSpec ref : c.getPushRefSpecs()) {
            if (ref.getDestination() == null) {
                ref.setDestination(ref.getSource());
            }
        }

        if (c.getPushRefSpecs().isEmpty()) {
            RefSpec spec = new RefSpec();
            spec = spec.setSourceDestination("refs/*", "refs/*");
            spec = spec.setForceUpdate(true);
            c.addPushRefSpec(spec);
        }

        r.add(new ReplicationConfig(injector, workQueue, c, cfg, database, replicationUserFactory));
    }
    return Collections.unmodifiableList(r);
}

From source file:net.erdfelt.android.sdkfido.git.internal.GitCloneCommand.java

License:Apache License

/**
 * Add a 'remote' configuration.//  w  ww  .  ja va2  s.c om
 * 
 * @param remoteName
 *            the name of the remote
 * @param uri
 *            the uri to the remote
 * @throws URISyntaxException
 *             if unable to process uri
 * @throws IOException
 *             if unable to add remote config
 */
private void addRemoteConfig(String remoteName, URIish uri) throws URISyntaxException, IOException {
    RemoteConfig rc = new RemoteConfig(repo.getConfig(), remoteName);
    rc.addURI(uri);

    String dest = Constants.R_HEADS + "*:" + Constants.R_REMOTES + remoteName + "/*";

    RefSpec refspec = new RefSpec(dest);
    refspec.setForceUpdate(true);
    rc.addFetchRefSpec(refspec);
    rc.update(repo.getConfig());
    repo.getConfig().save();
}

From source file:org.commonjava.gitwrap.BareGitRepository.java

License:Open Source License

protected final void doClone(final String remoteUrl, final String remoteName, final String branch)
        throws GitWrapException {
    final FileRepository repository = getRepository();

    final String branchRef = Constants.R_HEADS + (branch == null ? Constants.MASTER : branch);

    try {//  w w w.  ja  va 2s . co  m
        final RefUpdate head = repository.updateRef(Constants.HEAD);
        head.disableRefLog();
        head.link(branchRef);

        final RemoteConfig remoteConfig = new RemoteConfig(repository.getConfig(), remoteName);
        remoteConfig.addURI(new URIish(remoteUrl));

        final String remoteRef = Constants.R_REMOTES + remoteName;

        RefSpec spec = new RefSpec();
        spec = spec.setForceUpdate(true);
        spec = spec.setSourceDestination(Constants.R_HEADS + "*", remoteRef + "/*");

        remoteConfig.addFetchRefSpec(spec);

        remoteConfig.update(repository.getConfig());

        repository.getConfig().setString("branch", branch, "remote", remoteName);
        repository.getConfig().setString("branch", branch, "merge", branchRef);

        repository.getConfig().save();

        fetch(remoteName);
        postClone(remoteUrl, branchRef);
    } catch (final IOException e) {
        throw new GitWrapException("Failed to clone from: %s. Reason: %s", e, remoteUrl, e.getMessage());
    } catch (final URISyntaxException e) {
        throw new GitWrapException("Failed to clone from: %s. Reason: %s", e, remoteUrl, e.getMessage());
    }
}

From source file:org.eclipse.egit.core.op.CloneOperation.java

License:Open Source License

private void doInit(final IProgressMonitor monitor) throws URISyntaxException, IOException {
    monitor.setTaskName(CoreText.CloneOperation_initializingRepository);

    local = new FileRepository(gitdir);
    local.create();/*  w  ww .j av a2s . c o  m*/

    final RefUpdate head = local.updateRef(Constants.HEAD);
    head.disableRefLog();
    head.link(branch);

    remoteConfig = new RemoteConfig(local.getConfig(), remoteName);
    remoteConfig.addURI(uri);

    final String dst = Constants.R_REMOTES + remoteConfig.getName();
    RefSpec wcrs = new RefSpec();
    wcrs = wcrs.setForceUpdate(true);
    wcrs = wcrs.setSourceDestination(Constants.R_HEADS + "*", dst + "/*"); //$NON-NLS-1$ //$NON-NLS-2$

    if (allSelected) {
        remoteConfig.addFetchRefSpec(wcrs);
    } else {
        for (final Ref ref : selectedBranches)
            if (wcrs.matchSource(ref))
                remoteConfig.addFetchRefSpec(wcrs.expandFromSource(ref));
    }

    // we're setting up for a clone with a checkout
    local.getConfig().setBoolean("core", null, "bare", false); //$NON-NLS-1$ //$NON-NLS-2$

    remoteConfig.update(local.getConfig());

    // branch is like 'Constants.R_HEADS + branchName', we need only
    // the 'branchName' part
    String branchName = branch.substring(Constants.R_HEADS.length());

    // setup the default remote branch for branchName
    local.getConfig().setString("branch", branchName, "remote", remoteName); //$NON-NLS-1$ //$NON-NLS-2$
    local.getConfig().setString("branch", branchName, "merge", branch); //$NON-NLS-1$ //$NON-NLS-2$

    local.getConfig().save();
}

From source file:org.eclipse.egit.ui.internal.components.RefSpecPanel.java

License:Open Source License

private void createForceColumn(final TableColumnLayout columnLayout) {
    final TableViewerColumn column = createColumn(columnLayout, UIText.RefSpecPanel_columnForce,
            COLUMN_FORCE_WEIGHT, SWT.CENTER);
    column.setLabelProvider(new CheckboxLabelProvider(tableViewer.getControl()) {
        @Override/*from  ww w. ja  v a 2s. c  o m*/
        protected boolean isChecked(final Object element) {
            return ((RefSpec) element).isForceUpdate();
        }

        @Override
        protected boolean isEnabled(Object element) {
            return !isDeleteRefSpec(element);
        }

        @Override
        public String getToolTipText(Object element) {
            if (!isEnabled(element))
                return UIText.RefSpecPanel_forceDeleteDescription;
            if (isChecked(element))
                return UIText.RefSpecPanel_forceTrueDescription + '\n' + UIText.RefSpecPanel_clickToChange;
            return UIText.RefSpecPanel_forceFalseDescription + '\n' + UIText.RefSpecPanel_clickToChange;
        }
    });
    column.setEditingSupport(new EditingSupport(tableViewer) {
        @Override
        protected boolean canEdit(final Object element) {
            return !isDeleteRefSpec(element);
        }

        @Override
        protected CellEditor getCellEditor(final Object element) {
            return forceUpdateCellEditor;
        }

        @SuppressWarnings("boxing")
        @Override
        protected Object getValue(final Object element) {
            return ((RefSpec) element).isForceUpdate();
        }

        @SuppressWarnings("boxing")
        @Override
        protected void setValue(final Object element, final Object value) {
            final RefSpec oldSpec = (RefSpec) element;
            final RefSpec newSpec = oldSpec.setForceUpdate((Boolean) value);
            setRefSpec(oldSpec, newSpec);
        }
    });
}

From source file:org.eclipse.egit.ui.internal.components.RefSpecPanel.java

License:Open Source License

private void createSpecsButtonsPanel(final Composite parent) {
    final Composite specsPanel = new Composite(parent, SWT.NONE);
    specsPanel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
    final RowLayout layout = new RowLayout();
    layout.spacing = 10;// w  w  w. j a  v  a 2 s. c  o m
    specsPanel.setLayout(layout);

    forceUpdateAllButton = new Button(specsPanel, SWT.PUSH);
    forceUpdateAllButton.setText(UIText.RefSpecPanel_forceAll);
    forceUpdateAllButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            final List<RefSpec> specsCopy = new ArrayList<RefSpec>(specs);
            for (final RefSpec spec : specsCopy) {
                if (!isDeleteRefSpec(spec))
                    setRefSpec(spec, spec.setForceUpdate(true));
            }
        }
    });
    forceUpdateAllButton.setToolTipText(UIText.RefSpecPanel_forceAllDescription);
    updateForceUpdateAllButton();

    removeAllSpecButton = new Button(specsPanel, SWT.PUSH);
    removeAllSpecButton.setImage(imageRegistry.get(IMAGE_CLEAR));
    removeAllSpecButton.setText(UIText.RefSpecPanel_removeAll);
    removeAllSpecButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            clearRefSpecs();
        }
    });
    removeAllSpecButton.setToolTipText(UIText.RefSpecPanel_removeAllDescription);
    updateRemoveAllSpecButton();

    addRefSpecTableListener(new SelectionChangeListener() {
        public void selectionChanged() {
            updateForceUpdateAllButton();
            updateRemoveAllSpecButton();
        }
    });
}

From source file:org.eclipse.orion.server.git.jobs.FetchJob.java

License:Open Source License

private IStatus doFetch() throws IOException, CoreException, URISyntaxException, GitAPIException {
    Repository db = getRepository();//  w ww  . java 2s .  c  o m

    Git git = new Git(db);
    FetchCommand fc = git.fetch();

    RemoteConfig remoteConfig = new RemoteConfig(git.getRepository().getConfig(), remote);
    credentials.setUri(remoteConfig.getURIs().get(0));

    fc.setCredentialsProvider(credentials);
    fc.setRemote(remote);
    if (branch != null) {
        // refs/heads/{branch}:refs/remotes/{remote}/{branch}
        RefSpec spec = new RefSpec(
                Constants.R_HEADS + branch + ":" + Constants.R_REMOTES + remote + "/" + branch); //$NON-NLS-1$ //$NON-NLS-2$
        spec = spec.setForceUpdate(force);
        fc.setRefSpecs(spec);
    }
    FetchResult fetchResult = fc.call();
    return handleFetchResult(fetchResult);
}

From source file:org.eclipse.orion.server.git.servlets.FetchJob.java

License:Open Source License

private IStatus doFetch()
        throws IOException, CoreException, JGitInternalException, InvalidRemoteException, URISyntaxException {
    Repository db = getRepository();/*from  w ww .j  a  va 2s .c om*/
    String branch = getRemoteBranch();

    Git git = new Git(db);
    FetchCommand fc = git.fetch();

    RemoteConfig remoteConfig = new RemoteConfig(git.getRepository().getConfig(), remote);
    credentials.setUri(remoteConfig.getURIs().get(0));

    fc.setCredentialsProvider(credentials);
    fc.setRemote(remote);
    if (branch != null) {
        // refs/heads/{branch}:refs/remotes/{remote}/{branch}
        RefSpec spec = new RefSpec(
                Constants.R_HEADS + branch + ":" + Constants.R_REMOTES + remote + "/" + branch); //$NON-NLS-1$ //$NON-NLS-2$
        spec = spec.setForceUpdate(force);
        fc.setRefSpecs(spec);
    }
    FetchResult fetchResult = fc.call();

    // handle result
    for (TrackingRefUpdate updateRes : fetchResult.getTrackingRefUpdates()) {
        Result res = updateRes.getResult();
        // handle status for given ref
        switch (res) {
        case NOT_ATTEMPTED:
        case NO_CHANGE:
        case NEW:
        case FORCED:
        case FAST_FORWARD:
        case RENAMED:
            // do nothing, as these statuses are OK
            break;
        case REJECTED:
        case REJECTED_CURRENT_BRANCH:
            // show warning, as only force fetch can finish successfully 
            return new Status(IStatus.WARNING, GitActivator.PI_GIT, res.name());
        default:
            return new Status(IStatus.ERROR, GitActivator.PI_GIT, res.name());
        }
    }
    return Status.OK_STATUS;
}

From source file:org.fedoraproject.eclipse.packager.git.api.ConvertLocalToRemoteCommand.java

License:Open Source License

/**
 * Adds the corresponding remote repository as the default name 'origin' to
 * the existing local repository (uses the JGit API)
 *
 * @param uri/*from  www.  j ava 2s  .co  m*/
 * @param monitor
 * @throws LocalProjectConversionFailedException
 */
private void addRemoteRepository(String uri, IProgressMonitor monitor)
        throws LocalProjectConversionFailedException {

    try {
        RemoteConfig config = new RemoteConfig(git.getRepository().getConfig(), "origin"); //$NON-NLS-1$
        config.addURI(new URIish(uri));
        String dst = Constants.R_REMOTES + config.getName();
        RefSpec refSpec = new RefSpec();
        refSpec = refSpec.setForceUpdate(true);
        refSpec = refSpec.setSourceDestination(Constants.R_HEADS + "*", dst + "/*"); //$NON-NLS-1$ //$NON-NLS-2$

        config.addFetchRefSpec(refSpec);
        config.update(git.getRepository().getConfig());
        git.getRepository().getConfig().save();

        // fetch all the remote branches,
        // create corresponding branches locally and merge them
        FetchCommand fetch = git.fetch();
        fetch.setRemote("origin"); //$NON-NLS-1$
        fetch.setTimeout(0);
        fetch.setRefSpecs(refSpec);
        if (monitor.isCanceled()) {
            throw new OperationCanceledException();
        }
        fetch.call();

    } catch (Exception e) {
        throw new LocalProjectConversionFailedException(e.getCause().getMessage(), e);
    }
}

From source file:org.fedoraproject.eclipse.packager.tests.utils.git.GitConvertTestProject.java

License:Open Source License

/**
 * Adds a remote repository to the existing local packager project
 *
 * @throws Exception//from ww  w  . j  a v a  2  s .c om
 */
public void addRemoteRepository(String uri, Git git) throws Exception {

    RemoteConfig config = new RemoteConfig(git.getRepository().getConfig(), "origin"); //$NON-NLS-1$
    config.addURI(new URIish(uri));
    String dst = Constants.R_REMOTES + config.getName();
    RefSpec refSpec = new RefSpec();
    refSpec = refSpec.setForceUpdate(true);
    refSpec = refSpec.setSourceDestination(Constants.R_HEADS + "*", dst + "/*"); //$NON-NLS-1$ //$NON-NLS-2$

    config.addFetchRefSpec(refSpec);
    config.update(git.getRepository().getConfig());
    git.getRepository().getConfig().save();

    // fetch all the remote branches,
    // create corresponding branches locally and merge them
    FetchCommand fetch = git.fetch();
    fetch.setRemote("origin"); //$NON-NLS-1$
    fetch.setTimeout(0);
    fetch.setRefSpecs(refSpec);
    fetch.call();
    // refresh after checkout
    project.refreshLocal(IResource.DEPTH_INFINITE, null);
}