Skip to content

Commit 6180992

Browse files
committed
Support passing a context
1 parent 3ebbc5f commit 6180992

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# v1.3.0 (Tue Dec 17 2024)
2+
3+
- Added `WithContext` option.
4+
- Minor dependency update.
5+
16
# v1.2.0 (Sun Aug 20 2023)
27

38
- Added `GetLatestTagIgnoringPrefix`, which will ignore a user-defined prefix

refs.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gitrefs
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"net/http"
@@ -16,6 +17,7 @@ const (
1617
)
1718

1819
type opts struct {
20+
ctx context.Context
1921
client *http.Client
2022
tagsOnly bool
2123
}
@@ -34,9 +36,16 @@ func TagsOnly() Option {
3436
}
3537
}
3638

39+
func WithContext(ctx context.Context) Option {
40+
return func(o *opts) {
41+
o.ctx = ctx
42+
}
43+
}
44+
3745
// Fetch retrieves a list of all refs from the remote repository at the given url.
3846
func Fetch(url string, options ...Option) (map[string]string, error) {
3947
o := &opts{
48+
ctx: context.Background(),
4049
client: http.DefaultClient,
4150
tagsOnly: false,
4251
}
@@ -45,7 +54,12 @@ func Fetch(url string, options ...Option) (map[string]string, error) {
4554
options[i](o)
4655
}
4756

48-
res, err := o.client.Get(fmt.Sprintf("%s/%s", url, refsPath))
57+
req, err := http.NewRequestWithContext(o.ctx, http.MethodGet, fmt.Sprintf("%s/%s", url, refsPath), nil)
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
res, err := o.client.Do(req)
4963
if err != nil {
5064
return nil, err
5165
}

0 commit comments

Comments
 (0)