/LibGit2Sharp/TreeEntry.cs

http://github.com/libgit2/libgit2sharp · C# · 158 lines · 88 code · 22 blank · 48 comment · 1 complexity · bb1a3888bc3a35d44fd98840b4710a1f MD5 · raw file

  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.Runtime.InteropServices;
  5. using LibGit2Sharp.Core;
  6. using LibGit2Sharp.Core.Handles;
  7. namespace LibGit2Sharp
  8. {
  9. /// <summary>
  10. /// Representation of an entry in a <see cref="Tree"/>.
  11. /// </summary>
  12. [DebuggerDisplay("{DebuggerDisplay,nq}")]
  13. public class TreeEntry : IEquatable<TreeEntry>
  14. {
  15. private readonly ObjectId parentTreeId;
  16. private readonly Repository repo;
  17. private readonly Lazy<GitObject> target;
  18. private readonly ObjectId targetOid;
  19. private readonly Lazy<string> path;
  20. private static readonly LambdaEqualityHelper<TreeEntry> equalityHelper =
  21. new LambdaEqualityHelper<TreeEntry>(x => x.Name, x => x.parentTreeId);
  22. /// <summary>
  23. /// Needed for mocking purposes.
  24. /// </summary>
  25. protected TreeEntry()
  26. { }
  27. internal unsafe TreeEntry(TreeEntryHandle entry, ObjectId parentTreeId, Repository repo, FilePath parentPath)
  28. {
  29. this.parentTreeId = parentTreeId;
  30. this.repo = repo;
  31. targetOid = Proxy.git_tree_entry_id(entry);
  32. GitObjectType treeEntryTargetType = Proxy.git_tree_entry_type(entry);
  33. TargetType = treeEntryTargetType.ToTreeEntryTargetType();
  34. target = new Lazy<GitObject>(RetrieveTreeEntryTarget);
  35. Mode = Proxy.git_tree_entry_attributes(entry);
  36. Name = Proxy.git_tree_entry_name(entry);
  37. path = new Lazy<string>(() => System.IO.Path.Combine(parentPath.Native, Name));
  38. }
  39. /// <summary>
  40. /// Gets the file mode.
  41. /// </summary>
  42. public virtual Mode Mode { get; private set; }
  43. /// <summary>
  44. /// Gets the filename.
  45. /// </summary>
  46. public virtual string Name { get; private set; }
  47. /// <summary>
  48. /// Gets the path.
  49. /// <para>The path is expressed in a relative form from the latest known <see cref="Tree"/>. Path segments are separated with a forward or backslash, depending on the OS the libray is being run on."/></para>
  50. /// </summary>
  51. public virtual string Path { get { return path.Value; } }
  52. /// <summary>
  53. /// Gets the <see cref="GitObject"/> being pointed at.
  54. /// </summary>
  55. public virtual GitObject Target { get { return target.Value; } }
  56. internal ObjectId TargetId
  57. {
  58. get { return targetOid; }
  59. }
  60. /// <summary>
  61. /// Gets the <see cref="TreeEntryTargetType"/> of the <see cref="Target"/> being pointed at.
  62. /// </summary>
  63. public virtual TreeEntryTargetType TargetType { get; private set; }
  64. private GitObject RetrieveTreeEntryTarget()
  65. {
  66. switch (TargetType)
  67. {
  68. case TreeEntryTargetType.GitLink:
  69. return new GitLink(repo, targetOid);
  70. case TreeEntryTargetType.Blob:
  71. case TreeEntryTargetType.Tree:
  72. return GitObject.BuildFrom(repo, targetOid, TargetType.ToGitObjectType(), Path);
  73. default:
  74. throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
  75. "TreeEntry target of type '{0}' is not supported.",
  76. TargetType));
  77. }
  78. }
  79. /// <summary>
  80. /// Determines whether the specified <see cref="Object"/> is equal to the current <see cref="TreeEntry"/>.
  81. /// </summary>
  82. /// <param name="obj">The <see cref="Object"/> to compare with the current <see cref="TreeEntry"/>.</param>
  83. /// <returns>True if the specified <see cref="Object"/> is equal to the current <see cref="TreeEntry"/>; otherwise, false.</returns>
  84. public override bool Equals(object obj)
  85. {
  86. return Equals(obj as TreeEntry);
  87. }
  88. /// <summary>
  89. /// Determines whether the specified <see cref="TreeEntry"/> is equal to the current <see cref="TreeEntry"/>.
  90. /// </summary>
  91. /// <param name="other">The <see cref="TreeEntry"/> to compare with the current <see cref="TreeEntry"/>.</param>
  92. /// <returns>True if the specified <see cref="TreeEntry"/> is equal to the current <see cref="TreeEntry"/>; otherwise, false.</returns>
  93. public bool Equals(TreeEntry other)
  94. {
  95. return equalityHelper.Equals(this, other);
  96. }
  97. /// <summary>
  98. /// Returns the hash code for this instance.
  99. /// </summary>
  100. /// <returns>A 32-bit signed integer hash code.</returns>
  101. public override int GetHashCode()
  102. {
  103. return equalityHelper.GetHashCode(this);
  104. }
  105. /// <summary>
  106. /// Tests if two <see cref="TreeEntry"/> are equal.
  107. /// </summary>
  108. /// <param name="left">First <see cref="TreeEntry"/> to compare.</param>
  109. /// <param name="right">Second <see cref="TreeEntry"/> to compare.</param>
  110. /// <returns>True if the two objects are equal; false otherwise.</returns>
  111. public static bool operator ==(TreeEntry left, TreeEntry right)
  112. {
  113. return Equals(left, right);
  114. }
  115. /// <summary>
  116. /// Tests if two <see cref="TreeEntry"/> are different.
  117. /// </summary>
  118. /// <param name="left">First <see cref="TreeEntry"/> to compare.</param>
  119. /// <param name="right">Second <see cref="TreeEntry"/> to compare.</param>
  120. /// <returns>True if the two objects are different; false otherwise.</returns>
  121. public static bool operator !=(TreeEntry left, TreeEntry right)
  122. {
  123. return !Equals(left, right);
  124. }
  125. private string DebuggerDisplay
  126. {
  127. get
  128. {
  129. return string.Format(CultureInfo.InvariantCulture,
  130. "TreeEntry: {0} => {1}",
  131. Path,
  132. TargetId);
  133. }
  134. }
  135. }
  136. }