Example #1
0
        private void RemotesUpdated(object sender, EventArgs e)
        {
            if (TabControlTagBranch.SelectedTab == MultipleBranchTab)
            {
                UpdateMultiBranchView();
            }

            EnableLoadSshButton();

            // update the text box of the Remote Url combobox to show the URL of selected remote
            {
                string pushUrl = Module.GetPathSetting(string.Format(SettingKeyString.RemotePushUrl, _NO_TRANSLATE_Remotes.Text));
                if (pushUrl.IsNullOrEmpty())
                {
                    pushUrl = Module.GetPathSetting(string.Format(SettingKeyString.RemoteUrl, _NO_TRANSLATE_Remotes.Text));
                }
                PushDestination.Text = pushUrl;
            }

            if (string.IsNullOrEmpty(_NO_TRANSLATE_Branch.Text))
            {
                // Doing this makes it pretty easy to accidentally create a branch on the remote.
                // But leaving it blank will do the 'default' thing, meaning all branches are pushed.
                // Solution: when pushing a branch that doesn't exist on the remote, ask what to do
                var currentBranch = new GitRef(Module, null, _currentBranch, _NO_TRANSLATE_Remotes.Text);
                _NO_TRANSLATE_Branch.Items.Add(currentBranch);
                _NO_TRANSLATE_Branch.SelectedItem = currentBranch;
            }

            BranchSelectedValueChanged(null, null);
        }
            public void DeleteForce()
            {
                var branchHead = GitRef.CreateBranchRef(UICommands.Module, null, FullPath);
                var cmd        = new GitDeleteBranchCmd(new GitRef[] { branchHead }, true);

                UICommands.StartCommandLineProcessDialog(cmd, null);
            }
Example #3
0
        private void LinkWorkItems(TeamProjectReference project, GitRepository repo, GitRef newBranch, string branchName, List <WorkItem> relatedWorkitems)
        {
            var patchDocument = new JsonPatchDocument();
            var gitUri        = $"vstfs:///Git/Ref/{project.Id}%2F{repo.Id}%2FGBdev%2F{branchName}";

            patchDocument.Add(
                new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/relations/-",
                Value     = new
                {
                    rel        = "ArtifactLink",
                    url        = gitUri,
                    attributes = new { name = "Branch" }
                }
            }
                );

            foreach (var workItem in relatedWorkitems)
            {
                if (!workItem.Relations.Any(c => c.Rel == "ArtifactLink" && c.Url == gitUri))
                {
                    workItemTrackingClient.UpdateWorkItemAsync(patchDocument, (int)workItem.Id).Wait();
                }
            }
        }
Example #4
0
        private string GetBranchName(WorkItem workItem, GitRef releaseBranch)
        {
            var branchName = string.Empty;

            switch (workItem.Fields["System.WorkItemType"])
            {
            case "Feature": branchName = "feature-"; break;

            case "Product Backlog Item": branchName = "pbi-"; break;

            case "Bug": branchName = "bug-"; break;

            default: branchName = "branch-"; break;
            }

            branchName += workItem.Id.ToString();

            if (releaseBranch != null)
            {
                var releaseBranchName = releaseBranch.Name.Remove(0, releaseBranch.Name.IndexOf("release/")).Replace("/", "-");
                branchName += "-on-" + releaseBranchName;
            }

            return(branchName);
        }
Example #5
0
        /// <summary>
        /// Returns the default remote for push operation.
        /// </summary>
        /// <returns>The <see cref="GitRef.Name"/> if found, otheriwse <see langword="null"/>.</returns>
        // TODO: moved verbatim from FormPush.cs, perhaps needs refactoring
        public string GetDefaultPushRemote(GitRemote remote, string branch)
        {
            if (remote == null)
            {
                throw new ArgumentNullException(nameof(remote));
            }

            var module = GetModule();

            bool IsSettingForBranch(string setting, string branchName)
            {
                var head = new GitRef(module, string.Empty, setting);

                return(head.IsHead && head.Name.Equals(branchName, StringComparison.OrdinalIgnoreCase));
            }

            var remoteHead = remote.Push
                             .Select(s => s.Split(':'))
                             .Where(t => t.Length == 2)
                             .Where(t => IsSettingForBranch(t[0], branch))
                             .Select(t => new GitRef(module, string.Empty, t[1]))
                             .FirstOrDefault(h => h.IsHead);

            return(remoteHead?.Name);
        }
Example #6
0
        public void IsTrackingRemote_should_return_false_when_tracking_another_remote_branch()
        {
            GitRef localBranchRef = SetupLocalBranchWithATrackingReference("one_remote_branch", "origin");

            GitRef remoteBranchRef = SetupRemoteRef("another_remote_branch", "origin");

            Assert.IsFalse(localBranchRef.IsTrackingRemote(remoteBranchRef));
        }
Example #7
0
        public void IsTrackingRemote_should_return_false_when_supposedly_remote_branch_is_a_local_ref()
        {
            GitRef localBranchRef = SetupLocalBranchWithATrackingReference("a_remote_branch", "origin");

            GitRef remoteBranchRef = SetupLocalBranchWithATrackingReference("a_remote_branch", "origin");

            Assert.IsFalse(localBranchRef.IsTrackingRemote(remoteBranchRef));
        }
Example #8
0
        public TfsBranch(GitRef branch, TfsUrlBuilder urlBuilder)
        {
            DisplayName = ExtractDisplayName(branch.Name);
            FullName    = branch.Name;
            _id         = branch.ObjectId;

            WebUrl = urlBuilder.BuildBranchUrl(DisplayName);
        }
Example #9
0
        public void IsTrackingRemote_should_return_false_when_tracking_another_remote()
        {
            string remoteBranchShortName = "remote_branch";
            GitRef localBranchRef        = SetupLocalBranchWithATrackingReference(remoteBranchShortName, "origin");

            GitRef remoteBranchRef = SetupRemoteRef(remoteBranchShortName, "upstream");

            Assert.IsFalse(localBranchRef.IsTrackingRemote(remoteBranchRef));
        }
            public void DeleteAllForce()
            {
                var branches    = Nodes.DepthEnumerator <BranchNode>();
                var branchHeads =
                    branches.Select(branch => GitRef.CreateBranchRef(UICommands.Module, null, branch.FullPath));
                var cmd = new GitDeleteBranchCmd(branchHeads, true);

                UICommands.StartCommandLineProcessDialog(cmd, null);
            }
Example #11
0
        public void Remote_Should_prefix_LocalName_for_Name()
        {
            string remoteName   = "origin";
            string name         = "local_branch";
            string completeName = $"refs/remotes/{remoteName}/{name}";

            GitRef remoteBranchRef = SetupRawRemoteRef(name, remoteName, completeName);

            Assert.AreEqual(remoteBranchRef.LocalName, name);
        }
Example #12
0
        public void IsTrackingRemote_should_return_true_when_tracking_remote()
        {
            string remoteBranchShortName = "remote_branch";
            string remoteName            = "origin";
            GitRef localBranchRef        = SetupLocalBranchWithATrackingReference(remoteBranchShortName, remoteName);

            GitRef remoteBranchRef = SetupRemoteRef(remoteBranchShortName, remoteName);

            Assert.IsTrue(localBranchRef.IsTrackingRemote(remoteBranchRef));
        }
Example #13
0
        private static GitRef SetupRemoteRef(string remoteBranchShortName, string remoteName)
        {
            var remoteGitModule          = Substitute.For <IGitModule>();
            var remoteConfigFileSettings = Substitute.For <IConfigFileSettings>();

            remoteGitModule.LocalConfigFile.Returns(remoteConfigFileSettings);
            var remoteBranchRef = new GitRef(remoteGitModule, ObjectId.Random(), $"refs/remotes/{remoteName}/{remoteBranchShortName}", remoteName);

            return(remoteBranchRef);
        }
Example #14
0
        private void comboBoxBranches_TextChanged(object sender, EventArgs e)
        {
            if (comboBoxBranches.DataSource == null)
            {
                return;
            }

            _selectedBranch = ((List <GitRef>)comboBoxBranches.DataSource).FirstOrDefault(a => a.LocalName == comboBoxBranches.Text);
            SetSelectedRevisionByFocusedControl();
        }
Example #15
0
        private static GitPullRequest CreatePullRequestInternal(ClientSampleContext context, GitRepository repo, GitHttpClient gitClient)
        {
            // we need a new branch with changes in order to create a PR
            // first, find the default branch
            string defaultBranchName = WithoutRefsPrefix(repo.DefaultBranch);
            GitRef defaultBranch     = gitClient.GetRefsAsync(repo.Id, filter: defaultBranchName).Result.First();

            // next, craft the branch and commit that we'll push
            GitRefUpdate newBranch = new GitRefUpdate()
            {
                Name        = $"refs/heads/vsts-api-sample/{ChooseRefsafeName()}",
                OldObjectId = defaultBranch.ObjectId,
            };
            string       newFileName = $"{ChooseItemsafeName()}.md";
            GitCommitRef newCommit   = new GitCommitRef()
            {
                Comment = "Add a sample file",
                Changes = new GitChange[]
                {
                    new GitChange()
                    {
                        ChangeType = VersionControlChangeType.Add,
                        Item       = new GitItem()
                        {
                            Path = $"/vsts-api-sample/{newFileName}"
                        },
                        NewContent = new ItemContent()
                        {
                            Content     = "# Thank you for using VSTS!",
                            ContentType = ItemContentType.RawText,
                        },
                    }
                },
            };

            // create the push with the new branch and commit
            GitPush push = gitClient.CreatePushAsync(new GitPush()
            {
                RefUpdates = new GitRefUpdate[] { newBranch },
                Commits    = new GitCommitRef[] { newCommit },
            }, repo.Id).Result;

            // finally, create a PR
            var pr = gitClient.CreatePullRequestAsync(new GitPullRequest()
            {
                SourceRefName = newBranch.Name,
                TargetRefName = repo.DefaultBranch,
                Title         = $"Add {newFileName} (from VSTS REST samples)",
                Description   = "Adding this file from the pull request samples",
            },
                                                      repo.Id).Result;

            return(pr);
        }
Example #16
0
        private void comboBoxBranches_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (comboBoxBranches.SelectedValue == null)
            {
                return;
            }

            _selectedBranch = (GitRef)comboBoxBranches.SelectedValue;
            SetSelectedRevisionByFocusedControl();
            Go();
        }
Example #17
0
        public void If_Remote_is_not_prefix_of_Name_then_LocalName_should_return_Name()
        {
            // Not standard behavior but seem to occur for git-svn
            string remoteName   = "Remote_longer_than_Name";
            string name         = "a_short_name";
            string completeName = $"refs/remotes/{name}";

            GitRef remoteBranchRef = SetupRawRemoteRef(name, remoteName, completeName);

            Assert.AreEqual(remoteBranchRef.LocalName, name);
        }
Example #18
0
        public void ctor_should_have_expected_values()
        {
            const string remoteName      = "origin";
            string       completeName    = $"refs/remotes/{remoteName}/branch_name";
            GitRef       remoteBranchRef = SetupRawRemoteRef(remoteName, completeName);

            GitDeleteBranchCmd cmd = new(new IGitRef[] { remoteBranchRef }, force : false);

            Assert.IsFalse(cmd.AccessesRemote);
            Assert.IsTrue(cmd.ChangesRepoState);
        }
Example #19
0
        public void GitRef_Constructor()
        {
            // Arrange
            var obj = BaseTests.LoadJson("Get-VSTeamGitRef.json");

            // Act
            var actual = new GitRef(obj[0], "TestProject");

            // Assert
            Assert.IsNotNull(actual.Creator, "Creator");
            Assert.AreEqual("refs/heads/master", actual.RefName, "RefName");
        }
Example #20
0
        private static GitRef SetupLocalBranchWithATrackingReference(string remoteShortName, string remoteName)
        {
            var localGitModule          = Substitute.For <IGitModule>();
            var localConfigFileSettings = Substitute.For <IConfigFileSettings>();

            localConfigFileSettings.GetValue($"branch.local_branch.merge").Returns($"refs/heads/{remoteShortName}");
            localConfigFileSettings.GetValue($"branch.local_branch.remote").Returns(remoteName);
            localGitModule.LocalConfigFile.Returns(localConfigFileSettings);
            var localBranchRef = new GitRef(localGitModule, ObjectId.Random(), "refs/heads/local_branch");

            return(localBranchRef);
        }
        public void Validate_GetTagVerifyMessage(int usefulTagRefNumber, string expected)
        {
            var guid = Guid.NewGuid().ToString("N");

            GitRevision revision = new GitRevision(guid);

            GitRef gitRef;
            string gitRefCompleteName;

            switch (usefulTagRefNumber)
            {
            case 0:
                // Tag but not dereference
                gitRefCompleteName = "refs/tags/TagName";
                gitRef             = new GitRef(_module(), guid, gitRefCompleteName);
                revision.Refs.Add(gitRef);

                _module().RunGitCmd($"verify-tag  {gitRef.LocalName}").Returns("");

                break;

            case 1:
                // One tag that's also IsDereference == true
                gitRefCompleteName = "refs/tags/TagName^{}";
                gitRef             = new GitRef(_module(), guid, gitRefCompleteName);
                revision.Refs.Add(gitRef);

                _module().RunGitCmd($"verify-tag  {gitRef.LocalName}").Returns(gitRef.LocalName);

                break;

            case 2:
                // Two tag that's also IsDereference == true
                gitRefCompleteName = "refs/tags/FirstTag^{}";
                gitRef             = new GitRef(_module(), guid, gitRefCompleteName);
                revision.Refs.Add(gitRef);

                _module().RunGitCmd($"verify-tag  {gitRef.LocalName}").Returns(gitRef.LocalName);

                gitRefCompleteName = "refs/tags/SecondTag^{}";
                gitRef             = new GitRef(_module(), guid, gitRefCompleteName);
                revision.Refs.Add(gitRef);

                _module().RunGitCmd($"verify-tag  {gitRef.LocalName}").Returns(gitRef.LocalName);

                break;
            }

            var actual = _gpgController.GetTagVerifyMessage(revision);

            Assert.AreEqual(expected, actual);
        }
Example #22
0
        private void BranchesDropDown(object sender, EventArgs e)
        {
            if ((PullFromUrl.Checked && string.IsNullOrEmpty(comboBoxPullSource.Text)) &&
                (PullFromRemote.Checked && string.IsNullOrEmpty(_NO_TRANSLATE_Remotes.Text)))
            {
                Branches.DataSource = null;
                return;
            }

            using (WaitCursorScope.Enter())
            {
                LoadPuttyKey();

                if (_heads == null)
                {
                    if (PullFromUrl.Checked)
                    {
                        _heads = Module.GetRefs(false, true).ToList();
                    }
                    else
                    {
                        // The line below is the most reliable way to get a list containing
                        // all remote branches but it is also the slowest.
                        // Heads = GitCommands.GitCommands.GetRemoteHeads(Remotes.Text, false, true);

                        // The code below is a quick way to get a list contains all remote branches.
                        // It only returns the heads that are already known to the repository. This
                        // doesn't return heads that are new on the server. This can be updated using
                        // update branch info in the manage remotes dialog.
                        _heads = new List <IGitRef>();
                        foreach (var head in Module.GetRefs(true, true))
                        {
                            if (!head.IsRemote ||
                                !head.Name.StartsWith(_NO_TRANSLATE_Remotes.Text, StringComparison.CurrentCultureIgnoreCase))
                            {
                                continue;
                            }

                            _heads.Insert(0, head);
                        }
                    }
                }

                Branches.DisplayMember = nameof(IGitRef.LocalName);

                ////_heads.Insert(0, GitHead.AllHeads); --> disable this because it is only for expert users
                _heads.Insert(0, GitRef.NoHead(Module));
                Branches.DataSource = _heads;

                Branches.ResizeDropDownWidth(AppSettings.BranchDropDownMinWidth, AppSettings.BranchDropDownMaxWidth);
            }
        }
Example #23
0
        public static GitRef CreateBranch(this GitHttpClient gitClient, Guid repoId, GitRef sourceRef, string fullBranchName)
        {
            GitRefUpdateResult refCreateResult = gitClient.UpdateRefsAsync(
                new GitRefUpdate[] { new GitRefUpdate()
                                     {
                                         OldObjectId = new string('0', 40),
                                         NewObjectId = sourceRef.ObjectId,
                                         Name        = WithRefsAndHeadsPrefix(fullBranchName),
                                     } },
                repositoryId: repoId).Result.First();

            return(gitClient.GetRefsAsync(repoId, filter: "heads/" + WithoutRefsAndHeadsPrefix(fullBranchName)).Result.First());
        }
        public void CreatesSemverForReleaseBranch()
        {
            //arrange
            var gitRef = new GitRef {
                Id = "", Name = "refs/heads/release/1.0", ObjectId = ""
            };

            //act
            var result = new GitBranch(gitRef);

            //assert
            Assert.NotNull(result.Semver);
        }
Example #25
0
        private GitRef CreateBranch(GitRepository repo, GitRef sourceRef, string fullBranchName, string branchName)
        {
            GitRefUpdateResult refCreateResult = gitClient.UpdateRefsAsync(
                new GitRefUpdate[] { new GitRefUpdate()
                                     {
                                         OldObjectId = new string('0', 40),
                                         NewObjectId = sourceRef.ObjectId,
                                         Name        = fullBranchName,
                                     } },
                repositoryId: repo.Id).Result.First();

            return(gitClient.GetRefsAsync(repo.Id, filter: "heads/dev/" + branchName).Result.First());
        }
Example #26
0
        public async Task CreateBranch(string projectName, string repositoryName, string branchName)
        {
            string        refId             = new string('0', 40);
            string        defaultBranchName = "heads/master";
            GitRepository repo = null;

            try
            {
                repo = await GetRepository(projectName, repositoryName);
            }
            catch (Exception ex)
            {
                if (repo == null)
                {
                    throw new Exception($"Repository does not exist: {repositoryName}", ex);
                }
            }

            try
            {
                List <GitRef> refs = await Git.GetRefsAsync(repo.Id, filter : defaultBranchName);

                if (refs.Count > 0)
                {
                    GitRef defaultBranch = refs.First();
                    refId = defaultBranch.ObjectId;
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"Could not get reference to default branch; repositoryName = {repositoryName}, branchName = {defaultBranchName}", ex);
            }

            GitRefUpdate refUpdate = new GitRefUpdate()
            {
                Name         = $"refs/heads/{branchName}",
                NewObjectId  = refId,
                OldObjectId  = new string('0', 40),
                IsLocked     = false,
                RepositoryId = repo.Id
            };

            try
            {
                await Git.UpdateRefsAsync(new GitRefUpdate[] { refUpdate }, repositoryId : repo.Id);
            }
            catch (Exception ex)
            {
                throw new Exception($"Could not create new branch; repositoryName = {repositoryName}, branchName = {branchName}", ex);
            }
        }
Example #27
0
        private static async Task <GitPullRequest> CreatePlaceholderBranchAsync(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var gitClient  = ProjectCollection.GetClient <GitHttpClient>();
            var repository = await gitClient.GetRepositoryAsync(project : Options.TFSProjectName, repositoryId : "VS", cancellationToken : cancellationToken);

            var refs = await gitClient.GetRefsAsync(
                repository.Id,
                filter : $"heads/{Options.VisualStudioBranchName}",
                cancellationToken : cancellationToken);

            GitRef sourceBranch = refs.Single(r => r.Name == $"refs/heads/{Options.VisualStudioBranchName}");

            var branchName = GetNewBranchName();

            _ = await gitClient.CreatePushAsync(new GitPush()
            {
                RefUpdates = new[] {
                    new GitRefUpdate()
                    {
                        Name        = $"refs/heads/{branchName}",
                        OldObjectId = sourceBranch.ObjectId,
                    }
                },
                Commits = new[] {
                    new GitCommitRef()
                    {
                        Comment = $"PLACEHOLDER INSERTION FOR {Options.InsertionName}",
                        Changes = new GitChange[]
                        {
                            new GitChange()
                            {
                                ChangeType = VersionControlChangeType.Delete,
                                Item       = new GitItem()
                                {
                                    Path = "/Init.ps1"
                                }
                            },
                        }
                    },
                },
            }, repository.Id, cancellationToken : cancellationToken);

            return(await CreatePullRequestAsync(
                       branchName,
                       $"PLACEHOLDER INSERTION FOR {Options.InsertionName}",
                       "Not Specified",
                       Options.TitlePrefix,
                       reviewerId : MLInfraSwatUserId.ToString(),
                       cancellationToken));
        }
Example #28
0
        public void IsTrackingRemote_should_return_false_when_local_branch_is_tracking_nothing()
        {
            var localGitModule          = Substitute.For <IGitModule>();
            var localConfigFileSettings = Substitute.For <IConfigFileSettings>();

            localConfigFileSettings.GetValue($"branch.local_branch.merge").Returns(string.Empty);
            localConfigFileSettings.GetValue($"branch.local_branch.remote").Returns(string.Empty);
            localGitModule.LocalConfigFile.Returns(localConfigFileSettings);
            var localBranchRef = new GitRef(localGitModule, ObjectId.Random(), "refs/heads/local_branch");

            GitRef remoteBranchRef = SetupLocalBranchWithATrackingReference("a_remote_branch", "origin");

            Assert.IsFalse(localBranchRef.IsTrackingRemote(remoteBranchRef));
        }
        public void Validate_GetTagVerifyMessage(int usefulTagRefNumber, string expected)
        {
            var guid     = Guid.NewGuid().ToString("N");
            var revision = new GitRevision(guid);

            switch (usefulTagRefNumber)
            {
            case 0:
            {
                // Tag but not dereference
                var gitRef = new GitRef(_module(), guid, "refs/tags/TagName");
                revision.Refs = new[] { gitRef };

                _module().RunGitCmd($"verify-tag  {gitRef.LocalName}").Returns("");

                break;
            }

            case 1:
            {
                // One tag that's also IsDereference == true
                var gitRef = new GitRef(_module(), guid, "refs/tags/TagName^{}");
                revision.Refs = new[] { gitRef };

                _module().RunGitCmd($"verify-tag  {gitRef.LocalName}").Returns(gitRef.LocalName);

                break;
            }

            case 2:
            {
                // Two tag that's also IsDereference == true
                var gitRef1 = new GitRef(_module(), guid, "refs/tags/FirstTag^{}");
                revision.Refs = new[] { gitRef1 };

                _module().RunGitCmd($"verify-tag  {gitRef1.LocalName}").Returns(gitRef1.LocalName);

                var gitRef2 = new GitRef(_module(), guid, "refs/tags/SecondTag^{}");
                revision.Refs = new[] { gitRef1, gitRef2 };

                _module().RunGitCmd($"verify-tag  {gitRef2.LocalName}").Returns(gitRef2.LocalName);

                break;
            }
            }

            var actual = _gpgController.GetTagVerifyMessage(revision);

            Assert.AreEqual(expected, actual);
        }
Example #30
0
        private void ProcessHeads(string remote, IEnumerable <GitRef> localHeads, IEnumerable <GitRef> remoteHeads)
        {
            var remoteBranches = remoteHeads.ToHashSet(h => h.LocalName);

            // Add all the local branches.
            foreach (var head in localHeads)
            {
                DataRow row = _branchTable.NewRow();
                row["Force"]  = false;
                row["Delete"] = false;
                row["Local"]  = head.Name;

                string remoteName;
                if (head.Remote == remote)
                {
                    remoteName = head.MergeWith ?? head.Name;
                }
                else
                {
                    remoteName = head.Name;
                }

                row["Remote"] = remoteName;
                bool knownAtRemote = remoteBranches.Contains(remoteName);
                row["New"]  = knownAtRemote ? _no.Text : _yes.Text;
                row["Push"] = knownAtRemote;

                _branchTable.Rows.Add(row);
            }

            // Offer to delete all the left over remote branches.
            foreach (var remoteHead in remoteHeads)
            {
                GitRef head = remoteHead;
                if (localHeads.All(h => h.Name != head.LocalName))
                {
                    DataRow row = _branchTable.NewRow();
                    row["Local"]  = null;
                    row["Remote"] = remoteHead.LocalName;
                    row["New"]    = _no.Text;
                    row["Push"]   = false;
                    row["Force"]  = false;
                    row["Delete"] = false;
                    _branchTable.Rows.Add(row);
                }
            }

            BranchGrid.Enabled = true;
        }
Example #31
0
        private void SwitchDialog_Shown(object sender, EventArgs e)
        {
            using (var client = GetService<IGitClientPool>().GetNoUIClient())
            {
                _repositoryBranch = client.GetCurrentBranch(LocalPath);

                localBranchBox.BeginUpdate();
                localBranchBox.Items.Clear();
                trackingBranchBox.BeginUpdate();
                trackingBranchBox.Items.Clear();
                tagBox.BeginUpdate();
                tagBox.Items.Clear();

                // When a revision ref was provided, try to resolve it to a branch,
                // tag or remote branch.

                bool resolved = !(_providedRef != null && _providedRef.Type == GitRefType.Revision);

                GitRef resolvedRef = null;

                foreach (var @ref in client.GetRefs(LocalPath))
                {
                    if (
                        !resolved &&
                        String.Equals(_providedRef.Revision, @ref.Revision, StringComparison.OrdinalIgnoreCase)
                    ) {
                        resolvedRef = @ref;
                        resolved = true;
                    }

                    switch (@ref.Type)
                    {
                        case GitRefType.Branch:
                            localBranchBox.Items.Add(@ref);
                            break;

                        case GitRefType.RemoteBranch:
                            trackingBranchBox.Items.Add(@ref);
                            break;

                        case GitRefType.Tag:
                            tagBox.Items.Add(@ref);
                            break;
                    }
                }

                if (resolvedRef != null)
                {
                    _providedRef = resolvedRef;
                    SetFromProvided();
                }

                localBranchBox.EndUpdate();
                trackingBranchBox.EndUpdate();
                tagBox.EndUpdate();
            }

            if (SwitchToBranch == null)
                SwitchToBranch = _repositoryBranch;
        }
Example #32
0
        public override void OnExecute(CommandEventArgs e)
        {
            GitItem theItem = null;
            string path;
            GitRef currentBranch = null;

            string projectRoot = e.GetService<IVisualGitSolutionSettings>().ProjectRoot;

            if (e.Command == VisualGitCommand.SolutionSwitchDialog)
                path = projectRoot;
            else if (e.Command == VisualGitCommand.SwitchProject)
            {
                IProjectFileMapper mapper = e.GetService<IProjectFileMapper>();
                path = null;

                foreach (GitProject item in e.Selection.GetSelectedProjects(true))
                {
                    IGitProjectInfo pi = mapper.GetProjectInfo(item);

                    if (pi == null)
                        continue;

                    path = pi.ProjectDirectory;
                    break;
                }

                if (string.IsNullOrEmpty(path))
                    return;
            }
            else if (e.Command == VisualGitCommand.LogSwitchToRevision)
            {
                IGitLogItem item = EnumTools.GetSingle(e.Selection.GetSelection<IGitLogItem>());

                if (item == null)
                    return;

                path = item.RepositoryRoot;
                currentBranch = new GitRef(item.Revision);
            }
            else
            {
                foreach (GitItem item in e.Selection.GetSelectedGitItems(false))
                {
                    if (item.IsVersioned)
                    {
                        theItem = item;
                        break;
                    }
                    return;
                }
                path = theItem.FullPath;
            }

            IFileStatusCache statusCache = e.GetService<IFileStatusCache>();

            GitItem pathItem = statusCache[path];

            if (currentBranch == null)
            {
                using (var client = e.GetService<IGitClientPool>().GetNoUIClient())
                {
                    currentBranch = client.GetCurrentBranch(pathItem.FullPath);
                }

                if (currentBranch == null)
                    return; // Should never happen on a real workingcopy
            }

            GitRef target;
            bool force = false;

            if (e.Argument is string)
            {
                target = new GitRef((string)e.Argument);
            }
            else
                using (SwitchDialog dlg = new SwitchDialog())
                {
                    dlg.GitOrigin = new GitOrigin(pathItem);
                    dlg.Context = e.Context;

                    dlg.LocalPath = GitTools.GetRepositoryRoot(path);
                    dlg.SwitchToBranch = currentBranch;

                    if (dlg.ShowDialog(e.Context) != DialogResult.OK)
                        return;

                    target = dlg.SwitchToBranch;
                    force = dlg.Force;
                }

            // Get a list of all documents below the specified paths that are open in editors inside VS
            HybridCollection<string> lockPaths = new HybridCollection<string>(StringComparer.OrdinalIgnoreCase);
            IVisualGitOpenDocumentTracker documentTracker = e.GetService<IVisualGitOpenDocumentTracker>();

            foreach (string file in documentTracker.GetDocumentsBelow(path))
            {
                if (!lockPaths.Contains(file))
                    lockPaths.Add(file);
            }

            documentTracker.SaveDocuments(lockPaths); // Make sure all files are saved before merging!

            using (DocumentLock lck = documentTracker.LockDocuments(lockPaths, DocumentLockType.NoReload))
            using (lck.MonitorChangesForReload())
            {
                GitSwitchArgs args = new GitSwitchArgs();

                GitException exception = null;

                e.GetService<IProgressRunner>().RunModal(
                    "Changing Current Branch",
                    delegate(object sender, ProgressWorkerArgs a)
                    {
                        args.Force = force;

                        // TODO: Decide whether it is necessary for the switch
                        // command to report conflicts.
            #if NOT_IMPLEMENTED
                        e.GetService<IConflictHandler>().RegisterConflictHandler(args, a.Synchronizer);
            #endif

                        try
                        {
                            a.Client.Switch(path, target, args);
                        }
                        catch (GitException ex)
                        {
                            exception = ex;
                        }
                    });

                if (exception != null)
                {
                    e.GetService<IVisualGitErrorHandler>().OnWarning(exception);
                }
            }
        }