Skip to content

Commit

Permalink
Support passing a context
Browse files Browse the repository at this point in the history
  • Loading branch information
csmith committed Dec 17, 2024
1 parent 3ebbc5f commit 6180992
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v1.3.0 (Tue Dec 17 2024)

- Added `WithContext` option.
- Minor dependency update.

# v1.2.0 (Sun Aug 20 2023)

- Added `GetLatestTagIgnoringPrefix`, which will ignore a user-defined prefix
Expand Down
16 changes: 15 additions & 1 deletion refs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gitrefs

import (
"context"
"fmt"
"io"
"net/http"
Expand All @@ -16,6 +17,7 @@ const (
)

type opts struct {
ctx context.Context
client *http.Client
tagsOnly bool
}
Expand All @@ -34,9 +36,16 @@ func TagsOnly() Option {
}
}

func WithContext(ctx context.Context) Option {
return func(o *opts) {
o.ctx = ctx
}
}

// Fetch retrieves a list of all refs from the remote repository at the given url.
func Fetch(url string, options ...Option) (map[string]string, error) {
o := &opts{
ctx: context.Background(),
client: http.DefaultClient,
tagsOnly: false,
}
Expand All @@ -45,7 +54,12 @@ func Fetch(url string, options ...Option) (map[string]string, error) {
options[i](o)
}

res, err := o.client.Get(fmt.Sprintf("%s/%s", url, refsPath))
req, err := http.NewRequestWithContext(o.ctx, http.MethodGet, fmt.Sprintf("%s/%s", url, refsPath), nil)
if err != nil {
return nil, err
}

res, err := o.client.Do(req)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 6180992

Please sign in to comment.