Search in sources :

Example 51 with RemoteConfig

use of org.eclipse.jgit.transport.RemoteConfig in project egit by eclipse.

the class PushUpstreamOrBranchActionHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final Repository repository = getRepository(true, event);
    if (repository == null)
        return null;
    Shell shell = getShell(event);
    RemoteConfig config = SimpleConfigurePushDialog.getConfiguredRemote(repository);
    pushOrConfigure(repository, config, shell);
    return null;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Shell(org.eclipse.swt.widgets.Shell) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig)

Example 52 with RemoteConfig

use of org.eclipse.jgit.transport.RemoteConfig in project egit by eclipse.

the class PushConfiguredRemoteCommand method getRemoteConfig.

private RemoteConfig getRemoteConfig(RepositoryTreeNode node) {
    if (node instanceof RepositoryNode)
        return SimpleConfigurePushDialog.getConfiguredRemote(node.getRepository());
    if (node instanceof RemoteNode || node instanceof PushNode) {
        RemoteNode remoteNode;
        if (node instanceof PushNode)
            remoteNode = (RemoteNode) node.getParent();
        else
            remoteNode = (RemoteNode) node;
        try {
            RemoteConfig config = new RemoteConfig(remoteNode.getRepository().getConfig(), remoteNode.getObject());
            boolean fetchConfigured = !config.getFetchRefSpecs().isEmpty();
            boolean pushConfigured = !config.getPushRefSpecs().isEmpty();
            if (fetchConfigured || pushConfigured)
                return config;
            else
                return null;
        } catch (URISyntaxException e) {
            return null;
        }
    }
    return null;
}
Also used : RemoteNode(org.eclipse.egit.ui.internal.repository.tree.RemoteNode) URISyntaxException(java.net.URISyntaxException) RepositoryNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryNode) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) PushNode(org.eclipse.egit.ui.internal.repository.tree.PushNode)

Example 53 with RemoteConfig

use of org.eclipse.jgit.transport.RemoteConfig in project egit by eclipse.

the class FetchConfiguredRemoteCommand method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    RepositoryTreeNode node = getSelectedNodes(event).get(0);
    RemoteConfig config = getRemoteConfig(node);
    if (config == null) {
        MessageDialog.openInformation(getShell(event), UIText.SimpleFetchActionHandler_NothingToFetchDialogTitle, UIText.SimpleFetchActionHandler_NothingToFetchDialogMessage);
        return null;
    }
    int timeout = Activator.getDefault().getPreferenceStore().getInt(UIPreferences.REMOTE_CONNECTION_TIMEOUT);
    new FetchOperationUI(node.getRepository(), config, timeout, false).start();
    return null;
}
Also used : RepositoryTreeNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) FetchOperationUI(org.eclipse.egit.ui.internal.fetch.FetchOperationUI)

Example 54 with RemoteConfig

use of org.eclipse.jgit.transport.RemoteConfig in project gitblit by gitblit.

the class MirrorService method run.

@Override
public void run() {
    if (!isReady()) {
        return;
    }
    running.set(true);
    for (String repositoryName : repositoryManager.getRepositoryList()) {
        if (forceClose.get()) {
            break;
        }
        if (repositoryManager.isCollectingGarbage(repositoryName)) {
            logger.debug("mirror is skipping {} garbagecollection", repositoryName);
            continue;
        }
        RepositoryModel model = null;
        Repository repository = null;
        try {
            model = repositoryManager.getRepositoryModel(repositoryName);
            if (!model.isMirror && !model.isBare) {
                // repository must be a valid bare git mirror
                logger.debug("mirror is skipping {} !mirror !bare", repositoryName);
                continue;
            }
            repository = repositoryManager.getRepository(repositoryName);
            if (repository == null) {
                logger.warn(MessageFormat.format("MirrorExecutor is missing repository {0}?!?", repositoryName));
                continue;
            }
            // automatically repair (some) invalid fetch ref specs
            if (!repairAttempted.contains(repositoryName)) {
                repairAttempted.add(repositoryName);
                JGitUtils.repairFetchSpecs(repository);
            }
            // find the first mirror remote - there should only be one
            StoredConfig rc = repository.getConfig();
            RemoteConfig mirror = null;
            List<RemoteConfig> configs = RemoteConfig.getAllRemoteConfigs(rc);
            for (RemoteConfig config : configs) {
                if (config.isMirror()) {
                    mirror = config;
                    break;
                }
            }
            if (mirror == null) {
                // repository does not have a mirror remote
                logger.debug("mirror is skipping {} no mirror remote found", repositoryName);
                continue;
            }
            logger.debug("checking {} remote {} for ref updates", repositoryName, mirror.getName());
            final boolean testing = false;
            Git git = new Git(repository);
            CredentialsProvider creds = null;
            URIish fetchUri = mirror.getURIs().get(0);
            if (fetchUri.getUser() != null && fetchUri.getPass() != null) {
                creds = new UsernamePasswordCredentialsProvider(fetchUri.getUser(), fetchUri.getPass());
            }
            FetchResult result = git.fetch().setCredentialsProvider(creds).setRemote(mirror.getName()).setDryRun(testing).call();
            Collection<TrackingRefUpdate> refUpdates = result.getTrackingRefUpdates();
            if (refUpdates.size() > 0) {
                ReceiveCommand ticketBranchCmd = null;
                for (TrackingRefUpdate ru : refUpdates) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("updated mirror ");
                    sb.append(repositoryName);
                    sb.append(" ");
                    sb.append(ru.getRemoteName());
                    sb.append(" -> ");
                    sb.append(ru.getLocalName());
                    if (ru.getResult() == Result.FORCED) {
                        sb.append(" (forced)");
                    }
                    sb.append(" ");
                    sb.append(ru.getOldObjectId() == null ? "" : ru.getOldObjectId().abbreviate(7).name());
                    sb.append("..");
                    sb.append(ru.getNewObjectId() == null ? "" : ru.getNewObjectId().abbreviate(7).name());
                    logger.info(sb.toString());
                    if (BranchTicketService.BRANCH.equals(ru.getLocalName())) {
                        ReceiveCommand.Type type = null;
                        switch(ru.getResult()) {
                            case NEW:
                                type = Type.CREATE;
                                break;
                            case FAST_FORWARD:
                                type = Type.UPDATE;
                                break;
                            case FORCED:
                                type = Type.UPDATE_NONFASTFORWARD;
                                break;
                            default:
                                type = null;
                                break;
                        }
                        if (type != null) {
                            ticketBranchCmd = new ReceiveCommand(ru.getOldObjectId(), ru.getNewObjectId(), ru.getLocalName(), type);
                        }
                    }
                }
                if (ticketBranchCmd != null) {
                    repository.fireEvent(new ReceiveCommandEvent(model, ticketBranchCmd));
                }
            }
        } catch (Exception e) {
            logger.error("Error updating mirror " + repositoryName, e);
        } finally {
            // cleanup
            if (repository != null) {
                repository.close();
            }
        }
    }
    running.set(false);
}
Also used : URIish(org.eclipse.jgit.transport.URIish) ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) FetchResult(org.eclipse.jgit.transport.FetchResult) RepositoryModel(com.gitblit.models.RepositoryModel) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) TrackingRefUpdate(org.eclipse.jgit.transport.TrackingRefUpdate) Type(org.eclipse.jgit.transport.ReceiveCommand.Type) StoredConfig(org.eclipse.jgit.lib.StoredConfig) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) ReceiveCommandEvent(com.gitblit.git.ReceiveCommandEvent) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig)

Example 55 with RemoteConfig

use of org.eclipse.jgit.transport.RemoteConfig in project jbosstools-openshift by jbosstools.

the class ImportProjectOperation method checkoutBranch.

protected void checkoutBranch(String gitRef, String gitUrl, List<IProject> importedProjects, File repositoryFolder, IProgressMonitor monitor) throws CoreException {
    if (!StringUtils.isEmpty(gitRef) && !CollectionUtils.isEmpty(importedProjects)) {
        // all projects are in the same repo
        IProject project = importedProjects.get(0);
        Repository repository = EGitUtils.getRepository(project);
        try {
            if (!EGitUtils.hasBranch(gitRef, repository)) {
                Pattern gitURIPattern = Pattern.compile(RegExUtils.escapeRegex(gitUrl));
                RemoteConfig remote = EGitUtils.getRemoteByUrl(gitURIPattern, repository);
                fetchBranch(gitRef, remote, repository, monitor);
            }
            CheckoutResult result = EGitUtils.checkoutBranch(gitRef, repository, monitor);
            switch(result.getStatus()) {
                case CONFLICTS:
                case ERROR:
                    throw new CoreException(StatusFactory.errorStatus(OpenShiftCoreActivator.PLUGIN_ID, NLS.bind("Could not check out the branch {0} of the (reused) local repository at {1} because of {2}." + " Please resolve the problem and check out the branch manually so that it matches the code that's used in your OpenShift application.", new Object[] { gitRef, repositoryFolder, result.getStatus().toString().toLowerCase() })));
                default:
            }
        } catch (IOException e) {
            throw new CoreException(StatusFactory.errorStatus(OpenShiftCoreActivator.PLUGIN_ID, NLS.bind("Could check that branch {0} exists within the (reused) local repository at {1}.", gitRef, repositoryFolder), e));
        } catch (InvocationTargetException e) {
            throw new CoreException(StatusFactory.errorStatus(OpenShiftCoreActivator.PLUGIN_ID, NLS.bind("Could not fetch branch {0} from check that branch {0} exists within the (reused) local repository at {1}.", gitRef, repositoryFolder), e));
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Repository(org.eclipse.jgit.lib.Repository) CoreException(org.eclipse.core.runtime.CoreException) CheckoutResult(org.eclipse.jgit.api.CheckoutResult) IOException(java.io.IOException) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

RemoteConfig (org.eclipse.jgit.transport.RemoteConfig)63 URISyntaxException (java.net.URISyntaxException)23 StoredConfig (org.eclipse.jgit.lib.StoredConfig)21 URIish (org.eclipse.jgit.transport.URIish)21 IOException (java.io.IOException)16 RefSpec (org.eclipse.jgit.transport.RefSpec)14 Repository (org.eclipse.jgit.lib.Repository)13 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)7 Button (org.eclipse.swt.widgets.Button)6 Composite (org.eclipse.swt.widgets.Composite)6 File (java.io.File)5 Git (org.eclipse.jgit.api.Git)5 Ref (org.eclipse.jgit.lib.Ref)5 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)5 SelectionEvent (org.eclipse.swt.events.SelectionEvent)5 Label (org.eclipse.swt.widgets.Label)5 List (java.util.List)4 CoreException (org.eclipse.core.runtime.CoreException)4 UIText (org.eclipse.egit.ui.internal.UIText)4