using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp
{
///
/// The collection of in a
///
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class RemoteCollection : IEnumerable
{
private readonly Repository repository;
///
/// Needed for mocking purposes.
///
protected RemoteCollection()
{ }
internal RemoteCollection(Repository repository)
{
this.repository = repository;
}
///
/// Gets the with the specified name.
///
/// The name of the remote to retrieve.
/// The retrived if it has been found, null otherwise.
public virtual Remote this[string name]
{
get { return RemoteForName(name, false); }
}
internal Remote RemoteForName(string name, bool shouldThrowIfNotFound = true)
{
Ensure.ArgumentNotNull(name, "name");
using (RemoteSafeHandle handle = Proxy.git_remote_load(repository.Handle, name, shouldThrowIfNotFound))
{
return handle == null ? null : Remote.BuildFromPtr(handle, this.repository);
}
}
///
/// Returns an enumerator that iterates through the collection.
///
/// An object that can be used to iterate through the collection.
public virtual IEnumerator GetEnumerator()
{
return Proxy
.git_remote_list(repository.Handle)
.Select(n => this[n])
.GetEnumerator();
}
///
/// Returns an enumerator that iterates through the collection.
///
/// An object that can be used to iterate through the collection.
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
///
/// Creates a with the specified name and for the repository at the specified location.
///
/// A default fetch refspec will be added for this remote.
///
///
/// The name of the remote to create.
/// The location of the repository.
/// A new .
public virtual Remote Add(string name, string url)
{
Ensure.ArgumentNotNull(name, "name");
Ensure.ArgumentNotNull(url, "url");
using (RemoteSafeHandle handle = Proxy.git_remote_create(repository.Handle, name, url))
{
return Remote.BuildFromPtr(handle, this.repository);
}
}
///
/// Creates a with the specified name and for the repository at the specified location.
///
/// The name of the remote to create.
/// The location of the repository.
/// The refSpec to be used when fetching from this remote.
/// A new .
public virtual Remote Add(string name, string url, string fetchRefSpec)
{
Ensure.ArgumentNotNull(name, "name");
Ensure.ArgumentNotNull(url, "url");
Ensure.ArgumentNotNull(fetchRefSpec, "fetchRefSpec");
using (RemoteSafeHandle handle = Proxy.git_remote_create(repository.Handle, name, url))
{
Proxy.git_remote_add_fetch(handle, fetchRefSpec);
Proxy.git_remote_save(handle);
return Remote.BuildFromPtr(handle, this.repository);
}
}
///
/// Determines if the proposed remote name is well-formed.
///
/// The name to be checked.
/// true is the name is valid; false otherwise.
public virtual bool IsValidName(string name)
{
return Proxy.git_remote_is_valid_name(name);
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"Count = {0}", this.Count());
}
}
}
}