Skip to content
Snippets Groups Projects
Select Git revision
  • master default
  • test
  • v1.44.0
  • v1.43.0
  • v1.42.0
  • v1.41.0
  • v1.34.2
  • v1.40.0
  • v1.39.0
  • v1.38.0
  • v1.37.0
  • v1.36.0
  • v1.34.1
  • v1.27.2
  • v1.20.1
  • v1.35.0
  • v1.34.0
  • v1.33.0
  • v1.32.0
  • v1.31.0
  • v1.30.0
  • v1.27.1
22 results

updateref.go

Forked from GitLab.org / Gitaly
17936 commits behind, 1 commit ahead of the upstream repository.
updateref.go 1.72 KiB
package updateref

import (
	"context"
	"fmt"

	"gitlab.com/gitlab-org/gitaly/internal/command"
	"gitlab.com/gitlab-org/gitaly/internal/git"
	"gitlab.com/gitlab-org/gitaly/internal/git/repository"
)

// Updater wraps a `git update-ref --stdin` process, presenting an interface
// that allows references to be easily updated in bulk. It is not suitable for
// concurrent use.
type Updater struct {
	repo repository.GitRepo
	cmd  *command.Command
}

// New returns a new bulk updater, wrapping a `git update-ref` process. Call the
// various methods to enqueue updates, then call Wait() to attempt to apply all
// the updates at once.
//
// It is important that ctx gets canceled somewhere. If it doesn't, the process
// spawned by New() may never terminate.
func New(ctx context.Context, repo repository.GitRepo) (*Updater, error) {
	cmd, err := git.StdinCommand(ctx, repo, "update-ref", "-z", "--stdin")
	if err != nil {
		return nil, err
	}

	return &Updater{repo: repo, cmd: cmd}, nil
}

// Create commands the reference to be created with the sha specified in value
func (u *Updater) Create(ref, value string) error {
	_, err := fmt.Fprintf(u.cmd, "create %s\x00%s\x00", ref, value)
	return err
}

// Update commands the reference to be updated to point at the sha specified in
// newvalue
func (u *Updater) Update(ref, newvalue, oldvalue string) error {
	_, err := fmt.Fprintf(u.cmd, "update %s\x00%s\x00%s\x00", ref, newvalue, oldvalue)
	return err
}

// Delete commands the reference to be removed from the repository
func (u *Updater) Delete(ref string) error {
	_, err := fmt.Fprintf(u.cmd, "delete %s\x00\x00", ref)
	return err
}

// Wait applies the commands specified in other calls to the Updater
func (u *Updater) Wait() error {
	return u.cmd.Wait()
}