using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Globalization;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp
{
///
/// A remote repository whose branches are tracked.
///
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class Remote : IEquatable
{
private static readonly LambdaEqualityHelper equalityHelper =
new LambdaEqualityHelper(x => x.Name, x => x.Url);
internal readonly Repository repository;
private readonly RefSpecCollection refSpecs;
///
/// Needed for mocking purposes.
///
protected Remote()
{ }
private Remote(RemoteSafeHandle handle, Repository repository)
{
this.repository = repository;
Name = Proxy.git_remote_name(handle);
Url = Proxy.git_remote_url(handle);
TagFetchMode = Proxy.git_remote_autotag(handle);
refSpecs = new RefSpecCollection(handle);
}
internal static Remote BuildFromPtr(RemoteSafeHandle handle, Repository repo)
{
var remote = new Remote(handle, repo);
return remote;
}
///
/// Gets the alias of this remote repository.
///
public virtual string Name { get; private set; }
///
/// Gets the url to use to communicate with this remote repository.
///
public virtual string Url { get; private set; }
///
/// Gets the Tag Fetch Mode of the remote - indicating how tags are fetched.
///
public virtual TagFetchMode TagFetchMode { get; private set; }
///
/// Gets the list of s defined for this
///
public virtual IEnumerable RefSpecs { get { return refSpecs; } }
///
/// Gets the list of s defined for this
/// that are intended to be used during a Fetch operation
///
public virtual IEnumerable FetchRefSpecs
{
get { return refSpecs.Where(r => r.Direction == RefSpecDirection.Fetch); }
}
///
/// Gets the list of s defined for this
/// that are intended to be used during a Push operation
///
public virtual IEnumerable PushRefSpecs
{
get { return refSpecs.Where(r => r.Direction == RefSpecDirection.Push); }
}
///
/// Transform a reference to its source reference using the 's default fetchspec.
///
/// The reference to transform.
/// The transformed reference.
internal string FetchSpecTransformToSource(string reference)
{
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repository.Handle, Name, true))
{
GitRefSpecHandle fetchSpecPtr = Proxy.git_remote_get_refspec(remoteHandle, 0);
return Proxy.git_refspec_rtransform(fetchSpecPtr, reference);
}
}
///
/// Determines if the proposed remote name is well-formed.
///
/// The name to be checked.
/// true if the name is valid; false otherwise.
public static bool IsValidName(string name)
{
return Proxy.git_remote_is_valid_name(name);
}
///
/// Determines if the proposed remote URL is supported by the library.
///
/// The URL to be checked.
/// true if the url is supported; false otherwise.
public static bool IsSupportedUrl(string url)
{
return Proxy.git_remote_supported_url(url);
}
///
/// 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.
public override bool Equals(object obj)
{
return Equals(obj as Remote);
}
///
/// 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.
public bool Equals(Remote other)
{
return equalityHelper.Equals(this, other);
}
///
/// Returns the hash code for this instance.
///
/// A 32-bit signed integer hash code.
public override int GetHashCode()
{
return equalityHelper.GetHashCode(this);
}
///
/// Tests if two are equal.
///
/// First to compare.
/// Second to compare.
/// True if the two objects are equal; false otherwise.
public static bool operator ==(Remote left, Remote right)
{
return Equals(left, right);
}
///
/// Tests if two are different.
///
/// First to compare.
/// Second to compare.
/// True if the two objects are different; false otherwise.
public static bool operator !=(Remote left, Remote right)
{
return !Equals(left, right);
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"{0} => {1}", Name, Url);
}
}
}
}