Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1d7f25eed743a4252fcd89bd7c0eb15702fc62b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*******************************************************************************
 * Copyright (c) 2011, 2013 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *     Sascha Scholz (SAP) - improvements
 *******************************************************************************/

package org.eclipse.mylyn.internal.gerrit.ui.egit;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.UIPreferences;
import org.eclipse.egit.ui.internal.clone.AbstractGitCloneWizard;
import org.eclipse.egit.ui.internal.clone.GitCloneWizard;
import org.eclipse.egit.ui.internal.credentials.EGitCredentialsProvider;
import org.eclipse.egit.ui.internal.fetch.FetchOperationUI;
import org.eclipse.egit.ui.internal.provisional.wizards.GitRepositoryInfo;
import org.eclipse.egit.ui.internal.provisional.wizards.IRepositorySearchResult;
import org.eclipse.egit.ui.internal.provisional.wizards.NoRepositoryInfoException;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jgit.errors.AmbiguousObjectException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.FetchResult;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.mylyn.internal.gerrit.core.GerritCorePlugin;
import org.eclipse.mylyn.internal.gerrit.core.GerritUtil;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritConfiguration;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritException;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.swt.widgets.Shell;

import com.google.gerrit.reviewdb.AccountGeneralPreferences.DownloadScheme;
import com.google.gerrit.reviewdb.PatchSet;
import com.google.gerrit.reviewdb.Project;

/**
 * Provides common UI utilities.
 *
 * @author Steffen Pingel
 * @author Sascha Scholz
 */
public class EGitUiUtil {

	public static RevCommit getRevCommit(Repository repository, PatchSet target)
			throws AmbiguousObjectException, IOException, MissingObjectException, IncorrectObjectTypeException {
		ObjectId ref = repository.resolve(target.getRevision().get());
		try (RevWalk walker = new RevWalk(repository)) {
			return walker.parseCommit(ref);
		}
	}

	private static RevCommit fetchRefSpec(IProgressMonitor monitor, Repository repository, RemoteConfig remote,
			RefSpec refSpec) throws URISyntaxException, CoreException, MissingObjectException,
			IncorrectObjectTypeException, IOException {
		List<RefSpec> refSpecs = Collections.singletonList(refSpec);
		FetchOperationUI op = new FetchOperationUI(repository, remote.getURIs().get(0), refSpecs,
				Activator.getDefault().getPreferenceStore().getInt(UIPreferences.REMOTE_CONNECTION_TIMEOUT), false);
		op.setCredentialsProvider(new EGitCredentialsProvider());
		FetchResult result = op.execute(monitor);
		ObjectId resultRef = result.getAdvertisedRef(refSpec.getSource()).getObjectId();
		try (RevWalk walker = new RevWalk(repository)) {
			return walker.parseCommit(resultRef);
		}
	}

	public static RevCommit fetchPatchSet(IProgressMonitor monitor, Repository repository, RemoteConfig remote,
			PatchSet patchSet) throws IOException, CoreException, URISyntaxException {
		try {
			// commit was already fetched
			return EGitUiUtil.getRevCommit(repository, patchSet);
		} catch (MissingObjectException e) {
			// need to fetch it
			RefSpec refSpec = new RefSpec(patchSet.getRefName() + ":FETCH_HEAD"); //$NON-NLS-1$
			return fetchRefSpec(monitor, repository, remote, refSpec);
		}
	}

	public static int openCloneRepositoryWizard(Shell shell, final TaskRepository repository, final Project project) {
		AbstractGitCloneWizard cloneWizard = new GitCloneWizard(new IRepositorySearchResult() {

			@Override
			public GitRepositoryInfo getGitRepositoryInfo() throws NoRepositoryInfoException {
				GitRepositoryInfo gitRepositoryInfo;
				try {
					GerritConfiguration config = GerritCorePlugin.getGerritClient(repository).refreshConfig(null);
					gitRepositoryInfo = new GitRepositoryInfo(getCloneUriForRepo(repository, config, project));
					return gitRepositoryInfo;
				} catch (GerritException e) {

				}
				return null;
			}
		});
		WizardDialog dlg = new WizardDialog(shell, cloneWizard);
		dlg.setHelpAvailable(true);
		return dlg.open();
	}

	private static String getCloneUriForRepo(TaskRepository repository, GerritConfiguration config, Project project)
			throws NoRepositoryInfoException {
		try {
			Map<DownloadScheme, String> cloneUris = GerritUtil.getCloneUris(config, repository, project);
			if (cloneUris.keySet().contains(DownloadScheme.SSH)) {
				return cloneUris.get(DownloadScheme.SSH);
			}
			if (cloneUris.keySet().contains(DownloadScheme.HTTP)) {
				return cloneUris.get(DownloadScheme.HTTP);
			}
			for (DownloadScheme scheme : cloneUris.keySet()) {
				if (cloneUris.get(scheme) != null) {
					return cloneUris.get(scheme);
				}
			}
			return null;
		} catch (URISyntaxException e) {
			throw new NoRepositoryInfoException(e.getMessage(), e);
		}

	}

}

Back to the top