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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
package localrepo
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"regexp"
"strings"
"time"
"gitlab.com/gitlab-org/gitaly/v16/internal/command"
"gitlab.com/gitlab-org/gitaly/v16/internal/featureflag"
"gitlab.com/gitlab-org/gitaly/v16/internal/git"
"gitlab.com/gitlab-org/gitaly/v16/internal/git/catfile"
"gitlab.com/gitlab-org/gitaly/v16/internal/helper/text"
"gitlab.com/gitlab-org/gitaly/v16/internal/structerr"
"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
)
// ErrObjectNotFound is returned in case an object could not be found.
var ErrObjectNotFound = errors.New("object not found")
// WriteBlob writes a blob to the repository's object database and
// returns its object ID. Path is used by git to decide which filters to
// run on the content.
func (repo *Repo) WriteBlob(ctx context.Context, path string, content io.Reader) (git.ObjectID, error) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd, err := repo.Exec(ctx,
git.Command{
Name: "hash-object",
Flags: []git.Option{
git.ValueFlag{Name: "--path", Value: path},
git.Flag{Name: "--stdin"},
git.Flag{Name: "-w"},
},
},
git.WithStdin(content),
git.WithStdout(stdout),
git.WithStderr(stderr),
)
if err != nil {
return "", err
}
if err := cmd.Wait(); err != nil {
return "", errorWithStderr(err, stderr.Bytes())
}
objectHash, err := repo.ObjectHash(ctx)
if err != nil {
return "", fmt.Errorf("detecting object hash: %w", err)
}
oid, err := objectHash.FromHex(text.ChompBytes(stdout.Bytes()))
if err != nil {
return "", err
}
return oid, nil
}
// FormatTagError is used by FormatTag() below
type FormatTagError struct {
expectedLines int
actualLines int
}
func (e FormatTagError) Error() string {
return fmt.Sprintf("should have %d tag header lines, got %d", e.expectedLines, e.actualLines)
}
// FormatTag is used by WriteTag (or for testing) to make the tag
// signature to feed to git-mktag, i.e. the plain-text mktag
// format. This does not create an object, just crafts input for "git
// mktag" to consume.
//
// We are not being paranoid about exhaustive input validation here
// because we're just about to run git's own "fsck" check on this.
//
// However, if someone injected parameters with extra newlines they
// could cause subsequent values to be ignored via a crafted
// message. This someone could also locally craft a tag locally and
// "git push" it. But allowing e.g. someone to provide their own
// timestamp here would at best be annoying, and at worst run up
// against some other assumption (e.g. that some hook check isn't as
// strict on locally generated data).
func FormatTag(
objectID git.ObjectID,
objectType string,
tagName, tagBody []byte,
committer *gitalypb.User,
committerDate time.Time,
) (string, error) {
if committerDate.IsZero() {
committerDate = time.Now()
}
tagHeaderFormat := "object %s\n" +
"type %s\n" +
"tag %s\n" +
"tagger %s <%s> %d %s\n"
tagBuf := fmt.Sprintf(tagHeaderFormat, objectID.String(), objectType, tagName, committer.GetName(), committer.GetEmail(), committerDate.Unix(), committerDate.Format("-0700"))
maxHeaderLines := 4
actualHeaderLines := strings.Count(tagBuf, "\n")
if actualHeaderLines != maxHeaderLines {
return "", FormatTagError{expectedLines: maxHeaderLines, actualLines: actualHeaderLines}
}
tagBuf += "\n"
tagBuf += string(tagBody)
return tagBuf, nil
}
// MktagError is used by WriteTag() below
type MktagError struct {
tagName []byte
stderr string
}
func (e MktagError) Error() string {
// TODO: Upper-case error message purely for transitory backwards compatibility
return fmt.Sprintf("Could not update refs/tags/%s. Please refresh and try again.", e.tagName)
}
// WriteTag writes a tag to the repository's object database with
// git-mktag and returns its object ID.
//
// It's important that this be git-mktag and not git-hash-object due
// to its fsck sanity checking semantics.
func (repo *Repo) WriteTag(
ctx context.Context,
objectID git.ObjectID,
objectType string,
tagName, tagBody []byte,
committer *gitalypb.User,
committerDate time.Time,
) (git.ObjectID, error) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
tagBuf, err := FormatTag(objectID, objectType, tagName, tagBody, committer, committerDate)
if err != nil {
return "", err
}
content := strings.NewReader(tagBuf)
cmd, err := repo.Exec(ctx,
git.Command{
Name: "mktag",
},
git.WithStdin(content),
git.WithStdout(stdout),
git.WithStderr(stderr),
)
if err != nil {
return "", err
}
if err := cmd.Wait(); err != nil {
return "", MktagError{tagName: tagName, stderr: stderr.String()}
}
objectHash, err := repo.ObjectHash(ctx)
if err != nil {
return "", fmt.Errorf("detecting object hash: %w", err)
}
tagID, err := objectHash.FromHex(text.ChompBytes(stdout.Bytes()))
if err != nil {
return "", fmt.Errorf("could not parse tag ID: %w", err)
}
return tagID, nil
}
// InvalidObjectError is returned when trying to get an object id that is invalid or does not exist.
type InvalidObjectError string
func (err InvalidObjectError) Error() string { return fmt.Sprintf("invalid object %q", string(err)) }
// ReadObjectInfo attempts to read the object info based on a revision.
func (repo *Repo) ReadObjectInfo(ctx context.Context, rev git.Revision) (*catfile.ObjectInfo, error) {
infoReader, cleanup, err := repo.catfileCache.ObjectInfoReader(ctx, repo)
if err != nil {
return nil, fmt.Errorf("getting object info reader: %w", err)
}
defer cleanup()
objectInfo, err := infoReader.Info(ctx, rev)
if err != nil {
if catfile.IsNotFound(err) {
return nil, InvalidObjectError(rev)
}
return nil, fmt.Errorf("getting object info: %w", err)
}
return objectInfo, nil
}
// ReadObject reads an object from the repository's object database. InvalidObjectError
// is returned if the oid does not refer to a valid object.
func (repo *Repo) ReadObject(ctx context.Context, oid git.ObjectID) ([]byte, error) {
if featureflag.LocalrepoReadObjectCached.IsEnabled(ctx) {
return repo.readObjectCached(ctx, oid)
}
const msgInvalidObject = "fatal: Not a valid object name "
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd, err := repo.Exec(ctx,
git.Command{
Name: "cat-file",
Flags: []git.Option{git.Flag{Name: "-p"}},
Args: []string{oid.String()},
},
git.WithStdout(stdout),
git.WithStderr(stderr),
)
if err != nil {
return nil, err
}
if err := cmd.Wait(); err != nil {
msg := text.ChompBytes(stderr.Bytes())
if strings.HasPrefix(msg, msgInvalidObject) {
return nil, InvalidObjectError(strings.TrimPrefix(msg, msgInvalidObject))
}
return nil, errorWithStderr(err, stderr.Bytes())
}
return stdout.Bytes(), nil
}
func (repo *Repo) readObjectCached(ctx context.Context, oid git.ObjectID) ([]byte, error) {
objectReader, cancel, err := repo.catfileCache.ObjectReader(ctx, repo)
if err != nil {
return nil, fmt.Errorf("create object reader: %w", err)
}
defer cancel()
object, err := objectReader.Object(ctx, oid.Revision())
if err != nil {
if catfile.IsNotFound(err) {
return nil, InvalidObjectError(oid.String())
}
return nil, fmt.Errorf("get object from reader: %w", err)
}
data, err := io.ReadAll(object)
if err != nil {
return nil, fmt.Errorf("read object from reader: %w", err)
}
return data, nil
}
type readCommitConfig struct {
withTrailers bool
}
// ReadCommitOpt is an option for ReadCommit.
type ReadCommitOpt func(*readCommitConfig)
// WithTrailers will cause ReadCommit to parse commit trailers.
func WithTrailers() ReadCommitOpt {
return func(cfg *readCommitConfig) {
cfg.withTrailers = true
}
}
// ReadCommit reads the commit specified by the given revision. If no such
// revision exists, it will return an ErrObjectNotFound error.
func (repo *Repo) ReadCommit(ctx context.Context, revision git.Revision, opts ...ReadCommitOpt) (*gitalypb.GitCommit, error) {
var cfg readCommitConfig
for _, opt := range opts {
opt(&cfg)
}
objectReader, cancel, err := repo.catfileCache.ObjectReader(ctx, repo)
if err != nil {
return nil, err
}
defer cancel()
var commit *gitalypb.GitCommit
if cfg.withTrailers {
commit, err = catfile.GetCommitWithTrailers(ctx, repo.gitCmdFactory, repo, objectReader, revision)
} else {
commit, err = catfile.GetCommit(ctx, objectReader, revision)
}
if err != nil {
if catfile.IsNotFound(err) {
return nil, ErrObjectNotFound
}
return nil, err
}
return commit, nil
}
// InvalidCommitError is returned when the revision does not point to a valid commit object.
type InvalidCommitError git.Revision
func (err InvalidCommitError) Error() string {
return fmt.Sprintf("invalid commit: %q", string(err))
}
// IsAncestor returns whether the parent is an ancestor of the child. InvalidCommitError is returned
// if either revision does not point to a commit in the repository.
func (repo *Repo) IsAncestor(ctx context.Context, parent, child git.Revision) (bool, error) {
const notValidCommitName = "fatal: Not a valid commit name"
stderr := &bytes.Buffer{}
if err := repo.ExecAndWait(ctx,
git.Command{
Name: "merge-base",
Flags: []git.Option{git.Flag{Name: "--is-ancestor"}},
Args: []string{parent.String(), child.String()},
},
git.WithStderr(stderr),
); err != nil {
status, ok := command.ExitStatus(err)
if ok && status == 1 {
return false, nil
} else if ok && strings.HasPrefix(stderr.String(), notValidCommitName) {
commitOID := strings.TrimSpace(strings.TrimPrefix(stderr.String(), notValidCommitName))
return false, InvalidCommitError(commitOID)
}
return false, fmt.Errorf("determine ancestry: %w, stderr: %q", err, stderr)
}
return true, nil
}
// BadObjectError is returned when attempting to walk a bad object.
type BadObjectError struct {
// ObjectID is the object id of the object that was bad.
ObjectID git.ObjectID
}
// Error returns the error message.
func (err BadObjectError) Error() string {
return fmt.Sprintf("bad object %q", err.ObjectID)
}
// ObjectReadError is returned when reading an object fails.
type ObjectReadError struct {
// ObjectID is the object id of the object that git failed to read
ObjectID git.ObjectID
}
// Error returns the error message.
func (err ObjectReadError) Error() string {
return fmt.Sprintf("failed reading object %q", err.ObjectID)
}
var (
regexpBadObjectError = regexp.MustCompile(`^fatal: bad object ([[:xdigit:]]*)\n$`)
regexpObjectReadError = regexp.MustCompile(`^error: Could not read ([[:xdigit:]]*)\n`)
)
// WalkUnreachableObjects walks the object graph starting from heads and writes to the output object IDs
// that are included in the walk but unreachable from any of the repository's references. Heads should
// return object IDs separated with a newline. Output is object IDs separated by newlines.
func (repo *Repo) WalkUnreachableObjects(ctx context.Context, heads io.Reader, output io.Writer) error {
var stderr bytes.Buffer
if err := repo.ExecAndWait(ctx,
git.Command{
Name: "rev-list",
Flags: []git.Option{
git.Flag{Name: "--objects"},
git.Flag{Name: "--not"},
git.Flag{Name: "--all"},
git.Flag{Name: "--stdin"},
},
},
git.WithStdin(heads),
git.WithStdout(output),
git.WithStderr(&stderr),
); err != nil {
if matches := regexpBadObjectError.FindSubmatch(stderr.Bytes()); len(matches) > 1 {
return BadObjectError{ObjectID: git.ObjectID(matches[1])}
}
if matches := regexpObjectReadError.FindSubmatch(stderr.Bytes()); len(matches) > 1 {
return ObjectReadError{ObjectID: git.ObjectID(matches[1])}
}
return structerr.New("rev-list: %w", err).WithMetadata("stderr", stderr.String())
}
return nil
}
// PackObjects takes in object IDs separated by newlines. It packs the objects into a pack file and
// writes it into the output.
func (repo *Repo) PackObjects(ctx context.Context, objectIDs io.Reader, output io.Writer) error {
var stderr bytes.Buffer
if err := repo.ExecAndWait(ctx,
git.Command{
Name: "pack-objects",
Flags: []git.Option{
git.Flag{Name: "-q"},
git.Flag{Name: "--stdout"},
},
},
git.WithStdin(objectIDs),
git.WithStderr(&stderr),
git.WithStdout(output),
); err != nil {
return structerr.New("pack objects: %w", err).WithMetadata("stderr", stderr.String())
}
return nil
}
// UnpackObjects unpacks the objects from the pack file to the repository's object database.
func (repo *Repo) UnpackObjects(ctx context.Context, packFile io.Reader) error {
stderr := &bytes.Buffer{}
if err := repo.ExecAndWait(ctx,
git.Command{
Name: "unpack-objects",
Flags: []git.Option{
git.Flag{Name: "-q"},
},
},
git.WithStdin(packFile),
git.WithStderr(stderr),
); err != nil {
return structerr.New("unpack objects: %w", err).WithMetadata("stderr", stderr.String())
}
return nil
}
|