2 * Copyright 2000-2014 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.Trinity;
20 import jetbrains.buildServer.agent.AgentRunningBuild;
21 import jetbrains.buildServer.agent.BuildDirectoryCleanerCallback;
22 import jetbrains.buildServer.agent.BuildProgressLogger;
23 import jetbrains.buildServer.agent.SmartDirectoryCleaner;
24 import jetbrains.buildServer.buildTriggers.vcs.git.*;
25 import jetbrains.buildServer.buildTriggers.vcs.git.agent.command.*;
26 import jetbrains.buildServer.buildTriggers.vcs.git.agent.command.impl.CommandUtil;
27 import jetbrains.buildServer.buildTriggers.vcs.git.agent.command.impl.RefImpl;
28 import jetbrains.buildServer.buildTriggers.vcs.git.agent.errors.GitExecTimeout;
29 import jetbrains.buildServer.buildTriggers.vcs.git.agent.errors.GitIndexCorruptedException;
30 import jetbrains.buildServer.buildTriggers.vcs.git.agent.errors.GitOutdatedIndexException;
31 import jetbrains.buildServer.log.Loggers;
32 import jetbrains.buildServer.util.FileUtil;
33 import jetbrains.buildServer.vcs.*;
34 import org.apache.log4j.Logger;
35 import org.eclipse.jgit.errors.ConfigInvalidException;
36 import org.eclipse.jgit.lib.*;
37 import org.eclipse.jgit.lib.Constants;
38 import org.eclipse.jgit.transport.URIish;
39 import org.jetbrains.annotations.NotNull;
40 import org.jetbrains.annotations.Nullable;
43 import java.io.FileFilter;
44 import java.io.IOException;
45 import java.net.URISyntaxException;
46 import java.util.Collection;
47 import java.util.HashSet;
50 import java.util.regex.Matcher;
52 import static com.intellij.openapi.util.text.StringUtil.isEmpty;
53 import static jetbrains.buildServer.buildTriggers.vcs.git.GitUtils.*;
55 public class UpdaterImpl implements Updater {
57 private final static Logger LOG = Logger.getLogger(UpdaterImpl.class);
58 /** Git version which supports --progress option in the fetch command */
59 private final static GitVersion GIT_WITH_PROGRESS_VERSION = new GitVersion(1, 7, 1, 0);
60 //--force option in git submodule update introduced in 1.7.6
61 private final static GitVersion GIT_WITH_FORCE_SUBMODULE_UPDATE = new GitVersion(1, 7, 6);
62 public final static GitVersion GIT_WITH_SPARSE_CHECKOUT = new GitVersion(1, 7, 4);
63 public final static GitVersion BROKEN_SPARSE_CHECKOUT = new GitVersion(2, 7, 0);
65 * Git version supporting an empty credential helper - the only way to disable system/global/local cred helper
67 public final static GitVersion EMPTY_CRED_HELPER = new GitVersion(2, 9, 0);
68 private static final int SILENT_TIMEOUT = 24 * 60 * 60; //24 hours
70 protected final FS myFS;
71 private final SmartDirectoryCleaner myDirectoryCleaner;
72 protected final BuildProgressLogger myLogger;
73 protected final AgentPluginConfig myPluginConfig;
74 protected final GitFactory myGitFactory;
75 protected final File myTargetDirectory;
76 protected final String myRevision;
77 protected final AgentGitVcsRoot myRoot;
78 protected final String myFullBranchName;
79 protected final AgentRunningBuild myBuild;
80 private final CheckoutRules myRules;
81 private final CheckoutMode myCheckoutMode;
82 protected final MirrorManager myMirrorManager;
84 public UpdaterImpl(@NotNull FS fs,
85 @NotNull AgentPluginConfig pluginConfig,
86 @NotNull MirrorManager mirrorManager,
87 @NotNull SmartDirectoryCleaner directoryCleaner,
88 @NotNull GitFactory gitFactory,
89 @NotNull AgentRunningBuild build,
90 @NotNull VcsRoot root,
91 @NotNull String version,
92 @NotNull File targetDir,
93 @NotNull CheckoutRules rules,
94 @NotNull CheckoutMode checkoutMode) throws VcsException {
96 myPluginConfig = pluginConfig;
97 myDirectoryCleaner = directoryCleaner;
98 myGitFactory = gitFactory;
100 myLogger = build.getBuildLogger();
101 myRevision = GitUtils.versionRevision(version);
102 myTargetDirectory = targetDir;
103 myRoot = new AgentGitVcsRoot(mirrorManager, myTargetDirectory, root);
104 myFullBranchName = getBranch();
106 myCheckoutMode = checkoutMode;
107 myMirrorManager = mirrorManager;
111 private String getBranch() {
112 String defaultBranchName = GitUtils.expandRef(myRoot.getRef());
113 String rootBranchParam = GitUtils.getGitRootBranchParamName(myRoot.getOriginalRoot());
114 String customBranch = myBuild.getSharedConfigParameters().get(rootBranchParam);
115 return customBranch != null ? customBranch : defaultBranchName;
119 public void update() throws VcsException {
120 myLogger.message("Git version: " + myPluginConfig.getGitVersion());
121 checkAuthMethodIsSupported();
125 protected void doUpdate() throws VcsException {
128 removeRefLocks(new File(myTargetDirectory, ".git"));
133 private void logStartUpdating() {
134 LOG.info("Starting update of root " + myRoot.getName() + " in " + myTargetDirectory + " to revision " + myRevision);
135 LOG.debug("Updating " + myRoot.debugInfo());
140 * Init .git in the target dir
141 * @return true if there was no fetch in the target dir before
142 * @throws VcsException in teh case of any problems
144 private boolean initGitRepository() throws VcsException {
145 boolean firstFetch = false;
146 if (!new File(myTargetDirectory, ".git").exists()) {
147 initDirectory(false);
150 String remoteUrl = getRemoteUrl();
151 if (!remoteUrl.equals(myRoot.getRepositoryFetchURL().toString())) {
156 setupExistingRepository();
157 configureSparseCheckout();
158 } catch (Exception e) {
159 LOG.warn("Do clean checkout due to errors while configure use of local mirrors", e);
168 protected void setupNewRepository() throws VcsException {
172 protected void setupExistingRepository() throws VcsException {
178 private void updateSources() throws VcsException {
179 final GitFacade git = myGitFactory.create(myTargetDirectory);
180 boolean branchChanged = false;
182 if (isRegularBranch(myFullBranchName)) {
183 String branchName = getShortBranchName(myFullBranchName);
184 Branches branches = git.listBranches();
185 if (branches.isCurrentBranch(branchName)) {
187 runAndFixIndexErrors(git, new VcsCommand() {
189 public void call() throws VcsException {
190 reset(git).setHard(true).setRevision(myRevision).call();
193 git.setUpstream(branchName, GitUtils.createRemoteRef(myFullBranchName)).call();
195 branchChanged = true;
196 if (!branches.contains(branchName)) {
199 .setStartPoint(GitUtils.createRemoteRef(myFullBranchName))
203 git.updateRef().setRef(myFullBranchName).setRevision(myRevision).call();
204 final String finalBranchName = branchName;
205 runAndFixIndexErrors(git, new VcsCommand() {
207 public void call() throws VcsException {
208 checkout(git).setForce(true).setBranch(finalBranchName).setTimeout(myPluginConfig.getCheckoutIdleTimeoutSeconds()).call();
211 if (branches.contains(branchName)) {
212 git.setUpstream(branchName, GitUtils.createRemoteRef(myFullBranchName)).call();
215 } else if (isTag(myFullBranchName)) {
216 final String shortName = myFullBranchName.substring("refs/tags/".length());
217 runAndFixIndexErrors(git, new VcsCommand() {
219 public void call() throws VcsException {
220 checkout(git).setForce(true).setBranch(shortName).setTimeout(myPluginConfig.getCheckoutIdleTimeoutSeconds()).call();
223 Ref tag = getRef(myTargetDirectory, myFullBranchName);
224 if (tag != null && !tag.getObjectId().name().equals(myRevision)) {
225 runAndFixIndexErrors(git, new VcsCommand() {
227 public void call() throws VcsException {
228 checkout(git).setBranch(myRevision).setForce(true).setTimeout(myPluginConfig.getCheckoutIdleTimeoutSeconds()).call();
232 branchChanged = true;
234 runAndFixIndexErrors(git, new VcsCommand() {
236 public void call() throws VcsException {
237 checkout(git).setForce(true).setBranch(myRevision).setTimeout(myPluginConfig.getCheckoutIdleTimeoutSeconds()).call();
240 branchChanged = true;
243 doClean(branchChanged);
244 if (myRoot.isCheckoutSubmodules()) {
245 checkoutSubmodules(myTargetDirectory);
250 private void runAndFixIndexErrors(@NotNull GitFacade git, @NotNull VcsCommand cmd) throws VcsException {
253 } catch (GitIndexCorruptedException e) {
254 File gitIndex = e.getGitIndex();
255 myLogger.message("Git index '" + gitIndex.getAbsolutePath() + "' is corrupted, remove it and repeat the command");
256 FileUtil.delete(gitIndex);
258 } catch (GitOutdatedIndexException e) {
259 myLogger.message("Refresh outdated git index and repeat the command");
260 updateIndex(git).reallyRefresh(true).quiet(true).call();
262 } catch (Exception e) {
263 if (e instanceof VcsException)
264 throw (VcsException) e;
265 throw new VcsException(e);
271 private UpdateIndexCommand updateIndex(final GitFacade git) {
272 UpdateIndexCommand result = git.updateIndex()
273 .setAuthSettings(myRoot.getAuthSettings())
274 .setUseNativeSsh(myPluginConfig.isUseNativeSSH());
275 configureLFS(result);
281 private ResetCommand reset(final GitFacade git) {
282 ResetCommand result = git.reset()
283 .setAuthSettings(myRoot.getAuthSettings())
284 .setUseNativeSsh(myPluginConfig.isUseNativeSSH());
285 configureLFS(result);
290 private CheckoutCommand checkout(final GitFacade git) {
291 CheckoutCommand result = git.checkout()
292 .setAuthSettings(myRoot.getAuthSettings())
293 .setUseNativeSsh(myPluginConfig.isUseNativeSSH());
294 configureLFS(result);
298 private void checkoutSubmodules(@NotNull final File repositoryDir) throws VcsException {
299 File dotGitModules = new File(repositoryDir, ".gitmodules");
301 Config gitModules = readGitModules(dotGitModules);
302 if (gitModules == null)
305 myLogger.message("Checkout submodules in " + repositoryDir);
306 GitFacade git = myGitFactory.create(repositoryDir);
307 git.submoduleInit().call();
308 git.submoduleSync().call();
310 addSubmoduleUsernames(repositoryDir, gitModules);
312 long start = System.currentTimeMillis();
313 SubmoduleUpdateCommand submoduleUpdate = git.submoduleUpdate()
314 .setAuthSettings(myRoot.getAuthSettings())
315 .setUseNativeSsh(myPluginConfig.isUseNativeSSH())
316 .setTimeout(SILENT_TIMEOUT)
317 .setForce(isForceUpdateSupported());
318 configureLFS(submoduleUpdate);
319 submoduleUpdate.call();
321 if (recursiveSubmoduleCheckout()) {
322 for (String submodulePath : getSubmodulePaths(gitModules)) {
323 checkoutSubmodules(new File(repositoryDir, submodulePath));
326 Loggers.VCS.info("Submodules update in " + repositoryDir.getAbsolutePath() + " is finished in " +
327 (System.currentTimeMillis() - start) + " ms");
329 } catch (IOException e) {
330 Loggers.VCS.error("Submodules checkout failed", e);
331 throw new VcsException("Submodules checkout failed", e);
332 } catch (ConfigInvalidException e) {
333 Loggers.VCS.error("Submodules checkout failed", e);
334 throw new VcsException("Submodules checkout failed", e);
339 private boolean isForceUpdateSupported() {
340 return !GIT_WITH_FORCE_SUBMODULE_UPDATE.isGreaterThan(myPluginConfig.getGitVersion());
344 private void addSubmoduleUsernames(@NotNull File repositoryDir, @NotNull Config gitModules)
345 throws IOException, ConfigInvalidException, VcsException {
346 if (!myPluginConfig.isUseMainRepoUserForSubmodules())
349 Loggers.VCS.info("Update submodules credentials");
351 AuthSettings auth = myRoot.getAuthSettings();
352 final String userName = auth.getUserName();
353 if (userName == null) {
354 Loggers.VCS.info("Username is not specified in the main VCS root settings, skip updating credentials");
358 Repository r = new RepositoryBuilder().setBare().setGitDir(getGitDir(repositoryDir)).build();
359 StoredConfig gitConfig = r.getConfig();
361 Set<String> submodules = gitModules.getSubsections("submodule");
362 if (submodules.isEmpty()) {
363 Loggers.VCS.info("No submodule sections found in " + new File(repositoryDir, ".gitmodules").getCanonicalPath()
364 + ", skip updating credentials");
367 File modulesDir = new File(r.getDirectory(), Constants.MODULES);
368 for (String submoduleName : submodules) {
369 String url = gitModules.getString("submodule", submoduleName, "url");
370 Loggers.VCS.info("Update credentials for submodule with url " + url);
371 if (url == null || !isRequireAuth(url)) {
372 Loggers.VCS.info("Url " + url + " does not require authentication, skip updating credentials");
376 URIish uri = new URIish(url);
377 String updatedUrl = uri.setUser(userName).toASCIIString();
378 gitConfig.setString("submodule", submoduleName, "url", updatedUrl);
379 String submodulePath = gitModules.getString("submodule", submoduleName, "path");
380 if (submodulePath != null && myPluginConfig.isUpdateSubmoduleOriginUrl()) {
381 File submoduleDir = new File(modulesDir, submodulePath);
382 if (submoduleDir.isDirectory() && new File(submoduleDir, Constants.CONFIG).isFile())
383 updateOriginUrl(submoduleDir, updatedUrl);
385 Loggers.VCS.debug("Submodule url " + url + " changed to " + updatedUrl);
386 } catch (URISyntaxException e) {
387 Loggers.VCS.warn("Error while parsing an url " + url + ", skip updating submodule credentials", e);
388 } catch (Exception e) {
389 Loggers.VCS.warn("Error while updating the '" + submoduleName + "' submodule url", e);
395 private void updateOriginUrl(@NotNull File repoDir, @NotNull String url) throws IOException {
396 Repository r = new RepositoryBuilder().setBare().setGitDir(repoDir).build();
397 StoredConfig config = r.getConfig();
398 config.setString("remote", "origin", "url", url);
404 private Config readGitModules(@NotNull File dotGitModules) throws IOException, ConfigInvalidException {
405 if (!dotGitModules.exists())
407 String content = FileUtil.readText(dotGitModules);
408 Config config = new Config();
409 config.fromText(content);
414 private boolean isRequireAuth(@NotNull String url) {
416 URIish uri = new URIish(url);
417 String scheme = uri.getScheme();
418 if (scheme == null || "git".equals(scheme)) //no auth for anonymous protocol and for local repositories
420 String user = uri.getUser();
421 if (user != null) //respect a user specified in config
424 } catch (URISyntaxException e) {
430 private Set<String> getSubmodulePaths(@NotNull Config config) {
431 Set<String> paths = new HashSet<String>();
432 Set<String> submodules = config.getSubsections("submodule");
433 for (String submoduleName : submodules) {
434 String submodulePath = config.getString("submodule", submoduleName, "path");
435 paths.add(submodulePath.replaceAll("/", Matcher.quoteReplacement(File.separator)));
440 private boolean recursiveSubmoduleCheckout() {
441 return SubmodulesCheckoutPolicy.CHECKOUT.equals(myRoot.getSubmodulesCheckoutPolicy()) ||
442 SubmodulesCheckoutPolicy.CHECKOUT_IGNORING_ERRORS.equals(myRoot.getSubmodulesCheckoutPolicy());
446 private void doClean(boolean branchChanged) throws VcsException {
447 if (myRoot.getCleanPolicy() == AgentCleanPolicy.ALWAYS ||
448 branchChanged && myRoot.getCleanPolicy() == AgentCleanPolicy.ON_BRANCH_CHANGE) {
449 myLogger.message("Cleaning " + myRoot.getName() + " in " + myTargetDirectory + " the file set " + myRoot.getCleanFilesPolicy());
450 myGitFactory.create(myTargetDirectory).clean().setCleanPolicy(myRoot.getCleanFilesPolicy()).call();
452 if (myRoot.isCheckoutSubmodules())
453 cleanSubmodules(myTargetDirectory);
458 private void cleanSubmodules(@NotNull File repositoryDir) throws VcsException {
459 File dotGitModules = new File(repositoryDir, ".gitmodules");
462 gitModules = readGitModules(dotGitModules);
463 } catch (Exception e) {
464 Loggers.VCS.error("Error while reading " + dotGitModules.getAbsolutePath() + ": " + e.getMessage());
465 throw new VcsException("Error while reading " + dotGitModules.getAbsolutePath(), e);
468 if (gitModules == null)
471 for (String submodulePath : getSubmodulePaths(gitModules)) {
472 File submoduleDir = new File(repositoryDir, submodulePath);
474 myLogger.message("Cleaning files in " + submoduleDir + " the file set " + myRoot.getCleanFilesPolicy());
475 myGitFactory.create(submoduleDir).clean().setCleanPolicy(myRoot.getCleanFilesPolicy()).call();
476 } catch (Exception e) {
477 Loggers.VCS.error("Error while cleaning files in " + submoduleDir.getAbsolutePath(), e);
479 if (recursiveSubmoduleCheckout())
480 cleanSubmodules(submoduleDir);
485 protected void removeUrlSections() throws VcsException {
488 r = new RepositoryBuilder().setWorkTree(myTargetDirectory).build();
489 StoredConfig config = r.getConfig();
490 Set<String> urlSubsections = config.getSubsections("url");
491 for (String subsection : urlSubsections) {
492 config.unsetSection("url", subsection);
495 } catch (IOException e) {
496 String msg = "Error while remove url.* sections";
498 throw new VcsException(msg, e);
506 protected void disableAlternates() {
507 FileUtil.delete(new File(myTargetDirectory, ".git" + File.separator + "objects" + File.separator + "info" + File.separator + "alternates"));
511 private String getRemoteUrl() {
513 return myGitFactory.create(myTargetDirectory).getConfig().setPropertyName("remote.origin.url").call();
514 } catch (VcsException e) {
515 LOG.debug("Failed to read property", e);
522 protected Ref getRef(@NotNull File repositoryDir, @NotNull String ref) {
523 Map<String, Ref> refs = myGitFactory.create(repositoryDir).showRef().setPattern(ref).call().getValidRefs();
524 return refs.isEmpty() ? null : refs.get(ref);
529 * If some git process crashed in this repository earlier it can leave lock files for index.
530 * This method delete such lock file if it exists (with warning message), otherwise git operation will fail.
532 private void removeIndexLock() {
533 File indexLock = new File(myTargetDirectory, ".git" + File.separator + "index.lock");
534 if (indexLock.exists()) {
535 myLogger.warning("The .git/index.lock file exists. This probably means a git process crashed in this repository earlier. Deleting lock file");
536 FileUtil.delete(indexLock);
541 private void doFetch() throws VcsException {
542 boolean outdatedRefsFound = removeOutdatedRefs(myTargetDirectory);
543 ensureCommitLoaded(outdatedRefsFound);
547 protected void ensureCommitLoaded(boolean fetchRequired) throws VcsException {
548 fetchFromOriginalRepository(fetchRequired);
552 protected void fetchFromOriginalRepository(boolean fetchRequired) throws VcsException {
553 if (myPluginConfig.isFetchAllHeads()) {
554 String msg = getForcedHeadsFetchMessage();
556 myLogger.message(msg);
559 if (!myFullBranchName.startsWith("refs/heads/")) {
560 Ref remoteRef = getRef(myTargetDirectory, GitUtils.createRemoteRef(myFullBranchName));
561 if (!fetchRequired && remoteRef != null && myRevision.equals(remoteRef.getObjectId().name()) && hasRevision(myTargetDirectory, myRevision))
565 Ref remoteRef = getRef(myTargetDirectory, GitUtils.createRemoteRef(myFullBranchName));
566 if (!fetchRequired && remoteRef != null && myRevision.equals(remoteRef.getObjectId().name()) && hasRevision(myTargetDirectory, myRevision))
568 myLogger.message("Commit '" + myRevision + "' is not found in local clone. Running 'git fetch'...");
569 fetchDefaultBranch();
570 if (hasRevision(myTargetDirectory, myRevision))
572 myLogger.message("Commit still not found after fetching main branch. Fetching more branches.");
575 if (hasRevision(myTargetDirectory, myRevision))
577 throw new VcsException("Cannot find commit " + myRevision);
581 protected String getForcedHeadsFetchMessage() {
582 return "Forced fetch of all heads (" + PluginConfigImpl.FETCH_ALL_HEADS + "=" + myBuild.getSharedConfigParameters().get(PluginConfigImpl.FETCH_ALL_HEADS) + ")";
586 private void fetchDefaultBranch() throws VcsException {
587 fetch(myTargetDirectory, getRefspecForFetch(), false);
590 private String getRefspecForFetch() {
591 return "+" + myFullBranchName + ":" + GitUtils.createRemoteRef(myFullBranchName);
594 private void fetchAllBranches() throws VcsException {
595 fetch(myTargetDirectory, "+refs/heads/*:refs/remotes/origin/*", false);
598 protected boolean hasRevision(@NotNull File repositoryDir, @NotNull String revision) {
599 return getRevision(repositoryDir, revision) != null;
602 private String getRevision(@NotNull File repositoryDir, @NotNull String revision) {
603 return myGitFactory.create(repositoryDir).log()
605 .setPrettyFormat("%H%x20%s")
606 .setStartPoint(revision)
610 protected void fetch(@NotNull File repositoryDir, @NotNull String refspec, boolean shallowClone) throws VcsException {
611 boolean silent = isSilentFetch();
612 int timeout = getTimeout(silent);
615 getFetch(repositoryDir, refspec, shallowClone, silent, timeout).call();
616 } catch (GitIndexCorruptedException e) {
617 File gitIndex = e.getGitIndex();
618 myLogger.message("Git index '" + gitIndex.getAbsolutePath() + "' is corrupted, remove it and repeat git fetch");
619 FileUtil.delete(gitIndex);
620 getFetch(repositoryDir, refspec, shallowClone, silent, timeout).call();
621 } catch (GitExecTimeout e) {
623 myLogger.error("No output from git during " + timeout + " seconds. Try increasing idle timeout by setting parameter '"
624 + PluginConfigImpl.IDLE_TIMEOUT +
625 "' either in build or in agent configuration.");
632 private FetchCommand getFetch(@NotNull File repositoryDir, @NotNull String refspec, boolean shallowClone, boolean silent, int timeout) {
633 FetchCommand result = myGitFactory.create(repositoryDir).fetch()
634 .setAuthSettings(myRoot.getAuthSettings())
635 .setUseNativeSsh(myPluginConfig.isUseNativeSSH())
637 .setRefspec(refspec);
640 result.setQuite(true);
642 result.setShowProgress(true);
650 protected void removeRefLocks(@NotNull File dotGit) {
651 File refs = new File(dotGit, "refs");
652 if (!refs.isDirectory())
654 Collection<File> locks = FileUtil.findFiles(new FileFilter() {
655 public boolean accept(File f) {
656 return f.isFile() && f.getName().endsWith(".lock");
659 for (File lock : locks) {
660 LOG.info("Remove a lock file " + lock.getAbsolutePath());
661 FileUtil.delete(lock);
665 private boolean isSilentFetch() {
666 GitVersion version = myPluginConfig.getGitVersion();
667 return version.isLessThan(GIT_WITH_PROGRESS_VERSION);
670 private int getTimeout(boolean silentFetch) {
672 return SILENT_TIMEOUT;
674 return myPluginConfig.getIdleTimeoutSeconds();
678 private void checkAuthMethodIsSupported() throws VcsException {
679 checkAuthMethodIsSupported(myRoot, myPluginConfig);
683 static void checkAuthMethodIsSupported(@NotNull GitVcsRoot root, @NotNull AgentPluginConfig config) throws VcsException {
684 if ("git".equals(root.getRepositoryFetchURL().getScheme()))
685 return;//anonymous protocol, don't check anything
686 AuthSettings authSettings = root.getAuthSettings();
687 switch (authSettings.getAuthMethod()) {
689 if ("http".equals(root.getRepositoryFetchURL().getScheme()) ||
690 "https".equals(root.getRepositoryFetchURL().getScheme())) {
691 GitVersion actualVersion = config.getGitVersion();
692 GitVersion requiredVersion = getMinVersionForHttpAuth();
693 if (actualVersion.isLessThan(requiredVersion)) {
694 throw new VcsException("Password authentication requires git " + requiredVersion +
695 ", found git version is " + actualVersion +
696 ". Upgrade git or use different authentication method.");
699 throw new VcsException("TeamCity doesn't support authentication method '" +
700 root.getAuthSettings().getAuthMethod().uiName() +
701 "' with agent checkout and non-http protocols. Please use different authentication method.");
704 case PRIVATE_KEY_FILE:
705 throw new VcsException("TeamCity doesn't support authentication method '" +
706 root.getAuthSettings().getAuthMethod().uiName() +
707 "' with agent checkout. Please use different authentication method.");
712 private static GitVersion getMinVersionForHttpAuth() {
713 //core.askpass parameter was added in 1.7.1, but
714 //experiments show that it works only in 1.7.3 on linux
715 //and msysgit 1.7.3.1-preview20101002.
716 return new GitVersion(1, 7, 3);
720 * Clean and init directory and configure remote origin
722 * @throws VcsException if there are problems with initializing the directory
724 private void initDirectory(boolean removeTargetDir) throws VcsException {
725 if (removeTargetDir) {
726 BuildDirectoryCleanerCallback c = new BuildDirectoryCleanerCallback(myLogger, LOG);
727 myDirectoryCleaner.cleanFolder(myTargetDirectory, c);
728 //noinspection ResultOfMethodCallIgnored
729 if (c.isHasErrors()) {
730 throw new VcsException("Unable to clean directory " + myTargetDirectory + " for VCS root " + myRoot.getName());
734 myTargetDirectory.mkdirs();
735 myLogger.message("The .git directory is missing in '" + myTargetDirectory + "'. Running 'git init'...");
736 myGitFactory.create(myTargetDirectory).init().call();
738 myGitFactory.create(myRoot.getLocalRepositoryDir())
741 .setUrl(myRoot.getRepositoryFetchURL().toString())
743 URIish url = myRoot.getRepositoryPushURL();
744 String pushUrl = url == null ? null : url.toString();
745 if (pushUrl != null && !pushUrl.equals(myRoot.getRepositoryFetchURL().toString())) {
746 myGitFactory.create(myTargetDirectory).setConfig().setPropertyName("remote.origin.pushurl").setValue(pushUrl).call();
748 setupNewRepository();
749 configureSparseCheckout();
752 private void configureSparseCheckout() throws VcsException {
753 if (myCheckoutMode == CheckoutMode.SPARSE_CHECKOUT) {
754 setupSparseCheckout();
756 myGitFactory.create(myTargetDirectory).setConfig().setPropertyName("core.sparseCheckout").setValue("false").call();
760 private void setupSparseCheckout() throws VcsException {
761 myGitFactory.create(myTargetDirectory).setConfig().setPropertyName("core.sparseCheckout").setValue("true").call();
762 File sparseCheckout = new File(myTargetDirectory, ".git/info/sparse-checkout");
763 boolean hasIncludeRules = false;
764 StringBuilder sparseCheckoutContent = new StringBuilder();
765 for (IncludeRule rule : myRules.getIncludeRules()) {
766 if (isEmpty(rule.getFrom())) {
767 sparseCheckoutContent.append("/*\n");
769 sparseCheckoutContent.append("/").append(rule.getFrom()).append("\n");
771 hasIncludeRules = true;
773 if (!hasIncludeRules) {
774 sparseCheckoutContent.append("/*\n");
776 for (FileRule rule : myRules.getExcludeRules()) {
777 sparseCheckoutContent.append("!/").append(rule.getFrom()).append("\n");
780 FileUtil.writeFileAndReportErrors(sparseCheckout, sparseCheckoutContent.toString());
781 } catch (IOException e) {
782 LOG.warn("Error while writing sparse checkout config, disable sparse checkout", e);
783 myGitFactory.create(myTargetDirectory).setConfig().setPropertyName("core.sparseCheckout").setValue("false").call();
788 private void validateUrls() {
789 URIish fetch = myRoot.getRepositoryFetchURL();
790 if (isAnonymousGitWithUsername(fetch))
791 LOG.warn("Fetch URL '" + fetch.toString() + "' for root " + myRoot.getName() + " uses an anonymous git protocol and contains a username, fetch will probably fail");
792 URIish push = myRoot.getRepositoryPushURL();
793 if (!fetch.equals(push) && isAnonymousGitWithUsername(push))
794 LOG.warn("Push URL '" + push.toString() + "'for root " + myRoot.getName() + " uses an anonymous git protocol and contains a username, push will probably fail");
798 protected boolean removeOutdatedRefs(@NotNull File workingDir) throws VcsException {
799 boolean outdatedRefsRemoved = false;
800 GitFacade git = myGitFactory.create(workingDir);
801 ShowRefResult showRefResult = git.showRef().call();
802 Refs localRefs = new Refs(showRefResult.getValidRefs());
803 if (localRefs.isEmpty() && showRefResult.getInvalidRefs().isEmpty())
805 for (String invalidRef : showRefResult.getInvalidRefs()) {
806 git.updateRef().setRef(invalidRef).delete().call();
807 outdatedRefsRemoved = true;
809 final Refs remoteRefs;
811 remoteRefs = new Refs(git.lsRemote().setAuthSettings(myRoot.getAuthSettings())
812 .setUseNativeSsh(myPluginConfig.isUseNativeSSH())
814 } catch (VcsException e) {
815 if (CommandUtil.isCanceledError(e))
817 String msg = "Failed to list remote repository refs, outdated local refs will not be cleaned";
819 myLogger.warning(msg);
822 //We remove both outdated local refs (e.g. refs/heads/topic) and outdated remote
823 //tracking branches (refs/remote/origin/topic), while git remote origin prune
824 //removes only the latter. We need that because in some cases git cannot handle
825 //rename of the branch (TW-28735).
826 for (Ref localRef : localRefs.list()) {
827 Ref correspondingRemoteRef = createCorrespondingRemoteRef(localRef);
828 if (remoteRefs.isOutdated(correspondingRemoteRef)) {
829 git.updateRef().setRef(localRef.getName()).delete().call();
830 outdatedRefsRemoved = true;
833 return outdatedRefsRemoved;
836 private boolean isRemoteTrackingBranch(@NotNull Ref localRef) {
837 return localRef.getName().startsWith("refs/remotes/origin");
841 private Ref createCorrespondingRemoteRef(@NotNull Ref localRef) {
842 if (!isRemoteTrackingBranch(localRef))
844 return new RefImpl("refs/heads" + localRef.getName().substring("refs/remotes/origin".length()),
845 localRef.getObjectId().name());
849 private void configureLFS(@NotNull BaseCommand command) {
850 Trinity<String, String, String> lfsAuth = getLfsAuth();
853 File credentialsHelper = null;
855 ScriptGen scriptGen = myGitFactory.create(new File(".")).getScriptGen();
856 final File credHelper = scriptGen.generateCredentialsHelper();
857 credentialsHelper = credHelper;
858 if (!myPluginConfig.getGitVersion().isLessThan(UpdaterImpl.EMPTY_CRED_HELPER)) {
859 //Specify an empty helper if it is supported in order to disable
860 //helpers in system-global-local chain. If empty helper is not supported,
861 //then the only workaround is to disable helpers manually in config files.
862 command.addConfig("credential.helper", "");
864 String path = credHelper.getCanonicalPath();
865 path = path.replaceAll("\\\\", "/");
866 command.addConfig("credential.helper", path);
867 CredentialsHelperConfig config = new CredentialsHelperConfig();
868 config.addCredentials(lfsAuth.first, lfsAuth.second, lfsAuth.third);
869 config.setMatchAllUrls(myPluginConfig.isCredHelperMatchesAllUrls());
870 for (Map.Entry<String, String> e : config.getEnv().entrySet()) {
871 command.setEnv(e.getKey(), e.getValue());
873 command.addPostAction(new Runnable() {
876 FileUtil.delete(credHelper);
879 } catch (Exception e) {
880 if (credentialsHelper != null)
881 FileUtil.delete(credentialsHelper);
886 //returns (url, name, pass) for lfs or null if no authentication is required or
887 //root doesn't use http(s)
889 private Trinity<String, String, String> getLfsAuth() {
891 URIish uri = new URIish(myRoot.getRepositoryFetchURL().toString());
892 String scheme = uri.getScheme();
893 if (myRoot.getAuthSettings().getAuthMethod() == AuthenticationMethod.PASSWORD &&
894 ("http".equals(scheme) || "https".equals(scheme))) {
895 String lfsUrl = uri.setPass("").setUser("").toASCIIString();
896 if (lfsUrl.endsWith(".git")) {
897 lfsUrl += "/info/lfs";
899 lfsUrl += lfsUrl.endsWith("/") ? ".git/info/lfs" : "/.git/info/lfs";
901 return Trinity.create(lfsUrl, myRoot.getAuthSettings().getUserName(), myRoot.getAuthSettings().getPassword());
903 } catch (Exception e) {
904 LOG.debug("Cannot get lfs auth config", e);
910 private interface VcsCommand {
911 void call() throws VcsException;