diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f44285812..35f0241ef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,43 +15,58 @@ jobs: runs-on: ubuntu-22.04 steps: - name: Checkout - uses: actions/checkout@v3.5.0 + uses: actions/checkout@v4.1.2 with: fetch-depth: 0 - name: Install .NET SDK - uses: actions/setup-dotnet@v3.0.3 + uses: actions/setup-dotnet@v4.0.0 with: - dotnet-version: 7.0.x + dotnet-version: 8.0.x - name: Build run: dotnet build LibGit2Sharp.sln --configuration Release - name: Upload packages - uses: actions/upload-artifact@v3.1.2 + uses: actions/upload-artifact@v4.3.1 with: name: NuGet packages - path: bin/Packages/ + path: artifacts/package/ retention-days: 7 + - name: Verify trimming compatibility + run: dotnet publish TrimmingTestApp test: name: Test / ${{ matrix.os }} / ${{ matrix.arch }} / ${{ matrix.tfm }} runs-on: ${{ matrix.os }} strategy: matrix: - arch: [ amd64 ] - os: [ windows-2019, macos-11 ] - tfm: [ net472, net6.0, net7.0 ] + arch: [ x64 ] + os: [ windows-2019, windows-2022, macos-11, macos-12, macos-13 ] + tfm: [ net472, net6.0, net8.0 ] exclude: - os: macos-11 tfm: net472 + - os: macos-11 + tfm: net8.0 + - os: macos-12 + tfm: net472 + - os: macos-13 + tfm: net472 + include: + - arch: arm64 + os: macos-14 + tfm: net6.0 + - arch: arm64 + os: macos-14 + tfm: net8.0 fail-fast: false steps: - name: Checkout - uses: actions/checkout@v3.5.0 + uses: actions/checkout@v4.1.2 with: fetch-depth: 0 - name: Install .NET SDK - uses: actions/setup-dotnet@v3.0.3 + uses: actions/setup-dotnet@v4.0.0 with: dotnet-version: | - 7.0.x + 8.0.x 6.0.x - name: Run ${{ matrix.tfm }} tests run: dotnet test LibGit2Sharp.sln --configuration Release --framework ${{ matrix.tfm }} --logger "GitHubActions" /p:ExtraDefine=LEAKS_IDENTIFYING @@ -62,22 +77,32 @@ jobs: matrix: arch: [ amd64 ] # arch: [ amd64, arm64 ] - distro: [ alpine.3.13, alpine.3.14, alpine.3.15, alpine.3.16, alpine.3.17, centos.7, centos.stream.8, debian.10, debian.11, fedora.36, ubuntu.18.04, ubuntu.20.04, ubuntu.22.04 ] - sdk: [ '6.0', '7.0' ] + distro: [ alpine.3.13, alpine.3.14, alpine.3.15, alpine.3.16, alpine.3.17, alpine.3.18, centos.stream.8, debian.10, debian.11, fedora.36, fedora.37, ubuntu.18.04, ubuntu.20.04, ubuntu.22.04 ] + sdk: [ '6.0', '8.0' ] exclude: - distro: alpine.3.13 - sdk: '7.0' + sdk: '8.0' - distro: alpine.3.14 - sdk: '7.0' + sdk: '8.0' + - distro: alpine.3.15 + sdk: '8.0' + - distro: alpine.3.16 + sdk: '8.0' + - distro: debian.10 + sdk: '8.0' + - distro: fedora.36 + sdk: '8.0' + - distro: ubuntu.18.04 + sdk: '8.0' include: - sdk: '6.0' tfm: net6.0 - - sdk: '7.0' - tfm: net7.0 + - sdk: '8.0' + tfm: net8.0 fail-fast: false steps: - name: Checkout - uses: actions/checkout@v3.5.0 + uses: actions/checkout@v4.1.2 with: fetch-depth: 0 - name: Setup QEMU diff --git a/.gitignore b/.gitignore index 2f75ccc1d..32e17b4d0 100644 --- a/.gitignore +++ b/.gitignore @@ -38,5 +38,5 @@ _ReSharper*/ *.DotSettings _NCrunch_LibGit2Sharp/ -packages/ +artifacts/ worktree.playlist diff --git a/CHANGES.md b/CHANGES.md index a3a74c0d5..1d68e6169 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,27 @@ # LibGit2Sharp Changes +## v0.30 - ([diff](https://github.com/libgit2/libgit2sharp/compare/0.29.0..0.30.0)) + +### Changes +- This release includes [libgit2 v1.7.2](https://github.com/libgit2/libgit2/releases/tag/v1.7.2). +- Updates for trimming compatibility [#2084](https://github.com/libgit2/libgit2sharp/pull/2084) +- Updates for .NET 8 [#2085](https://github.com/libgit2/libgit2sharp/pull/2085) + +## v0.29 - ([diff](https://github.com/libgit2/libgit2sharp/compare/0.28.0..0.29.0)) + +### Changes +- This release includes [libgit2 v1.7.1](https://github.com/libgit2/libgit2/releases/tag/v1.7.1). + - CI changes for the native binaries has removed support for CentOS 7. See [#2066](https://github.com/libgit2/libgit2sharp/pull/2066) for details. + +### Additions +- Add proxy options [#2065](https://github.com/libgit2/libgit2sharp/pull/2065) + - See PR for details, including some breaking changes to `CloneOptions` and `SubmoduleUpdateOptions` + +## v0.28 - ([diff](https://github.com/libgit2/libgit2sharp/compare/0.27.2..0.28.0)) + +### Additions +- Add CustomHeaders to PushOptions [#2052](https://github.com/libgit2/libgit2sharp/pull/2052) + ## v0.27.2 - ([diff](https://github.com/libgit2/libgit2sharp/compare/0.27.1..0.27.2)) ### Changes diff --git a/Directory.Build.props b/Directory.Build.props index 72eda8864..2c14cc2bd 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,10 +1,9 @@ + true true - $(MSBuildThisFileDirectory)bin\$(MSBuildProjectName)\$(Configuration)\ - $(MSBuildThisFileDirectory)obj\$(MSBuildProjectName)\ - $(MSBuildThisFileDirectory)bin\Packages\ + true $(DefineConstants);$(ExtraDefine) diff --git a/LibGit2Sharp.Tests/BranchFixture.cs b/LibGit2Sharp.Tests/BranchFixture.cs index 736b0faec..88247e256 100644 --- a/LibGit2Sharp.Tests/BranchFixture.cs +++ b/LibGit2Sharp.Tests/BranchFixture.cs @@ -4,7 +4,6 @@ using System.Linq; using LibGit2Sharp.Tests.TestHelpers; using Xunit; -using Xunit.Extensions; namespace LibGit2Sharp.Tests { @@ -103,7 +102,7 @@ public void CanCreateAnUnbornBranch() public void CanCreateBranchUsingAbbreviatedSha() { string path = SandboxBareTestRepo(); - using (var repo = new Repository(path, new RepositoryOptions{ Identity = Constants.Identity })) + using (var repo = new Repository(path, new RepositoryOptions { Identity = Constants.Identity })) { EnableRefLog(repo); @@ -1001,7 +1000,7 @@ public void OnlyOneBranchIsTheHead() continue; } - Assert.True(false, string.Format("Both '{0}' and '{1}' appear to be Head.", head.CanonicalName, branch.CanonicalName)); + Assert.Fail(string.Format("Both '{0}' and '{1}' appear to be Head.", head.CanonicalName, branch.CanonicalName)); } Assert.NotNull(head); diff --git a/LibGit2Sharp.Tests/CloneFixture.cs b/LibGit2Sharp.Tests/CloneFixture.cs index 1b26c1226..f205eddc2 100644 --- a/LibGit2Sharp.Tests/CloneFixture.cs +++ b/LibGit2Sharp.Tests/CloneFixture.cs @@ -151,10 +151,14 @@ public void CallsProgressCallbacks(string url) Repository.Clone(url, scd.DirectoryPath, new CloneOptions() { - OnTransferProgress = _ => { transferWasCalled = true; return true; }, - OnProgress = progress => { progressWasCalled = true; return true; }, - OnUpdateTips = (name, oldId, newId) => { updateTipsWasCalled = true; return true; }, + FetchOptions = + { + OnTransferProgress = _ => { transferWasCalled = true; return true; }, + OnProgress = progress => { progressWasCalled = true; return true; }, + OnUpdateTips = (name, oldId, newId) => { updateTipsWasCalled = true; return true; } + }, OnCheckoutProgress = (a, b, c) => checkoutWasCalled = true + }); Assert.True(transferWasCalled); @@ -174,7 +178,7 @@ public void CanCloneWithCredentials() string clonedRepoPath = Repository.Clone(Constants.PrivateRepoUrl, scd.DirectoryPath, new CloneOptions() { - CredentialsProvider = Constants.PrivateRepoCredentials + FetchOptions = { CredentialsProvider = Constants.PrivateRepoCredentials } }); @@ -249,43 +253,46 @@ public void CanInspectCertificateOnClone(string url, string hostname, Type certT var options = new CloneOptions { - CertificateCheck = (cert, valid, host) => + FetchOptions = { - wasCalled = true; - - Assert.Equal(hostname, host); - Assert.Equal(certType, cert.GetType()); - - if (certType == typeof(CertificateX509)) + CertificateCheck = (cert, valid, host) => { - Assert.True(valid); - var x509 = ((CertificateX509)cert).Certificate; - // we get a string with the different fields instead of a structure, so... - Assert.Contains("CN=github.com,", x509.Subject); - checksHappy = true; - return false; - } + wasCalled = true; + + Assert.Equal(hostname, host); + Assert.Equal(certType, cert.GetType()); + + if (certType == typeof(CertificateX509)) + { + Assert.True(valid); + var x509 = ((CertificateX509)cert).Certificate; + // we get a string with the different fields instead of a structure, so... + Assert.Contains("CN=github.com", x509.Subject); + checksHappy = true; + return false; + } + + if (certType == typeof(CertificateSsh)) + { + var hostkey = (CertificateSsh)cert; + Assert.True(hostkey.HasMD5); + /* + * Once you've connected and thus your ssh has stored the hostkey, + * you can get the hostkey for a host with + * + * ssh-keygen -F github.com -l | tail -n 1 | cut -d ' ' -f 2 | tr -d ':' + * + * though GitHub's hostkey won't change anytime soon. + */ + Assert.Equal("1627aca576282d36631b564debdfa648", + BitConverter.ToString(hostkey.HashMD5).ToLower().Replace("-", "")); + checksHappy = true; + return false; + } - if (certType == typeof(CertificateSsh)) - { - var hostkey = (CertificateSsh)cert; - Assert.True(hostkey.HasMD5); - /* - * Once you've connected and thus your ssh has stored the hostkey, - * you can get the hostkey for a host with - * - * ssh-keygen -F github.com -l | tail -n 1 | cut -d ' ' -f 2 | tr -d ':' - * - * though GitHub's hostkey won't change anytime soon. - */ - Assert.Equal("1627aca576282d36631b564debdfa648", - BitConverter.ToString(hostkey.HashMD5).ToLower().Replace("-", "")); - checksHappy = true; return false; } - - return false; - }, + } }; Assert.Throws(() => @@ -432,9 +439,12 @@ public void CanRecursivelyCloneSubmodules() { RecurseSubmodules = true, OnCheckoutProgress = checkoutProgressHandler, - OnUpdateTips = remoteRefUpdated, - RepositoryOperationStarting = repositoryOperationStarting, - RepositoryOperationCompleted = repositoryOperationCompleted, + FetchOptions = + { + OnUpdateTips = remoteRefUpdated, + RepositoryOperationStarting = repositoryOperationStarting, + RepositoryOperationCompleted = repositoryOperationCompleted + } }; string clonedRepoPath = Repository.Clone(uri.AbsolutePath, scd.DirectoryPath, options); @@ -517,7 +527,7 @@ public void CanCancelRecursiveClone() CloneOptions options = new CloneOptions() { RecurseSubmodules = true, - RepositoryOperationStarting = repositoryOperationStarting, + FetchOptions = { RepositoryOperationStarting = repositoryOperationStarting } }; Assert.Throws(() => @@ -557,10 +567,8 @@ public void CannotCloneWithForbiddenCustomHeaders() const string url = "https://github.com/libgit2/TestGitRepository"; const string knownHeader = "User-Agent: mygit-201"; - var cloneOptions = new CloneOptions() - { - FetchOptions = new FetchOptions { CustomHeaders = new String[] { knownHeader } } - }; + var cloneOptions = new CloneOptions(); + cloneOptions.FetchOptions.CustomHeaders = new string[] { knownHeader }; Assert.Throws(() => Repository.Clone(url, scd.DirectoryPath, cloneOptions)); } @@ -573,10 +581,8 @@ public void CannotCloneWithMalformedCustomHeaders() const string url = "https://github.com/libgit2/TestGitRepository"; const string knownHeader = "hello world"; - var cloneOptions = new CloneOptions() - { - FetchOptions = new FetchOptions { CustomHeaders = new String[] { knownHeader } } - }; + var cloneOptions = new CloneOptions(); + cloneOptions.FetchOptions.CustomHeaders = new string[] { knownHeader }; Assert.Throws(() => Repository.Clone(url, scd.DirectoryPath, cloneOptions)); } @@ -589,10 +595,8 @@ public void CanCloneWithCustomHeaders() const string url = "https://github.com/libgit2/TestGitRepository"; const string knownHeader = "X-Hello: world"; - var cloneOptions = new CloneOptions() - { - FetchOptions = new FetchOptions { CustomHeaders = new String[] { knownHeader } } - }; + var cloneOptions = new CloneOptions(); + cloneOptions.FetchOptions.CustomHeaders = new string[] { knownHeader }; var clonedRepoPath = Repository.Clone(url, scd.DirectoryPath, cloneOptions); Assert.True(Directory.Exists(clonedRepoPath)); diff --git a/LibGit2Sharp.Tests/CommitFixture.cs b/LibGit2Sharp.Tests/CommitFixture.cs index f555e7874..e99ca918f 100644 --- a/LibGit2Sharp.Tests/CommitFixture.cs +++ b/LibGit2Sharp.Tests/CommitFixture.cs @@ -3,10 +3,8 @@ using System.IO; using System.Linq; using System.Text; -using LibGit2Sharp.Core; using LibGit2Sharp.Tests.TestHelpers; using Xunit; -using Xunit.Extensions; namespace LibGit2Sharp.Tests { @@ -150,10 +148,10 @@ public void CanEnumerateCommitsWithReverseTimeSorting() using (var repo = new Repository(path)) { foreach (Commit commit in repo.Commits.QueryBy(new CommitFilter - { - IncludeReachableFrom = "a4a7dce85cf63874e984719f4fdd239f5145052f", - SortBy = CommitSortStrategies.Time | CommitSortStrategies.Reverse - })) + { + IncludeReachableFrom = "a4a7dce85cf63874e984719f4fdd239f5145052f", + SortBy = CommitSortStrategies.Time | CommitSortStrategies.Reverse + })) { Assert.NotNull(commit); Assert.StartsWith(reversedShas[count], commit.Sha); @@ -170,10 +168,10 @@ public void CanEnumerateCommitsWithReverseTopoSorting() using (var repo = new Repository(path)) { List commits = repo.Commits.QueryBy(new CommitFilter - { - IncludeReachableFrom = "a4a7dce85cf63874e984719f4fdd239f5145052f", - SortBy = CommitSortStrategies.Time | CommitSortStrategies.Reverse - }).ToList(); + { + IncludeReachableFrom = "a4a7dce85cf63874e984719f4fdd239f5145052f", + SortBy = CommitSortStrategies.Time | CommitSortStrategies.Reverse + }).ToList(); foreach (Commit commit in commits) { Assert.NotNull(commit); @@ -216,10 +214,10 @@ public void CanEnumerateCommitsWithTimeSorting() using (var repo = new Repository(path)) { foreach (Commit commit in repo.Commits.QueryBy(new CommitFilter - { - IncludeReachableFrom = "a4a7dce85cf63874e984719f4fdd239f5145052f", - SortBy = CommitSortStrategies.Time - })) + { + IncludeReachableFrom = "a4a7dce85cf63874e984719f4fdd239f5145052f", + SortBy = CommitSortStrategies.Time + })) { Assert.NotNull(commit); Assert.StartsWith(expectedShas[count], commit.Sha); @@ -236,10 +234,10 @@ public void CanEnumerateCommitsWithTopoSorting() using (var repo = new Repository(path)) { List commits = repo.Commits.QueryBy(new CommitFilter - { - IncludeReachableFrom = "a4a7dce85cf63874e984719f4fdd239f5145052f", - SortBy = CommitSortStrategies.Topological - }).ToList(); + { + IncludeReachableFrom = "a4a7dce85cf63874e984719f4fdd239f5145052f", + SortBy = CommitSortStrategies.Topological + }).ToList(); foreach (Commit commit in commits) { Assert.NotNull(commit); @@ -331,9 +329,12 @@ public void CanEnumerateCommitsFromTwoHeads() public void CanEnumerateCommitsFromMixedStartingPoints() { AssertEnumerationOfCommits( - repo => new CommitFilter { IncludeReachableFrom = new object[] { repo.Branches["br2"], + repo => new CommitFilter + { + IncludeReachableFrom = new object[] { repo.Branches["br2"], "refs/heads/master", - new ObjectId("e90810b8df3e80c413d903f631643c716887138d") } }, + new ObjectId("e90810b8df3e80c413d903f631643c716887138d") } + }, new[] { "4c062a6", "e90810b", "6dcf9bf", "a4a7dce", @@ -389,9 +390,9 @@ public void CanEnumerateAllCommits() { AssertEnumerationOfCommits( repo => new CommitFilter - { - IncludeReachableFrom = repo.Refs.OrderBy(r => r.CanonicalName, StringComparer.Ordinal), - }, + { + IncludeReachableFrom = repo.Refs.OrderBy(r => r.CanonicalName, StringComparer.Ordinal), + }, new[] { "44d5d18", "bb65291", "532740a", "503a16f", "3dfd6fd", @@ -406,7 +407,7 @@ public void CanEnumerateCommitsFromATagWhichPointsToABlob() { AssertEnumerationOfCommits( repo => new CommitFilter { IncludeReachableFrom = repo.Tags["point_to_blob"] }, - new string[] { }); + Array.Empty()); } [Fact] @@ -421,7 +422,7 @@ public void CanEnumerateCommitsFromATagWhichPointsToATree() AssertEnumerationOfCommitsInRepo(repo, r => new CommitFilter { IncludeReachableFrom = tag }, - new string[] { }); + Array.Empty()); } } @@ -680,8 +681,12 @@ public void CanCommitALittleBit() Assert.Equal(identity.Name, reflogEntry.Committer.Name); Assert.Equal(identity.Email, reflogEntry.Committer.Email); - var now = DateTimeOffset.Now; - Assert.InRange(reflogEntry.Committer.When, before, now); + // When verifying the timestamp range, give a little more room on the range. + // Git or file system datetime truncation seems to cause these stamps to jump up to a second earlier + // than we expect. See https://github.com/libgit2/libgit2sharp/issues/1764 + var low = before - TimeSpan.FromSeconds(1); + var high = DateTimeOffset.Now.TruncateMilliseconds() + TimeSpan.FromSeconds(1); + Assert.InRange(reflogEntry.Committer.When, low, high); Assert.Equal(commit.Id, reflogEntry.To); Assert.Equal(ObjectId.Zero, reflogEntry.From); @@ -859,21 +864,21 @@ public void CanRetrieveChildrenOfASpecificCommit() const string parentSha = "5b5b025afb0b4c913b4c338a42934a3863bf3644"; var filter = new CommitFilter - { - /* Revwalk from all the refs (git log --all) ... */ - IncludeReachableFrom = repo.Refs, + { + /* Revwalk from all the refs (git log --all) ... */ + IncludeReachableFrom = repo.Refs, - /* ... and stop when the parent is reached */ - ExcludeReachableFrom = parentSha - }; + /* ... and stop when the parent is reached */ + ExcludeReachableFrom = parentSha + }; var commits = repo.Commits.QueryBy(filter); var children = from c in commits - from p in c.Parents - let pId = p.Id - where pId.Sha == parentSha - select c; + from p in c.Parents + let pId = p.Id + where pId.Sha == parentSha + select c; var expectedChildren = new[] { "c47800c7266a2be04c571c04d5a6614691ea99bd", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045" }; diff --git a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs index 0fe7adee1..9212dfa99 100644 --- a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs +++ b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs @@ -9,7 +9,7 @@ namespace LibGit2Sharp.Tests { public class DiffTreeToTreeFixture : BaseFixture { - private static readonly string subBranchFilePath = String.Join("/", "1", "branch_file.txt"); + private static readonly string subBranchFilePath = string.Join("/", "1", "branch_file.txt"); [Fact] public void ComparingATreeAgainstItselfReturnsNoDifference() @@ -27,7 +27,7 @@ public void ComparingATreeAgainstItselfReturnsNoDifference() using (var patch = repo.Diff.Compare(tree, tree)) { Assert.Empty(patch); - Assert.Equal(String.Empty, patch); + Assert.Equal(string.Empty, patch); } } } diff --git a/LibGit2Sharp.Tests/FetchFixture.cs b/LibGit2Sharp.Tests/FetchFixture.cs index c0c6f0478..b36da7ccd 100644 --- a/LibGit2Sharp.Tests/FetchFixture.cs +++ b/LibGit2Sharp.Tests/FetchFixture.cs @@ -42,7 +42,7 @@ public void CanFetchIntoAnEmptyRepository(string url) } // Perform the actual fetch - Commands.Fetch(repo, remoteName, new string[0], new FetchOptions { OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler }, null); + Commands.Fetch(repo, remoteName, Array.Empty(), new FetchOptions { OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler }, null); // Verify the expected expectedFetchState.CheckUpdatedReferences(repo); @@ -62,7 +62,7 @@ public void CanFetchIntoAnEmptyRepositoryWithCredentials() repo.Network.Remotes.Add(remoteName, Constants.PrivateRepoUrl); // Perform the actual fetch - Commands.Fetch(repo, remoteName, new string[0], new FetchOptions + Commands.Fetch(repo, remoteName, Array.Empty(), new FetchOptions { CredentialsProvider = Constants.PrivateRepoCredentials }, null); @@ -98,7 +98,7 @@ public void CanFetchAllTagsIntoAnEmptyRepository(string url) } // Perform the actual fetch - Commands.Fetch(repo, remoteName, new string[0], new FetchOptions + Commands.Fetch(repo, remoteName, Array.Empty(), new FetchOptions { TagFetchMode = TagFetchMode.All, OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler @@ -179,7 +179,7 @@ public void FetchRespectsConfiguredAutoTagSetting(TagFetchMode tagFetchMode, int r => r.TagFetchMode = tagFetchMode); // Perform the actual fetch. - Commands.Fetch(repo, remoteName, new string[0], null, null); + Commands.Fetch(repo, remoteName, Array.Empty(), null, null); // Verify the number of fetched tags. Assert.Equal(expectedTagCount, repo.Tags.Count()); @@ -197,7 +197,7 @@ public void CanFetchAllTagsAfterAnInitialClone() using (var repo = new Repository(clonedRepoPath)) { - Commands.Fetch(repo, "origin", new string[0], new FetchOptions { TagFetchMode = TagFetchMode.All }, null); + Commands.Fetch(repo, "origin", Array.Empty(), new FetchOptions { TagFetchMode = TagFetchMode.All }, null); } } @@ -223,17 +223,17 @@ public void FetchHonorsTheFetchPruneConfigurationEntry() // No pruning when the configuration entry isn't defined Assert.Null(clonedRepo.Config.Get("fetch.prune")); - Commands.Fetch(clonedRepo, "origin", new string[0], null, null); + Commands.Fetch(clonedRepo, "origin", Array.Empty(), null, null); Assert.Equal(5, clonedRepo.Branches.Count(b => b.IsRemote && b.FriendlyName != "origin/HEAD")); // No pruning when the configuration entry is set to false clonedRepo.Config.Set("fetch.prune", false); - Commands.Fetch(clonedRepo, "origin", new string[0], null, null); + Commands.Fetch(clonedRepo, "origin", Array.Empty(), null, null); Assert.Equal(5, clonedRepo.Branches.Count(b => b.IsRemote && b.FriendlyName != "origin/HEAD")); // Auto pruning when the configuration entry is set to true clonedRepo.Config.Set("fetch.prune", true); - Commands.Fetch(clonedRepo, "origin", new string[0], null, null); + Commands.Fetch(clonedRepo, "origin", Array.Empty(), null, null); Assert.Equal(4, clonedRepo.Branches.Count(b => b.IsRemote && b.FriendlyName != "origin/HEAD")); } } @@ -248,10 +248,10 @@ public void CannotFetchWithForbiddenCustomHeaders() string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath); const string knownHeader = "User-Agent: mygit-201"; - var options = new FetchOptions { CustomHeaders = new String[] { knownHeader } }; + var options = new FetchOptions { CustomHeaders = new string[] { knownHeader } }; using (var repo = new Repository(clonedRepoPath)) { - Assert.Throws(() => Commands.Fetch(repo, "origin", new string[0], options, null)); + Assert.Throws(() => Commands.Fetch(repo, "origin", Array.Empty(), options, null)); } } @@ -265,10 +265,10 @@ public void CanFetchWithCustomHeaders() string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath); const string knownHeader = "X-Hello: mygit-201"; - var options = new FetchOptions { CustomHeaders = new String[] { knownHeader } }; + var options = new FetchOptions { CustomHeaders = new string[] { knownHeader } }; using (var repo = new Repository(clonedRepoPath)) { - Commands.Fetch(repo, "origin", new string[0], options, null); + Commands.Fetch(repo, "origin", Array.Empty(), options, null); } } @@ -282,10 +282,10 @@ public void CannotFetchWithMalformedCustomHeaders() string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath); const string knownHeader = "Hello world"; - var options = new FetchOptions { CustomHeaders = new String[] { knownHeader } }; + var options = new FetchOptions { CustomHeaders = new string[] { knownHeader } }; using (var repo = new Repository(clonedRepoPath)) { - Assert.Throws(() => Commands.Fetch(repo, "origin", new string[0], options, null)); + Assert.Throws(() => Commands.Fetch(repo, "origin", Array.Empty(), options, null)); } } diff --git a/LibGit2Sharp.Tests/FilterBranchFixture.cs b/LibGit2Sharp.Tests/FilterBranchFixture.cs index 60aee38f3..2ca3e507a 100644 --- a/LibGit2Sharp.Tests/FilterBranchFixture.cs +++ b/LibGit2Sharp.Tests/FilterBranchFixture.cs @@ -234,7 +234,7 @@ public void CanRewriteTreesByInjectingTreeEntry() AssertSucceedingButNotError(); - Assert.Equal(new Commit[0], + Assert.Equal(Array.Empty(), repo.Commits .QueryBy(new CommitFilter {IncludeReachableFrom = repo.Branches}) .Where(c => c["README"] != null diff --git a/LibGit2Sharp.Tests/FilterFixture.cs b/LibGit2Sharp.Tests/FilterFixture.cs index 4de354003..8fd9c8392 100644 --- a/LibGit2Sharp.Tests/FilterFixture.cs +++ b/LibGit2Sharp.Tests/FilterFixture.cs @@ -1,10 +1,9 @@ using System; using System.Collections.Generic; -using System.Linq; using System.IO; +using System.Threading.Tasks; using LibGit2Sharp.Tests.TestHelpers; using Xunit; -using System.Threading.Tasks; namespace LibGit2Sharp.Tests { @@ -174,7 +173,7 @@ public void CleanFilterWritesOutputToObjectTree() } [Fact] - public void CanHandleMultipleSmudgesConcurrently() + public async Task CanHandleMultipleSmudgesConcurrently() { const string decodedInput = "This is a substitution cipher"; const string encodedInput = "Guvf vf n fhofgvghgvba pvcure"; @@ -193,20 +192,18 @@ public void CanHandleMultipleSmudgesConcurrently() for (int i = 0; i < count; i++) { - tasks[i] = Task.Factory.StartNew(() => + tasks[i] = Task.Run(() => { string repoPath = InitNewRepository(); return CheckoutFileForSmudge(repoPath, branchName, encodedInput); }); } - Task.WaitAll(tasks); + var files = await Task.WhenAll(tasks); - foreach(var task in tasks) + foreach (var file in files) { - FileInfo expectedFile = task.Result; - - string readAllText = File.ReadAllText(expectedFile.FullName); + string readAllText = File.ReadAllText(file.FullName); Assert.Equal(decodedInput, readAllText); } } @@ -399,7 +396,7 @@ private FileInfo CheckoutFileForSmudge(string repoPath, string branchName, strin return expectedPath; } - private static FileInfo CommitFileOnBranch(Repository repo, string branchName, String content) + private static FileInfo CommitFileOnBranch(Repository repo, string branchName, string content) { var branch = repo.CreateBranch(branchName); Commands.Checkout(repo, branch.FriendlyName); diff --git a/LibGit2Sharp.Tests/GlobalSettingsFixture.cs b/LibGit2Sharp.Tests/GlobalSettingsFixture.cs index cd237663e..dd7aef1e1 100644 --- a/LibGit2Sharp.Tests/GlobalSettingsFixture.cs +++ b/LibGit2Sharp.Tests/GlobalSettingsFixture.cs @@ -100,7 +100,7 @@ public void SetExtensions() // Enable two new extensions (it will reset the configuration and "noop" will be enabled) GlobalSettings.SetExtensions("partialclone", "newext"); extensions = GlobalSettings.GetExtensions(); - Assert.Equal(new[] { "noop", "objectformat", "partialclone", "newext" }, extensions); + Assert.Equal(new[] { "newext", "noop", "objectformat", "partialclone" }, extensions); } } } diff --git a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj index c5cbb5f24..4d648eead 100644 --- a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj +++ b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj @@ -1,7 +1,7 @@  - net472;net6.0;net7.0 + net472;net6.0;net8.0 @@ -11,12 +11,12 @@ - - - - - - + + + + + + diff --git a/LibGit2Sharp.Tests/MetaFixture.cs b/LibGit2Sharp.Tests/MetaFixture.cs index b70d9022c..1d0a1d101 100644 --- a/LibGit2Sharp.Tests/MetaFixture.cs +++ b/LibGit2Sharp.Tests/MetaFixture.cs @@ -1,15 +1,12 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Globalization; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Text; using LibGit2Sharp.Tests.TestHelpers; using Xunit; -using Xunit.Extensions; -using Moq; namespace LibGit2Sharp.Tests { @@ -86,7 +83,7 @@ public void TypesInLibGit2DecoratedWithDebuggerDisplayMustFollowTheStandardImplP var typesWithDebuggerDisplayAndInvalidImplPattern = new List(); IEnumerable libGit2SharpTypes = typeof(IRepository).GetTypeInfo().Assembly.GetExportedTypes() - .Where(t => t.GetTypeInfo().GetCustomAttributes(typeof(DebuggerDisplayAttribute), false).Any()); + .Where(t => t.GetTypeInfo().GetCustomAttributes(typeof(DebuggerDisplayAttribute), false).Length != 0); foreach (Type type in libGit2SharpTypes) { @@ -113,9 +110,9 @@ public void TypesInLibGit2DecoratedWithDebuggerDisplayMustFollowTheStandardImplP } } - if (typesWithDebuggerDisplayAndInvalidImplPattern.Any()) + if (typesWithDebuggerDisplayAndInvalidImplPattern.Count != 0) { - Assert.True(false, Environment.NewLine + BuildMissingDebuggerDisplayPropertyMessage(typesWithDebuggerDisplayAndInvalidImplPattern)); + Assert.Fail(Environment.NewLine + BuildMissingDebuggerDisplayPropertyMessage(typesWithDebuggerDisplayAndInvalidImplPattern)); } } @@ -134,7 +131,7 @@ public void TypesInLibGit2SharpMustBeExtensibleInATestingContext() continue; var nonVirtualMethodNamesForType = GetNonVirtualPublicMethodsNames(type).ToList(); - if (nonVirtualMethodNamesForType.Any()) + if (nonVirtualMethodNamesForType.Count != 0) { nonTestableTypes.Add(type, nonVirtualMethodNamesForType); continue; @@ -168,9 +165,9 @@ public void TypesInLibGit2SharpMustBeExtensibleInATestingContext() } } - if (nonTestableTypes.Any()) + if (nonTestableTypes.Count != 0) { - Assert.True(false, Environment.NewLine + BuildNonTestableTypesMessage(nonTestableTypes)); + Assert.Fail(Environment.NewLine + BuildNonTestableTypesMessage(nonTestableTypes)); } } @@ -196,7 +193,7 @@ private static bool MustBeMockable(Type type) public void EnumsWithFlagsHaveMutuallyExclusiveValues() { var flagsEnums = typeof(IRepository).GetTypeInfo().Assembly.GetExportedTypes() - .Where(t => t.GetTypeInfo().IsEnum && t.GetTypeInfo().GetCustomAttributes(typeof(FlagsAttribute), false).Any()); + .Where(t => t.GetTypeInfo().IsEnum && t.GetTypeInfo().GetCustomAttributes(typeof(FlagsAttribute), false).Length != 0); var overlaps = from t in flagsEnums from int x in Enum.GetValues(t) @@ -280,7 +277,7 @@ public void GetEnumeratorMethodsInLibGit2SharpMustBeVirtualForTestability() (!m.IsVirtual || m.IsFinal)) .ToList(); - if (nonVirtualGetEnumeratorMethods.Any()) + if (nonVirtualGetEnumeratorMethods.Count != 0) { var sb = new StringBuilder(); @@ -290,7 +287,7 @@ public void GetEnumeratorMethodsInLibGit2SharpMustBeVirtualForTestability() method.DeclaringType, Environment.NewLine); } - Assert.True(false, Environment.NewLine + sb.ToString()); + Assert.Fail(Environment.NewLine + sb.ToString()); } } @@ -309,7 +306,7 @@ public void NoPublicTypesUnderLibGit2SharpCoreNamespace() .Where(t => t.FullName != "LibGit2Sharp.Core.LeaksContainer") .ToList(); - if (types.Any()) + if (types.Count != 0) { var sb = new StringBuilder(); @@ -319,7 +316,7 @@ public void NoPublicTypesUnderLibGit2SharpCoreNamespace() type.FullName, coreNamespace, Environment.NewLine); } - Assert.True(false, Environment.NewLine + sb.ToString()); + Assert.Fail(Environment.NewLine + sb.ToString()); } } diff --git a/LibGit2Sharp.Tests/NetworkFixture.cs b/LibGit2Sharp.Tests/NetworkFixture.cs index 3a3517432..f4ad922f6 100644 --- a/LibGit2Sharp.Tests/NetworkFixture.cs +++ b/LibGit2Sharp.Tests/NetworkFixture.cs @@ -249,7 +249,7 @@ public void CanMergeFetchedRefs() Assert.False(repo.RetrieveStatus().Any()); Assert.Equal(repo.Lookup("refs/remotes/origin/master~1"), repo.Head.Tip); - Commands.Fetch(repo, repo.Head.RemoteName, new string[0], null, null); + Commands.Fetch(repo, repo.Head.RemoteName, Array.Empty(), null, null); MergeOptions mergeOptions = new MergeOptions() { @@ -276,7 +276,7 @@ public void CanPruneRefs() using (var repo = new Repository(clonedRepoPath)) { repo.Network.Remotes.Add("pruner", clonedRepoPath2); - Commands.Fetch(repo, "pruner", new string[0], null, null); + Commands.Fetch(repo, "pruner", Array.Empty(), null, null); Assert.NotNull(repo.Refs["refs/remotes/pruner/master"]); // Remove the branch from the source repository @@ -286,11 +286,11 @@ public void CanPruneRefs() } // and by default we don't prune it - Commands.Fetch(repo, "pruner", new string[0], null, null); + Commands.Fetch(repo, "pruner", Array.Empty(), null, null); Assert.NotNull(repo.Refs["refs/remotes/pruner/master"]); // but we do when asked by the user - Commands.Fetch(repo, "pruner", new string[0], new FetchOptions { Prune = true }, null); + Commands.Fetch(repo, "pruner", Array.Empty(), new FetchOptions { Prune = true }, null); Assert.Null(repo.Refs["refs/remotes/pruner/master"]); } } diff --git a/LibGit2Sharp.Tests/PushFixture.cs b/LibGit2Sharp.Tests/PushFixture.cs index d8cf2befe..824c1d8c0 100644 --- a/LibGit2Sharp.Tests/PushFixture.cs +++ b/LibGit2Sharp.Tests/PushFixture.cs @@ -12,8 +12,7 @@ public class PushFixture : BaseFixture { private void OnPushStatusError(PushStatusError pushStatusErrors) { - Assert.True(false, string.Format("Failed to update reference '{0}': {1}", - pushStatusErrors.Reference, pushStatusErrors.Message)); + Assert.Fail(string.Format("Failed to update reference '{0}': {1}", pushStatusErrors.Reference, pushStatusErrors.Message)); } private void AssertPush(Action push) @@ -196,6 +195,33 @@ public void CanForcePush() } } + [Fact] + public void CanPushWithCustomHeaders() + { + const string knownHeader = "X-Hello: mygit-201"; + var options = new PushOptions { CustomHeaders = new string[] { knownHeader } }; + AssertPush(repo => + repo.Network.Push(repo.Network.Remotes["origin"], "HEAD", @"refs/heads/master", options)); + } + + [Fact] + public void CannotPushWithForbiddenCustomHeaders() + { + const string knownHeader = "User-Agent: mygit-201"; + var options = new PushOptions { CustomHeaders = new string[] { knownHeader } }; + Assert.Throws( + () => AssertPush(repo => repo.Network.Push(repo.Network.Remotes["origin"], "HEAD", @"refs/heads/master", options))); + } + + [Fact] + public void CannotPushWithMalformedCustomHeaders() + { + const string knownHeader = "Hello world"; + var options = new PushOptions { CustomHeaders = new string[] { knownHeader } }; + Assert.Throws( + () => AssertPush(repo => repo.Network.Push(repo.Network.Remotes["origin"], "HEAD", @"refs/heads/master", options))); + } + private static void AssertRemoteHeadTipEquals(IRepository localRepo, string sha) { var remoteReferences = localRepo.Network.ListReferences(localRepo.Network.Remotes.Single()); diff --git a/LibGit2Sharp.Tests/ReferenceFixture.cs b/LibGit2Sharp.Tests/ReferenceFixture.cs index 186d2e869..ad34d0fb2 100644 --- a/LibGit2Sharp.Tests/ReferenceFixture.cs +++ b/LibGit2Sharp.Tests/ReferenceFixture.cs @@ -894,7 +894,7 @@ public void CanHandleInvalidArguments() Assert.Throws(() => repo.Refs.ReachableFrom(null)); Assert.Throws(() => repo.Refs.ReachableFrom(null, repo.Commits.Take(2))); Assert.Throws(() => repo.Refs.ReachableFrom(repo.Refs, null)); - Assert.Empty(repo.Refs.ReachableFrom(new Commit[] { })); + Assert.Empty(repo.Refs.ReachableFrom(Array.Empty())); } } } diff --git a/LibGit2Sharp.Tests/ReflogFixture.cs b/LibGit2Sharp.Tests/ReflogFixture.cs index f93952e6e..52973454b 100644 --- a/LibGit2Sharp.Tests/ReflogFixture.cs +++ b/LibGit2Sharp.Tests/ReflogFixture.cs @@ -3,7 +3,6 @@ using System.Linq; using LibGit2Sharp.Tests.TestHelpers; using Xunit; -using Xunit.Extensions; namespace LibGit2Sharp.Tests { @@ -59,7 +58,7 @@ public void CommitShouldCreateReflogEntryOnHeadAndOnTargetedDirectReference() var identity = Constants.Identity; - using (var repo = new Repository(repoPath, new RepositoryOptions{ Identity = identity })) + using (var repo = new Repository(repoPath, new RepositoryOptions { Identity = identity })) { // setup refs as HEAD => unit_test => master var newRef = repo.Refs.Add("refs/heads/unit_test", "refs/heads/master"); @@ -84,8 +83,12 @@ public void CommitShouldCreateReflogEntryOnHeadAndOnTargetedDirectReference() Assert.Equal(identity.Name, reflogEntry.Committer.Name); Assert.Equal(identity.Email, reflogEntry.Committer.Email); - var now = DateTimeOffset.Now; - Assert.InRange(reflogEntry.Committer.When, before, now); + // When verifying the timestamp range, give a little more room on the range. + // Git or file system datetime truncation seems to cause these stamps to jump up to a second earlier + // than we expect. See https://github.com/libgit2/libgit2sharp/issues/1764 + var low = before - TimeSpan.FromSeconds(1); + var high = DateTimeOffset.Now.TruncateMilliseconds() + TimeSpan.FromSeconds(1); + Assert.InRange(reflogEntry.Committer.When, low, high); Assert.Equal(commit.Id, reflogEntry.To); Assert.Equal(ObjectId.Zero, reflogEntry.From); @@ -97,7 +100,7 @@ public void CommitShouldCreateReflogEntryOnHeadAndOnTargetedDirectReference() Assert.Equal(identity.Name, reflogEntry.Committer.Name); Assert.Equal(identity.Email, reflogEntry.Committer.Email); - Assert.InRange(reflogEntry.Committer.When, before, now); + Assert.InRange(reflogEntry.Committer.When, low, high); Assert.Equal(commit.Id, reflogEntry.To); Assert.Equal(ObjectId.Zero, reflogEntry.From); @@ -160,8 +163,12 @@ public void CommitOnDetachedHeadShouldInsertReflogEntry() Assert.Equal(identity.Name, reflogEntry.Committer.Name); Assert.Equal(identity.Email, reflogEntry.Committer.Email); - var now = DateTimeOffset.Now; - Assert.InRange(reflogEntry.Committer.When, before, now); + // When verifying the timestamp range, give a little more room on the range. + // Git or file system datetime truncation seems to cause these stamps to jump up to a second earlier + // than we expect. See https://github.com/libgit2/libgit2sharp/issues/1764 + var low = before - TimeSpan.FromSeconds(1); + var high = DateTimeOffset.Now.TruncateMilliseconds() + TimeSpan.FromSeconds(1); + Assert.InRange(reflogEntry.Committer.When, low, high); Assert.Equal(commit.Id, reflogEntry.To); Assert.Equal(string.Format("commit: {0}", commitMessage), repo.Refs.Log("HEAD").First().Message); @@ -202,7 +209,7 @@ public void AppendingToReflogDependsOnCoreLogAllRefUpdatesSetting(bool isBare, b public void UnsignedMethodsWriteCorrectlyToTheReflog() { var repoPath = InitNewRepository(true); - using (var repo = new Repository(repoPath, new RepositoryOptions{ Identity = Constants.Identity })) + using (var repo = new Repository(repoPath, new RepositoryOptions { Identity = Constants.Identity })) { EnableRefLog(repo); diff --git a/LibGit2Sharp.Tests/RemoveFixture.cs b/LibGit2Sharp.Tests/RemoveFixture.cs index e97636d9c..921bc417d 100644 --- a/LibGit2Sharp.Tests/RemoveFixture.cs +++ b/LibGit2Sharp.Tests/RemoveFixture.cs @@ -182,7 +182,7 @@ public void RemovingFileWithBadParamsThrows() { Assert.Throws(() => Commands.Remove(repo, string.Empty)); Assert.Throws(() => Commands.Remove(repo, (string)null)); - Assert.Throws(() => Commands.Remove(repo, new string[] { })); + Assert.Throws(() => Commands.Remove(repo, Array.Empty())); Assert.Throws(() => Commands.Remove(repo, new string[] { null })); } } diff --git a/LibGit2Sharp.Tests/RepositoryFixture.cs b/LibGit2Sharp.Tests/RepositoryFixture.cs index bf27b6091..4860e74ec 100644 --- a/LibGit2Sharp.Tests/RepositoryFixture.cs +++ b/LibGit2Sharp.Tests/RepositoryFixture.cs @@ -212,13 +212,13 @@ public void CanFetchFromRemoteByName() } // Perform the actual fetch - Commands.Fetch(repo, remoteName, new string[0], new FetchOptions { OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler }, null); + Commands.Fetch(repo, remoteName, Array.Empty(), new FetchOptions { OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler }, null); // Verify the expected state expectedFetchState.CheckUpdatedReferences(repo); // Now fetch the rest of the tags - Commands.Fetch(repo, remoteName, new string[0], new FetchOptions { TagFetchMode = TagFetchMode.All }, null); + Commands.Fetch(repo, remoteName, Array.Empty(), new FetchOptions { TagFetchMode = TagFetchMode.All }, null); // Verify that the "nearly-dangling" tag is now in the repo. Tag nearlyDanglingTag = repo.Tags["nearly-dangling"]; @@ -775,5 +775,46 @@ public void ReadingReferenceTargetFromListRemoteReferencesThrows(string url) }); } } + + [Fact] + public void RevWalkRepository() + { + string path = SandboxBareTestRepo(); + using (var repo = new Repository(path)) + using (var walker = new RevWalker(repo)) + { + walker.Sorting(CommitSortStrategies.Topological); + + Assert.Empty(GetCommits().ToList()); + + walker.PushRef("HEAD"); + Assert.Equal(7, GetCommits().Count()); + + walker.HideRef("HEAD"); + Assert.Empty(GetCommits()); + + walker.Reset(); + + walker.PushGlob("refs/heads/*"); + Assert.Equal(12, GetCommits().Count()); + + walker.HideGlob("refs/*"); + Assert.Empty(GetCommits()); + + IEnumerable GetCommits() + { + while (true) + { + var objectId = walker.Next(); + if (objectId == null) + break; + + var commit = repo.Lookup(objectId); + yield return commit; + } + } + } + + } } } diff --git a/LibGit2Sharp.Tests/StageFixture.cs b/LibGit2Sharp.Tests/StageFixture.cs index 51cb31a51..c087aa7be 100644 --- a/LibGit2Sharp.Tests/StageFixture.cs +++ b/LibGit2Sharp.Tests/StageFixture.cs @@ -265,7 +265,7 @@ public void StagingFileWithBadParamsThrows() { Assert.Throws(() => Commands.Stage(repo, string.Empty)); Assert.Throws(() => Commands.Stage(repo, (string)null)); - Assert.Throws(() => Commands.Stage(repo, new string[] { })); + Assert.Throws(() => Commands.Stage(repo, Array.Empty())); Assert.Throws(() => Commands.Stage(repo, new string[] { null })); } } @@ -362,7 +362,7 @@ public void IgnoredFilesAreOnlyStagedIfTheyreInTheRepo(string filename, FileStat using (var repo = new Repository(path)) { File.WriteAllText(Path.Combine(repo.Info.WorkingDirectory, ".gitignore"), - String.Format("{0}\n", filename)); + string.Format("{0}\n", filename)); Commands.Stage(repo, filename); Assert.Equal(expected, repo.RetrieveStatus(filename)); @@ -384,7 +384,7 @@ public void CanStageConflictedIgnoredFiles(string filename, FileStatus expected) using (var repo = new Repository(path)) { File.WriteAllText(Path.Combine(repo.Info.WorkingDirectory, ".gitignore"), - String.Format("{0}\n", filename)); + string.Format("{0}\n", filename)); Commands.Stage(repo, filename); Assert.Equal(expected, repo.RetrieveStatus(filename)); diff --git a/LibGit2Sharp.Tests/SubmoduleFixture.cs b/LibGit2Sharp.Tests/SubmoduleFixture.cs index 735bfd938..2d7f04e6d 100644 --- a/LibGit2Sharp.Tests/SubmoduleFixture.cs +++ b/LibGit2Sharp.Tests/SubmoduleFixture.cs @@ -3,7 +3,6 @@ using System.Linq; using LibGit2Sharp.Tests.TestHelpers; using Xunit; -using Xunit.Extensions; namespace LibGit2Sharp.Tests { @@ -240,9 +239,10 @@ public void CanUpdateSubmodule() OnCheckoutProgress = (x, y, z) => checkoutProgressCalled = true, OnCheckoutNotify = (x, y) => { checkoutNotifyCalled = true; return true; }, CheckoutNotifyFlags = CheckoutNotifyFlags.Updated, - OnUpdateTips = (x, y, z) => { updateTipsCalled = true; return true; }, }; + options.FetchOptions.OnUpdateTips = (x, y, z) => { updateTipsCalled = true; return true; }; + repo.Submodules.Init(submodule.Name, false); repo.Submodules.Update(submodule.Name, options); diff --git a/LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs b/LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs index 8d9b9ba0c..e9429d562 100644 --- a/LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs +++ b/LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs @@ -278,7 +278,7 @@ public virtual void Dispose() if (Core.LeaksContainer.TypeNames.Any()) { - Assert.False(true, string.Format("Some handles of the following types haven't been properly released: {0}.{1}" + Assert.Fail(string.Format("Some handles of the following types haven't been properly released: {0}.{1}" + "In order to get some help fixing those leaks, uncomment the define LEAKS_TRACKING in Libgit2Object.cs{1}" + "and run the tests locally.", string.Join(", ", Core.LeaksContainer.TypeNames), Environment.NewLine)); } @@ -471,10 +471,12 @@ protected static void AssertRefLogEntry(IRepository repo, string canonicalName, Assert.Equal(committer.Email, reflogEntry.Committer.Email); - // When verifying the timestamp range, give a little more room on the 'before' side. + // When verifying the timestamp range, give a little more room on the range. // Git or file system datetime truncation seems to cause these stamps to jump up to a second earlier // than we expect. See https://github.com/libgit2/libgit2sharp/issues/1764 - Assert.InRange(reflogEntry.Committer.When, before - TimeSpan.FromSeconds(1), DateTimeOffset.Now); + var low = before - TimeSpan.FromSeconds(1); + var high = DateTimeOffset.Now.TruncateMilliseconds() + TimeSpan.FromSeconds(1); + Assert.InRange(reflogEntry.Committer.When, low, high); } protected static void EnableRefLog(IRepository repository, bool enable = true) diff --git a/LibGit2Sharp.Tests/TestHelpers/Constants.cs b/LibGit2Sharp.Tests/TestHelpers/Constants.cs index b5cd96d7e..d8c14dbca 100644 --- a/LibGit2Sharp.Tests/TestHelpers/Constants.cs +++ b/LibGit2Sharp.Tests/TestHelpers/Constants.cs @@ -61,10 +61,10 @@ public static string BuildPath() if (Environment.GetEnvironmentVariables().Contains(LibGit2TestPath)) { Trace.TraceInformation("{0} environment variable detected", LibGit2TestPath); - tempPath = Environment.GetEnvironmentVariables()[LibGit2TestPath] as String; + tempPath = Environment.GetEnvironmentVariables()[LibGit2TestPath] as string; } - if (String.IsNullOrWhiteSpace(tempPath) || !Directory.Exists(tempPath)) + if (string.IsNullOrWhiteSpace(tempPath) || !Directory.Exists(tempPath)) { Trace.TraceInformation("Using default test path value"); tempPath = Path.GetTempPath(); diff --git a/LibGit2Sharp.Tests/UnstageFixture.cs b/LibGit2Sharp.Tests/UnstageFixture.cs index a5dc143d3..1eeee0e72 100644 --- a/LibGit2Sharp.Tests/UnstageFixture.cs +++ b/LibGit2Sharp.Tests/UnstageFixture.cs @@ -251,7 +251,7 @@ public void UnstagingFileWithBadParamsThrows() { Assert.Throws(() => Commands.Unstage(repo, string.Empty)); Assert.Throws(() => Commands.Unstage(repo, (string)null)); - Assert.Throws(() => Commands.Unstage(repo, new string[] { })); + Assert.Throws(() => Commands.Unstage(repo, Array.Empty())); Assert.Throws(() => Commands.Unstage(repo, new string[] { null })); } } diff --git a/LibGit2Sharp.Tests/desktop/SmartSubtransportFixture.cs b/LibGit2Sharp.Tests/desktop/SmartSubtransportFixture.cs index ff4d0fcd6..4e3b03ce3 100644 --- a/LibGit2Sharp.Tests/desktop/SmartSubtransportFixture.cs +++ b/LibGit2Sharp.Tests/desktop/SmartSubtransportFixture.cs @@ -61,7 +61,7 @@ public void CustomSmartSubtransportTest(string scheme, string url) } // Perform the actual fetch - Commands.Fetch(repo, remoteName, new string[0], + Commands.Fetch(repo, remoteName, Array.Empty(), new FetchOptions { OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler, TagFetchMode = TagFetchMode.Auto }, null); @@ -161,29 +161,29 @@ public void CannotUnregisterTwice() private class MockSmartSubtransport : RpcSmartSubtransport { - protected override SmartSubtransportStream Action(String url, GitSmartSubtransportAction action) + protected override SmartSubtransportStream Action(string url, GitSmartSubtransportAction action) { - String endpointUrl, contentType = null; + string endpointUrl, contentType = null; bool isPost = false; switch (action) { case GitSmartSubtransportAction.UploadPackList: - endpointUrl = String.Concat(url, "/info/refs?service=git-upload-pack"); + endpointUrl = string.Concat(url, "/info/refs?service=git-upload-pack"); break; case GitSmartSubtransportAction.UploadPack: - endpointUrl = String.Concat(url, "/git-upload-pack"); + endpointUrl = string.Concat(url, "/git-upload-pack"); contentType = "application/x-git-upload-pack-request"; isPost = true; break; case GitSmartSubtransportAction.ReceivePackList: - endpointUrl = String.Concat(url, "/info/refs?service=git-receive-pack"); + endpointUrl = string.Concat(url, "/info/refs?service=git-receive-pack"); break; case GitSmartSubtransportAction.ReceivePack: - endpointUrl = String.Concat(url, "/git-receive-pack"); + endpointUrl = string.Concat(url, "/git-receive-pack"); contentType = "application/x-git-receive-pack-request"; isPost = true; break; @@ -313,7 +313,7 @@ private HttpWebResponse GetResponseWithRedirects() } // rethrow if it's not 401 - throw ex; + throw; } if (response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect) diff --git a/LibGit2Sharp/AmbiguousSpecificationException.cs b/LibGit2Sharp/AmbiguousSpecificationException.cs index 16c77f6df..b5ddd7963 100644 --- a/LibGit2Sharp/AmbiguousSpecificationException.cs +++ b/LibGit2Sharp/AmbiguousSpecificationException.cs @@ -1,13 +1,17 @@ -using LibGit2Sharp.Core; using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif +using LibGit2Sharp.Core; namespace LibGit2Sharp { /// /// The exception that is thrown when the provided specification cannot uniquely identify a reference, an object or a path. /// +#if NETFRAMEWORK [Serializable] +#endif public class AmbiguousSpecificationException : NativeException { /// @@ -27,10 +31,10 @@ public AmbiguousSpecificationException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public AmbiguousSpecificationException(string format, params object[] args) - : base(String.Format(format, args)) + : base(string.Format(format, args)) { } @@ -43,6 +47,7 @@ public AmbiguousSpecificationException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -51,6 +56,7 @@ public AmbiguousSpecificationException(string message, Exception innerException) protected AmbiguousSpecificationException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif internal override GitErrorCode ErrorCode { diff --git a/LibGit2Sharp/BareRepositoryException.cs b/LibGit2Sharp/BareRepositoryException.cs index 7ee830a0c..412e5e4d4 100644 --- a/LibGit2Sharp/BareRepositoryException.cs +++ b/LibGit2Sharp/BareRepositoryException.cs @@ -1,5 +1,7 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif using LibGit2Sharp.Core; namespace LibGit2Sharp @@ -8,7 +10,9 @@ namespace LibGit2Sharp /// The exception that is thrown when an operation which requires a /// working directory is performed against a bare repository. /// +#if NETFRAMEWORK [Serializable] +#endif public class BareRepositoryException : NativeException { /// @@ -28,7 +32,7 @@ public BareRepositoryException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public BareRepositoryException(string format, params object[] args) : base(format, args) @@ -43,6 +47,7 @@ public BareRepositoryException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -51,6 +56,7 @@ public BareRepositoryException(string message, Exception innerException) protected BareRepositoryException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif internal BareRepositoryException(string message, GitErrorCategory category) : base(message, category) diff --git a/LibGit2Sharp/BlameHunk.cs b/LibGit2Sharp/BlameHunk.cs index 553efb14e..4feac239d 100644 --- a/LibGit2Sharp/BlameHunk.cs +++ b/LibGit2Sharp/BlameHunk.cs @@ -135,10 +135,10 @@ public bool Equals(BlameHunk other) } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as BlameHunk); diff --git a/LibGit2Sharp/BlameHunkCollection.cs b/LibGit2Sharp/BlameHunkCollection.cs index 869daf527..2766ee7a6 100644 --- a/LibGit2Sharp/BlameHunkCollection.cs +++ b/LibGit2Sharp/BlameHunkCollection.cs @@ -82,7 +82,7 @@ public virtual BlameHunk HunkForLine(int line) { return hunk; } - throw new ArgumentOutOfRangeException("line", "No hunk for that line"); + throw new ArgumentOutOfRangeException(nameof(line), "No hunk for that line"); } /// diff --git a/LibGit2Sharp/Blob.cs b/LibGit2Sharp/Blob.cs index d3cccf6ac..29ef8d812 100644 --- a/LibGit2Sharp/Blob.cs +++ b/LibGit2Sharp/Blob.cs @@ -13,7 +13,7 @@ namespace LibGit2Sharp /// public class Blob : GitObject { - private readonly ILazy lazySize; + private readonly ILazy lazySize; private readonly ILazy lazyIsBinary; /// diff --git a/LibGit2Sharp/CheckoutConflictException.cs b/LibGit2Sharp/CheckoutConflictException.cs index f2f5092e9..67dc8d2cc 100644 --- a/LibGit2Sharp/CheckoutConflictException.cs +++ b/LibGit2Sharp/CheckoutConflictException.cs @@ -1,5 +1,7 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif using LibGit2Sharp.Core; namespace LibGit2Sharp @@ -9,7 +11,9 @@ namespace LibGit2Sharp /// because of a conflicting change staged in the index, or unstaged /// in the working directory. /// +#if NETFRAMEWORK [Serializable] +#endif public class CheckoutConflictException : NativeException { /// @@ -29,7 +33,7 @@ public CheckoutConflictException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public CheckoutConflictException(string format, params object[] args) : base(format, args) @@ -44,6 +48,7 @@ public CheckoutConflictException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -52,6 +57,7 @@ public CheckoutConflictException(string message, Exception innerException) protected CheckoutConflictException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif internal CheckoutConflictException(string message, GitErrorCategory category) : base(message, category) diff --git a/LibGit2Sharp/CloneOptions.cs b/LibGit2Sharp/CloneOptions.cs index f88ff58d7..a315d76fc 100644 --- a/LibGit2Sharp/CloneOptions.cs +++ b/LibGit2Sharp/CloneOptions.cs @@ -6,7 +6,7 @@ namespace LibGit2Sharp /// /// Options to define clone behaviour /// - public sealed class CloneOptions : FetchOptionsBase, IConvertableToGitCheckoutOpts + public sealed class CloneOptions : IConvertableToGitCheckoutOpts { /// /// Creates default for a non-bare clone @@ -46,7 +46,7 @@ public CloneOptions() /// /// Gets or sets the fetch options. /// - public FetchOptions FetchOptions { get; set; } + public FetchOptions FetchOptions { get; } = new(); #region IConvertableToGitCheckoutOpts diff --git a/LibGit2Sharp/Commands/Fetch.cs b/LibGit2Sharp/Commands/Fetch.cs index d61fca5a5..e531aac51 100644 --- a/LibGit2Sharp/Commands/Fetch.cs +++ b/LibGit2Sharp/Commands/Fetch.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using LibGit2Sharp; using LibGit2Sharp.Core; using LibGit2Sharp.Core.Handles; @@ -75,7 +74,7 @@ public static void Fetch(Repository repository, string remote, IEnumerable(), options.FetchOptions, null); return repository.MergeFetchedRefs(merger, options.MergeOptions); } } diff --git a/LibGit2Sharp/Commands/Stage.cs b/LibGit2Sharp/Commands/Stage.cs index a1febafcb..d11bf6f76 100644 --- a/LibGit2Sharp/Commands/Stage.cs +++ b/LibGit2Sharp/Commands/Stage.cs @@ -237,7 +237,7 @@ public static void Move(IRepository repository, IEnumerable sourcePaths, if (batch.Count == 0) { - throw new ArgumentNullException("sourcePaths"); + throw new ArgumentNullException(nameof(sourcePaths)); } foreach (KeyValuePair, Tuple> keyValuePair in batch) diff --git a/LibGit2Sharp/CommitLog.cs b/LibGit2Sharp/CommitLog.cs index 4a6ab1de3..9b1e858a7 100644 --- a/LibGit2Sharp/CommitLog.cs +++ b/LibGit2Sharp/CommitLog.cs @@ -108,19 +108,34 @@ public IEnumerable QueryBy(string path, CommitFilter filter) private class CommitEnumerator : IEnumerator { private readonly Repository repo; - private readonly RevWalkerHandle handle; + private readonly RevWalker walker; private ObjectId currentOid; public CommitEnumerator(Repository repo, CommitFilter filter) { this.repo = repo; - handle = Proxy.git_revwalk_new(repo.Handle); - repo.RegisterForCleanup(handle); - Sort(filter.SortBy); - Push(filter.SinceList); - Hide(filter.UntilList); - FirstParentOnly(filter.FirstParentOnly); + walker = new RevWalker(repo); + + walker.Sorting(filter.SortBy); + + foreach (ObjectId actedOn in repo.Committishes(filter.SinceList).TakeWhile(o => o != null)) + { + walker.Push(actedOn); + } + + if(filter.UntilList != null) + { + foreach (ObjectId actedOn in repo.Committishes(filter.UntilList).TakeWhile(o => o != null)) + { + walker.Hide(actedOn); + } + } + + if (filter.FirstParentOnly) + { + walker.SimplifyFirstParent(); + } } #region IEnumerator Members @@ -137,21 +152,19 @@ object IEnumerator.Current public bool MoveNext() { - ObjectId id = Proxy.git_revwalk_next(handle); - + ObjectId id = walker.Next(); if (id == null) { return false; } currentOid = id; - return true; } public void Reset() { - Proxy.git_revwalk_reset(handle); + walker.Reset(); } #endregion @@ -164,47 +177,7 @@ public void Dispose() private void Dispose(bool disposing) { - handle.SafeDispose(); - } - - private delegate void HidePushSignature(RevWalkerHandle handle, ObjectId id); - - private void InternalHidePush(IList identifier, HidePushSignature hidePush) - { - IEnumerable oids = repo.Committishes(identifier).TakeWhile(o => o != null); - - foreach (ObjectId actedOn in oids) - { - hidePush(handle, actedOn); - } - } - - private void Push(IList identifier) - { - InternalHidePush(identifier, Proxy.git_revwalk_push); - } - - private void Hide(IList identifier) - { - if (identifier == null) - { - return; - } - - InternalHidePush(identifier, Proxy.git_revwalk_hide); - } - - private void Sort(CommitSortStrategies options) - { - Proxy.git_revwalk_sorting(handle, options); - } - - private void FirstParentOnly(bool firstParent) - { - if (firstParent) - { - Proxy.git_revwalk_simplify_first_parent(handle); - } + walker.SafeDispose(); } } } diff --git a/LibGit2Sharp/Conflict.cs b/LibGit2Sharp/Conflict.cs index 252535af1..705f66d15 100644 --- a/LibGit2Sharp/Conflict.cs +++ b/LibGit2Sharp/Conflict.cs @@ -61,12 +61,12 @@ public virtual IndexEntry Theirs } /// - /// Determines whether the specified is + /// Determines whether the specified is /// equal to the current . /// - /// The to compare with + /// The to compare with /// the current . - /// true if the specified is equal + /// true if the specified is equal /// to the current ; otherwise, /// false. public override bool Equals(object obj) diff --git a/LibGit2Sharp/Core/EncodingMarshaler.cs b/LibGit2Sharp/Core/EncodingMarshaler.cs index 0cafd9aa1..cb02c649b 100644 --- a/LibGit2Sharp/Core/EncodingMarshaler.cs +++ b/LibGit2Sharp/Core/EncodingMarshaler.cs @@ -32,7 +32,7 @@ public int GetNativeDataSize() return -1; } - public virtual IntPtr MarshalManagedToNative(Object managedObj) + public virtual IntPtr MarshalManagedToNative(object managedObj) { if (managedObj == null) { @@ -51,14 +51,14 @@ public virtual IntPtr MarshalManagedToNative(Object managedObj) return FromManaged(encoding, str); } - public virtual Object MarshalNativeToManaged(IntPtr pNativeData) + public virtual object MarshalNativeToManaged(IntPtr pNativeData) { return FromNative(encoding, pNativeData); } #endregion - public static unsafe IntPtr FromManaged(Encoding encoding, String value) + public static unsafe IntPtr FromManaged(Encoding encoding, string value) { if (encoding == null || value == null) { @@ -114,10 +114,10 @@ public static unsafe string FromNative(Encoding encoding, byte* pNativeData) if (walk == start) { - return String.Empty; + return string.Empty; } - return new String((sbyte*)pNativeData, 0, (int)(walk - start), encoding); + return new string((sbyte*)pNativeData, 0, (int)(walk - start), encoding); } public static unsafe string FromNative(Encoding encoding, IntPtr pNativeData, int length) @@ -129,10 +129,10 @@ public static unsafe string FromNative(Encoding encoding, IntPtr pNativeData, in if (length == 0) { - return String.Empty; + return string.Empty; } - return new String((sbyte*)pNativeData.ToPointer(), 0, length, encoding); + return new string((sbyte*)pNativeData.ToPointer(), 0, length, encoding); } public static string FromBuffer(Encoding encoding, byte[] buffer) @@ -160,7 +160,7 @@ public static string FromBuffer(Encoding encoding, byte[] buffer, int length) if (length == 0) { - return String.Empty; + return string.Empty; } return encoding.GetString(buffer, 0, length); diff --git a/LibGit2Sharp/Core/Ensure.cs b/LibGit2Sharp/Core/Ensure.cs index 3cf03d24b..7adea961a 100644 --- a/LibGit2Sharp/Core/Ensure.cs +++ b/LibGit2Sharp/Core/Ensure.cs @@ -49,7 +49,7 @@ public static void ArgumentNotNullOrEmptyString(string argumentValue, string arg { ArgumentNotNull(argumentValue, argumentName); - if (String.IsNullOrWhiteSpace (argumentValue)) + if (string.IsNullOrWhiteSpace (argumentValue)) { throw new ArgumentException("String cannot be empty", argumentName); } diff --git a/LibGit2Sharp/Core/FileHistory.cs b/LibGit2Sharp/Core/FileHistory.cs index 5c10a1a24..5775d0ab8 100644 --- a/LibGit2Sharp/Core/FileHistory.cs +++ b/LibGit2Sharp/Core/FileHistory.cs @@ -74,7 +74,7 @@ internal FileHistory(Repository repo, string path, CommitFilter queryFilter) if (!AllowedSortStrategies.Contains(queryFilter.SortBy)) { throw new ArgumentException("Unsupported sort strategy. Only 'Topological', 'Time', or 'Topological | Time' are allowed.", - "queryFilter"); + nameof(queryFilter)); } _repo = repo; diff --git a/LibGit2Sharp/Core/FilePathMarshaler.cs b/LibGit2Sharp/Core/FilePathMarshaler.cs index 209254ac5..af6afb048 100644 --- a/LibGit2Sharp/Core/FilePathMarshaler.cs +++ b/LibGit2Sharp/Core/FilePathMarshaler.cs @@ -57,7 +57,7 @@ internal class StrictFilePathMarshaler : StrictUtf8Marshaler #region ICustomMarshaler - public override IntPtr MarshalManagedToNative(Object managedObj) + public override IntPtr MarshalManagedToNative(object managedObj) { if (null == managedObj) { @@ -105,7 +105,7 @@ internal class LaxFilePathMarshaler : LaxUtf8Marshaler #region ICustomMarshaler - public override Object MarshalNativeToManaged(IntPtr pNativeData) + public override object MarshalNativeToManaged(IntPtr pNativeData) { return FromNative(pNativeData); } diff --git a/LibGit2Sharp/Core/GitBlame.cs b/LibGit2Sharp/Core/GitBlame.cs index df99f44b7..b2683cc73 100644 --- a/LibGit2Sharp/Core/GitBlame.cs +++ b/LibGit2Sharp/Core/GitBlame.cs @@ -46,7 +46,7 @@ internal class git_blame_options public uint version = 1; public GitBlameOptionFlags flags; - public UInt16 min_match_characters; + public ushort min_match_characters; public git_oid newest_commit; public git_oid oldest_commit; public UIntPtr min_line; diff --git a/LibGit2Sharp/Core/GitDiff.cs b/LibGit2Sharp/Core/GitDiff.cs index cc1ab1af1..44679124d 100644 --- a/LibGit2Sharp/Core/GitDiff.cs +++ b/LibGit2Sharp/Core/GitDiff.cs @@ -231,7 +231,7 @@ internal class GitDiffOptions : IDisposable public uint ContextLines; public uint InterhunkLines; public ushort IdAbbrev; - public Int64 MaxSize; + public long MaxSize; public IntPtr OldPrefixString; public IntPtr NewPrefixString; @@ -255,10 +255,10 @@ internal unsafe struct git_diff_file { public git_oid Id; public char* Path; - public Int64 Size; + public long Size; public GitDiffFlags Flags; - public UInt16 Mode; - public UInt16 IdAbbrev; + public ushort Mode; + public ushort IdAbbrev; } [StructLayout(LayoutKind.Sequential)] @@ -266,8 +266,8 @@ internal unsafe struct git_diff_delta { public ChangeKind status; public GitDiffFlags flags; - public UInt16 similarity; - public UInt16 nfiles; + public ushort similarity; + public ushort nfiles; public git_diff_file old_file; public git_diff_file new_file; } @@ -293,7 +293,7 @@ internal class GitDiffLine public int NewLineNo; public int NumLines; public UIntPtr contentLen; - public Int64 contentOffset; + public long contentOffset; public IntPtr content; } @@ -371,10 +371,10 @@ internal class GitDiffFindOptions { public uint Version = 1; public GitDiffFindFlags Flags; - public UInt16 RenameThreshold; - public UInt16 RenameFromRewriteThreshold; - public UInt16 CopyThreshold; - public UInt16 BreakRewriteThreshold; + public ushort RenameThreshold; + public ushort RenameFromRewriteThreshold; + public ushort CopyThreshold; + public ushort BreakRewriteThreshold; public UIntPtr RenameLimit; // TODO diff --git a/LibGit2Sharp/Core/GitFetchOptions.cs b/LibGit2Sharp/Core/GitFetchOptions.cs index d82e2f219..26f4c8c7e 100644 --- a/LibGit2Sharp/Core/GitFetchOptions.cs +++ b/LibGit2Sharp/Core/GitFetchOptions.cs @@ -11,6 +11,7 @@ internal class GitFetchOptions public bool UpdateFetchHead = true; public TagFetchMode download_tags; public GitProxyOptions ProxyOptions; + public int Depth = 0; // GIT_FETCH_DEPTH_FULL public RemoteRedirectMode FollowRedirects = RemoteRedirectMode.Initial; public GitStrArrayManaged CustomHeaders; } diff --git a/LibGit2Sharp/Core/GitFetchOptionsWrapper.cs b/LibGit2Sharp/Core/GitFetchOptionsWrapper.cs index 351947bbe..9c7f3952a 100644 --- a/LibGit2Sharp/Core/GitFetchOptionsWrapper.cs +++ b/LibGit2Sharp/Core/GitFetchOptionsWrapper.cs @@ -2,8 +2,8 @@ namespace LibGit2Sharp.Core { - /// - /// Git fetch options wrapper. Disposable wrapper for GitFetchOptions + /// + /// Git fetch options wrapper. Disposable wrapper for GitFetchOptions /// internal class GitFetchOptionsWrapper : IDisposable { @@ -11,7 +11,7 @@ public GitFetchOptionsWrapper() : this(new GitFetchOptions()) { } public GitFetchOptionsWrapper(GitFetchOptions fetchOptions) { - this.Options = fetchOptions; + Options = fetchOptions; } public GitFetchOptions Options { get; private set; } @@ -23,7 +23,8 @@ protected virtual void Dispose(bool disposing) if (disposedValue) return; - this.Options.CustomHeaders.Dispose(); + Options.CustomHeaders.Dispose(); + EncodingMarshaler.Cleanup(Options.ProxyOptions.Url); disposedValue = true; } diff --git a/LibGit2Sharp/Core/GitObjectLazyGroup.cs b/LibGit2Sharp/Core/GitObjectLazyGroup.cs index 11c83a81e..f00900837 100644 --- a/LibGit2Sharp/Core/GitObjectLazyGroup.cs +++ b/LibGit2Sharp/Core/GitObjectLazyGroup.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using LibGit2Sharp.Core.Handles; namespace LibGit2Sharp.Core @@ -21,7 +22,12 @@ protected override void EvaluateInternal(Action evaluator) } } +#if NET + public static ILazy Singleton<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TResult>(Repository repo, ObjectId id, Func resultSelector, bool throwIfMissing = false) +#else public static ILazy Singleton(Repository repo, ObjectId id, Func resultSelector, bool throwIfMissing = false) +#endif + { return Singleton(() => { diff --git a/LibGit2Sharp/Core/GitOdbBackend.cs b/LibGit2Sharp/Core/GitOdbBackend.cs index 6f017542d..c102c94eb 100644 --- a/LibGit2Sharp/Core/GitOdbBackend.cs +++ b/LibGit2Sharp/Core/GitOdbBackend.cs @@ -137,7 +137,7 @@ public delegate int write_callback( public delegate int writestream_callback( out IntPtr stream_out, IntPtr backend, - Int64 length, + long length, GitObjectType type); /// diff --git a/LibGit2Sharp/Core/GitOdbBackendStream.cs b/LibGit2Sharp/Core/GitOdbBackendStream.cs index 984274f04..14b126c7a 100644 --- a/LibGit2Sharp/Core/GitOdbBackendStream.cs +++ b/LibGit2Sharp/Core/GitOdbBackendStream.cs @@ -22,8 +22,8 @@ static GitOdbBackendStream() public GitOdbBackendStreamMode Mode; public IntPtr HashCtx; - public Int64 DeclaredSize; - public Int64 ReceivedBytes; + public long DeclaredSize; + public long ReceivedBytes; public read_callback Read; public write_callback Write; diff --git a/LibGit2Sharp/Core/GitProxyOptions.cs b/LibGit2Sharp/Core/GitProxyOptions.cs index b62b8e08f..85d4057ab 100644 --- a/LibGit2Sharp/Core/GitProxyOptions.cs +++ b/LibGit2Sharp/Core/GitProxyOptions.cs @@ -16,8 +16,8 @@ internal struct GitProxyOptions public uint Version; public GitProxyType Type; public IntPtr Url; - public IntPtr CredentialsCb; - public IntPtr CertificateCheck; - public IntPtr CbPayload; + public NativeMethods.git_cred_acquire_cb Credentials; + public NativeMethods.git_transport_certificate_check_cb CertificateCheck; + public IntPtr Payload; } } diff --git a/LibGit2Sharp/Core/GitProxyOptionsWrapper.cs b/LibGit2Sharp/Core/GitProxyOptionsWrapper.cs new file mode 100644 index 000000000..053213e96 --- /dev/null +++ b/LibGit2Sharp/Core/GitProxyOptionsWrapper.cs @@ -0,0 +1,32 @@ +using System; + +namespace LibGit2Sharp.Core +{ + internal class GitProxyOptionsWrapper : IDisposable + { + public GitProxyOptionsWrapper() : this(new GitProxyOptions()) { } + + public GitProxyOptionsWrapper(GitProxyOptions fetchOptions) + { + Options = fetchOptions; + } + + public GitProxyOptions Options { get; private set; } + + private bool disposedValue = false; + + protected virtual void Dispose(bool disposing) + { + if (disposedValue) + return; + + EncodingMarshaler.Cleanup(Options.Url); + disposedValue = true; + } + + public void Dispose() + { + Dispose(true); + } + } +} diff --git a/LibGit2Sharp/Core/GitPushOptionsWrapper.cs b/LibGit2Sharp/Core/GitPushOptionsWrapper.cs new file mode 100644 index 000000000..3ccffcf06 --- /dev/null +++ b/LibGit2Sharp/Core/GitPushOptionsWrapper.cs @@ -0,0 +1,37 @@ +using System; + +namespace LibGit2Sharp.Core +{ + /// + /// Git push options wrapper. Disposable wrapper for . + /// + internal class GitPushOptionsWrapper : IDisposable + { + public GitPushOptionsWrapper() : this(new GitPushOptions()) { } + + public GitPushOptionsWrapper(GitPushOptions pushOptions) + { + this.Options = pushOptions; + } + + public GitPushOptions Options { get; private set; } + + #region IDisposable + private bool disposedValue = false; // To detect redundant calls + protected virtual void Dispose(bool disposing) + { + if (disposedValue) + return; + + this.Options.CustomHeaders.Dispose(); + EncodingMarshaler.Cleanup(Options.ProxyOptions.Url); + disposedValue = true; + } + + public void Dispose() + { + Dispose(true); + } + #endregion + } +} diff --git a/LibGit2Sharp/Core/GitStrArrayNative.cs b/LibGit2Sharp/Core/GitStrArrayNative.cs index 8813f8e6e..01cd18e6e 100644 --- a/LibGit2Sharp/Core/GitStrArrayNative.cs +++ b/LibGit2Sharp/Core/GitStrArrayNative.cs @@ -15,11 +15,11 @@ internal struct GitStrArrayNative : IDisposable /// /// Enumerates each string from the array using the UTF-8 marshaler. /// - public String[] ReadStrings() + public string[] ReadStrings() { var count = checked((int)Array.Count.ToUInt32()); - String[] toReturn = new String[count]; + string[] toReturn = new string[count]; for (int i = 0; i < count; i++) { diff --git a/LibGit2Sharp/Core/GitSubmoduleOptions.cs b/LibGit2Sharp/Core/GitSubmoduleOptions.cs index 59c2b3f80..09a8e8265 100644 --- a/LibGit2Sharp/Core/GitSubmoduleOptions.cs +++ b/LibGit2Sharp/Core/GitSubmoduleOptions.cs @@ -11,8 +11,6 @@ internal struct GitSubmoduleUpdateOptions public GitFetchOptions FetchOptions; - public CheckoutStrategy CloneCheckoutStrategy; - public int AllowFetch; } } diff --git a/LibGit2Sharp/Core/HistoryRewriter.cs b/LibGit2Sharp/Core/HistoryRewriter.cs index c4cc2be8b..094d5ca1c 100644 --- a/LibGit2Sharp/Core/HistoryRewriter.cs +++ b/LibGit2Sharp/Core/HistoryRewriter.cs @@ -170,7 +170,7 @@ private Reference RewriteReference( if (repo.Refs.Resolve(backupName) != null) { - throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, + throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Can't back up reference '{0}' - '{1}' already exists", oldRef.CanonicalName, backupName)); diff --git a/LibGit2Sharp/Core/LazyGroup.cs b/LibGit2Sharp/Core/LazyGroup.cs index d8b13fa42..bcd160290 100644 --- a/LibGit2Sharp/Core/LazyGroup.cs +++ b/LibGit2Sharp/Core/LazyGroup.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace LibGit2Sharp.Core { @@ -44,7 +45,11 @@ public void Evaluate() protected abstract void EvaluateInternal(Action evaluator); +#if NET + protected static ILazy Singleton<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TResult>(Func resultSelector) +#else protected static ILazy Singleton(Func resultSelector) +#endif { return new LazyWrapper(resultSelector); } @@ -90,7 +95,11 @@ void IEvaluator.Evaluate(TInput input) } } +#if NET + protected class LazyWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TType> : Lazy, ILazy +#else protected class LazyWrapper : Lazy, ILazy +#endif { public LazyWrapper(Func evaluator) : base(evaluator) diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index e20d755ba..a81c60255 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -102,7 +102,7 @@ private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImpor { // The libraries are located at 'runtimes//native/lib{libraryName}.so' // The ends with the processor architecture. e.g. fedora-x64. - string assemblyDirectory = Path.GetDirectoryName(typeof(NativeMethods).Assembly.Location); + string assemblyDirectory = Path.GetDirectoryName(AppContext.BaseDirectory); string processorArchitecture = RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant(); string runtimesDirectory = Path.Combine(assemblyDirectory, "runtimes"); @@ -177,11 +177,11 @@ internal static extern int git_error_set_str( internal static extern void git_error_set_oom(); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] - internal static extern unsafe UInt32 git_blame_get_hunk_count(git_blame* blame); + internal static extern unsafe uint git_blame_get_hunk_count(git_blame* blame); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe git_blame_hunk* git_blame_get_hunk_byindex( - git_blame* blame, UInt32 index); + git_blame* blame, uint index); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe int git_blame_file( @@ -227,7 +227,7 @@ internal static extern unsafe int git_blob_filtered_content( internal static extern unsafe IntPtr git_blob_rawcontent(git_object* blob); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] - internal static extern unsafe Int64 git_blob_rawsize(git_object* blob); + internal static extern unsafe long git_blob_rawsize(git_object* blob); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe int git_branch_create_from_annotated( @@ -1078,7 +1078,7 @@ internal static extern unsafe int git_odb_foreach( IntPtr payload); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] - internal static extern unsafe int git_odb_open_wstream(out git_odb_stream* stream, git_odb* odb, Int64 size, GitObjectType type); + internal static extern unsafe int git_odb_open_wstream(out git_odb_stream* stream, git_odb* odb, long size, GitObjectType type); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe void git_odb_free(git_odb* odb); @@ -1175,7 +1175,7 @@ internal static extern unsafe int git_packbuilder_insert_tree( internal static extern unsafe UIntPtr git_packbuilder_object_count(git_packbuilder* packbuilder); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] - internal static extern unsafe UInt32 git_packbuilder_set_threads(git_packbuilder* packbuilder, UInt32 numThreads); + internal static extern unsafe uint git_packbuilder_set_threads(git_packbuilder* packbuilder, uint numThreads); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe int git_packbuilder_write( @@ -1686,6 +1686,14 @@ internal static extern unsafe int git_revparse_ext( [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe int git_revwalk_hide(git_revwalk* walker, ref GitOid commit_id); + [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] + internal static extern unsafe int git_revwalk_hide_glob(git_revwalk* walker, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string glob); + + [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] + internal static extern unsafe int git_revwalk_hide_ref(git_revwalk* walker, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string refname); + [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe int git_revwalk_new(out git_revwalk* walker, git_repository* repo); @@ -1695,6 +1703,14 @@ internal static extern unsafe int git_revparse_ext( [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe int git_revwalk_push(git_revwalk* walker, ref GitOid id); + [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] + internal static extern unsafe int git_revwalk_push_glob(git_revwalk* walker, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string glob); + + [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] + internal static extern unsafe int git_revwalk_push_ref(git_revwalk* walker, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string refname); + [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe int git_revwalk_reset(git_revwalk* walker); diff --git a/LibGit2Sharp/Core/Platform.cs b/LibGit2Sharp/Core/Platform.cs index 42b752612..1fcb59faf 100644 --- a/LibGit2Sharp/Core/Platform.cs +++ b/LibGit2Sharp/Core/Platform.cs @@ -58,7 +58,11 @@ public static string GetNativeLibraryExtension() /// Returns true if the runtime is Mono. /// public static bool IsRunningOnMono() +#if NETFRAMEWORK => Type.GetType("Mono.Runtime") != null; +#else + => false; +#endif /// /// Returns true if the runtime is .NET Framework. diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index 50cefc0df..30409b830 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -86,7 +86,7 @@ public static unsafe UnmanagedMemoryStream git_blob_filtered_content_stream(Repo new[] { buf }); } - public static unsafe UnmanagedMemoryStream git_blob_rawcontent_stream(RepositoryHandle repo, ObjectId id, Int64 size) + public static unsafe UnmanagedMemoryStream git_blob_rawcontent_stream(RepositoryHandle repo, ObjectId id, long size) { var handle = new ObjectSafeWrapper(id, repo, throwIfMissing: true).ObjectPtr; return new RawContentStream(handle, h => NativeMethods.git_blob_rawcontent(h), h => size); @@ -341,7 +341,7 @@ public static unsafe string git_commit_create_buffer( using (var treeHandle = Proxy.git_object_lookup(tree.repo.Handle, tree.Id, GitObjectType.Tree)) using (var buf = new GitBuf()) { - ObjectHandle[] handles = new ObjectHandle[0]; + ObjectHandle[] handles = Array.Empty(); try { handles = parents.Select(c => Proxy.git_object_lookup(c.repo.Handle, c.Id, GitObjectType.Commit)).ToArray(); @@ -871,7 +871,7 @@ public static int git_error_set_str(GitErrorCategory error_class, Exception exce } } - public static int git_error_set_str(GitErrorCategory error_class, String errorString) + public static int git_error_set_str(GitErrorCategory error_class, string errorString) { return NativeMethods.git_error_set_str(error_class, errorString); } @@ -2368,7 +2368,7 @@ public static unsafe IEnumerable git_remote_ls(Repository repository, } var refs = directRefs.Values.ToList(); - refs.Sort((r1, r2) => String.CompareOrdinal(r1.CanonicalName, r2.CanonicalName)); + refs.Sort((r1, r2) => string.CompareOrdinal(r1.CanonicalName, r2.CanonicalName)); return refs; } @@ -2752,6 +2752,18 @@ public static unsafe void git_revwalk_hide(RevWalkerHandle walker, ObjectId comm Ensure.ZeroResult(res); } + public static unsafe void git_revwalk_hide_glob(RevWalkerHandle walker, string glob) + { + int res = NativeMethods.git_revwalk_hide_glob(walker, glob); + Ensure.ZeroResult(res); + } + + public static unsafe void git_revwalk_hide_ref(RevWalkerHandle walker, string refName) + { + int res = NativeMethods.git_revwalk_hide_ref(walker, refName); + Ensure.ZeroResult(res); + } + public static unsafe RevWalkerHandle git_revwalk_new(RepositoryHandle repo) { git_revwalk* handle; @@ -2783,19 +2795,34 @@ public static unsafe void git_revwalk_push(RevWalkerHandle walker, ObjectId id) Ensure.ZeroResult(res); } + public static unsafe void git_revwalk_push_glob(RevWalkerHandle walker, string glob) + { + int res = NativeMethods.git_revwalk_push_glob(walker, glob); + Ensure.ZeroResult(res); + } + + public static unsafe void git_revwalk_push_ref(RevWalkerHandle walker, string refName) + { + int res = NativeMethods.git_revwalk_push_ref(walker, refName); + Ensure.ZeroResult(res); + } + public static unsafe void git_revwalk_reset(RevWalkerHandle walker) { - NativeMethods.git_revwalk_reset(walker); + int res = NativeMethods.git_revwalk_reset(walker); + Ensure.ZeroResult(res); } - public static unsafe int git_revwalk_sorting(RevWalkerHandle walker, CommitSortStrategies options) + public static unsafe void git_revwalk_sorting(RevWalkerHandle walker, CommitSortStrategies options) { - return NativeMethods.git_revwalk_sorting(walker, options); + int res = NativeMethods.git_revwalk_sorting(walker, options); + Ensure.ZeroResult(res); } - public static unsafe int git_revwalk_simplify_first_parent(RevWalkerHandle walker) + public static unsafe void git_revwalk_simplify_first_parent(RevWalkerHandle walker) { - return NativeMethods.git_revwalk_simplify_first_parent(walker); + int res = NativeMethods.git_revwalk_simplify_first_parent(walker); + Ensure.ZeroResult(res); } #endregion @@ -3211,7 +3238,7 @@ public static void git_trace_set(LogLevel level, NativeMethods.git_trace_cb call #region git_transport_ - public static void git_transport_register(String prefix, IntPtr transport_cb, IntPtr param) + public static void git_transport_register(string prefix, IntPtr transport_cb, IntPtr param) { int res = NativeMethods.git_transport_register(prefix, transport_cb, param); @@ -3224,7 +3251,7 @@ public static void git_transport_register(String prefix, IntPtr transport_cb, In Ensure.ZeroResult(res); } - public static void git_transport_unregister(String prefix) + public static void git_transport_unregister(string prefix) { int res = NativeMethods.git_transport_unregister(prefix); @@ -3707,7 +3734,7 @@ private static ICollection git_foreach( if (ignoredErrorCodes != null && ignoredErrorCodes.Contains((GitErrorCode)res)) { - return new TResult[0]; + return Array.Empty(); } Ensure.ZeroResult(res); @@ -3728,7 +3755,7 @@ private static ICollection git_foreach( if (ignoredErrorCodes != null && ignoredErrorCodes.Contains((GitErrorCode)res)) { - return new TResult[0]; + return Array.Empty(); } Ensure.ZeroResult(res); @@ -3749,7 +3776,7 @@ private static ICollection git_foreach( if (ignoredErrorCodes != null && ignoredErrorCodes.Contains((GitErrorCode)res)) { - return new TResult[0]; + return Array.Empty(); } Ensure.ZeroResult(res); @@ -3772,7 +3799,7 @@ private static ICollection git_foreach( if (ignoredErrorCodes != null && ignoredErrorCodes.Contains((GitErrorCode)res)) { - return new TResult[0]; + return Array.Empty(); } Ensure.ZeroResult(res); diff --git a/LibGit2Sharp/Core/TarWriter.cs b/LibGit2Sharp/Core/TarWriter.cs index 609449316..0a051b9e6 100644 --- a/LibGit2Sharp/Core/TarWriter.cs +++ b/LibGit2Sharp/Core/TarWriter.cs @@ -301,31 +301,31 @@ public UsTarHeader( if (userName.Length > 32) { - throw new ArgumentException("ustar userName cannot be longer than 32 characters.", "userName"); + throw new ArgumentException("ustar userName cannot be longer than 32 characters.", nameof(userName)); } if (groupName.Length > 32) { - throw new ArgumentException("ustar groupName cannot be longer than 32 characters.", "groupName"); + throw new ArgumentException("ustar groupName cannot be longer than 32 characters.", nameof(groupName)); } if (userId.Length > 7) { - throw new ArgumentException("ustar userId cannot be longer than 7 characters.", "userId"); + throw new ArgumentException("ustar userId cannot be longer than 7 characters.", nameof(userId)); } if (groupId.Length > 7) { - throw new ArgumentException("ustar groupId cannot be longer than 7 characters.", "groupId"); + throw new ArgumentException("ustar groupId cannot be longer than 7 characters.", nameof(groupId)); } if (deviceMajorNumber.Length > 7) { - throw new ArgumentException("ustar deviceMajorNumber cannot be longer than 7 characters.", "deviceMajorNumber"); + throw new ArgumentException("ustar deviceMajorNumber cannot be longer than 7 characters.", nameof(deviceMajorNumber)); } if (deviceMinorNumber.Length > 7) { - throw new ArgumentException("ustar deviceMinorNumber cannot be longer than 7 characters.", "deviceMinorNumber"); + throw new ArgumentException("ustar deviceMinorNumber cannot be longer than 7 characters.", nameof(deviceMinorNumber)); } if (link.Length > 100) { - throw new ArgumentException("ustar link cannot be longer than 100 characters.", "link"); + throw new ArgumentException("ustar link cannot be longer than 100 characters.", nameof(link)); } #endregion diff --git a/LibGit2Sharp/Core/Utf8Marshaler.cs b/LibGit2Sharp/Core/Utf8Marshaler.cs index a6fddb808..54e0086cb 100644 --- a/LibGit2Sharp/Core/Utf8Marshaler.cs +++ b/LibGit2Sharp/Core/Utf8Marshaler.cs @@ -67,7 +67,7 @@ public static ICustomMarshaler GetInstance(string cookie) #region ICustomMarshaler - public override Object MarshalNativeToManaged(IntPtr pNativeData) + public override object MarshalNativeToManaged(IntPtr pNativeData) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} cannot be used to retrieve data from libgit2.", @@ -76,7 +76,7 @@ public override Object MarshalNativeToManaged(IntPtr pNativeData) #endregion - public static IntPtr FromManaged(String value) + public static IntPtr FromManaged(string value) { return FromManaged(encoding, value); } diff --git a/LibGit2Sharp/Diff.cs b/LibGit2Sharp/Diff.cs index 087ee8d6d..857eb8ed1 100644 --- a/LibGit2Sharp/Diff.cs +++ b/LibGit2Sharp/Diff.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.Linq; using System.Text; using LibGit2Sharp.Core; @@ -546,7 +545,7 @@ private DiffHandle BuildDiffList( MatchedPathsAggregator matchedPaths = null; - // We can't match paths unless we've got something to match + // We can't match paths unless we've got something to match // against and we're told to do so. if (filePaths != null && explicitPathsOptions != null) { @@ -653,7 +652,7 @@ private static void DispatchUnmatchedPaths( List unmatchedPaths = (filePaths != null ? filePaths.Except(matchedPaths) : Enumerable.Empty()).ToList(); - if (!unmatchedPaths.Any()) + if (unmatchedPaths.Count == 0) { return; } diff --git a/LibGit2Sharp/EmptyCommitException.cs b/LibGit2Sharp/EmptyCommitException.cs index 8cd48e49f..00d1081e5 100644 --- a/LibGit2Sharp/EmptyCommitException.cs +++ b/LibGit2Sharp/EmptyCommitException.cs @@ -1,5 +1,7 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif namespace LibGit2Sharp { @@ -7,7 +9,9 @@ namespace LibGit2Sharp /// The exception that is thrown when a commit would create an "empty" /// commit that is treesame to its parent without an explicit override. /// +#if NETFRAMEWORK [Serializable] +#endif public class EmptyCommitException : LibGit2SharpException { /// @@ -27,7 +31,7 @@ public EmptyCommitException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public EmptyCommitException(string format, params object[] args) : base(format, args) @@ -42,6 +46,7 @@ public EmptyCommitException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -50,5 +55,6 @@ public EmptyCommitException(string message, Exception innerException) protected EmptyCommitException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif } } diff --git a/LibGit2Sharp/EntryExistsException.cs b/LibGit2Sharp/EntryExistsException.cs index 2c46e4acd..3ebfbdfba 100644 --- a/LibGit2Sharp/EntryExistsException.cs +++ b/LibGit2Sharp/EntryExistsException.cs @@ -1,5 +1,8 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif + using LibGit2Sharp.Core; namespace LibGit2Sharp @@ -7,7 +10,9 @@ namespace LibGit2Sharp /// /// The exception that is thrown attempting to create a resource that already exists. /// +#if NETFRAMEWORK [Serializable] +#endif public class EntryExistsException : LibGit2SharpException { /// @@ -27,7 +32,7 @@ public EntryExistsException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public EntryExistsException(string format, params object[] args) : base(format, args) @@ -42,6 +47,7 @@ public EntryExistsException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -50,6 +56,7 @@ public EntryExistsException(string message, Exception innerException) protected EntryExistsException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif internal EntryExistsException(string message, GitErrorCode code, GitErrorCategory category) : base(message, code, category) diff --git a/LibGit2Sharp/FetchHead.cs b/LibGit2Sharp/FetchHead.cs index 456abedc2..812865cf3 100644 --- a/LibGit2Sharp/FetchHead.cs +++ b/LibGit2Sharp/FetchHead.cs @@ -61,7 +61,7 @@ public virtual GitObject Target /// The URL of the remote repository this /// has been built from. /// - public virtual String Url { get; private set; } + public virtual string Url { get; private set; } /// /// Determines if this fetch head entry has been explicitly fetched. diff --git a/LibGit2Sharp/FetchOptions.cs b/LibGit2Sharp/FetchOptions.cs index 487baed97..5bcf74bfa 100644 --- a/LibGit2Sharp/FetchOptions.cs +++ b/LibGit2Sharp/FetchOptions.cs @@ -28,14 +28,14 @@ public sealed class FetchOptions : FetchOptionsBase /// /// Get/Set the custom headers. - /// - /// - /// This allows you to set custom headers (e.g. X-Forwarded-For, + /// + /// + /// This allows you to set custom headers (e.g. X-Forwarded-For, /// X-Request-Id, etc), /// /// /// - /// Libgit2 sets some headers for HTTP requests (User-Agent, Host, + /// Libgit2 sets some headers for HTTP requests (User-Agent, Host, /// Accept, Content-Type, Transfer-Encoding, Content-Length, Accept) that /// cannot be overriden. /// diff --git a/LibGit2Sharp/FetchOptionsBase.cs b/LibGit2Sharp/FetchOptionsBase.cs index 751678cf9..0e548652f 100644 --- a/LibGit2Sharp/FetchOptionsBase.cs +++ b/LibGit2Sharp/FetchOptionsBase.cs @@ -35,7 +35,7 @@ internal FetchOptionsBase() /// /// This handler will be called to let the user make a decision on whether to allow - /// the connection to preoceed based on the certificate presented by the server. + /// the connection to proceed based on the certificate presented by the server. /// public CertificateCheckHandler CertificateCheck { get; set; } @@ -48,5 +48,10 @@ internal FetchOptionsBase() /// Completed operating on the current repository. /// public RepositoryOperationCompleted RepositoryOperationCompleted { get; set; } + + /// + /// Options for connecting through a proxy. + /// + public ProxyOptions ProxyOptions { get; } = new(); } } diff --git a/LibGit2Sharp/Filter.cs b/LibGit2Sharp/Filter.cs index 5a46e12b5..50f5fe8c5 100644 --- a/LibGit2Sharp/Filter.cs +++ b/LibGit2Sharp/Filter.cs @@ -165,10 +165,10 @@ protected virtual void Smudge(string path, string root, Stream input, Stream out } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as Filter); @@ -306,7 +306,7 @@ int StreamCloseCallback(IntPtr stream) if(!activeStreams.TryGetValue(stream, out state)) { - throw new ArgumentException("Unknown stream pointer", "stream"); + throw new ArgumentException("Unknown stream pointer", nameof(stream)); } Ensure.ArgumentIsExpectedIntPtr(stream, state.thisPtr, "stream"); @@ -339,7 +339,7 @@ void StreamFreeCallback(IntPtr stream) if (!activeStreams.TryRemove(stream, out state)) { - throw new ArgumentException("Double free or invalid stream pointer", "stream"); + throw new ArgumentException("Double free or invalid stream pointer", nameof(stream)); } Ensure.ArgumentIsExpectedIntPtr(stream, state.thisPtr, "stream"); @@ -365,7 +365,7 @@ unsafe int StreamWriteCallback(IntPtr stream, IntPtr buffer, UIntPtr len) if (!activeStreams.TryGetValue(stream, out state)) { - throw new ArgumentException("Invalid or already freed stream pointer", "stream"); + throw new ArgumentException("Invalid or already freed stream pointer", nameof(stream)); } Ensure.ArgumentIsExpectedIntPtr(stream, state.thisPtr, "stream"); diff --git a/LibGit2Sharp/GitObject.cs b/LibGit2Sharp/GitObject.cs index 539fe6f8b..f9813a3ea 100644 --- a/LibGit2Sharp/GitObject.cs +++ b/LibGit2Sharp/GitObject.cs @@ -129,10 +129,10 @@ public virtual T Peel() where T : GitObject } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as GitObject); @@ -180,7 +180,7 @@ public override int GetHashCode() } /// - /// Returns the , a representation of the current . + /// Returns the , a representation of the current . /// /// The that represents the current . public override string ToString() diff --git a/LibGit2Sharp/GlobalSettings.cs b/LibGit2Sharp/GlobalSettings.cs index 31cba0965..bf7875f96 100644 --- a/LibGit2Sharp/GlobalSettings.cs +++ b/LibGit2Sharp/GlobalSettings.cs @@ -247,9 +247,9 @@ public static FilterRegistration RegisterFilter(Filter filter, int priority) Ensure.ArgumentNotNull(filter, "filter"); if (priority < FilterRegistration.FilterPriorityMin || priority > FilterRegistration.FilterPriorityMax) { - throw new ArgumentOutOfRangeException("priority", + throw new ArgumentOutOfRangeException(nameof(priority), priority, - String.Format(System.Globalization.CultureInfo.InvariantCulture, + string.Format(System.Globalization.CultureInfo.InvariantCulture, "Filter priorities must be within the inclusive range of [{0}, {1}].", FilterRegistration.FilterPriorityMin, FilterRegistration.FilterPriorityMax)); diff --git a/LibGit2Sharp/IndexEntry.cs b/LibGit2Sharp/IndexEntry.cs index e07e284b7..554d9a9f1 100644 --- a/LibGit2Sharp/IndexEntry.cs +++ b/LibGit2Sharp/IndexEntry.cs @@ -60,10 +60,10 @@ internal static unsafe IndexEntry BuildFromPtr(git_index_entry* entry) } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as IndexEntry); diff --git a/LibGit2Sharp/IndexNameEntry.cs b/LibGit2Sharp/IndexNameEntry.cs index 79b3f6993..40c202acc 100644 --- a/LibGit2Sharp/IndexNameEntry.cs +++ b/LibGit2Sharp/IndexNameEntry.cs @@ -63,10 +63,10 @@ internal static unsafe IndexNameEntry BuildFromPtr(git_index_name_entry* entry) public virtual string Theirs { get; private set; } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as IndexNameEntry); diff --git a/LibGit2Sharp/IndexReucEntry.cs b/LibGit2Sharp/IndexReucEntry.cs index 583df95ba..becd20122 100644 --- a/LibGit2Sharp/IndexReucEntry.cs +++ b/LibGit2Sharp/IndexReucEntry.cs @@ -88,10 +88,10 @@ internal static unsafe IndexReucEntry BuildFromPtr(git_index_reuc_entry* entry) public virtual Mode TheirMode { get; private set; } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as IndexReucEntry); diff --git a/LibGit2Sharp/InvalidSpecificationException.cs b/LibGit2Sharp/InvalidSpecificationException.cs index 3d34571a4..d9625dc32 100644 --- a/LibGit2Sharp/InvalidSpecificationException.cs +++ b/LibGit2Sharp/InvalidSpecificationException.cs @@ -1,5 +1,7 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif using LibGit2Sharp.Core; namespace LibGit2Sharp @@ -10,7 +12,9 @@ namespace LibGit2Sharp /// if the spec refers to an object of an incorrect type (e.g. asking to /// create a branch from a blob, or peeling a blob to a commit). /// +#if NETFRAMEWORK [Serializable] +#endif public class InvalidSpecificationException : NativeException { /// @@ -30,7 +34,7 @@ public InvalidSpecificationException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public InvalidSpecificationException(string format, params object[] args) : base(format, args) @@ -45,6 +49,7 @@ public InvalidSpecificationException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -53,6 +58,7 @@ public InvalidSpecificationException(string message, Exception innerException) protected InvalidSpecificationException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif internal InvalidSpecificationException(string message, GitErrorCategory category) : base(message, category) diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index f690c9107..28404d948 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -2,6 +2,7 @@ net472;net6.0 + 10.0 true LibGit2Sharp brings all the might and speed of libgit2, a native Git implementation, to the managed world of .NET LibGit2Sharp contributors @@ -18,10 +19,15 @@ App_Readme/README.md App_Readme/LICENSE.md true + $(ArtifactsPath)\package preview.0 libgit2-$(libgit2_hash.Substring(0,7)) + + true + + @@ -33,9 +39,9 @@ - - - + + + @@ -50,8 +56,6 @@ $(MinVerMajor).$(MinVerMinor).0.0 - $(MinVerMajor).$(MinVerMinor).$(MinVerPatch) - $(PackageVersion)-$(MinVerPreRelease) diff --git a/LibGit2Sharp/LibGit2SharpException.cs b/LibGit2Sharp/LibGit2SharpException.cs index 5d1c33f25..0518fa757 100644 --- a/LibGit2Sharp/LibGit2SharpException.cs +++ b/LibGit2Sharp/LibGit2SharpException.cs @@ -1,14 +1,17 @@ using System; using System.Globalization; +#if NETFRAMEWORK using System.Runtime.Serialization; -using LibGit2Sharp.Core; +#endif namespace LibGit2Sharp { /// /// The exception that is thrown when an error occurs during application execution. /// +#if NETFRAMEWORK [Serializable] +#endif public class LibGit2SharpException : Exception { /// @@ -37,13 +40,14 @@ public LibGit2SharpException(string message, Exception innerException) /// /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public LibGit2SharpException(string format, params object[] args) - : base(String.Format(CultureInfo.InvariantCulture, format, args)) + : base(string.Format(CultureInfo.InvariantCulture, format, args)) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -52,5 +56,6 @@ public LibGit2SharpException(string format, params object[] args) protected LibGit2SharpException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif } } diff --git a/LibGit2Sharp/LockedFileException.cs b/LibGit2Sharp/LockedFileException.cs index 44fd65b02..b38f40496 100644 --- a/LibGit2Sharp/LockedFileException.cs +++ b/LibGit2Sharp/LockedFileException.cs @@ -1,5 +1,7 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif using LibGit2Sharp.Core; namespace LibGit2Sharp @@ -7,7 +9,9 @@ namespace LibGit2Sharp /// /// The exception that is thrown attempting to open a locked file. /// +#if NETFRAMEWORK [Serializable] +#endif public class LockedFileException : NativeException { /// @@ -27,7 +31,7 @@ public LockedFileException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public LockedFileException(string format, params object[] args) : base(format, args) @@ -42,6 +46,7 @@ public LockedFileException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -50,6 +55,7 @@ public LockedFileException(string message, Exception innerException) protected LockedFileException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif internal LockedFileException(string message, GitErrorCategory category) : base(message, category) diff --git a/LibGit2Sharp/MergeFetchHeadNotFoundException.cs b/LibGit2Sharp/MergeFetchHeadNotFoundException.cs index a86bf5caf..d7d761c1d 100644 --- a/LibGit2Sharp/MergeFetchHeadNotFoundException.cs +++ b/LibGit2Sharp/MergeFetchHeadNotFoundException.cs @@ -1,12 +1,16 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif namespace LibGit2Sharp { /// /// The exception that is thrown when the ref to merge with was as part of a pull operation not fetched. /// +#if NETFRAMEWORK [Serializable] +#endif public class MergeFetchHeadNotFoundException : NotFoundException { /// @@ -26,7 +30,7 @@ public MergeFetchHeadNotFoundException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public MergeFetchHeadNotFoundException(string format, params object[] args) : base(format, args) @@ -41,6 +45,7 @@ public MergeFetchHeadNotFoundException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -49,5 +54,6 @@ public MergeFetchHeadNotFoundException(string message, Exception innerException) protected MergeFetchHeadNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif } } diff --git a/LibGit2Sharp/NameConflictException.cs b/LibGit2Sharp/NameConflictException.cs index 0dcffc648..0517f2550 100644 --- a/LibGit2Sharp/NameConflictException.cs +++ b/LibGit2Sharp/NameConflictException.cs @@ -1,5 +1,7 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif using LibGit2Sharp.Core; namespace LibGit2Sharp @@ -7,7 +9,9 @@ namespace LibGit2Sharp /// /// The exception that is thrown when a reference, a remote, a submodule... with the same name already exists in the repository /// +#if NETFRAMEWORK [Serializable] +#endif public class NameConflictException : NativeException { /// @@ -27,7 +31,7 @@ public NameConflictException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public NameConflictException(string format, params object[] args) : base(format, args) @@ -42,6 +46,7 @@ public NameConflictException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -50,6 +55,7 @@ public NameConflictException(string message, Exception innerException) protected NameConflictException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif internal NameConflictException(string message, GitErrorCategory category) : base(message, category) diff --git a/LibGit2Sharp/NativeException.cs b/LibGit2Sharp/NativeException.cs index 292372db7..66dc03c57 100644 --- a/LibGit2Sharp/NativeException.cs +++ b/LibGit2Sharp/NativeException.cs @@ -1,16 +1,17 @@ -using LibGit2Sharp.Core; -using System; -using System.Collections.Generic; -using System.Globalization; +using System; +#if NETFRAMEWORK using System.Runtime.Serialization; -using System.Text; +#endif +using LibGit2Sharp.Core; namespace LibGit2Sharp { /// /// An exception thrown that corresponds to a libgit2 (native library) error. /// +#if NETFRAMEWORK [Serializable] +#endif public abstract class NativeException : LibGit2SharpException { /// @@ -32,9 +33,11 @@ internal NativeException(string format, params object[] args) { } +#if NETFRAMEWORK internal NativeException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif internal NativeException(string message, GitErrorCategory category) : this(message) { diff --git a/LibGit2Sharp/Network.cs b/LibGit2Sharp/Network.cs index d5f032058..bd80834bb 100644 --- a/LibGit2Sharp/Network.cs +++ b/LibGit2Sharp/Network.cs @@ -52,7 +52,26 @@ public virtual IEnumerable ListReferences(Remote remote) { Ensure.ArgumentNotNull(remote, "remote"); - return ListReferencesInternal(remote.Url, null); + return ListReferencesInternal(remote.Url, null, new ProxyOptions()); + } + + /// + /// List references in a repository. + /// + /// When the remote tips are ahead of the local ones, the retrieved + /// s may point to non existing + /// s in the local repository. In that + /// case, will return null. + /// + /// + /// The to list from. + /// Options for connecting through a proxy. + /// The references in the repository. + public virtual IEnumerable ListReferences(Remote remote, ProxyOptions proxyOptions) + { + Ensure.ArgumentNotNull(remote, "remote"); + + return ListReferencesInternal(remote.Url, null, proxyOptions); } /// @@ -72,7 +91,28 @@ public virtual IEnumerable ListReferences(Remote remote, CredentialsH Ensure.ArgumentNotNull(remote, "remote"); Ensure.ArgumentNotNull(credentialsProvider, "credentialsProvider"); - return ListReferencesInternal(remote.Url, credentialsProvider); + return ListReferencesInternal(remote.Url, credentialsProvider, new ProxyOptions()); + } + + /// + /// List references in a repository. + /// + /// When the remote tips are ahead of the local ones, the retrieved + /// s may point to non existing + /// s in the local repository. In that + /// case, will return null. + /// + /// + /// The to list from. + /// The used to connect to remote repository. + /// Options for connecting through a proxy. + /// The references in the repository. + public virtual IEnumerable ListReferences(Remote remote, CredentialsHandler credentialsProvider, ProxyOptions proxyOptions) + { + Ensure.ArgumentNotNull(remote, "remote"); + Ensure.ArgumentNotNull(credentialsProvider, "credentialsProvider"); + + return ListReferencesInternal(remote.Url, credentialsProvider, proxyOptions); } /// @@ -90,7 +130,26 @@ public virtual IEnumerable ListReferences(string url) { Ensure.ArgumentNotNull(url, "url"); - return ListReferencesInternal(url, null); + return ListReferencesInternal(url, null, new ProxyOptions()); + } + + /// + /// List references in a remote repository. + /// + /// When the remote tips are ahead of the local ones, the retrieved + /// s may point to non existing + /// s in the local repository. In that + /// case, will return null. + /// + /// + /// The url to list from. + /// Options for connecting through a proxy. + /// The references in the remote repository. + public virtual IEnumerable ListReferences(string url, ProxyOptions proxyOptions) + { + Ensure.ArgumentNotNull(url, "url"); + + return ListReferencesInternal(url, null, proxyOptions); } /// @@ -110,25 +169,49 @@ public virtual IEnumerable ListReferences(string url, CredentialsHand Ensure.ArgumentNotNull(url, "url"); Ensure.ArgumentNotNull(credentialsProvider, "credentialsProvider"); - return ListReferencesInternal(url, credentialsProvider); + return ListReferencesInternal(url, credentialsProvider, new ProxyOptions()); + } + + /// + /// List references in a remote repository. + /// + /// When the remote tips are ahead of the local ones, the retrieved + /// s may point to non existing + /// s in the local repository. In that + /// case, will return null. + /// + /// + /// The url to list from. + /// The used to connect to remote repository. + /// Options for connecting through a proxy. + /// The references in the remote repository. + public virtual IEnumerable ListReferences(string url, CredentialsHandler credentialsProvider, ProxyOptions proxyOptions) + { + Ensure.ArgumentNotNull(url, "url"); + Ensure.ArgumentNotNull(credentialsProvider, "credentialsProvider"); + + return ListReferencesInternal(url, credentialsProvider, new ProxyOptions()); } - private IEnumerable ListReferencesInternal(string url, CredentialsHandler credentialsProvider) + private IEnumerable ListReferencesInternal(string url, CredentialsHandler credentialsProvider, ProxyOptions proxyOptions) { - using (RemoteHandle remoteHandle = BuildRemoteHandle(repository.Handle, url)) - { - GitRemoteCallbacks gitCallbacks = new GitRemoteCallbacks { version = 1 }; - GitProxyOptions proxyOptions = new GitProxyOptions { Version = 1 }; + proxyOptions ??= new(); - if (credentialsProvider != null) - { - var callbacks = new RemoteCallbacks(credentialsProvider); - gitCallbacks = callbacks.GenerateCallbacks(); - } + using RemoteHandle remoteHandle = BuildRemoteHandle(repository.Handle, url); + using var proxyOptionsWrapper = new GitProxyOptionsWrapper(proxyOptions.CreateGitProxyOptions()); + + GitRemoteCallbacks gitCallbacks = new GitRemoteCallbacks { version = 1 }; - Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch, ref gitCallbacks, ref proxyOptions); - return Proxy.git_remote_ls(repository, remoteHandle); + if (credentialsProvider != null) + { + var callbacks = new RemoteCallbacks(credentialsProvider); + gitCallbacks = callbacks.GenerateCallbacks(); } + + var gitProxyOptions = proxyOptionsWrapper.Options; + + Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch, ref gitCallbacks, ref gitProxyOptions); + return Proxy.git_remote_ls(repository, remoteHandle); } static RemoteHandle BuildRemoteHandle(RepositoryHandle repoHandle, string url) @@ -365,18 +448,27 @@ public virtual void Push(Remote remote, IEnumerable pushRefSpecs, PushOp // Load the remote. using (RemoteHandle remoteHandle = Proxy.git_remote_lookup(repository.Handle, remote.Name, true)) + + // Create a git options wrapper so managed strings are disposed. + using (var pushOptionsWrapper = new GitPushOptionsWrapper()) { var callbacks = new RemoteCallbacks(pushOptions); GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks(); + var gitPushOptions = pushOptionsWrapper.Options; + gitPushOptions.PackbuilderDegreeOfParallelism = pushOptions.PackbuilderDegreeOfParallelism; + gitPushOptions.RemoteCallbacks = gitCallbacks; + gitPushOptions.ProxyOptions = pushOptions.ProxyOptions.CreateGitProxyOptions(); + + // If there are custom headers, create a managed string array. + if (pushOptions.CustomHeaders != null && pushOptions.CustomHeaders.Length > 0) + { + gitPushOptions.CustomHeaders = GitStrArrayManaged.BuildFrom(pushOptions.CustomHeaders); + } + Proxy.git_remote_push(remoteHandle, pushRefSpecs, - new GitPushOptions() - { - PackbuilderDegreeOfParallelism = pushOptions.PackbuilderDegreeOfParallelism, - RemoteCallbacks = gitCallbacks, - ProxyOptions = new GitProxyOptions { Version = 1 }, - }); + gitPushOptions); } } diff --git a/LibGit2Sharp/NonFastForwardException.cs b/LibGit2Sharp/NonFastForwardException.cs index b5a858f47..d8ed8f474 100644 --- a/LibGit2Sharp/NonFastForwardException.cs +++ b/LibGit2Sharp/NonFastForwardException.cs @@ -1,5 +1,7 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif using LibGit2Sharp.Core; namespace LibGit2Sharp @@ -8,7 +10,9 @@ namespace LibGit2Sharp /// The exception that is thrown when push cannot be performed /// against the remote without losing commits. /// +#if NETFRAMEWORK [Serializable] +#endif public class NonFastForwardException : NativeException { /// @@ -28,7 +32,7 @@ public NonFastForwardException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public NonFastForwardException(string format, params object[] args) : base(format, args) @@ -43,6 +47,7 @@ public NonFastForwardException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -51,6 +56,7 @@ public NonFastForwardException(string message, Exception innerException) protected NonFastForwardException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif internal NonFastForwardException(string message, GitErrorCategory category) : base(message, category) diff --git a/LibGit2Sharp/NotFoundException.cs b/LibGit2Sharp/NotFoundException.cs index f8c49cc91..f282c4340 100644 --- a/LibGit2Sharp/NotFoundException.cs +++ b/LibGit2Sharp/NotFoundException.cs @@ -1,5 +1,7 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif using LibGit2Sharp.Core; namespace LibGit2Sharp @@ -7,7 +9,9 @@ namespace LibGit2Sharp /// /// The exception that is thrown attempting to reference a resource that does not exist. /// +#if NETFRAMEWORK [Serializable] +#endif public class NotFoundException : NativeException { /// @@ -27,7 +31,7 @@ public NotFoundException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public NotFoundException(string format, params object[] args) : base(format, args) @@ -42,6 +46,7 @@ public NotFoundException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -50,6 +55,7 @@ public NotFoundException(string message, Exception innerException) protected NotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif internal NotFoundException(string message, GitErrorCategory category) : base(message, category) diff --git a/LibGit2Sharp/Note.cs b/LibGit2Sharp/Note.cs index 1df0125e4..2ffc89690 100644 --- a/LibGit2Sharp/Note.cs +++ b/LibGit2Sharp/Note.cs @@ -59,10 +59,10 @@ internal static Note BuildFromPtr(NoteHandle note, string @namespace, ObjectId t } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as Note); diff --git a/LibGit2Sharp/ObjectDatabase.cs b/LibGit2Sharp/ObjectDatabase.cs index b48c72a99..42b65d7d0 100644 --- a/LibGit2Sharp/ObjectDatabase.cs +++ b/LibGit2Sharp/ObjectDatabase.cs @@ -199,7 +199,7 @@ public virtual ObjectId Write(Stream stream, long numberOfBytesToConsume) whe if (!stream.CanRead) { - throw new ArgumentException("The stream cannot be read from.", "stream"); + throw new ArgumentException("The stream cannot be read from.", nameof(stream)); } using (var odbStream = Proxy.git_odb_open_wstream(handle, numberOfBytesToConsume, GitObjectType.Blob)) @@ -274,7 +274,7 @@ private unsafe Blob CreateBlob(Stream stream, string hintpath, long? numberOfByt if (!stream.CanRead) { - throw new ArgumentException("The stream cannot be read from.", "stream"); + throw new ArgumentException("The stream cannot be read from.", nameof(stream)); } IntPtr writestream_ptr = Proxy.git_blob_create_from_stream(repo.Handle, hintpath); @@ -588,7 +588,7 @@ public virtual MergeTreeResult CherryPickCommit(Commit cherryPickCommit, Commit // Stopped due to FailOnConflict so there's no index or conflict list if (earlyStop) { - return new MergeTreeResult(new Conflict[] { }); + return new MergeTreeResult(Array.Empty()); } if (Proxy.git_index_has_conflicts(indexHandle)) @@ -639,7 +639,7 @@ public virtual string ShortenObjectId(GitObject gitObject, int minLength) if (minLength <= 0 || minLength > ObjectId.HexSize) { - throw new ArgumentOutOfRangeException("minLength", + throw new ArgumentOutOfRangeException(nameof(minLength), minLength, string.Format("Expected value should be greater than zero and less than or equal to {0}.", ObjectId.HexSize)); @@ -714,7 +714,7 @@ public virtual Commit FindMergeBase(IEnumerable commits, MergeBaseFindin { if (commit == null) { - throw new ArgumentException("Enumerable contains null at position: " + count.ToString(CultureInfo.InvariantCulture), "commits"); + throw new ArgumentException("Enumerable contains null at position: " + count.ToString(CultureInfo.InvariantCulture), nameof(commits)); } ids.Add(commit.Id.Oid); count++; @@ -722,7 +722,7 @@ public virtual Commit FindMergeBase(IEnumerable commits, MergeBaseFindin if (count < 2) { - throw new ArgumentException("The enumerable must contains at least two commits.", "commits"); + throw new ArgumentException("The enumerable must contains at least two commits.", nameof(commits)); } switch (strategy) @@ -736,7 +736,7 @@ public virtual Commit FindMergeBase(IEnumerable commits, MergeBaseFindin break; default: - throw new ArgumentException("", "strategy"); + throw new ArgumentException("", nameof(strategy)); } return id == null ? null : repo.Lookup(id); @@ -780,7 +780,7 @@ public virtual MergeTreeResult MergeCommits(Commit ours, Commit theirs, MergeTre // Stopped due to FailOnConflict so there's no index or conflict list if (earlyStop) { - return new MergeTreeResult(new Conflict[] { }); + return new MergeTreeResult(Array.Empty()); } if (Proxy.git_index_has_conflicts(indexHandle)) @@ -1063,7 +1063,7 @@ public virtual MergeTreeResult RevertCommit(Commit revertCommit, Commit revertOn // Stopped due to FailOnConflict so there's no index or conflict list if (earlyStop) { - return new MergeTreeResult(new Conflict[] { }); + return new MergeTreeResult(Array.Empty()); } if (Proxy.git_index_has_conflicts(indexHandle)) diff --git a/LibGit2Sharp/ObjectId.cs b/LibGit2Sharp/ObjectId.cs index 9d754781c..ad61cba36 100644 --- a/LibGit2Sharp/ObjectId.cs +++ b/LibGit2Sharp/ObjectId.cs @@ -39,7 +39,7 @@ internal ObjectId(GitOid oid) { if (oid.Id == null || oid.Id.Length != rawSize) { - throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "A non null array of {0} bytes is expected.", rawSize), "oid"); + throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "A non null array of {0} bytes is expected.", rawSize), nameof(oid)); } this.oid = oid; @@ -145,10 +145,10 @@ public static bool TryParse(string sha, out ObjectId result) } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as ObjectId); @@ -174,7 +174,7 @@ public override int GetHashCode() } /// - /// Returns the , a representation of the current . + /// Returns the , a representation of the current . /// /// The that represents the current . public override string ToString() @@ -183,7 +183,7 @@ public override string ToString() } /// - /// Returns the , a representation of the current . + /// Returns the , a representation of the current . /// /// The number of chars the should be truncated to. /// The that represents the current . @@ -327,7 +327,7 @@ private static bool LooksValid(string objectId, bool throwIfInvalid) "'{0}' is not a valid object identifier. Its length should be {1}.", objectId, HexSize), - "objectId"); + nameof(objectId)); } return objectId.All(c => hexDigits.IndexOf(c) >= 0); diff --git a/LibGit2Sharp/OdbBackend.cs b/LibGit2Sharp/OdbBackend.cs index 9736ed803..645d0ac5f 100644 --- a/LibGit2Sharp/OdbBackend.cs +++ b/LibGit2Sharp/OdbBackend.cs @@ -63,7 +63,7 @@ protected unsafe UnmanagedMemoryStream Allocate(long size) { if (size < 0 || (UIntPtr.Size == sizeof(int) && size > int.MaxValue)) { - throw new ArgumentOutOfRangeException("size"); + throw new ArgumentOutOfRangeException(nameof(size)); } IntPtr buffer = Proxy.git_odb_backend_malloc(this.GitOdbBackendPointer, new UIntPtr((ulong)size)); diff --git a/LibGit2Sharp/PackBuilder.cs b/LibGit2Sharp/PackBuilder.cs index dcaa2617a..2ede4ab7b 100644 --- a/LibGit2Sharp/PackBuilder.cs +++ b/LibGit2Sharp/PackBuilder.cs @@ -189,7 +189,7 @@ public int MaximumNumberOfThreads { if (value < 0) { - throw new ArgumentException("Argument can not be negative", "value"); + throw new ArgumentException("Argument can not be negative", nameof(value)); } nThreads = value; diff --git a/LibGit2Sharp/PeelException.cs b/LibGit2Sharp/PeelException.cs index d7758d7c9..b5a3d628b 100644 --- a/LibGit2Sharp/PeelException.cs +++ b/LibGit2Sharp/PeelException.cs @@ -1,5 +1,7 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif using LibGit2Sharp.Core; namespace LibGit2Sharp @@ -8,7 +10,9 @@ namespace LibGit2Sharp /// The exception that is thrown when a tag cannot be peeled to the /// target type due to the object model. /// +#if NETFRAMEWORK [Serializable] +#endif public class PeelException : NativeException { /// @@ -28,7 +32,7 @@ public PeelException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public PeelException(string format, params object[] args) : base(format, args) @@ -43,6 +47,7 @@ public PeelException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -51,6 +56,7 @@ public PeelException(string message, Exception innerException) protected PeelException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif internal PeelException(string message, GitErrorCategory category) : base(message, category) diff --git a/LibGit2Sharp/ProxyOptions.cs b/LibGit2Sharp/ProxyOptions.cs new file mode 100644 index 000000000..076c4e357 --- /dev/null +++ b/LibGit2Sharp/ProxyOptions.cs @@ -0,0 +1,119 @@ +using System; +using LibGit2Sharp.Core; +using LibGit2Sharp.Handlers; + +namespace LibGit2Sharp +{ + /// + /// Options for connecting through a proxy. + /// + public sealed class ProxyOptions + { + /// + /// The type of proxy to use. Set to Auto by default. + /// + public ProxyType ProxyType { get; set; } = ProxyType.Auto; + + /// + /// The URL of the proxy when is set to Specified. + /// + public string Url { get; set; } + + /// + /// Handler to generate for authentication. + /// + public CredentialsHandler CredentialsProvider { get; set; } + + /// + /// This handler will be called to let the user make a decision on whether to allow + /// the connection to proceed based on the certificate presented by the server. + /// + public CertificateCheckHandler CertificateCheck { get; set; } + + internal unsafe GitProxyOptions CreateGitProxyOptions() + { + var gitProxyOptions = new GitProxyOptions + { + Version = 1, + Type = (GitProxyType)ProxyType + }; + + if (Url is not null) + { + gitProxyOptions.Url = StrictUtf8Marshaler.FromManaged(Url); + } + + if (CredentialsProvider is not null) + { + gitProxyOptions.Credentials = GitCredentialHandler; + } + + if (CertificateCheck is not null) + { + gitProxyOptions.CertificateCheck = GitCertificateCheck; + } + + return gitProxyOptions; + } + + private int GitCredentialHandler(out IntPtr ptr, IntPtr cUrl, IntPtr usernameFromUrl, GitCredentialType credTypes, IntPtr payload) + { + string url = LaxUtf8Marshaler.FromNative(cUrl); + string username = LaxUtf8Marshaler.FromNative(usernameFromUrl); + + SupportedCredentialTypes types = default(SupportedCredentialTypes); + if (credTypes.HasFlag(GitCredentialType.UserPassPlaintext)) + { + types |= SupportedCredentialTypes.UsernamePassword; + } + if (credTypes.HasFlag(GitCredentialType.Default)) + { + types |= SupportedCredentialTypes.Default; + } + + ptr = IntPtr.Zero; + try + { + var cred = CredentialsProvider(url, username, types); + if (cred == null) + { + return (int)GitErrorCode.PassThrough; + } + return cred.GitCredentialHandler(out ptr); + } + catch (Exception exception) + { + Proxy.git_error_set_str(GitErrorCategory.Callback, exception); + return (int)GitErrorCode.Error; + } + } + + private unsafe int GitCertificateCheck(git_certificate* certPtr, int valid, IntPtr cHostname, IntPtr payload) + { + string hostname = LaxUtf8Marshaler.FromNative(cHostname); + Certificate cert = null; + + switch (certPtr->type) + { + case GitCertificateType.X509: + cert = new CertificateX509((git_certificate_x509*)certPtr); + break; + case GitCertificateType.Hostkey: + cert = new CertificateSsh((git_certificate_ssh*)certPtr); + break; + } + + bool result = false; + try + { + result = CertificateCheck(cert, valid != 0, hostname); + } + catch (Exception exception) + { + Proxy.git_error_set_str(GitErrorCategory.Callback, exception); + } + + return Proxy.ConvertResultToCancelFlag(result); + } + } +} diff --git a/LibGit2Sharp/ProxyType.cs b/LibGit2Sharp/ProxyType.cs new file mode 100644 index 000000000..13ec705ee --- /dev/null +++ b/LibGit2Sharp/ProxyType.cs @@ -0,0 +1,23 @@ +namespace LibGit2Sharp +{ + /// + /// The type of proxy to use. + /// + public enum ProxyType + { + /// + /// Do not attempt to connect through a proxy. + /// + None, + + /// + /// Try to auto-detect the proxy from the git configuration. + /// + Auto, + + /// + /// Connect via the URL given in the options. + /// + Specified + } +} diff --git a/LibGit2Sharp/PushOptions.cs b/LibGit2Sharp/PushOptions.cs index 99c65dd8b..829eb0d60 100644 --- a/LibGit2Sharp/PushOptions.cs +++ b/LibGit2Sharp/PushOptions.cs @@ -51,5 +51,30 @@ public sealed class PushOptions /// information about what updates will be performed. /// public PrePushHandler OnNegotiationCompletedBeforePush { get; set; } + + /// + /// Get/Set the custom headers. + /// + /// This allows you to set custom headers (e.g. X-Forwarded-For, + /// X-Request-Id, etc), + /// + /// + /// + /// Libgit2 sets some headers for HTTP requests (User-Agent, Host, + /// Accept, Content-Type, Transfer-Encoding, Content-Length, Accept) that + /// cannot be overriden. + /// + /// + /// var pushOptions - new PushOptions() { + /// CustomHeaders = new String[] {"X-Request-Id: 12345"} + /// }; + /// + /// The custom headers string array + public string[] CustomHeaders { get; set; } + + /// + /// Options for connecting through a proxy. + /// + public ProxyOptions ProxyOptions { get; set; } = new(); } } diff --git a/LibGit2Sharp/PushUpdate.cs b/LibGit2Sharp/PushUpdate.cs index bbabb6817..0aa915dc0 100644 --- a/LibGit2Sharp/PushUpdate.cs +++ b/LibGit2Sharp/PushUpdate.cs @@ -29,9 +29,9 @@ internal unsafe PushUpdate(git_push_update* update) protected PushUpdate() { DestinationObjectId = ObjectId.Zero; - DestinationRefName = String.Empty; + DestinationRefName = string.Empty; SourceObjectId = ObjectId.Zero; - SourceRefName = String.Empty; + SourceRefName = string.Empty; } /// diff --git a/LibGit2Sharp/RecurseSubmodulesException.cs b/LibGit2Sharp/RecurseSubmodulesException.cs index cf4479701..2269f0d16 100644 --- a/LibGit2Sharp/RecurseSubmodulesException.cs +++ b/LibGit2Sharp/RecurseSubmodulesException.cs @@ -1,5 +1,7 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif namespace LibGit2Sharp { @@ -8,7 +10,9 @@ namespace LibGit2Sharp /// through submodules. The inner exception contains the exception that was /// initially thrown while operating on the submodule. /// +#if NETFRAMEWORK [Serializable] +#endif public class RecurseSubmodulesException : LibGit2SharpException { /// @@ -34,6 +38,7 @@ public RecurseSubmodulesException(string message, Exception innerException, stri InitialRepositoryPath = initialRepositoryPath; } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -42,5 +47,6 @@ public RecurseSubmodulesException(string message, Exception innerException, stri protected RecurseSubmodulesException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif } } diff --git a/LibGit2Sharp/Reference.cs b/LibGit2Sharp/Reference.cs index 40a85f79f..616b386a9 100644 --- a/LibGit2Sharp/Reference.cs +++ b/LibGit2Sharp/Reference.cs @@ -153,10 +153,10 @@ public virtual string TargetIdentifier } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as Reference); @@ -204,7 +204,7 @@ public override int GetHashCode() } /// - /// Returns the , a representation of the current . + /// Returns the , a representation of the current . /// /// The that represents the current . public override string ToString() diff --git a/LibGit2Sharp/ReferenceCollection.cs b/LibGit2Sharp/ReferenceCollection.cs index 602a20f17..456b1a043 100644 --- a/LibGit2Sharp/ReferenceCollection.cs +++ b/LibGit2Sharp/ReferenceCollection.cs @@ -539,7 +539,7 @@ public virtual Reference UpdateTarget(string name, string canonicalRefNameOrObje if (refState == RefState.DoesNotLookValid) { - throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The reference specified by {0} is a Symbolic reference, you must provide a reference canonical name as the target.", name), "canonicalRefNameOrObjectish"); + throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The reference specified by {0} is a Symbolic reference, you must provide a reference canonical name as the target.", name), nameof(canonicalRefNameOrObjectish)); } return UpdateTarget(symbolicReference, targetRef, logMessage); diff --git a/LibGit2Sharp/ReferenceWrapper.cs b/LibGit2Sharp/ReferenceWrapper.cs index 3e4243a7e..7fb8497c6 100644 --- a/LibGit2Sharp/ReferenceWrapper.cs +++ b/LibGit2Sharp/ReferenceWrapper.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using LibGit2Sharp.Core; @@ -10,7 +11,11 @@ namespace LibGit2Sharp /// /// The type of the referenced Git object. [DebuggerDisplay("{DebuggerDisplay,nq}")] +#if NET + public abstract class ReferenceWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TObject> : IEquatable>, IBelongToARepository where TObject : GitObject +#else public abstract class ReferenceWrapper : IEquatable>, IBelongToARepository where TObject : GitObject +#endif { /// /// The repository. @@ -124,10 +129,10 @@ public bool Equals(ReferenceWrapper other) } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as ReferenceWrapper); diff --git a/LibGit2Sharp/RemoveFromIndexException.cs b/LibGit2Sharp/RemoveFromIndexException.cs index 6d9718c18..847e4026e 100644 --- a/LibGit2Sharp/RemoveFromIndexException.cs +++ b/LibGit2Sharp/RemoveFromIndexException.cs @@ -1,13 +1,16 @@ using System; -using System.Globalization; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif namespace LibGit2Sharp { /// /// The exception that is thrown when a file cannot be removed from the index. /// +#if NETFRAMEWORK [Serializable] +#endif public class RemoveFromIndexException : LibGit2SharpException { /// @@ -27,7 +30,7 @@ public RemoveFromIndexException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public RemoveFromIndexException(string format, params object[] args) : base(format, args) @@ -43,6 +46,7 @@ public RemoveFromIndexException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -51,5 +55,6 @@ public RemoveFromIndexException(string message, Exception innerException) protected RemoveFromIndexException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif } } diff --git a/LibGit2Sharp/RenameDetails.cs b/LibGit2Sharp/RenameDetails.cs index b866aac60..8742ff0d3 100644 --- a/LibGit2Sharp/RenameDetails.cs +++ b/LibGit2Sharp/RenameDetails.cs @@ -56,10 +56,10 @@ public virtual int Similarity } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as RenameDetails); diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index 41aaecfbf..e23c9cd3b 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -656,7 +656,18 @@ internal Commit LookupCommit(string committish) /// The references in the remote repository. public static IEnumerable ListRemoteReferences(string url) { - return ListRemoteReferences(url, null); + return ListRemoteReferences(url, null, new ProxyOptions()); + } + + /// + /// Lists the Remote Repository References. + /// + /// The url to list from. + /// Options for connecting through a proxy. + /// The references in the remote repository. + public static IEnumerable ListRemoteReferences(string url, ProxyOptions proxyOptions) + { + return ListRemoteReferences(url, null, proxyOptions); } /// @@ -671,24 +682,44 @@ public static IEnumerable ListRemoteReferences(string url) /// The used to connect to remote repository. /// The references in the remote repository. public static IEnumerable ListRemoteReferences(string url, CredentialsHandler credentialsProvider) + { + return ListRemoteReferences(url, credentialsProvider, new ProxyOptions()); + } + + /// + /// Lists the Remote Repository References. + /// + /// + /// Does not require a local Repository. The retrieved + /// + /// throws in this case. + /// + /// The url to list from. + /// The used to connect to remote repository. + /// Options for connecting through a proxy. + /// The references in the remote repository. + public static IEnumerable ListRemoteReferences(string url, CredentialsHandler credentialsProvider, ProxyOptions proxyOptions) { Ensure.ArgumentNotNull(url, "url"); - using (RepositoryHandle repositoryHandle = Proxy.git_repository_new()) - using (RemoteHandle remoteHandle = Proxy.git_remote_create_anonymous(repositoryHandle, url)) - { - var gitCallbacks = new GitRemoteCallbacks { version = 1 }; - var proxyOptions = new GitProxyOptions { Version = 1 }; + proxyOptions ??= new(); - if (credentialsProvider != null) - { - var callbacks = new RemoteCallbacks(credentialsProvider); - gitCallbacks = callbacks.GenerateCallbacks(); - } + using RepositoryHandle repositoryHandle = Proxy.git_repository_new(); + using RemoteHandle remoteHandle = Proxy.git_remote_create_anonymous(repositoryHandle, url); + using var proxyOptionsWrapper = new GitProxyOptionsWrapper(proxyOptions.CreateGitProxyOptions()); + + var gitCallbacks = new GitRemoteCallbacks { version = 1 }; - Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch, ref gitCallbacks, ref proxyOptions); - return Proxy.git_remote_ls(null, remoteHandle); + if (credentialsProvider != null) + { + var callbacks = new RemoteCallbacks(credentialsProvider); + gitCallbacks = callbacks.GenerateCallbacks(); } + + var gitProxyOptions = proxyOptionsWrapper.Options; + + Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch, ref gitCallbacks, ref gitProxyOptions); + return Proxy.git_remote_ls(null, remoteHandle); } /// @@ -747,14 +778,14 @@ public static string Clone(string sourceUrl, string workdirPath, Ensure.ArgumentNotNull(sourceUrl, "sourceUrl"); Ensure.ArgumentNotNull(workdirPath, "workdirPath"); - options = options ?? new CloneOptions(); + options ??= new CloneOptions(); // context variable that contains information on the repository that // we are cloning. var context = new RepositoryOperationContext(Path.GetFullPath(workdirPath), sourceUrl); // Notify caller that we are starting to work with the current repository. - bool continueOperation = OnRepositoryOperationStarting(options.RepositoryOperationStarting, + bool continueOperation = OnRepositoryOperationStarting(options.FetchOptions.RepositoryOperationStarting, context); if (!continueOperation) @@ -768,8 +799,8 @@ public static string Clone(string sourceUrl, string workdirPath, var gitCheckoutOptions = checkoutOptionsWrapper.Options; var gitFetchOptions = fetchOptionsWrapper.Options; - gitFetchOptions.ProxyOptions = new GitProxyOptions { Version = 1 }; - gitFetchOptions.RemoteCallbacks = new RemoteCallbacks(options).GenerateCallbacks(); + gitFetchOptions.ProxyOptions = options.FetchOptions.ProxyOptions.CreateGitProxyOptions(); + gitFetchOptions.RemoteCallbacks = new RemoteCallbacks(options.FetchOptions).GenerateCallbacks(); if (options.FetchOptions != null && options.FetchOptions.CustomHeaders != null) { gitFetchOptions.CustomHeaders = @@ -801,7 +832,7 @@ public static string Clone(string sourceUrl, string workdirPath, } // Notify caller that we are done with the current repository. - OnRepositoryOperationCompleted(options.RepositoryOperationCompleted, + OnRepositoryOperationCompleted(options.FetchOptions.RepositoryOperationCompleted, context); // Recursively clone submodules if requested. @@ -834,14 +865,11 @@ private static void RecursivelyCloneSubmodules(CloneOptions options, string repo using (Repository repo = new Repository(repoPath)) { - SubmoduleUpdateOptions updateOptions = new SubmoduleUpdateOptions() + var updateOptions = new SubmoduleUpdateOptions() { Init = true, - CredentialsProvider = options.CredentialsProvider, OnCheckoutProgress = options.OnCheckoutProgress, - OnProgress = options.OnProgress, - OnTransferProgress = options.OnTransferProgress, - OnUpdateTips = options.OnUpdateTips, + FetchOptions = options.FetchOptions }; string parentRepoWorkDir = repo.Info.WorkingDirectory; @@ -862,7 +890,7 @@ private static void RecursivelyCloneSubmodules(CloneOptions options, string repo sm.Name, recursionDepth); - bool continueOperation = OnRepositoryOperationStarting(options.RepositoryOperationStarting, + bool continueOperation = OnRepositoryOperationStarting(options.FetchOptions.RepositoryOperationStarting, context); if (!continueOperation) @@ -872,7 +900,7 @@ private static void RecursivelyCloneSubmodules(CloneOptions options, string repo repo.Submodules.Update(sm.Name, updateOptions); - OnRepositoryOperationCompleted(options.RepositoryOperationCompleted, + OnRepositoryOperationCompleted(options.FetchOptions.RepositoryOperationCompleted, context); submodules.Add(Path.Combine(repo.Info.WorkingDirectory, sm.Path)); @@ -1050,7 +1078,7 @@ public Commit Commit(string message, Signature author, Signature committer, Comm if (treesame && !amendMergeCommit) { - throw (options.AmendPreviousCommit ? + throw (options.AmendPreviousCommit ? new EmptyCommitException("Amending this commit would produce a commit that is identical to its parent (id = {0})", parents[0].Id) : new EmptyCommitException("No changes; nothing to commit.")); } @@ -1241,7 +1269,7 @@ public MergeResult MergeFetchedRefs(Signature merger, MergeOptions options) if (fetchHeads.Length == 0) { var expectedRef = this.Head.UpstreamBranchCanonicalName; - throw new MergeFetchHeadNotFoundException("The current branch is configured to merge with the reference '{0}' from the remote, but this reference was not fetched.", + throw new MergeFetchHeadNotFoundException("The current branch is configured to merge with the reference '{0}' from the remote, but this reference was not fetched.", expectedRef); } @@ -1428,7 +1456,7 @@ private FastForwardStrategy FastForwardStrategyFromMergePreference(GitMergePrefe case GitMergePreference.GIT_MERGE_PREFERENCE_NO_FASTFORWARD: return FastForwardStrategy.NoFastForward; default: - throw new InvalidOperationException(String.Format("Unknown merge preference: {0}", preference)); + throw new InvalidOperationException(string.Format("Unknown merge preference: {0}", preference)); } } @@ -1646,7 +1674,7 @@ internal FilePath[] ToFilePaths(IEnumerable paths) { if (string.IsNullOrEmpty(path)) { - throw new ArgumentException("At least one provided path is either null or empty.", "paths"); + throw new ArgumentException("At least one provided path is either null or empty.", nameof(paths)); } filePaths.Add(this.BuildRelativePathFrom(path)); @@ -1654,7 +1682,7 @@ internal FilePath[] ToFilePaths(IEnumerable paths) if (filePaths.Count == 0) { - throw new ArgumentException("No path has been provided.", "paths"); + throw new ArgumentException("No path has been provided.", nameof(paths)); } return filePaths.ToArray(); diff --git a/LibGit2Sharp/RepositoryNotFoundException.cs b/LibGit2Sharp/RepositoryNotFoundException.cs index 2255c0891..e2bc63d8b 100644 --- a/LibGit2Sharp/RepositoryNotFoundException.cs +++ b/LibGit2Sharp/RepositoryNotFoundException.cs @@ -1,5 +1,7 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif namespace LibGit2Sharp { @@ -7,7 +9,9 @@ namespace LibGit2Sharp /// The exception that is thrown when a is being built with /// a path that doesn't point at a valid Git repository or workdir. /// +#if NETFRAMEWORK [Serializable] +#endif public class RepositoryNotFoundException : LibGit2SharpException { /// @@ -27,7 +31,7 @@ public RepositoryNotFoundException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public RepositoryNotFoundException(string format, params object[] args) : base(format, args) @@ -42,6 +46,7 @@ public RepositoryNotFoundException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -50,5 +55,6 @@ public RepositoryNotFoundException(string message, Exception innerException) protected RepositoryNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif } } diff --git a/LibGit2Sharp/RevWalker.cs b/LibGit2Sharp/RevWalker.cs new file mode 100644 index 000000000..cca213de6 --- /dev/null +++ b/LibGit2Sharp/RevWalker.cs @@ -0,0 +1,179 @@ +using System; +using LibGit2Sharp.Core; +using LibGit2Sharp.Core.Handles; + +namespace LibGit2Sharp +{ + /// + /// Creates a new revision walker to iterate through repository. + /// + /// + /// This revision walker uses a custom memory pool and an internal commit cache, so it is relatively expensive to allocate. + /// + /// For maximum performance, this revision walker should be reused for different walks. + /// + /// This revision walker is *not* thread safe: it may only be used to walk a repository on a single thread; + /// however, it is possible to have several revision walkers in several different threads walking the same repository. + /// + public sealed class RevWalker : IDisposable + { + private readonly RevWalkerHandle handle; + + /// + /// Creates a new revision walker to iterate through a repo. + /// + public RevWalker(Repository repo) + { + handle = Proxy.git_revwalk_new(repo.Handle); + repo.RegisterForCleanup(handle); + } + + /// + /// Resets the revision walker for reuse. + /// + /// + /// This will clear all the pushed and hidden commits, and leave the walker in a blank state (just like at creation) + /// ready to receive new commit pushes and start a new walk. + /// + /// The revision walk is automatically reset when a walk is over. + /// + public void Reset() + { + Proxy.git_revwalk_reset(handle); + } + + /// + /// Change the sorting mode when iterating through the repository's contents. + /// + /// + /// Changing the sorting mode resets the walker. + /// + public void Sorting(CommitSortStrategies sortMode) + { + Proxy.git_revwalk_sorting(handle, sortMode); + } + + /// + /// Gets the next commit from the revision walk. + /// + /// + /// The initial call to this method is *not* blocking when iterating through a repo with a time-sorting mode. + /// + /// Iterating with Topological or inverted modes makes the initial call blocking to preprocess the commit list, + /// but this block should be mostly unnoticeable on most repositories (topological preprocessing times at 0.3s + /// on the git.git repo). + /// + /// The revision walker is reset when the walk is over. + /// + /// New commit ID or null on error. + public ObjectId Next() + { + return Proxy.git_revwalk_next(handle); + } + + /// + /// Marks a commit (and its ancestors) uninteresting for the output. + /// + /// + /// The given ID must belong to a committish on the walked repository. + /// + /// The resolved commit and all its parents will be hidden from the output on the revision walk. + /// + public void Hide(ObjectId commitId) + { + Proxy.git_revwalk_hide(handle, commitId); + } + + /// + /// Hide matching references. + /// + /// + /// The OIDs pointed to by the references that match the given glob pattern and their ancestors will be hidden + /// from the output on the// revision walk. + /// + /// A leading 'refs/' is implied if not present as well as a trailing '/\*' if the glob lacks '?', '\*' or '['. + /// + /// Any references matching this glob which do not point to a committish will be ignored. + /// + /// the glob pattern references should match + public void HideGlob(string glob) + { + Proxy.git_revwalk_hide_glob(handle, glob); + } + + /// + /// Hide the OID pointed to by a reference + /// + /// + /// The reference must point to a committish. + /// + /// the reference to hide + public void HideRef(string refName) + { + Proxy.git_revwalk_hide_ref(handle, refName); + } + + /// + /// Marks a commit to start traversal from. + /// + /// + /// The given OID must belong to a commit on the walked repository. + /// + /// The given commit will be used as one of the roots when starting the revision walk. At least one commit must + /// be pushed the repository before a walk can be started. + public void Push(ObjectId commitId) + { + Proxy.git_revwalk_push(handle, commitId); + } + + /// + /// Push matching references + /// + /// + /// The OIDs pointed to by the references that match the given glob pattern will be pushed to the revision walker. + /// + /// A leading 'refs/' is implied if not present as well as a trailing '/\*' if the glob lacks '?', '\*' or '['. + /// + /// Any references matching this glob which do not point to a committish will be ignored. + /// + /// The glob pattern references should match + public void PushGlob(string glob) + { + Proxy.git_revwalk_push_glob(handle, glob); + } + + /// + /// Push the OID pointed to by a reference + /// + /// + /// The reference must point to a committish. + /// + public void PushRef(string refName) + { + Proxy.git_revwalk_push_ref(handle, refName); + } + + /// + /// Simplify the history by first-parent. + /// + /// + /// No parents other than the first for each commit will be enqueued. + /// + public void SimplifyFirstParent() + { + Proxy.git_revwalk_simplify_first_parent(handle); + } + + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + handle.SafeDispose(); + } + } +} diff --git a/LibGit2Sharp/Signature.cs b/LibGit2Sharp/Signature.cs index bad656d05..7ed7a4916 100644 --- a/LibGit2Sharp/Signature.cs +++ b/LibGit2Sharp/Signature.cs @@ -86,10 +86,10 @@ public DateTimeOffset When } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as Signature); diff --git a/LibGit2Sharp/SmartSubtransport.cs b/LibGit2Sharp/SmartSubtransport.cs index 66fcd12bf..e7a331b4b 100644 --- a/LibGit2Sharp/SmartSubtransport.cs +++ b/LibGit2Sharp/SmartSubtransport.cs @@ -158,7 +158,7 @@ public int AcquireCredentials(out Credentials cred, string user, params Type[] m /// The endpoint to connect to /// The type of connection to create /// A SmartSubtransportStream representing the connection - protected abstract SmartSubtransportStream Action(String url, GitSmartSubtransportAction action); + protected abstract SmartSubtransportStream Action(string url, GitSmartSubtransportAction action); /// /// Invoked by libgit2 when this subtransport is no longer needed, but may be re-used in the future. @@ -225,7 +225,7 @@ private static int Action( stream = IntPtr.Zero; SmartSubtransport t = GCHandle.FromIntPtr(Marshal.ReadIntPtr(subtransport, GitSmartSubtransport.GCHandleOffset)).Target as SmartSubtransport; - String urlAsString = LaxUtf8Marshaler.FromNative(url); + string urlAsString = LaxUtf8Marshaler.FromNative(url); if (t == null) { @@ -233,12 +233,12 @@ private static int Action( return (int)GitErrorCode.Error; } - if (String.IsNullOrEmpty(urlAsString)) + if (string.IsNullOrEmpty(urlAsString)) { urlAsString = t.LastActionUrl; } - if (String.IsNullOrEmpty(urlAsString)) + if (string.IsNullOrEmpty(urlAsString)) { Proxy.git_error_set_str(GitErrorCategory.Net, "no url provided"); return (int)GitErrorCode.Error; diff --git a/LibGit2Sharp/StashCollection.cs b/LibGit2Sharp/StashCollection.cs index 5fe775eba..42162ada5 100644 --- a/LibGit2Sharp/StashCollection.cs +++ b/LibGit2Sharp/StashCollection.cs @@ -67,7 +67,7 @@ public virtual Stash this[int index] { if (index < 0) { - throw new ArgumentOutOfRangeException("index", "The passed index must be a positive integer."); + throw new ArgumentOutOfRangeException(nameof(index), "The passed index must be a positive integer."); } GitObject stashCommit = repo.Lookup(string.Format(CultureInfo.InvariantCulture, @@ -146,7 +146,7 @@ public virtual StashApplyStatus Apply(int index, StashApplyOptions options) { if (index < 0) { - throw new ArgumentException("The passed index must be a positive integer.", "index"); + throw new ArgumentException("The passed index must be a positive integer.", nameof(index)); } if (options == null) @@ -189,7 +189,7 @@ public virtual StashApplyStatus Pop(int index, StashApplyOptions options) { if (index < 0) { - throw new ArgumentException("The passed index must be a positive integer.", "index"); + throw new ArgumentException("The passed index must be a positive integer.", nameof(index)); } if (options == null) @@ -231,7 +231,7 @@ public virtual void Remove(int index) { if (index < 0) { - throw new ArgumentException("The passed index must be a positive integer.", "index"); + throw new ArgumentException("The passed index must be a positive integer.", nameof(index)); } Proxy.git_stash_drop(repo.Handle, index); diff --git a/LibGit2Sharp/StatusEntry.cs b/LibGit2Sharp/StatusEntry.cs index 7008712c6..bd2ef8883 100644 --- a/LibGit2Sharp/StatusEntry.cs +++ b/LibGit2Sharp/StatusEntry.cs @@ -66,10 +66,10 @@ public virtual RenameDetails IndexToWorkDirRenameDetails } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as StatusEntry); diff --git a/LibGit2Sharp/Submodule.cs b/LibGit2Sharp/Submodule.cs index ace995205..f8193af13 100644 --- a/LibGit2Sharp/Submodule.cs +++ b/LibGit2Sharp/Submodule.cs @@ -107,10 +107,10 @@ public virtual SubmoduleStatus RetrieveStatus() } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as Submodule); @@ -136,7 +136,7 @@ public override int GetHashCode() } /// - /// Returns the , a representation of the current . + /// Returns the , a representation of the current . /// /// The that represents the current . public override string ToString() diff --git a/LibGit2Sharp/SubmoduleCollection.cs b/LibGit2Sharp/SubmoduleCollection.cs index fc508107a..061196c7d 100644 --- a/LibGit2Sharp/SubmoduleCollection.cs +++ b/LibGit2Sharp/SubmoduleCollection.cs @@ -74,43 +74,41 @@ public virtual void Init(string name, bool overwrite) /// Update specified submodule. /// /// This will: - /// 1) Optionally initialize the if it not already initialzed, + /// 1) Optionally initialize the if it not already initialized, /// 2) clone the sub repository if it has not already been cloned, and /// 3) checkout the commit ID for the submodule in the sub repository. /// /// /// The name of the submodule to update. - /// Options controlling submodule udpate behavior and callbacks. + /// Options controlling submodule update behavior and callbacks. public virtual void Update(string name, SubmoduleUpdateOptions options) { - options = options ?? new SubmoduleUpdateOptions(); + options ??= new SubmoduleUpdateOptions(); - using (var handle = Proxy.git_submodule_lookup(repo.Handle, name)) - { - if (handle == null) - { - throw new NotFoundException("Submodule lookup failed for '{0}'.", - name); - } + using var handle = Proxy.git_submodule_lookup(repo.Handle, name) ?? throw new NotFoundException("Submodule lookup failed for '{0}'.", name); + using var checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options); + using var fetchOptionsWrapper = new GitFetchOptionsWrapper(); - using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options)) - { - var gitCheckoutOptions = checkoutOptionsWrapper.Options; + var gitCheckoutOptions = checkoutOptionsWrapper.Options; - var remoteCallbacks = new RemoteCallbacks(options); - var gitRemoteCallbacks = remoteCallbacks.GenerateCallbacks(); + var gitFetchOptions = fetchOptionsWrapper.Options; + gitFetchOptions.ProxyOptions = options.FetchOptions.ProxyOptions.CreateGitProxyOptions(); + gitFetchOptions.RemoteCallbacks = new RemoteCallbacks(options.FetchOptions).GenerateCallbacks(); - var gitSubmoduleUpdateOpts = new GitSubmoduleUpdateOptions - { - Version = 1, - CheckoutOptions = gitCheckoutOptions, - FetchOptions = new GitFetchOptions { ProxyOptions = new GitProxyOptions { Version = 1 }, RemoteCallbacks = gitRemoteCallbacks }, - CloneCheckoutStrategy = CheckoutStrategy.GIT_CHECKOUT_SAFE - }; - - Proxy.git_submodule_update(handle, options.Init, ref gitSubmoduleUpdateOpts); - } + if (options.FetchOptions != null && options.FetchOptions.CustomHeaders != null) + { + gitFetchOptions.CustomHeaders = + GitStrArrayManaged.BuildFrom(options.FetchOptions.CustomHeaders); } + + var gitSubmoduleUpdateOpts = new GitSubmoduleUpdateOptions + { + Version = 1, + CheckoutOptions = gitCheckoutOptions, + FetchOptions = gitFetchOptions + }; + + Proxy.git_submodule_update(handle, options.Init, ref gitSubmoduleUpdateOpts); } /// diff --git a/LibGit2Sharp/SubmoduleUpdateOptions.cs b/LibGit2Sharp/SubmoduleUpdateOptions.cs index 89f895d75..082e17338 100644 --- a/LibGit2Sharp/SubmoduleUpdateOptions.cs +++ b/LibGit2Sharp/SubmoduleUpdateOptions.cs @@ -6,7 +6,7 @@ namespace LibGit2Sharp /// /// Options controlling Submodule Update behavior and callbacks. /// - public sealed class SubmoduleUpdateOptions : FetchOptionsBase, IConvertableToGitCheckoutOpts + public sealed class SubmoduleUpdateOptions : IConvertableToGitCheckoutOpts { /// /// Initialize the submodule if it is not already initialized. @@ -30,6 +30,11 @@ public sealed class SubmoduleUpdateOptions : FetchOptionsBase, IConvertableToGit /// public CheckoutNotifyFlags CheckoutNotifyFlags { get; set; } + /// + /// Collection of parameters controlling Fetch behavior. + /// + public FetchOptions FetchOptions { get; internal set; } = new(); + CheckoutCallbacks IConvertableToGitCheckoutOpts.GenerateCallbacks() { return CheckoutCallbacks.From(OnCheckoutProgress, OnCheckoutNotify); diff --git a/LibGit2Sharp/Tree.cs b/LibGit2Sharp/Tree.cs index f8494ad8b..64d46145d 100644 --- a/LibGit2Sharp/Tree.cs +++ b/LibGit2Sharp/Tree.cs @@ -90,7 +90,7 @@ internal static string CombinePath(string a, string b) { var bld = new StringBuilder(); bld.Append(a); - if (!String.IsNullOrEmpty(a) && + if (!string.IsNullOrEmpty(a) && !a.EndsWith("/", StringComparison.Ordinal) && !b.StartsWith("/", StringComparison.Ordinal)) { diff --git a/LibGit2Sharp/TreeChanges.cs b/LibGit2Sharp/TreeChanges.cs index 6e8a0eff5..6a54d9c09 100644 --- a/LibGit2Sharp/TreeChanges.cs +++ b/LibGit2Sharp/TreeChanges.cs @@ -55,7 +55,7 @@ private IEnumerable GetChangesOfKind(ChangeKind changeKind) private unsafe bool TryGetEntryWithChangeTypeAt(int index, ChangeKind changeKind, out TreeEntryChanges entry) { if (index < 0 || index > count.Value) - throw new ArgumentOutOfRangeException("index", "Index was out of range. Must be non-negative and less than the size of the collection."); + throw new ArgumentOutOfRangeException(nameof(index), "Index was out of range. Must be non-negative and less than the size of the collection."); var delta = Proxy.git_diff_get_delta(diff, index); @@ -90,7 +90,7 @@ public virtual IEnumerator GetEnumerator() private unsafe TreeEntryChanges GetEntryAt(int index) { if (index < 0 || index > count.Value) - throw new ArgumentOutOfRangeException("index", "Index was out of range. Must be non-negative and less than the size of the collection."); + throw new ArgumentOutOfRangeException(nameof(index), "Index was out of range. Must be non-negative and less than the size of the collection."); return new TreeEntryChanges(Proxy.git_diff_get_delta(diff, index)); } diff --git a/LibGit2Sharp/TreeEntry.cs b/LibGit2Sharp/TreeEntry.cs index e500a8ee1..943e14570 100644 --- a/LibGit2Sharp/TreeEntry.cs +++ b/LibGit2Sharp/TreeEntry.cs @@ -93,10 +93,10 @@ private GitObject RetrieveTreeEntryTarget() } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as TreeEntry); diff --git a/LibGit2Sharp/TreeEntryDefinition.cs b/LibGit2Sharp/TreeEntryDefinition.cs index 2a3ceb35f..d32cc722c 100644 --- a/LibGit2Sharp/TreeEntryDefinition.cs +++ b/LibGit2Sharp/TreeEntryDefinition.cs @@ -112,10 +112,10 @@ internal static TreeEntryDefinition From(Tree tree) } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as TreeEntryDefinition); diff --git a/LibGit2Sharp/UnbornBranchException.cs b/LibGit2Sharp/UnbornBranchException.cs index 34ef437cb..8f01a63ab 100644 --- a/LibGit2Sharp/UnbornBranchException.cs +++ b/LibGit2Sharp/UnbornBranchException.cs @@ -1,5 +1,7 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif namespace LibGit2Sharp { @@ -7,7 +9,9 @@ namespace LibGit2Sharp /// The exception that is thrown when a operation requiring an existing /// branch is performed against an unborn branch. /// +#if NETFRAMEWORK [Serializable] +#endif public class UnbornBranchException : LibGit2SharpException { /// @@ -27,7 +31,7 @@ public UnbornBranchException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public UnbornBranchException(string format, params object[] args) : base(format, args) @@ -42,6 +46,7 @@ public UnbornBranchException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -50,5 +55,6 @@ public UnbornBranchException(string message, Exception innerException) protected UnbornBranchException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif } } diff --git a/LibGit2Sharp/UnmatchedPathException.cs b/LibGit2Sharp/UnmatchedPathException.cs index 7d118346d..96e5654c7 100644 --- a/LibGit2Sharp/UnmatchedPathException.cs +++ b/LibGit2Sharp/UnmatchedPathException.cs @@ -1,12 +1,16 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif namespace LibGit2Sharp { /// /// The exception that is thrown when an explicit path or a list of explicit paths could not be matched. /// +#if NETFRAMEWORK [Serializable] +#endif public class UnmatchedPathException : LibGit2SharpException { /// @@ -26,7 +30,7 @@ public UnmatchedPathException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public UnmatchedPathException(string format, params object[] args) : base(format, args) @@ -41,6 +45,7 @@ public UnmatchedPathException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -49,5 +54,6 @@ public UnmatchedPathException(string message, Exception innerException) protected UnmatchedPathException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif } } diff --git a/LibGit2Sharp/UnmergedIndexEntriesException.cs b/LibGit2Sharp/UnmergedIndexEntriesException.cs index 7594049b1..f9f1a834b 100644 --- a/LibGit2Sharp/UnmergedIndexEntriesException.cs +++ b/LibGit2Sharp/UnmergedIndexEntriesException.cs @@ -1,5 +1,7 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif using LibGit2Sharp.Core; namespace LibGit2Sharp @@ -8,7 +10,9 @@ namespace LibGit2Sharp /// The exception that is thrown when an operation that requires a fully merged index /// is performed against an index with unmerged entries /// +#if NETFRAMEWORK [Serializable] +#endif public class UnmergedIndexEntriesException : NativeException { /// @@ -28,7 +32,7 @@ public UnmergedIndexEntriesException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public UnmergedIndexEntriesException(string format, params object[] args) : base(format, args) @@ -43,6 +47,7 @@ public UnmergedIndexEntriesException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -51,6 +56,7 @@ public UnmergedIndexEntriesException(string message, Exception innerException) protected UnmergedIndexEntriesException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif internal UnmergedIndexEntriesException(string message, GitErrorCategory category) : base(message, category) diff --git a/LibGit2Sharp/UserCanceledException.cs b/LibGit2Sharp/UserCanceledException.cs index ba6458049..f3c6af7dd 100644 --- a/LibGit2Sharp/UserCanceledException.cs +++ b/LibGit2Sharp/UserCanceledException.cs @@ -1,5 +1,7 @@ using System; +#if NETFRAMEWORK using System.Runtime.Serialization; +#endif using LibGit2Sharp.Core; namespace LibGit2Sharp @@ -7,7 +9,9 @@ namespace LibGit2Sharp /// /// The exception that is thrown when an operation is canceled. /// +#if NETFRAMEWORK [Serializable] +#endif public class UserCancelledException : NativeException { /// @@ -27,7 +31,7 @@ public UserCancelledException(string message) /// /// Initializes a new instance of the class with a specified error message. /// - /// A composite format string for use in . + /// A composite format string for use in . /// An object array that contains zero or more objects to format. public UserCancelledException(string format, params object[] args) : base(format, args) @@ -42,6 +46,7 @@ public UserCancelledException(string message, Exception innerException) : base(message, innerException) { } +#if NETFRAMEWORK /// /// Initializes a new instance of the class with a serialized data. /// @@ -50,6 +55,7 @@ public UserCancelledException(string message, Exception innerException) protected UserCancelledException(SerializationInfo info, StreamingContext context) : base(info, context) { } +#endif internal UserCancelledException(string message, GitErrorCategory category) : base(message, category) diff --git a/LibGit2Sharp/Worktree.cs b/LibGit2Sharp/Worktree.cs index 13fea072b..bd10f6110 100644 --- a/LibGit2Sharp/Worktree.cs +++ b/LibGit2Sharp/Worktree.cs @@ -65,10 +65,10 @@ internal WorktreeHandle GetWorktreeHandle() public virtual string LockReason { get { return worktreeLock == null ? null : worktreeLock.Reason; } } /// - /// Determines whether the specified is equal to the current . + /// Determines whether the specified is equal to the current . /// - /// The to compare with the current . - /// True if the specified is equal to the current ; otherwise, false. + /// The to compare with the current . + /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as Worktree); @@ -118,7 +118,7 @@ public override int GetHashCode() } /// - /// Returns the , a representation of the current . + /// Returns the , a representation of the current . /// /// The that represents the current . public override string ToString() diff --git a/NativeLibraryLoadTestApp/Directory.Build.props b/NativeLibraryLoadTestApp/Directory.Build.props deleted file mode 100644 index c55b35c72..000000000 --- a/NativeLibraryLoadTestApp/Directory.Build.props +++ /dev/null @@ -1,7 +0,0 @@ - - - - false - - - diff --git a/TrimmingTestApp/Program.cs b/TrimmingTestApp/Program.cs new file mode 100644 index 000000000..e568c227b --- /dev/null +++ b/TrimmingTestApp/Program.cs @@ -0,0 +1,3 @@ +using LibGit2Sharp; + +_ = new Repository(); diff --git a/TrimmingTestApp/TrimmingTestApp.csproj b/TrimmingTestApp/TrimmingTestApp.csproj new file mode 100644 index 000000000..9cb7e75b4 --- /dev/null +++ b/TrimmingTestApp/TrimmingTestApp.csproj @@ -0,0 +1,18 @@ + + + + net8.0 + Exe + enable + enable + true + true + true + + + + + + + +