2 * Copyright 2000-2018 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package jetbrains.buildServer.buildTriggers.vcs.git.agent;
19 import com.intellij.openapi.util.io.FileUtil;
20 import jetbrains.buildServer.agent.AgentRunningBuild;
21 import jetbrains.buildServer.agent.SmartDirectoryCleaner;
22 import jetbrains.buildServer.buildTriggers.vcs.git.GitUtils;
23 import jetbrains.buildServer.buildTriggers.vcs.git.MirrorManager;
24 import jetbrains.buildServer.buildTriggers.vcs.git.agent.errors.GitExecTimeout;
25 import jetbrains.buildServer.util.StringUtil;
26 import jetbrains.buildServer.util.ThreadUtil;
27 import jetbrains.buildServer.vcs.CheckoutRules;
28 import jetbrains.buildServer.vcs.VcsException;
29 import jetbrains.buildServer.vcs.VcsRoot;
30 import com.intellij.openapi.diagnostic.Logger;
31 import org.eclipse.jgit.lib.Ref;
32 import org.eclipse.jgit.lib.Repository;
33 import org.eclipse.jgit.lib.RepositoryBuilder;
34 import org.eclipse.jgit.transport.URIish;
35 import org.jetbrains.annotations.NotNull;
38 import java.io.IOException;
39 import java.net.URISyntaxException;
40 import java.util.List;
44 * @author dmitry.neverov
46 public class UpdaterWithMirror extends UpdaterImpl {
48 private final static Logger LOG = Logger.getInstance(UpdaterWithMirror.class.getName());
50 public UpdaterWithMirror(@NotNull FS fs,
51 @NotNull AgentPluginConfig pluginConfig,
52 @NotNull MirrorManager mirrorManager,
53 @NotNull SmartDirectoryCleaner directoryCleaner,
54 @NotNull GitFactory gitFactory,
55 @NotNull AgentRunningBuild build,
56 @NotNull VcsRoot root,
57 @NotNull String version,
58 @NotNull File targetDir,
59 @NotNull CheckoutRules rules,
60 @NotNull CheckoutMode mode) throws VcsException {
61 super(fs, pluginConfig, mirrorManager, directoryCleaner, gitFactory, build, root, version, targetDir, rules, mode);
65 protected void doUpdate() throws VcsException {
70 private void updateLocalMirror() throws VcsException {
71 String message = "Update git mirror (" + myRoot.getRepositoryDir() + ")";
72 myLogger.activityStarted(message, GitBuildProgressLogger.GIT_PROGRESS_ACTIVITY);
74 updateLocalMirror(true);
75 //prepare refs for copying into working dir repository
76 myGitFactory.create(myRoot.getRepositoryDir()).packRefs().call();
78 myLogger.activityFinished(message, GitBuildProgressLogger.GIT_PROGRESS_ACTIVITY);
82 private void updateLocalMirror(boolean repeatFetchAttempt) throws VcsException {
83 File bareRepositoryDir = myRoot.getRepositoryDir();
84 String mirrorDescription = "local mirror of root " + myRoot.getName() + " at " + bareRepositoryDir;
85 LOG.info("Update " + mirrorDescription);
86 boolean fetchRequired = true;
87 if (isValidGitRepo(bareRepositoryDir)) {
88 removeOrphanedIdxFiles(bareRepositoryDir);
90 FileUtil.delete(bareRepositoryDir);
92 boolean newMirror = false;
93 if (!bareRepositoryDir.exists()) {
94 LOG.info("Init " + mirrorDescription);
95 bareRepositoryDir.mkdirs();
96 GitFacade git = myGitFactory.create(bareRepositoryDir);
97 git.init().setBare(true).call();
98 configureRemoteUrl(bareRepositoryDir);
101 configureRemoteUrl(bareRepositoryDir);
102 boolean outdatedTagsFound = removeOutdatedRefs(bareRepositoryDir);
103 if (!outdatedTagsFound) {
104 LOG.debug("Try to find revision " + myRevision + " in " + mirrorDescription);
105 Ref ref = getRef(bareRepositoryDir, GitUtils.createRemoteRef(myFullBranchName));
106 if (ref != null && myRevision.equals(ref.getObjectId().name())) {
107 LOG.info("No fetch required for revision '" + myRevision + "' in " + mirrorDescription);
108 fetchRequired = false;
112 FetchHeadsMode fetchHeadsMode = myPluginConfig.getFetchHeadsMode();
113 Ref ref = getRef(bareRepositoryDir, myFullBranchName);
115 fetchRequired = true;
116 if (!fetchRequired && fetchHeadsMode != FetchHeadsMode.ALWAYS)
118 if (!newMirror && optimizeMirrorBeforeFetch()) {
119 GitFacade git = myGitFactory.create(bareRepositoryDir);
124 switch (fetchHeadsMode) {
126 String msg = getForcedHeadsFetchMessage();
128 myLogger.message(msg);
129 fetchMirror(repeatFetchAttempt, bareRepositoryDir, "+refs/heads/*:refs/heads/*");
130 if (!myFullBranchName.startsWith("refs/heads/") && !hasRevision(bareRepositoryDir, myRevision))
131 fetchMirror(repeatFetchAttempt, bareRepositoryDir, "+" + myFullBranchName + ":" + GitUtils.expandRef(myFullBranchName));
133 case BEFORE_BUILD_BRANCH:
134 fetchMirror(repeatFetchAttempt, bareRepositoryDir, "+refs/heads/*:refs/heads/*");
135 if (!myFullBranchName.startsWith("refs/heads/") && !hasRevision(bareRepositoryDir, myRevision))
136 fetchMirror(repeatFetchAttempt, bareRepositoryDir, "+" + myFullBranchName + ":" + GitUtils.expandRef(myFullBranchName));
138 case AFTER_BUILD_BRANCH:
139 fetchMirror(repeatFetchAttempt, bareRepositoryDir, "+" + myFullBranchName + ":" + GitUtils.expandRef(myFullBranchName));
140 if (!hasRevision(bareRepositoryDir, myRevision))
141 fetchMirror(repeatFetchAttempt, bareRepositoryDir, "+refs/heads/*:refs/heads/*");
144 throw new VcsException("Unknown FetchHeadsMode: " + fetchHeadsMode);
149 private boolean optimizeMirrorBeforeFetch() {
150 return "true".equals(myBuild.getSharedConfigParameters().get("teamcity.git.optimizeMirrorBeforeFetch"));
154 private void fetchMirror(boolean repeatFetchAttempt,
155 @NotNull File repositoryDir,
156 @NotNull String refspec) throws VcsException {
157 removeRefLocks(repositoryDir);
159 final int[] retryTimeouts = getRetryTimeouts();
160 for (int i = 0; i <= retryTimeouts.length; i++) {
162 fetch(repositoryDir, refspec, false);
164 } catch (VcsException e) {
165 // Throw exception after latest attempt
166 if (i == retryTimeouts.length) throw e;
167 int wait = retryTimeouts[i];
168 LOG.warnAndDebugDetails("Failed to fetch mirror, will retry after " + wait + " seconds.", e);
169 ThreadUtil.sleep(wait * 1000);
172 } catch (VcsException e) {
173 if (myPluginConfig.isFailOnCleanCheckout() || !repeatFetchAttempt || !shouldFetchFromScratch(e))
175 LOG.warnAndDebugDetails("Failed to fetch mirror", e);
176 if (cleanDir(repositoryDir)) {
177 GitFacade git = myGitFactory.create(repositoryDir);
178 git.init().setBare(true).call();
179 configureRemoteUrl(repositoryDir);
180 fetch(repositoryDir, refspec, false);
182 LOG.info("Failed to delete repository " + repositoryDir + " after failed checkout, clone repository in another directory");
183 myMirrorManager.invalidate(repositoryDir);
184 updateLocalMirror(false);
190 private boolean shouldFetchFromScratch(@NotNull VcsException e) {
191 if (e instanceof GitExecTimeout)
193 String msg = e.getMessage();
194 if (msg.contains("Couldn't find remote ref") ||
195 msg.contains("Could not read from remote repository")) {
202 private boolean cleanDir(final @NotNull File repositoryDir) {
203 return myFS.delete(repositoryDir) && myFS.mkdirs(repositoryDir);
207 private boolean isValidGitRepo(@NotNull File gitDir) {
209 new RepositoryBuilder().setGitDir(gitDir).setMustExist(true).build();
211 } catch (IOException e) {
218 protected void setupExistingRepository() throws VcsException {
225 protected void setupNewRepository() throws VcsException {
231 protected void ensureCommitLoaded(boolean fetchRequired) throws VcsException {
232 if (myPluginConfig.isUseShallowClone()) {
233 File mirrorRepositoryDir = myRoot.getRepositoryDir();
234 if (GitUtils.isTag(myFullBranchName)) {
235 //handle tags specially: if we fetch a temporary branch which points to a commit
236 //tags points to, git fetches both branch and tag, tries to make a local
237 //branch to track both of them and fails.
238 String refspec = "+" + myFullBranchName + ":" + myFullBranchName;
239 fetch(myTargetDirectory, refspec, true);
241 String tmpBranchName = createTmpBranch(mirrorRepositoryDir, myRevision);
242 String tmpBranchRef = "refs/heads/" + tmpBranchName;
243 String refspec = "+" + tmpBranchRef + ":" + GitUtils.createRemoteRef(myFullBranchName);
244 fetch(myTargetDirectory, refspec, true);
245 myGitFactory.create(mirrorRepositoryDir).deleteBranch().setName(tmpBranchName).call();
248 super.ensureCommitLoaded(fetchRequired);
254 private String readRemoteUrl() throws VcsException {
255 Repository repository = null;
257 repository = new RepositoryBuilder().setWorkTree(myTargetDirectory).build();
258 return repository.getConfig().getString("remote", "origin", "url");
259 } catch (IOException e) {
260 throw new VcsException("Error while reading remote repository url", e);
262 if (repository != null)
268 private void setUseLocalMirror() throws VcsException {
269 //read remote url from config instead of VCS root, they can be different
270 //e.g. due to username exclusion from http(s) urls
271 String remoteUrl = readRemoteUrl();
272 String localMirrorUrl = getLocalMirrorUrl();
273 GitFacade git = myGitFactory.create(myTargetDirectory);
275 .setPropertyName("url." + localMirrorUrl + ".insteadOf")
279 .setPropertyName("url." + remoteUrl + ".pushInsteadOf")
284 private String getLocalMirrorUrl() throws VcsException {
286 return new URIish(myRoot.getRepositoryDir().toURI().toASCIIString()).toString();
287 } catch (URISyntaxException e) {
288 throw new VcsException("Cannot create uri for local mirror " + myRoot.getRepositoryDir().getAbsolutePath(), e);
292 private String createTmpBranch(@NotNull File repositoryDir, @NotNull String branchStartingPoint) throws VcsException {
293 String tmpBranchName = getUnusedBranchName(repositoryDir);
294 myGitFactory.create(repositoryDir)
296 .setName(tmpBranchName)
297 .setStartPoint(branchStartingPoint)
299 return tmpBranchName;
302 private String getUnusedBranchName(@NotNull File repositoryDir) {
303 final String tmpBranchName = "tmp_branch_for_build";
304 String branchName = tmpBranchName;
305 Map<String, Ref> existingRefs = myGitFactory.create(repositoryDir).showRef().call().getValidRefs();
307 while (existingRefs.containsKey("refs/heads/" + branchName)) {
308 branchName = tmpBranchName + i;
314 private int[] getRetryTimeouts() {
315 String value = myBuild.getSharedConfigParameters().get("teamcity.git.fetchMirrorRetryTimeouts");
316 if (value == null) return new int[]{5, 10, 15, 30}; // total 60 seconds
318 List<String> split = StringUtil.split(value, true, ',');
319 int[] result = new int[split.size()];
320 for (int i = 0; i < result.length; i++) {
323 parsed = Integer.parseInt(split.get(i));
324 } catch (NumberFormatException ignored) {