Пример #1
0
        public SourceControlResult SyncRepository(RepositorySyncInfo repositorySyncInfo, string[] branchMatchers)
        {
            try
            {
                if (IsLocalRepository(repositorySyncInfo.LocalRepositoryDirectory))
                {
                    _logger.Information("{Path} is already a repository. Attempting to update.",
                                        repositorySyncInfo.LocalRepositoryDirectory);
                    return(SyncLocalRepository(repositorySyncInfo.LocalRepositoryDirectory, branchMatchers));
                }

                return(SyncRemoteRepository(repositorySyncInfo, branchMatchers));
            }
            catch (LibGit2SharpException ex)
            {
                // TODO: Fix possible infinite loop here
                if (ex.Message.ToLowerInvariant().Contains("failed to send request"))
                {
                    _logger.Information("{ExMessage}... Retrying", ex.Message);
                    return(SyncRepository(repositorySyncInfo, branchMatchers));
                }

                if (ex.Message.ToLowerInvariant().Contains("unsupported url protocol"))
                {
                    _logger.Information("{ExMessage}... Skipping", ex.Message);
                    return(SourceControlResultFactory.MakeFailure(ex.Message));
                }

                throw;
            }
        }
Пример #2
0
        public SourceControlResult SyncRemoteRepository(RepositorySyncInfo repositorySyncInfo,
                                                        string[] branchMatchers = null)
        {
            branchMatchers = branchMatchers ?? new string[0];
            var transferLastUpdate = DateTime.UtcNow;

            bool OnTransferProgress(TransferProgress progress)
            {
                // Log an update every x seconds
                if (DateTime.UtcNow > transferLastUpdate.AddSeconds(5))
                {
                    transferLastUpdate = DateTime.UtcNow;
                    _logger.Debug(
                        $"Transfer progress {100 * ((double) progress.ReceivedObjects / progress.TotalObjects):0.##}%, Objects: {progress.ReceivedObjects} of {progress.TotalObjects}, Bytes: {progress.ReceivedBytes}");
                }

                return(true);
            }

            var checkoutLastUpdate = DateTime.UtcNow;

            void OnCheckoutProgress(string path, int completedSteps, int totalSteps)
            {
                // Log an update every x seconds
                if (DateTime.UtcNow > checkoutLastUpdate.AddSeconds(5))
                {
                    checkoutLastUpdate = DateTime.UtcNow;
                    _logger.Debug($"Checkout progress {100 * ((double) completedSteps / totalSteps):0.##}%");
                }
            }

            try
            {
                Repository.Clone(repositorySyncInfo.RemoteUrl, repositorySyncInfo.LocalRepositoryDirectory,
                                 new CloneOptions
                {
                    CredentialsProvider = (url, user, cred) => _credentials,
                    OnTransferProgress  = OnTransferProgress,
                    OnCheckoutProgress  = OnCheckoutProgress
                });
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Failed to clone repository. Continuing");

                return(SourceControlResultFactory.MakeFailure(ex.Message));
            }

            return(SyncLocalRepository(repositorySyncInfo.LocalRepositoryDirectory, branchMatchers));
        }