Avoid refresh on up-to-date pull operation
[egit/eclipse.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / FetchOperation.java
blob4f55149f6d0e784a3d6ed805b0d79a5c37eba60a
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;
30 /**
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;
52 /**
53 * Constructs a FetchOperation based on URI and RefSpecs
55 * @param repository
56 * @param uri
57 * @param refSpecs
58 * @param timeout
59 * @param dryRun
62 public FetchOperation(Repository repository, URIish uri,
63 List<RefSpec> refSpecs, int timeout, boolean dryRun) {
64 this.repository = repository;
65 this.timeout = timeout;
66 this.dryRun = dryRun;
67 this.uri = uri;
68 this.specs = refSpecs;
69 this.rc = null;
72 /**
73 * Constructs a FetchOperation based on a RemoteConfig
75 * @param repository
76 * @param config
77 * @param timeout
78 * @param dryRun
80 public FetchOperation(Repository repository, RemoteConfig config,
81 int timeout, boolean dryRun) {
82 this.repository = repository;
83 this.timeout = timeout;
84 this.dryRun = dryRun;
85 this.uri = null;
86 this.specs = null;
87 this.rc = config;
90 /**
91 * @param credentialsProvider
93 public void setCredentialsProvider(CredentialsProvider credentialsProvider) {
94 this.credentialsProvider = credentialsProvider;
97 /**
98 * @return the operation's credentials provider
100 public CredentialsProvider getCredentialsProvider() {
101 return credentialsProvider;
105 * @param tagOpt
107 public void setTagOpt(TagOpt tagOpt) {
108 this.tagOpt = tagOpt;
112 * @param monitor
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(
120 monitor);
121 try (Git git = new Git(repository)) {
122 FetchCommand command;
123 if (rc == null)
124 command = git.fetch().setRemote(uri.toPrivateString())
125 .setRefSpecs(specs);
126 else
127 command = git.fetch().setRemote(rc.getName());
128 command.setCredentialsProvider(credentialsProvider)
129 .setTimeout(timeout).setDryRun(dryRun)
130 .setProgressMonitor(gitMonitor);
131 if (tagOpt != null)
132 command.setTagOpt(tagOpt);
133 try {
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
146 * executed
148 public FetchResult getOperationResult() {
149 return operationResult;