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

GitObject.cs « LibGit2Sharp - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9418f3f055d67e0aa441676c438f0a685568af7d (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Globalization;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;

namespace LibGit2Sharp
{
    /// <summary>
    ///   A GitObject
    /// </summary>
    public class GitObject : IEquatable<GitObject>
    {
        internal static GitObjectTypeMap TypeToTypeMap =
            new GitObjectTypeMap
                {
                    { typeof(Commit), GitObjectType.Commit },
                    { typeof(Tree), GitObjectType.Tree },
                    { typeof(Blob), GitObjectType.Blob },
                    { typeof(TagAnnotation), GitObjectType.Tag },
                    { typeof(GitObject), GitObjectType.Any },
                };

        private static readonly LambdaEqualityHelper<GitObject> equalityHelper =
            new LambdaEqualityHelper<GitObject>(new Func<GitObject, object>[] { x => x.Id });

        /// <summary>
        ///   Initializes a new instance of the <see cref = "GitObject" /> class.
        /// </summary>
        /// <param name = "id">The <see cref = "ObjectId" /> it should be identified by.</param>
        protected GitObject(ObjectId id)
        {
            Id = id;
        }

        /// <summary>
        ///   Gets the id of this object
        /// </summary>
        public ObjectId Id { get; private set; }

        /// <summary>
        ///   Gets the 40 character sha1 of this object.
        /// </summary>
        public string Sha
        {
            get { return Id.Sha; }
        }

        internal static GitObject CreateFromPtr(GitObjectSafeHandle obj, ObjectId id, Repository repo, FilePath path)
        {
            GitObjectType type = NativeMethods.git_object_type(obj);
            switch (type)
            {
                case GitObjectType.Commit:
                    return Commit.BuildFromPtr(obj, id, repo);
                case GitObjectType.Tree:
                    return Tree.BuildFromPtr(obj, id, repo, path);
                case GitObjectType.Tag:
                    return TagAnnotation.BuildFromPtr(obj, id, repo);
                case GitObjectType.Blob:
                    return Blob.BuildFromPtr(obj, id, repo);
                default:
                    throw new LibGit2Exception(string.Format(CultureInfo.InvariantCulture, "Unexpected type '{0}' for object '{1}'.", type, id));
            }
        }

        internal static ObjectId ObjectIdOf(GitObjectSafeHandle gitObjHandle)
        {
            return NativeMethods.git_object_id(gitObjHandle).MarshalAsObjectId();
        }

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

        /// <summary>
        ///   Determines whether the specified <see cref = "GitObject" /> is equal to the current <see cref = "GitObject" />.
        /// </summary>
        /// <param name = "other">The <see cref = "GitObject" /> to compare with the current <see cref = "GitObject" />.</param>
        /// <returns>True if the specified <see cref = "GitObject" /> is equal to the current <see cref = "GitObject" />; otherwise, false.</returns>
        public bool Equals(GitObject 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 = "GitObject" /> are equal.
        /// </summary>
        /// <param name = "left">First <see cref = "GitObject" /> to compare.</param>
        /// <param name = "right">Second <see cref = "GitObject" /> to compare.</param>
        /// <returns>True if the two objects are equal; false otherwise.</returns>
        public static bool operator ==(GitObject left, GitObject right)
        {
            return Equals(left, right);
        }

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