Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/BranchFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ public void CanSetUpstreamBranch()
Branch trackedBranch = repo.Branches[trackedBranchName];
Branch updatedBranch = repo.Branches.Update(branch,
b => b.Remote = remoteName,
b => b.UpstreamBranch = upstreamBranchName);
b => b.UpstreamBranch = upstreamBranchName);

// Verify the immutability of the branch.
Assert.False(branch.IsTracking);
Expand Down
28 changes: 28 additions & 0 deletions LibGit2Sharp.Tests/FetchFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;

namespace LibGit2Sharp.Tests
Expand Down Expand Up @@ -95,5 +97,31 @@ public void CanFetchAllTagsIntoAnEmptyRepository(string url)
expectedFetchState.CheckUpdatedReferences(repo);
}
}

[Theory]
[InlineData(TagFetchMode.All, 4)]
[InlineData(TagFetchMode.None, 0)]
[InlineData(TagFetchMode.Auto, 3)]
public void FetchRespectsConfiguredAutoTagSetting(TagFetchMode tagFetchMode, int expectedTagCount)
{
string url = "https://github.com/libgit2/TestGitRepository";

var scd = BuildSelfCleaningDirectory();
using (var repo = Repository.Init(scd.RootedDirectoryPath))
{
Remote remote = repo.Network.Remotes.Add(remoteName, url);
Assert.NotNull(remote);

// Update the configured autotag setting.
repo.Network.Remotes.Update(remote,
r => r.TagFetchMode = tagFetchMode);

// Perform the actual fetch.
repo.Network.Fetch(remote);

// Verify the number of fetched tags.
Assert.Equal(expectedTagCount, repo.Tags.Count());
}
}
}
}
23 changes: 23 additions & 0 deletions LibGit2Sharp.Tests/RemoteFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ public void CanEnumerateTheRemotes()
}
}

[Theory]
[InlineData(TagFetchMode.All)]
[InlineData(TagFetchMode.Auto)]
[InlineData(TagFetchMode.None)]
public void CanSetTagFetchMode(TagFetchMode tagFetchMode)
{
string path = CloneBareTestRepo();
using (var repo = new Repository(path))
{
const string name = "upstream";
const string url = "https://github.com/libgit2/libgit2sharp.git";

repo.Network.Remotes.Add(name, url);
Remote remote = repo.Network.Remotes[name];
Assert.NotNull(remote);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While playing with your PR, I detected a strange behaviour. Applying the following patch makes the test fail:

diff --git a/LibGit2Sharp.Tests/RemoteFixture.cs b/LibGit2Sharp.Tests/RemoteFixture.cs
index 02a293b..89f0d55 100644
--- a/LibGit2Sharp.Tests/RemoteFixture.cs
+++ b/LibGit2Sharp.Tests/RemoteFixture.cs
@@ -56,8 +56,11 @@ public void CanSetTagFetchMode(TagFetchMode tagFetchMode)
                 const string name = "upstream";
                 const string url = "https://github.com/libgit2/libgit2sharp.git";

-                repo.Network.Remotes.Add(name, url);
+                Remote created = repo.Network.Remotes.Add(name, url);
                 Remote remote = repo.Network.Remotes[name];
+
+                Assert.Equal(created.TagFetchMode, remote.TagFetchMode);
+
                 Assert.NotNull(remote);

                 Remote updatedremote = repo.Network.Remotes.Update(remote,

I've submitted libgit2/libgit2#1623 in order to solve this.

/cc @carlosmn

Remote updatedremote = repo.Network.Remotes.Update(remote,
r => r.TagFetchMode = tagFetchMode);

Assert.Equal(tagFetchMode, updatedremote.TagFetchMode);
}
}

[Fact]
public void CanCheckEqualityOfRemote()
{
Expand Down
3 changes: 3 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,9 @@ internal static extern int git_refspec_rtransform(
GitRefSpecHandle refSpec,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(Utf8Marshaler))] string name);

[DllImport(libgit2)]
internal static extern int git_remote_autotag(RemoteSafeHandle remote);

[DllImport(libgit2)]
internal static extern int git_remote_connect(RemoteSafeHandle remote, GitDirection direction);

Expand Down
5 changes: 5 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,11 @@ public static string git_refspec_rtransform(GitRefSpecHandle refSpecPtr, string

#region git_remote_

public static TagFetchMode git_remote_autotag(RemoteSafeHandle remote)
{
return (TagFetchMode) NativeMethods.git_remote_autotag(remote);
}

public static RemoteSafeHandle git_remote_create(RepositorySafeHandle repo, string name, string url)
{
using (ThreadAffinity())
Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Compile Include="ReferenceExtensions.cs" />
<Compile Include="Conflict.cs" />
<Compile Include="ConflictCollection.cs" />
<Compile Include="RemoteUpdater.cs" />
<Compile Include="RemoveFromIndexException.cs" />
<Compile Include="Core\Handles\GitRefSpecHandle.cs" />
<Compile Include="Core\Handles\NullRepositorySafeHandle.cs" />
Expand Down
7 changes: 5 additions & 2 deletions LibGit2Sharp/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public virtual IEnumerable<DirectReference> ListReferences(Remote remote)
/// <param name="credentials">Credentials to use for username/password authentication.</param>
public virtual void Fetch(
Remote remote,
TagFetchMode tagFetchMode = TagFetchMode.Auto,
TagFetchMode? tagFetchMode = null,
ProgressHandler onProgress = null,
CompletionHandler onCompletion = null,
UpdateTipsHandler onUpdateTips = null,
Expand All @@ -121,7 +121,10 @@ public virtual void Fetch(
var callbacks = new RemoteCallbacks(onProgress, onCompletion, onUpdateTips);
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();

Proxy.git_remote_set_autotag(remoteHandle, tagFetchMode);
if (tagFetchMode.HasValue)
{
Proxy.git_remote_set_autotag(remoteHandle, tagFetchMode.Value);
}

if (credentials != null)
{
Expand Down
11 changes: 9 additions & 2 deletions LibGit2Sharp/Remote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@ public class Remote : IEquatable<Remote>
protected Remote()
{ }

private Remote(Repository repository, string name, string url)
private Remote(Repository repository, string name, string url, TagFetchMode tagFetchMode)
{
this.repository = repository;
Name = name;
Url = url;
TagFetchMode = tagFetchMode;
}

internal static Remote BuildFromPtr(RemoteSafeHandle handle, Repository repo)
{
string name = Proxy.git_remote_name(handle);
string url = Proxy.git_remote_url(handle);
TagFetchMode tagFetchMode = Proxy.git_remote_autotag(handle);

var remote = new Remote(repo, name, url);
var remote = new Remote(repo, name, url, tagFetchMode);

return remote;
}
Expand All @@ -50,6 +52,11 @@ internal static Remote BuildFromPtr(RemoteSafeHandle handle, Repository repo)
/// </summary>
public virtual string Url { get; private set; }

/// <summary>
/// Gets the Tag Fetch Mode of the remote - indicating how tags are fetched.
/// </summary>
public virtual TagFetchMode TagFetchMode { get; private set; }

/// <summary>
/// Transform a reference to its source reference using the <see cref = "Remote" />'s default fetchspec.
/// </summary>
Expand Down
21 changes: 20 additions & 1 deletion LibGit2Sharp/RemoteCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -47,6 +48,24 @@ internal Remote RemoteForName(string name, bool shouldThrowIfNotFound = true)
}
}

/// <summary>
/// Update properties of a remote.
/// </summary>
/// <param name="remote">The remote to update.</param>
/// <param name="actions">Delegate to perform updates on the remote.</param>
/// <returns>The updated remote.</returns>
public virtual Remote Update(Remote remote, params Action<RemoteUpdater>[] actions)
{
var updater = new RemoteUpdater(this.repository, remote);

foreach (Action<RemoteUpdater> action in actions)
{
action(updater);
}

return this[remote.Name];
}

/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
Expand Down
48 changes: 48 additions & 0 deletions LibGit2Sharp/RemoteUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;

namespace LibGit2Sharp
{
/// <summary>
/// Exposes properties of a remote that can be updated.
/// </summary>
public class RemoteUpdater
{
private readonly Repository repo;
private readonly Remote remote;

/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected RemoteUpdater()
{ }

internal RemoteUpdater(Repository repo, Remote remote)
{
Ensure.ArgumentNotNull(repo, "repo");
Ensure.ArgumentNotNull(remote, "remote");

this.repo = repo;
this.remote = remote;
}

/// <summary>
/// Set the default TagFetchMode value for the remote.
/// </summary>
public virtual TagFetchMode TagFetchMode
{
set
{
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repo.Handle, remote.Name, true))
{
Proxy.git_remote_set_autotag(remoteHandle, value);
Proxy.git_remote_save(remoteHandle);
}
}
}
}
}