Welcome to mirror list, hosted at ThFree Co, Russian Federation.

IndexEntry.cs « LibGit2Sharp - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8bb5ea468763afb29dd566aab5d609f646956dd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Runtime.InteropServices;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;

namespace LibGit2Sharp
{
    /// <summary>
    ///   A reference to a <see cref = "Blob" /> known by the <see cref = "Index" />.
    /// </summary>
    public class IndexEntry : IEquatable<IndexEntry>
    {
        private static readonly LambdaEqualityHelper<IndexEntry> equalityHelper =
            new LambdaEqualityHelper<IndexEntry>(new Func<IndexEntry, object>[] { x => x.Path, x => x.Id, x => x.State });

        private Func<FileStatus> state;

        private static readonly Utf8Marshaler marshaler = (Utf8Marshaler)Utf8Marshaler.GetInstance(string.Empty);

        /// <summary>
        ///   State of the version of the <see cref = "Blob" /> pointed at by this <see cref = "IndexEntry" />, 
        ///   compared against the <see cref = "Blob" /> known from the <see cref = "Repository.Head" /> and the file in the working directory.
        /// </summary>
        public FileStatus State
        {
            get { return state(); }
        }

        /// <summary>
        ///   Gets the relative path to the file within the working directory.
        /// </summary>
        public string Path { get; private set; }

        /// <summary>
        ///   Gets the id of the <see cref = "Blob" /> pointed at by this index entry.
        /// </summary>
        public ObjectId Id { get; private set; }

        internal static IndexEntry CreateFromPtr(Repository repo, IndexEntrySafeHandle handle)
        {
            GitIndexEntry entry = handle.MarshalAsGitIndexEntry();

            FilePath path = (string)marshaler.MarshalNativeToManaged(entry.Path);

            return new IndexEntry
                       {
                           Path = path.Native,
                           Id = new ObjectId(entry.oid),
                           state = () => repo.Index.RetrieveStatus(path.Native)
                       };
        }

        /// <summary>
        ///   Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "IndexEntry" />.
        /// </summary>
        /// <param name = "obj">The <see cref = "Object" /> to compare with the current <see cref = "IndexEntry" />.</param>
        /// <returns>True if the specified <see cref = "Object" /> is equal to the current <see cref = "IndexEntry" />; otherwise, false.</returns>
        public override bool Equals(object obj)
        {
            return Equals(obj as IndexEntry);
        }

        /// <summary>
        ///   Determines whether the specified <see cref = "IndexEntry" /> is equal to the current <see cref = "IndexEntry" />.
        /// </summary>
        /// <param name = "other">The <see cref = "IndexEntry" /> to compare with the current <see cref = "IndexEntry" />.</param>
        /// <returns>True if the specified <see cref = "IndexEntry" /> is equal to the current <see cref = "IndexEntry" />; otherwise, false.</returns>
        public bool Equals(IndexEntry other)
        {
            return equalityHelper.Equals(this, other);
        }

        /// <summary>
        ///   Returns the hash code for this instance.
        /// </summary>
        /// <returns>A 32-bit signed integer hash code.</returns>
        public override int GetHashCode()
        {
            return equalityHelper.GetHashCode(this);
        }

        /// <summary>
        ///   Tests if two <see cref = "IndexEntry" /> are equal.
        /// </summary>
        /// <param name = "left">First <see cref = "IndexEntry" /> to compare.</param>
        /// <param name = "right">Second <see cref = "IndexEntry" /> to compare.</param>
        /// <returns>True if the two objects are equal; false otherwise.</returns>
        public static bool operator ==(IndexEntry left, IndexEntry right)
        {
            return Equals(left, right);
        }

        /// <summary>
        ///   Tests if two <see cref = "IndexEntry" /> are different.
        /// </summary>
        /// <param name = "left">First <see cref = "IndexEntry" /> to compare.</param>
        /// <param name = "right">Second <see cref = "IndexEntry" /> to compare.</param>
        /// <returns>True if the two objects are different; false otherwise.</returns>
        public static bool operator !=(IndexEntry left, IndexEntry right)
        {
            return !Equals(left, right);
        }
    }
}