예제 #1
0
        public async Task <string> CloneIfNotExistLocally(string remoteUrl, string localRepository, bool isPrivateRepository)
        {
            if (Directory.Exists(localRepository) && Repository.IsValid(localRepository))
            {
                return(localRepository);
            }

            var cloneOption = new CloneOptions()
            {
                OnTransferProgress = CloneTransferProgressHandler
            };

            if (isPrivateRepository)
            {
                cloneOption.CredentialsProvider = (url, user, cred) => _gitCredential;
            }

            try
            {
                return(await Task.Run(() => Repository.Clone(remoteUrl, localRepository, cloneOption)));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                return("");
            }
        }
        public void Pull(Octokit.Repository rep)
        {
            string reppath = Path.Combine(_account.TempRepPath, rep.Name);

            if (!Repository.IsValid(reppath))
            {
                Repository.Clone(rep.CloneUrl, reppath);
                using (var repo = new Repository(reppath))
                {
                    var upstream = repo.Network.Remotes.Add("upstream", rep.Parent.CloneUrl);
                    Commands.Fetch(repo, "upstream", new List <string>()
                    {
                    }, new FetchOptions(), null);
                    Branch upstreamMaster = repo.Branches["upstream/master"];
                    Branch localMaster    = repo.Branches["master"];
                    repo.Branches.Update(localMaster, b => b.TrackedBranch = upstreamMaster.CanonicalName);
                    var sign = new LibGit2Sharp.Signature(_account.UserName, _account.Email, new DateTimeOffset(DateTime.Now));
                    Commands.Pull(repo, sign, new PullOptions());
                }
            }
            else
            {
                using (var repo = new Repository(reppath))
                {
                    var branchMaster = repo.Branches["master"];

                    Commands.Checkout(repo, branchMaster);
                    var sign = new LibGit2Sharp.Signature(_account.UserName, _account.Email, new DateTimeOffset(DateTime.Now));
                    Commands.Pull(repo, sign, new PullOptions());
                }
            }
        }
예제 #3
0
        public void CreateRepository(string p_DirectoryPath)
        {
            if (!Directory.Exists(p_DirectoryPath))
            {
                s_Logger.Error($"Invalid directory provided. Directory does not exist. Absolute Path: {p_DirectoryPath}");
                throw new InvalidDirectoryException("Invalid directory provided.");
            }

            if (!GitRepository.IsValid(p_DirectoryPath))
            {
                s_Logger.Error($"This is not a valid git repository. Path Provided Repository: {p_DirectoryPath}");
                throw new InvalidRepositoryException("This is not a valid git repository.");
            }

            m_Repository = new GitRepository(p_DirectoryPath);
        }
예제 #4
0
        private string GetLocalRepoFolder(Models.Repository repo, string personalAccessKey)
        {
            var    checkOutFolder = GetLocalFolder(repo);
            string logMessage     = "";

            if (Repository.IsValid(checkOutFolder) == false)
            {
                checkOutFolder = Repository.Clone(repo.CloneUrl, checkOutFolder, getCloneOptions(personalAccessKey));
            }
            else
            {
                using (var tmpRepo = new Repository(checkOutFolder))
                {
                    var remote   = tmpRepo.Network.Remotes["origin"];
                    var refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);

                    Commands.Fetch(tmpRepo, remote.Name, refSpecs, getFetchOptions(personalAccessKey), logMessage);
                }
            }

            return(checkOutFolder);
        }
예제 #5
0
        /// <summary>
        /// Ensure that the repository exists on disk and its origin remote points to the correct url
        /// </summary>
        /// <param name="repo"></param>
        private void EnsureRepository(List <RepositoryInfo> repos)
        {
            foreach (var repo in repos)
            {
                var repoPath = repo.Path;
                s_logger.Info($"Verifying repository {repo} at {repo.Path}");
                if (!Directory.Exists(repoPath) || !Repository.IsValid(repoPath))
                {
                    if (Directory.Exists(repoPath))
                    {
                        Directory.Delete(repoPath, true);
                    }

                    s_logger.Info($"Cloning the repo from {repo.CloneUrl}");
                    Repository.Clone(repo.CloneUrl, repoPath);
                }
                using (var repository = new Repository(repoPath))
                {
                    var remote = repository.Network.Remotes["origin"];
                    if (remote == null)
                    {
                        repository.Network.Remotes.Add("origin", repo.CloneUrl);
                    }
                    else if (remote.Url != repo.CloneUrl)
                    {
                        repository.Network.Remotes.Update("origin", u => u.Url = repo.CloneUrl);
                    }
                    var master = repository.Branches["master"] ?? repository.CreateBranch("master");
                    repository.Branches.Update(master, b => b.Remote = "origin", b => b.UpstreamBranch = "refs/heads/master");

                    remote = repository.Network.Remotes["upstream"];
                    if (remote == null)
                    {
                        repository.Network.Remotes.Add("upstream", @"https://github.com/" + repo.UpstreamOwner + @"/" + repo.Name + ".git");
                    }
                }
            }
        }