1 /*******************************************************************************
2 * Copyright (C) 2011, Mathias Kinzler <mathias.kinzler@sap.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11 package org
.eclipse
.egit
.core
.op
;
13 import java
.lang
.reflect
.InvocationTargetException
;
14 import java
.util
.List
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
17 import org
.eclipse
.egit
.core
.EclipseGitProgressTransformer
;
18 import org
.eclipse
.egit
.core
.internal
.CoreText
;
19 import org
.eclipse
.jgit
.api
.FetchCommand
;
20 import org
.eclipse
.jgit
.api
.Git
;
21 import org
.eclipse
.jgit
.api
.errors
.JGitInternalException
;
22 import org
.eclipse
.jgit
.lib
.Repository
;
23 import org
.eclipse
.jgit
.transport
.CredentialsProvider
;
24 import org
.eclipse
.jgit
.transport
.FetchResult
;
25 import org
.eclipse
.jgit
.transport
.RefSpec
;
26 import org
.eclipse
.jgit
.transport
.RemoteConfig
;
27 import org
.eclipse
.jgit
.transport
.TagOpt
;
28 import org
.eclipse
.jgit
.transport
.URIish
;
31 * Used to fetch from another Repository
33 public class FetchOperation
{
34 private final Repository repository
;
36 private final RemoteConfig rc
;
38 private final URIish uri
;
40 private final int timeout
;
42 private final List
<RefSpec
> specs
;
44 private final boolean dryRun
;
46 private FetchResult operationResult
;
48 private CredentialsProvider credentialsProvider
;
50 private TagOpt tagOpt
;
53 * Constructs a FetchOperation based on URI and RefSpecs
62 public FetchOperation(Repository repository
, URIish uri
,
63 List
<RefSpec
> refSpecs
, int timeout
, boolean dryRun
) {
64 this.repository
= repository
;
65 this.timeout
= timeout
;
68 this.specs
= refSpecs
;
73 * Constructs a FetchOperation based on a RemoteConfig
80 public FetchOperation(Repository repository
, RemoteConfig config
,
81 int timeout
, boolean dryRun
) {
82 this.repository
= repository
;
83 this.timeout
= timeout
;
91 * @param credentialsProvider
93 public void setCredentialsProvider(CredentialsProvider credentialsProvider
) {
94 this.credentialsProvider
= credentialsProvider
;
98 * @return the operation's credentials provider
100 public CredentialsProvider
getCredentialsProvider() {
101 return credentialsProvider
;
107 public void setTagOpt(TagOpt tagOpt
) {
108 this.tagOpt
= tagOpt
;
113 * @throws InvocationTargetException
115 public void run(IProgressMonitor monitor
) throws InvocationTargetException
{
116 if (operationResult
!= null)
117 throw new IllegalStateException(CoreText
.OperationAlreadyExecuted
);
119 EclipseGitProgressTransformer gitMonitor
= new EclipseGitProgressTransformer(
121 try (Git git
= new Git(repository
)) {
122 FetchCommand command
;
124 command
= git
.fetch().setRemote(uri
.toPrivateString())
127 command
= git
.fetch().setRemote(rc
.getName());
128 command
.setCredentialsProvider(credentialsProvider
)
129 .setTimeout(timeout
).setDryRun(dryRun
)
130 .setProgressMonitor(gitMonitor
);
132 command
.setTagOpt(tagOpt
);
134 operationResult
= command
.call();
135 } catch (JGitInternalException e
) {
136 throw new InvocationTargetException(
137 e
.getCause() != null ? e
.getCause() : e
);
138 } catch (Exception e
) {
139 throw new InvocationTargetException(e
);
145 * @return the result, or <code>null</code> if the operation has not been
148 public FetchResult
getOperationResult() {
149 return operationResult
;