diff options
author | Mike Krüger <mikkrg@microsoft.com> | 2019-09-19 09:59:52 +0300 |
---|---|---|
committer | Mike Krüger <mikkrg@microsoft.com> | 2019-09-20 11:56:37 +0300 |
commit | c90fe0b2a8bde1de63e8750e1ef201006a610457 (patch) | |
tree | 0bd8fe935ececcd32583e28c12fec7e0c888c50b /main | |
parent | 8f3af3af59918cc0d0fcd95a3fd747f9740cbb88 (diff) |
[VersionControl] Implemented git fetch command.master-gitclient
We had some issues with git fetch from libgit2.
Diffstat (limited to 'main')
6 files changed, 172 insertions, 11 deletions
diff --git a/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary.Tests/MonoDevelop.VersionControl.Git.ClientLibrary.Tests.csproj b/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary.Tests/MonoDevelop.VersionControl.Git.ClientLibrary.Tests.csproj index 99d076ab60..e25c7fdea7 100644 --- a/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary.Tests/MonoDevelop.VersionControl.Git.ClientLibrary.Tests.csproj +++ b/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary.Tests/MonoDevelop.VersionControl.Git.ClientLibrary.Tests.csproj @@ -46,6 +46,7 @@ <Compile Include="MonoDevelop.VersionControl.Git.ClientLibrary.Tests\GitBranchTests.cs" /> <Compile Include="MonoDevelop.VersionControl.Git.ClientLibrary.Tests\GitInitTests.cs" /> <Compile Include="MonoDevelop.VersionControl.Git.ClientLibrary.Tests\TestUtil.cs" /> + <Compile Include="MonoDevelop.VersionControl.Git.ClientLibrary.Tests\GitFetchTests.cs" /> </ItemGroup> <ItemGroup> <None Include="packages.config" /> diff --git a/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary.Tests/MonoDevelop.VersionControl.Git.ClientLibrary.Tests/GitFetchTests.cs b/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary.Tests/MonoDevelop.VersionControl.Git.ClientLibrary.Tests/GitFetchTests.cs new file mode 100644 index 0000000000..01c1869529 --- /dev/null +++ b/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary.Tests/MonoDevelop.VersionControl.Git.ClientLibrary.Tests/GitFetchTests.cs @@ -0,0 +1,47 @@ +// +// GitFetchTests.cs +// +// Author: +// Mike Krüger <mikkrg@microsoft.com> +// +// Copyright (c) 2019 Microsoft Corporation. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using NUnit.Framework; +using System.Threading.Tasks; +using NUnit.Framework.Internal; +using System.IO; +using System.Diagnostics; +using System.Linq; + +namespace MonoDevelop.VersionControl.Git.ClientLibrary.Tests +{ + + [TestFixture] + public class GitFetchTests + { + [Ignore("TODO: Implement me")] + [Test] + public Task TestFetch () + { + return Task.CompletedTask; + } + } +} diff --git a/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary/MonoDevelop.VersionControl.Git.ClientLibrary.csproj b/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary/MonoDevelop.VersionControl.Git.ClientLibrary.csproj index 8ed885882a..c2a869abd8 100644 --- a/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary/MonoDevelop.VersionControl.Git.ClientLibrary.csproj +++ b/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary/MonoDevelop.VersionControl.Git.ClientLibrary.csproj @@ -61,6 +61,7 @@ <Compile Include="MonoDevelop.VersionControl.Git.ClientLibrary\Branches\BranchUtil.cs" /> <Compile Include="MonoDevelop.VersionControl.Git.ClientLibrary\Branches\GitTag.cs" /> <Compile Include="MonoDevelop.VersionControl.Git.ClientLibrary\Init\GitInit.cs" /> + <Compile Include="MonoDevelop.VersionControl.Git.ClientLibrary\Fetch\GitFetch.cs" /> </ItemGroup> <ItemGroup> <Folder Include="MonoDevelop.VersionControl.Git.ClientLibrary\" /> @@ -69,6 +70,7 @@ <Folder Include="MonoDevelop.VersionControl.Git.ClientLibrary\Status\" /> <Folder Include="MonoDevelop.VersionControl.Git.ClientLibrary\Branches\" /> <Folder Include="MonoDevelop.VersionControl.Git.ClientLibrary\Init\" /> + <Folder Include="MonoDevelop.VersionControl.Git.ClientLibrary\Fetch\" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> </Project>
\ No newline at end of file diff --git a/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary/MonoDevelop.VersionControl.Git.ClientLibrary/AbstractGitCallbackHandler.cs b/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary/MonoDevelop.VersionControl.Git.ClientLibrary/AbstractGitCallbackHandler.cs index 3638771e73..a047413994 100644 --- a/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary/MonoDevelop.VersionControl.Git.ClientLibrary/AbstractGitCallbackHandler.cs +++ b/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary/MonoDevelop.VersionControl.Git.ClientLibrary/AbstractGitCallbackHandler.cs @@ -30,6 +30,8 @@ namespace MonoDevelop.VersionControl.Git.ClientLibrary { public abstract class AbstractGitCallbackHandler { + public static readonly AbstractGitCallbackHandler NullHandler = new DoNothingGitCallbackHandler (); + public abstract void OnOutput (string line); public abstract void OnReportProgress (string operation, int percentage); @@ -38,5 +40,36 @@ namespace MonoDevelop.VersionControl.Git.ClientLibrary public abstract string OnGetSSHPassword (string userName); public abstract string OnGetSSHPassphrase (string key); public abstract bool OnGetContinueConnecting (); + + class DoNothingGitCallbackHandler : AbstractGitCallbackHandler + { + public override bool OnGetContinueConnecting () + { + return false; + } + + public override GitCredentials OnGetCredentials (string url) + { + return null; + } + + public override string OnGetSSHPassphrase (string key) + { + return null; + } + + public override string OnGetSSHPassword (string userName) + { + return null; + } + + public override void OnOutput (string line) + { + } + + public override void OnReportProgress (string operation, int percentage) + { + } + } } } diff --git a/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary/MonoDevelop.VersionControl.Git.ClientLibrary/Fetch/GitFetch.cs b/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary/MonoDevelop.VersionControl.Git.ClientLibrary/Fetch/GitFetch.cs new file mode 100644 index 0000000000..95dadee481 --- /dev/null +++ b/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git.ClientLibrary/MonoDevelop.VersionControl.Git.ClientLibrary/Fetch/GitFetch.cs @@ -0,0 +1,81 @@ +// +// GitFetch.cs +// +// Author: +// Mike Krüger <mikkrg@microsoft.com> +// +// Copyright (c) 2019 Microsoft Corporation. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.Threading.Tasks; +using System.Text; +using System.Threading; +using System.Text.RegularExpressions; +using System.IO; +using System.Collections; +using System.Collections.Generic; + +namespace MonoDevelop.VersionControl.Git.ClientLibrary +{ + public class GitFetchOptions + { + /// <summary> + /// The "remote" repository that is the source of a fetch or pull operation. This parameter can be either a URL or the name of a remote. + /// </summary> + /// <value>The repository.</value> + public string Repository { get; set; } + + /// <summary> + /// Specifies which refs to fetch and which local refs to update. + /// </summary> + public IEnumerable<string> RefSpec { get; set; } + + /// <summary> + /// Fetch all remotes. + /// </summary> + public bool FetchAll { get; set; } + + /// <summary> + /// When git fetch is used with <src>:<dst> refspec it may refuse to update the local branch as discussed in the <refspec> part below.This option overrides that check. + /// </summary> + public bool Force { get; set; } + } + + public static class GitFetch + { + public static async Task<GitResult> FetchAsync (string rootPath, GitFetchOptions options, AbstractGitCallbackHandler callbackHandler, CancellationToken cancellationToken = default) + { + var arguments = new GitArguments (rootPath); + arguments.AddArgument ("fetch"); + if (options.FetchAll) + arguments.AddArgument ("--all"); + if (options.Force) + arguments.AddArgument ("-f"); + arguments.EndOptions (); + if (!string.IsNullOrEmpty (options.Repository)) + arguments.AddArgument (options.Repository); + if (options.RefSpec != null) { + foreach (var refSpec in options.RefSpec) + arguments.AddArgument (refSpec); + } + return await new GitProcess ().StartAsync (arguments, callbackHandler, false, cancellationToken); + } + } +}
\ No newline at end of file diff --git a/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git/MonoDevelop.VersionControl.Git/GitRepository.cs b/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git/MonoDevelop.VersionControl.Git/GitRepository.cs index a49e299d39..9b289717be 100644 --- a/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git/MonoDevelop.VersionControl.Git/GitRepository.cs +++ b/main/src/addins/VersionControl/MonoDevelop.VersionControl.Git/MonoDevelop.VersionControl.Git/GitRepository.cs @@ -1248,17 +1248,14 @@ namespace MonoDevelop.VersionControl.Git monitor.Log.WriteLine (GettextCatalog.GetString ("Fetching from '{0}'", remote)); int progress = 0; - var innerTask = await RunBlockingOperationAsync (() => { - var refSpec = RootRepository.Network.Remotes [remote]?.FetchRefSpecs.Select (spec => spec.Specification); - return RetryUntilSuccessAsync (monitor, credType => { - LibGit2Sharp.Commands.Fetch (RootRepository, remote, refSpec, new FetchOptions { - CredentialsProvider = (url, userFromUrl, types) => GitCredentials.TryGet (url, userFromUrl, types, credType), - OnTransferProgress = tp => OnTransferProgress (tp, monitor, ref progress), - }, string.Empty); - return Task.CompletedTask; - }); - }).ConfigureAwait (false); - await innerTask.ConfigureAwait (false); + var options = new GitFetchOptions { + Repository = remote, + RefSpec = RootRepository.Network.Remotes [remote]?.FetchRefSpecs.Select (spec => spec.Specification) + }; + + var result = await GitFetch.FetchAsync (RootPath, options, CreateCallbacks (monitor), monitor.CancellationToken).ConfigureAwait (false); + if (!result.Success) + monitor.ReportError (result.ErrorMessage); monitor.Step (1); monitor.EndTask (); |