MCPcopy Create free account
hub / github.com/bufbuild/buf / GetArchive

Method GetArchive

private/pkg/github/githubtesting/archive_reader.go:58–131  ·  view source on GitHub ↗
(
	ctx context.Context,
	outputDirPath string,
	owner string,
	repository string,
	ref string,
)

Source from the content-addressed store, hash-verified

56}
57
58func (a *archiveReader) GetArchive(
59 ctx context.Context,
60 outputDirPath string,
61 owner string,
62 repository string,
63 ref string,
64) (retErr error) {
65 a.lock.Lock()
66 defer a.lock.Unlock()
67
68 outputDirPath = normalpath.Unnormalize(outputDirPath)
69
70 // creates a file in the same parent directory as outputDirPath
71 unlocker, err := filelock.Lock(
72 ctx,
73 outputDirPath+".lock",
74 filelock.LockWithTimeout(filelockTimeout),
75 )
76 if err != nil {
77 return err
78 }
79 defer func() {
80 retErr = errors.Join(retErr, unlocker.Unlock())
81 }()
82
83 // check if already exists, if so, do nothing
84 // OK to use os.Stat here
85 if fileInfo, err := os.Stat(outputDirPath); err == nil {
86 if !fileInfo.IsDir() {
87 return fmt.Errorf("expected %s to be a directory", outputDirPath)
88 }
89 return nil
90 }
91 request, err := http.NewRequestWithContext(
92 ctx,
93 "GET",
94 fmt.Sprintf("https://github.com/%s/%s/archive/%s.tar.gz", owner, repository, ref),
95 nil,
96 )
97 if err != nil {
98 return err
99 }
100 response, err := a.httpClient.Do(request)
101 if err != nil {
102 return err
103 }
104 defer func() {
105 retErr = errors.Join(retErr, response.Body.Close())
106 }()
107 if response.StatusCode != http.StatusOK {
108 return fmt.Errorf("expected HTTP status code %d to be %d", response.StatusCode, http.StatusOK)
109 }
110 gzipReader, err := gzip.NewReader(response.Body)
111 if err != nil {
112 return err
113 }
114 defer func() {
115 retErr = errors.Join(retErr, gzipReader.Close())

Callers

nothing calls this directly

Calls 12

UnnormalizeFunction · 0.92
LockFunction · 0.92
LockWithTimeoutFunction · 0.92
NormalizeFunction · 0.92
UntarFunction · 0.92
DoMethod · 0.80
LockMethod · 0.65
UnlockMethod · 0.65
StatMethod · 0.65
CloseMethod · 0.65
NewReadWriteBucketMethod · 0.65

Tested by

no test coverage detected