using System;
using System.Diagnostics;
using System.Globalization;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
///
/// A Submodule.
///
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class Submodule : IEquatable, IBelongToARepository
{
private static readonly LambdaEqualityHelper equalityHelper =
new LambdaEqualityHelper(x => x.Name, x => x.HeadCommitId);
private readonly Repository repo;
private readonly string name;
private readonly string path;
private readonly string url;
private readonly ILazy headCommitId;
private readonly ILazy indexCommitId;
private readonly ILazy workdirCommitId;
private readonly ILazy fetchRecurseSubmodulesRule;
private readonly ILazy ignoreRule;
private readonly ILazy updateRule;
///
/// Needed for mocking purposes.
///
protected Submodule()
{ }
internal Submodule(Repository repo, string name, string path, string url)
{
this.repo = repo;
this.name = name;
this.path = path;
this.url = url;
var commitIds = new SubmoduleLazyGroup(repo, name);
headCommitId = commitIds.AddLazy(Proxy.git_submodule_head_id);
indexCommitId = commitIds.AddLazy(Proxy.git_submodule_index_id);
workdirCommitId = commitIds.AddLazy(Proxy.git_submodule_wd_id);
var rules = new SubmoduleLazyGroup(repo, name);
fetchRecurseSubmodulesRule = rules.AddLazy(Proxy.git_submodule_fetch_recurse_submodules);
ignoreRule = rules.AddLazy(Proxy.git_submodule_ignore);
updateRule = rules.AddLazy(Proxy.git_submodule_update_strategy);
}
///
/// The name of the submodule.
///
public virtual string Name { get { return name; } }
///
/// The path of the submodule.
///
public virtual string Path { get { return path; } }
///
/// The URL of the submodule.
///
public virtual string Url { get { return url; } }
///
/// The commit ID for this submodule in the current HEAD tree.
///
public virtual ObjectId HeadCommitId { get { return headCommitId.Value; } }
///
/// The commit ID for this submodule in the index.
///
public virtual ObjectId IndexCommitId { get { return indexCommitId.Value; } }
///
/// The commit ID for this submodule in the current working directory.
///
public virtual ObjectId WorkDirCommitId { get { return workdirCommitId.Value; } }
///
/// The fetchRecurseSubmodules rule for the submodule.
///
/// Note that at this time, LibGit2Sharp does not honor this setting and the
/// fetch functionality current ignores submodules.
///
public virtual SubmoduleRecurse FetchRecurseSubmodulesRule { get { return fetchRecurseSubmodulesRule.Value; } }
///
/// The ignore rule of the submodule.
///
public virtual SubmoduleIgnore IgnoreRule { get { return ignoreRule.Value; } }
///
/// The update rule of the submodule.
///
public virtual SubmoduleUpdate UpdateRule { get { return updateRule.Value; } }
///
/// Retrieves the state of this submodule in the working directory compared to the staging area and the latest commit.
///
/// The of this submodule.
public virtual SubmoduleStatus RetrieveStatus()
{
using (var handle = Proxy.git_submodule_lookup(repo.Handle, Name))
{
return Proxy.git_submodule_status(handle);
}
}
///
/// 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 Submodule);
}
///
/// 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(Submodule 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);
}
///
/// Returns the , a representation of the current .
///
/// The that represents the current .
public override string ToString()
{
return Name;
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"{0} => {1}", Name, Url);
}
}
IRepository IBelongToARepository.Repository { get { return repo; } }
}
}