-
Notifications
You must be signed in to change notification settings - Fork 318
/
describe.go
226 lines (188 loc) · 6.87 KB
/
describe.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
// DescribeOptions represents the describe operation configuration.
//
// You can use DefaultDescribeOptions() to get default options.
type DescribeOptions struct {
// How many tags as candidates to consider to describe the input commit-ish.
// Increasing it above 10 will take slightly longer but may produce a more
// accurate result. 0 will cause only exact matches to be output.
MaxCandidatesTags uint // default: 10
// By default describe only shows annotated tags. Change this in order
// to show all refs from refs/tags or refs/.
Strategy DescribeOptionsStrategy // default: DescribeDefault
// Only consider tags matching the given glob(7) pattern, excluding
// the "refs/tags/" prefix. Can be used to avoid leaking private
// tags from the repo.
Pattern string
// When calculating the distance from the matching tag or
// reference, only walk down the first-parent ancestry.
OnlyFollowFirstParent bool
// If no matching tag or reference is found, the describe
// operation would normally fail. If this option is set, it
// will instead fall back to showing the full id of the commit.
ShowCommitOidAsFallback bool
}
// DefaultDescribeOptions returns default options for the describe operation.
func DefaultDescribeOptions() (DescribeOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
opts := C.git_describe_options{}
ecode := C.git_describe_options_init(&opts, C.GIT_DESCRIBE_OPTIONS_VERSION)
if ecode < 0 {
return DescribeOptions{}, MakeGitError(ecode)
}
return DescribeOptions{
MaxCandidatesTags: uint(opts.max_candidates_tags),
Strategy: DescribeOptionsStrategy(opts.describe_strategy),
}, nil
}
// DescribeFormatOptions can be used for formatting the describe string.
//
// You can use DefaultDescribeFormatOptions() to get default options.
type DescribeFormatOptions struct {
// Size of the abbreviated commit id to use. This value is the
// lower bound for the length of the abbreviated string.
AbbreviatedSize uint // default: 7
// Set to use the long format even when a shorter name could be used.
AlwaysUseLongFormat bool
// If the workdir is dirty and this is set, this string will be
// appended to the description string.
DirtySuffix string
}
// DefaultDescribeFormatOptions returns default options for formatting
// the output.
func DefaultDescribeFormatOptions() (DescribeFormatOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
opts := C.git_describe_format_options{}
ecode := C.git_describe_format_options_init(&opts, C.GIT_DESCRIBE_FORMAT_OPTIONS_VERSION)
if ecode < 0 {
return DescribeFormatOptions{}, MakeGitError(ecode)
}
return DescribeFormatOptions{
AbbreviatedSize: uint(opts.abbreviated_size),
AlwaysUseLongFormat: opts.always_use_long_format == 1,
}, nil
}
// DescribeOptionsStrategy behaves like the --tags and --all options
// to git-describe, namely they say to look for any reference in
// either refs/tags/ or refs/ respectively.
//
// By default it only shows annotated tags.
type DescribeOptionsStrategy uint
// Describe strategy options.
const (
DescribeDefault DescribeOptionsStrategy = C.GIT_DESCRIBE_DEFAULT
DescribeTags DescribeOptionsStrategy = C.GIT_DESCRIBE_TAGS
DescribeAll DescribeOptionsStrategy = C.GIT_DESCRIBE_ALL
)
// Describe performs the describe operation on the commit.
func (c *Commit) Describe(opts *DescribeOptions) (*DescribeResult, error) {
var resultPtr *C.git_describe_result
var cDescribeOpts *C.git_describe_options
if opts != nil {
var cpattern *C.char
if len(opts.Pattern) > 0 {
cpattern = C.CString(opts.Pattern)
defer C.free(unsafe.Pointer(cpattern))
}
cDescribeOpts = &C.git_describe_options{
version: C.GIT_DESCRIBE_OPTIONS_VERSION,
max_candidates_tags: C.uint(opts.MaxCandidatesTags),
describe_strategy: C.uint(opts.Strategy),
pattern: cpattern,
only_follow_first_parent: cbool(opts.OnlyFollowFirstParent),
show_commit_oid_as_fallback: cbool(opts.ShowCommitOidAsFallback),
}
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_describe_commit(&resultPtr, c.ptr, cDescribeOpts)
runtime.KeepAlive(c)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newDescribeResultFromC(resultPtr), nil
}
// DescribeWorkdir describes the working tree. It means describe HEAD
// and appends <mark> (-dirty by default) if the working tree is dirty.
func (repo *Repository) DescribeWorkdir(opts *DescribeOptions) (*DescribeResult, error) {
var resultPtr *C.git_describe_result
var cDescribeOpts *C.git_describe_options
if opts != nil {
var cpattern *C.char
if len(opts.Pattern) > 0 {
cpattern = C.CString(opts.Pattern)
defer C.free(unsafe.Pointer(cpattern))
}
cDescribeOpts = &C.git_describe_options{
version: C.GIT_DESCRIBE_OPTIONS_VERSION,
max_candidates_tags: C.uint(opts.MaxCandidatesTags),
describe_strategy: C.uint(opts.Strategy),
pattern: cpattern,
only_follow_first_parent: cbool(opts.OnlyFollowFirstParent),
show_commit_oid_as_fallback: cbool(opts.ShowCommitOidAsFallback),
}
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_describe_workdir(&resultPtr, repo.ptr, cDescribeOpts)
runtime.KeepAlive(repo)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newDescribeResultFromC(resultPtr), nil
}
// DescribeResult represents the output from the 'git_describe_commit'
// and 'git_describe_workdir' functions in libgit2.
//
// Use Format() to get a string out of it.
type DescribeResult struct {
doNotCompare
ptr *C.git_describe_result
}
func newDescribeResultFromC(ptr *C.git_describe_result) *DescribeResult {
result := &DescribeResult{
ptr: ptr,
}
runtime.SetFinalizer(result, (*DescribeResult).Free)
return result
}
// Format prints the DescribeResult as a string.
func (result *DescribeResult) Format(opts *DescribeFormatOptions) (string, error) {
resultBuf := C.git_buf{}
var cFormatOpts *C.git_describe_format_options
if opts != nil {
cDirtySuffix := C.CString(opts.DirtySuffix)
defer C.free(unsafe.Pointer(cDirtySuffix))
cFormatOpts = &C.git_describe_format_options{
version: C.GIT_DESCRIBE_FORMAT_OPTIONS_VERSION,
abbreviated_size: C.uint(opts.AbbreviatedSize),
always_use_long_format: cbool(opts.AlwaysUseLongFormat),
dirty_suffix: cDirtySuffix,
}
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_describe_format(&resultBuf, result.ptr, cFormatOpts)
runtime.KeepAlive(result)
if ecode < 0 {
return "", MakeGitError(ecode)
}
defer C.git_buf_dispose(&resultBuf)
return C.GoString(resultBuf.ptr), nil
}
// Free cleans up the C reference.
func (result *DescribeResult) Free() {
runtime.SetFinalizer(result, nil)
C.git_describe_result_free(result.ptr)
result.ptr = nil
}