mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-11 14:41:50 +00:00
tools: Add gitiles package
Wraps the luci package to make it a bit friendlier. Bug: dawn:1342 Change-Id: I70bf01b7ef5fcf210a2ff7ccafa49ea0720ae125 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/86526 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Ben Clayton <bclayton@chromium.org>
This commit is contained in:
committed by
Dawn LUCI CQ
parent
51bab8067a
commit
ac068c9a31
72
tools/src/gitiles/gitiles.go
Normal file
72
tools/src/gitiles/gitiles.go
Normal file
@@ -0,0 +1,72 @@
|
||||
// Copyright 2022 The Tint Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package gitiles provides helpers for interfacing with gitiles
|
||||
package gitiles
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"go.chromium.org/luci/common/api/gitiles"
|
||||
gpb "go.chromium.org/luci/common/proto/gitiles"
|
||||
)
|
||||
|
||||
// Gitiles is the client to communicate with Gitiles.
|
||||
type Gitiles struct {
|
||||
client gpb.GitilesClient
|
||||
project string
|
||||
}
|
||||
|
||||
// New creates a client to communicate with Gitiles, for the given host and
|
||||
// project.
|
||||
func New(ctx context.Context, host, project string) (*Gitiles, error) {
|
||||
client, err := gitiles.NewRESTClient(http.DefaultClient, host, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Gitiles{client, project}, nil
|
||||
}
|
||||
|
||||
// Hash returns the git hash of the object with the given 'committish' reference.
|
||||
func (g *Gitiles) Hash(ctx context.Context, ref string) (string, error) {
|
||||
res, err := g.client.Log(ctx, &gpb.LogRequest{
|
||||
Project: g.project,
|
||||
Committish: ref,
|
||||
PageSize: 1,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
log := res.GetLog()
|
||||
if len(log) == 0 {
|
||||
return "", fmt.Errorf("gitiles returned log was empty")
|
||||
}
|
||||
return log[0].Id, nil
|
||||
}
|
||||
|
||||
// DownloadFile downloads a single file with the given project-relative path at
|
||||
// the given reference.
|
||||
func (g *Gitiles) DownloadFile(ctx context.Context, ref, path string) (string, error) {
|
||||
res, err := g.client.DownloadFile(ctx, &gpb.DownloadFileRequest{
|
||||
Project: g.project,
|
||||
Committish: ref,
|
||||
Path: path,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return res.GetContents(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user