using System;
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);
}
}
///
/// Update properties of a remote.
///
/// The remote to update.
/// Delegate to perform updates on the remote.
/// The updated remote.
public virtual Remote Update(Remote remote, params Action[] actions)
{
var updater = new RemoteUpdater(this.repository, remote);
foreach (Action action in actions)
{
action(updater);
}
return this[remote.Name];
}
///
/// 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_with_fetchspec(repository.Handle, name, url, fetchRefSpec))
{
return Remote.BuildFromPtr(handle, this.repository);
}
}
///
/// Deletes the with the specified name.
///
/// The name of the remote to remove.
/// A new .
public virtual void Remove(string name)
{
Ensure.ArgumentNotNull(name, "name");
Proxy.git_remote_delete(repository.Handle, name);
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"Count = {0}", this.Count());
}
}
}
}