Ejemplo n.º 1
0
        public IObservable <Unit> Checkout(ILocalRepositoryModel repository, IPullRequestModel pullRequest, string localBranchName)
        {
            return(Observable.Defer(async() =>
            {
                var repo = gitService.GetRepository(repository.LocalPath);
                var existing = repo.Branches[localBranchName];

                if (existing != null)
                {
                    await gitClient.Checkout(repo, localBranchName);
                }
                else if (repository.CloneUrl.ToRepositoryUrl() == pullRequest.Head.RepositoryCloneUrl.ToRepositoryUrl())
                {
                    var remote = await gitClient.GetHttpRemote(repo, "origin");
                    await gitClient.Fetch(repo, remote.Name);
                    await gitClient.Checkout(repo, localBranchName);
                }
                else
                {
                    var refSpec = $"{pullRequest.Head.Ref}:{localBranchName}";
                    var remoteName = await CreateRemote(repo, pullRequest.Head.RepositoryCloneUrl);

                    await gitClient.Fetch(repo, remoteName);
                    await gitClient.Fetch(repo, remoteName, new[] { refSpec });
                    await gitClient.Checkout(repo, localBranchName);
                    await gitClient.SetTrackingBranch(repo, localBranchName, $"refs/remotes/{remoteName}/{pullRequest.Head.Ref}");
                }

                // Store the PR number in the branch config with the key "ghfvs-pr".
                var prConfigKey = $"branch.{localBranchName}.{SettingGHfVSPullRequest}";
                await gitClient.SetConfig(repo, prConfigKey, BuildGHfVSConfigKeyValue(pullRequest));

                return Observable.Return(Unit.Default);
            }));
        }
Ejemplo n.º 2
0
        public IObservable <Unit> Checkout(ILocalRepositoryModel repository, IPullRequestModel pullRequest, string localBranchName)
        {
            return(Observable.Defer(async() =>
            {
                var repo = gitService.GetRepository(repository.LocalPath);
                var existing = repo.Branches[localBranchName];

                if (existing != null)
                {
                    await gitClient.Checkout(repo, localBranchName);
                }
                else if (repository.CloneUrl.ToRepositoryUrl() == pullRequest.Head.RepositoryCloneUrl.ToRepositoryUrl())
                {
                    await gitClient.Fetch(repo, "origin");
                    await gitClient.Checkout(repo, localBranchName);
                }
                else
                {
                    var refSpec = $"{pullRequest.Head.Ref}:{localBranchName}";
                    var prConfigKey = $"branch.{localBranchName}.ghfvs-pr";
                    var remoteName = pullRequest.Head.RepositoryCloneUrl.Owner;
                    var remoteUri = pullRequest.Head.RepositoryCloneUrl;

                    await gitClient.SetRemote(repo, remoteName, new Uri(remoteUri));
                    await gitClient.Fetch(repo, remoteName);
                    await gitClient.Fetch(repo, remoteName, new[] { refSpec });
                    await gitClient.Checkout(repo, localBranchName);
                    await gitClient.SetTrackingBranch(repo, localBranchName, $"refs/remotes/{remoteName}/{pullRequest.Head.Ref}");
                    await gitClient.SetConfig(repo, prConfigKey, pullRequest.Number.ToString());
                }

                return Observable.Return(Unit.Default);
            }));
        }
Ejemplo n.º 3
0
        public IObservable <Unit> SwitchToBranch(ILocalRepositoryModel repository, IPullRequestModel pullRequest)
        {
            return(Observable.Defer(async() =>
            {
                var repo = gitService.GetRepository(repository.LocalPath);
                var branchName = GetLocalBranchesInternal(repository, repo, pullRequest).FirstOrDefault();

                Debug.Assert(branchName != null, "PullRequestService.SwitchToBranch called but no local branch found.");

                if (branchName != null)
                {
                    await gitClient.Fetch(repo, "origin");

                    var branch = repo.Branches[branchName];

                    if (branch == null)
                    {
                        var trackedBranchName = $"refs/remotes/origin/" + branchName;
                        var trackedBranch = repo.Branches[trackedBranchName];

                        if (trackedBranch != null)
                        {
                            branch = repo.CreateBranch(branchName, trackedBranch.Tip);
                            await gitClient.SetTrackingBranch(repo, branchName, trackedBranchName);
                        }
                        else
                        {
                            throw new InvalidOperationException($"Could not find branch '{trackedBranchName}'.");
                        }
                    }

                    await gitClient.Checkout(repo, branchName);
                }

                return Observable.Empty <Unit>();
            }));
        }
Ejemplo n.º 4
0
        public IObservable <Unit> Checkout(LocalRepositoryModel repository, PullRequestDetailModel pullRequest, string localBranchName)
        {
            return(Observable.Defer(async() =>
            {
                using (var repo = gitService.GetRepository(repository.LocalPath))
                {
                    var existing = repo.Branches[localBranchName];

                    if (existing != null)
                    {
                        await gitClient.Checkout(repo, localBranchName);
                    }
                    else if (string.Equals(repository.CloneUrl.Owner, pullRequest.HeadRepositoryOwner, StringComparison.OrdinalIgnoreCase))
                    {
                        var remote = await gitClient.GetHttpRemote(repo, "origin");
                        await gitClient.Fetch(repo, remote.Name);
                        await gitClient.Checkout(repo, localBranchName);
                    }
                    else
                    {
                        var refSpec = $"{pullRequest.HeadRefName}:{localBranchName}";
                        var remoteName = await CreateRemote(repo, repository.CloneUrl.WithOwner(pullRequest.HeadRepositoryOwner));

                        await gitClient.Fetch(repo, remoteName);
                        await gitClient.Fetch(repo, remoteName, new[] { refSpec });
                        await gitClient.Checkout(repo, localBranchName);
                        await gitClient.SetTrackingBranch(repo, localBranchName, $"refs/remotes/{remoteName}/{pullRequest.HeadRefName}");
                    }

                    // Store the PR number in the branch config with the key "ghfvs-pr".
                    var prConfigKey = $"branch.{localBranchName}.{SettingGHfVSPullRequest}";
                    await gitClient.SetConfig(repo, prConfigKey, BuildGHfVSConfigKeyValue(pullRequest.BaseRepositoryOwner, pullRequest.Number));

                    return Observable.Return(Unit.Default);
                }
            }));
        }