Example #1
0
        public void FetchWithoutConflict(string remoteUrl, string branchName)
        {
            var tracer = _tracerFactory.GetTracer();

            try
            {
                using (var repo = new LibGit2Sharp.Repository(RepositoryPath))
                {
                    var trackedBranchName = string.Format("{0}/{1}", _remoteAlias, branchName);
                    var refSpec           = string.Format("+refs/heads/{0}:refs/remotes/{1}", branchName, trackedBranchName);

                    // Configure the remote
                    var remote = repo.Network.Remotes.Add(_remoteAlias, remoteUrl, refSpec);

                    // This will only retrieve the "master"
                    repo.Network.Fetch(remote);

                    // Optionally set up the branch tracking configuration
                    var trackedBranch = repo.Branches[trackedBranchName];
                    if (trackedBranch == null)
                    {
                        throw new BranchNotFoundException(branchName, null);
                    }
                    var branch = repo.Branches[branchName] ?? repo.CreateBranch(branchName, trackedBranch.Tip);
                    repo.Branches.Update(branch,
                                         b => b.TrackedBranch = trackedBranch.CanonicalName);

                    //Update the raw ref to point the head of branchName to the latest fetched branch
                    UpdateRawRef(string.Format("refs/heads/{0}", branchName), trackedBranchName);

                    // Now checkout out our branch, which points to the right place
                    Update(branchName);
                }
            }
            catch (LibGit2SharpException exception)
            {
                tracer.TraceWarning("LibGit2SharpRepository fetch failed with {0}", exception);

                // LibGit2Sharp doesn't support SSH yet. Use GitExeRepository
                // LibGit2Sharp only supports smart Http protocol
                if (exception.Message.Equals("Unsupported URL protocol") ||
                    exception.Message.Equals("Received unexpected content-type"))
                {
                    tracer.TraceWarning("LibGit2SharpRepository fallback to git.exe");

                    _legacyGitExeRepository.FetchWithoutConflict(remoteUrl, branchName);
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                using (var repo = new LibGit2Sharp.Repository(RepositoryPath))
                {
                    repo.Network.Remotes.Remove(_remoteAlias);
                }
            }
        }
Example #2
0
        public void FetchWithoutConflictOnGitEmptyRepo()
        {
            using (TestRepository testRepository = GetRepository())
            {
                // Arrange
                var gitRepo = new GitExeRepository(testRepository.Environment, new MockDeploymentSettingsManager(), NullTracerFactory.Instance);

                // Act
                gitRepo.Initialize();
                Assert.Throws<BranchNotFoundException>(() => gitRepo.FetchWithoutConflict("https://github.com/KuduApps/EmptyGitRepo.git", "master"));
            }
        }
Example #3
0
        public void FetchWithoutConflictOnEmptyRepoReturnsFalse()
        {
            using (TestRepository testRepository = GetRepository())
            {
                // Arrange
                var gitRepo = new GitExeRepository(testRepository.PhysicalPath, "", new MockDeploymentSettingsManager(), NullTracerFactory.Instance);

                // Act
                gitRepo.Initialize();
                var ex = Assert.Throws<InvalidOperationException>(() => gitRepo.FetchWithoutConflict("https://github.com/KuduApps/EmptyGitRepo.git", "master"));

                // Assert
                Assert.Equal("Could not fetch remote branch 'master'. Verify that the branch exists in the repository.", ex.Message);
            }
        }
Example #4
0
        public void FetchWithoutConflict(string remoteUrl, string branchName)
        {
            var tracer = _tracerFactory.GetTracer();

            try
            {
                using (var repo = new LibGit2Sharp.Repository(RepositoryPath))
                {
                    var trackedBranchName = string.Format("{0}/{1}", _remoteAlias, branchName);
                    var refSpec           = string.Format("+refs/heads/{0}:refs/remotes/{1}", branchName, trackedBranchName);

                    LibGit2Sharp.Remote remote = null;
                    using (tracer.Step("LibGit2SharpRepository Add Remote"))
                    {
                        // only add if matching remote does not exist
                        // to address strange LibGit2SharpRepository remove and add remote issue (remote already exists!)
                        remote = repo.Network.Remotes[_remoteAlias];
                        if (remote != null &&
                            string.Equals(remote.Url, remoteUrl, StringComparison.OrdinalIgnoreCase) &&
                            remote.FetchRefSpecs.Any(rf => string.Equals(rf.Specification, refSpec, StringComparison.OrdinalIgnoreCase)))
                        {
                            tracer.Trace("Git remote exists");
                        }
                        else
                        {
                            // Remove it if it already exists (does not throw if it doesn't)
                            repo.Network.Remotes.Remove(_remoteAlias);

                            // Configure the remote
                            remote = repo.Network.Remotes.Add(_remoteAlias, remoteUrl, refSpec);

                            tracer.Trace("Git remote added");
                        }
                    }

                    using (tracer.Step("LibGit2SharpRepository Fetch"))
                    {
                        // This will only retrieve the "master"
                        repo.Network.Fetch(remote);
                    }

                    using (tracer.Step("LibGit2SharpRepository Update"))
                    {
                        // Are we fetching a tag?
                        if (branchName.Trim().StartsWith("refs/tags/", StringComparison.OrdinalIgnoreCase))
                        {
                            var trackedTag = repo.Tags[branchName];
                            if (trackedTag == null)
                            {
                                throw new BranchNotFoundException(branchName, null);
                            }

                            // Update the raw ref to point to the tag
                            UpdateRawRef(branchName, branchName);

                            // Now checkout out the tag, which points to the right place
                            Update(branchName);
                        }
                        else
                        {
                            // Optionally set up the branch tracking configuration
                            var trackedBranch = repo.Branches[trackedBranchName];
                            if (trackedBranch == null)
                            {
                                throw new BranchNotFoundException(branchName, null);
                            }

                            var branch = repo.Branches[branchName] ?? repo.CreateBranch(branchName, trackedBranch.Tip);
                            repo.Branches.Update(branch,
                                                 b => b.TrackedBranch = trackedBranch.CanonicalName);

                            // Update the raw ref to point the head of branchName to the latest fetched branch
                            UpdateRawRef(string.Format("refs/heads/{0}", branchName), trackedBranchName);

                            // Now checkout out our branch, which points to the right place
                            Update(branchName);
                        }
                    }
                }
            }
            catch (LibGit2SharpException exception)
            {
                // LibGit2Sharp doesn't support SSH yet. Use GitExeRepository
                // LibGit2Sharp only supports smart Http protocol
                if (exception.Message.Equals("Unsupported URL protocol") ||
                    exception.Message.Equals("Received unexpected content-type"))
                {
                    tracer.TraceWarning("LibGit2SharpRepository fallback to git.exe due to {0}", exception.Message);

                    _legacyGitExeRepository.FetchWithoutConflict(remoteUrl, branchName);
                }
                else
                {
                    throw;
                }
            }
        }