using System; 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); private readonly Repository repository; /// /// Needed for mocking purposes. /// protected Remote() { } private Remote(Repository repository, string name, string url) { this.repository = repository; Name = name; Url = url; } internal static Remote BuildFromPtr(RemoteSafeHandle handle, Repository repo) { string name = Proxy.git_remote_name(handle); string url = Proxy.git_remote_url(handle); var remote = new Remote(repo, name, url); 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; } /// /// 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 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); } } } }