@@ -9,13 +9,23 @@ function GitHash(ptr::Ptr{UInt8})
9
9
return oid_ptr[]
10
10
end
11
11
12
+ """
13
+ GitHash(id::Vector{UInt8})
14
+
15
+ Construct a `GitHash` from a vector of $OID_RAWSZ bytes.
16
+ """
12
17
function GitHash (id:: Array{UInt8,1} )
13
18
if length (id) != OID_RAWSZ
14
19
throw (ArgumentError (" invalid raw buffer size" ))
15
20
end
16
21
return GitHash (pointer (id))
17
22
end
18
23
24
+ """
25
+ GitHash(id::AbstractString)
26
+
27
+ Construct a `GitHash` from a string of $OID_HEXSZ hexadecimal digits.
28
+ """
19
29
function GitHash (id:: AbstractString )
20
30
bstr = String (id)
21
31
len = sizeof (bstr)
@@ -27,6 +37,19 @@ function GitHash(id::AbstractString)
27
37
(Ptr{GitHash}, Ptr{UInt8}, Csize_t), oid_ptr, bstr, len)
28
38
return oid_ptr[]
29
39
end
40
+
41
+ function GitShortHash (buf:: Buffer )
42
+ oid_ptr = Ref {GitHash} ()
43
+ @check ccall ((:git_oid_fromstrn , :libgit2 ), Cint,
44
+ (Ptr{GitHash}, Ptr{UInt8}, Csize_t), oid_ptr, buf. ptr, buf. size)
45
+ GitShortHash (oid_ptr[], buf. size)
46
+ end
47
+
48
+ """
49
+ GitShortHash(id::AbstractString)
50
+
51
+ Construct a `GitShortHash` from a string of at most $OID_HEXSZ hexadecimal digits.
52
+ """
30
53
function GitShortHash (id:: AbstractString )
31
54
bstr = String (id)
32
55
len = sizeof (bstr)
@@ -44,6 +67,15 @@ macro githash_str(id)
44
67
GitHash (id)
45
68
end
46
69
end
70
+
71
+
72
+ """
73
+ GitHash(ref::GitReference)
74
+
75
+ Get the identifier (`GitHash`) of the object referred to by the direct reference
76
+ `ref`. Note: this does not work for symbolic references; in such cases use
77
+ `GitHash(repo::GitRepo, ref_name::AbstractString)` instead.
78
+ """
47
79
function GitHash (ref:: GitReference )
48
80
isempty (ref) && return GitHash ()
49
81
reftype (ref) != Consts. REF_OID && return GitHash ()
@@ -52,6 +84,13 @@ function GitHash(ref::GitReference)
52
84
return GitHash (oid_ptr)
53
85
end
54
86
87
+
88
+ """
89
+ GitHash(repo::GitRepo, ref_name::AbstractString)
90
+
91
+ Get the identifier (`GitHash`) of the object referred to by reference specified by
92
+ `ref_name`.
93
+ """
55
94
function GitHash (repo:: GitRepo , ref_name:: AbstractString )
56
95
isempty (repo) && return GitHash ()
57
96
oid_ptr = Ref (GitHash ())
@@ -61,10 +100,31 @@ function GitHash(repo::GitRepo, ref_name::AbstractString)
61
100
return oid_ptr[]
62
101
end
63
102
103
+ """
104
+ GitHash(obj::GitObject)
105
+
106
+ Get the identifier (`GitHash`) of `obj`.
107
+ """
64
108
function GitHash (obj:: GitObject )
65
109
GitHash (ccall ((:git_object_id , :libgit2 ), Ptr{UInt8}, (Ptr{Void},), obj. ptr))
66
110
end
67
111
112
+ """
113
+ GitShortHash(obj::GitObject)
114
+
115
+ Get a shortened identifier (`GitShortHash`) of `obj`. The minimum length (in characters)
116
+ is determined by the `core.abbrev` config option, and will be of sufficient length to
117
+ unambiuously identify the object in the repository.
118
+ """
119
+ function GitShortHash (obj:: GitObject )
120
+ buf_ref = Ref (Buffer ())
121
+ @check ccall ((:git_object_short_id , :libgit2 ), Cint,
122
+ (Ptr{Buffer},Ptr{Void}), buf_ref, obj. ptr)
123
+ sid = GitShortHash (buf_ref[])
124
+ free (buf_ref)
125
+ return sid
126
+ end
127
+
68
128
Base. hex (id:: GitHash ) = join ([hex (i,2 ) for i in id. val])
69
129
Base. hex (id:: GitShortHash ) = hex (id. hash)[1 : id. len]
70
130
0 commit comments