Skip to content
quarry Artifact [3aca150e81]
Login

Artifact [3aca150e81]

Artifact 3aca150e810a6023a317f45f219683c42ed17b9feb777ae3e69a595883ad1c85:


package api

import (
	"context"
	"fmt"
	"strings"
	"sync"

	"craftdesign.group/quarry/internal/ops"
	"craftdesign.group/quarry/internal/zitcli"
)

type landingConflictsIn struct {
	Repo string `json:"repo" validate:"required,max=64"`
	// ID names the landing as landing.get takes it: hash, prefix of at
	// least four, or number.
	ID string `json:"id" validate:"required,min=1,max=64"`
}

// conflictSide is one version of a conflicted file: its blob and, for a
// text file within the limit, its lines.
type conflictSide struct {
	Blob  string   `json:"blob,omitempty"`
	Mode  string   `json:"mode,omitempty"`
	Lines []string `json:"lines,omitempty"`
}

// conflictRegion is a stretch of the file in each version's own lines,
// as half-open ranges counted from 0: same, ours (changed only on the
// branch, and merged), theirs (changed only on the target, and merged),
// both (the same change on each side, merged), or conflict.
type conflictRegion struct {
	Kind   string   `json:"kind"`
	Base   lineSpan `json:"base"`
	Ours   lineSpan `json:"ours"`
	Theirs lineSpan `json:"theirs"`
}

// lineSpan is a half-open range of a version's lines, counted from 0.
type lineSpan struct {
	Start int `json:"start"`
	End   int `json:"end"`
}

func spanOf(r [2]int) lineSpan { return lineSpan{Start: r[0], End: r[1]} }

type conflictFile struct {
	Path string `json:"path"`
	// Kind is zit's: text, binary, or the kind of a file that is not
	// content (a mode or type change).
	Kind    string           `json:"kind"`
	Ours    conflictSide     `json:"ours"`
	Theirs  conflictSide     `json:"theirs"`
	Base    conflictSide     `json:"base"`
	Regions []conflictRegion `json:"regions,omitempty"`
	// Omitted says why a file's lines are not here: binary, too large,
	// or past the file limit. Its blobs and links still are.
	Omitted string `json:"omitted,omitempty"`
}

type landingConflictsOut struct {
	Branch string `json:"branch"`
	Target string `json:"target"`
	// The pair the report is about, resolved once: the view names both
	// and a tip that moves makes it a different report.
	BranchTip string `json:"branchTip"`
	TargetTip string `json:"targetTip"`
	// Verdict is zit's: conflict, clean, or up-to-date.
	Verdict string `json:"verdict"`
	// Total is how many files conflict. Files lists the first
	// maxListedFiles of them, in order, and Omitted counts the rest: a
	// pair of 26,000 files is named to the count, and listed to what a
	// view can draw.
	Total   int            `json:"total"`
	Omitted int            `json:"omitted,omitempty"`
	Files   []conflictFile `json:"files"`
}

// summary is the report without its files: the verdict, the tips and
// the counts, for a caller that has the files already or does not draw
// them. The report it is taken from is left whole.
func (o landingConflictsOut) summary() landingConflictsOut {
	o.Files = []conflictFile{}
	return o
}

// Limits on what one view carries. A file past them is listed by name,
// with its blobs, rather than drawn.
const (
	maxConflictFiles = 20
	maxConflictBytes = 1 << 20
	// maxListedFiles is how many files a report lists: the merge view
	// draws 50 and names 200 more by path, and nothing else reads past
	// them. It must stay above maxConflictFiles, which the view relies
	// on to count the files past the limit it cannot answer.
	maxListedFiles = 250
)

// registerLandingConflicts serves a conflicting landing's merge region
// by region, from zit's own report (`zit merge-tree -F json`,
// danmestas/zit#231): the view shows what `zit merge` would leave in
// the author's checkout, and quarry never merges or reads conflict
// markers itself.
//
// Ours is the branch being landed and theirs is its target: whoever
// resolves is committing to the branch, and that is what they see
// running `zit merge <target>` there. zit names the sides the other way
// round (current is the target), and this is the one place the forge
// translates.
func (d Deps) registerLandingConflicts(r *ops.Registry) error {
	return ops.Register(r, ops.Op[landingConflictsIn, landingConflictsOut]{
		Name: "landing.conflicts",
		Caps: "o",
		Doc: "A landing's merge with its target, file by file and region by region, from zit's merge report. " +
			"Ours is the branch, theirs the target. Each region is same, ours, theirs or conflict, with half-open line ranges in each version's own lines. " +
			"Text files within the limits carry each version's lines; binary and oversized files carry their blobs and say why. " +
			"`total` counts the conflicting files; `files` lists the first 250 of them and `omitted` counts the rest.",
		Kind:  ops.Query,
		Auth:  ops.Anonymous,
		Route: "GET /api/v1/r/{repo}/landing/{id}/conflicts",
		Run: func(ctx context.Context, in landingConflictsIn) (landingConflictsOut, error) {
			repo, err := d.repoFor(ctx, in.Repo)
			if err != nil {
				return landingConflictsOut{}, err
			}
			id := d.landingRef(in.Repo, repo, in.ID)
			l, ok, err := repo.Index.Landing(id)
			if err != nil {
				return landingConflictsOut{}, err
			}
			if !ok && len(id) >= 4 {
				if full, found, rerr := repo.Index.LandingByPrefix(id); rerr == nil && found {
					l, ok = full, true
				}
			}
			if !ok {
				return landingConflictsOut{}, fmt.Errorf("no landing %q here: %w", in.ID, ops.ErrNotFound)
			}
			return d.pairConflicts(ctx, in.Repo, repo, l.Branch, l.Target)
		},
	})
}

// pairConflicts is a branch's merge onto a target, file by file and
// region by region, from zit's report: what a landing shows, and what
// the merge page shows for any two branches (#351). The tips are read
// once, here, and the answer is cached on them.
func (d Deps) pairConflicts(ctx context.Context, repoName string, repo *Repo, branch, onto string) (landingConflictsOut, error) {
	if repo.Zit == nil {
		return landingConflictsOut{}, fmt.Errorf("this repository's store cannot be asked: %w", ops.ErrUnavailable)
	}
	out := landingConflictsOut{Branch: branch, Target: onto,
		BranchTip: sourceTipOf(repo, branch), TargetTip: branchTipOf(repo, onto), Files: []conflictFile{}}
	if out.BranchTip == "" || out.TargetTip == "" {
		return landingConflictsOut{}, fmt.Errorf("the branch or its target has no check-in: %w", ops.ErrNotFound)
	}
	key := repoName + "\x00" + out.BranchTip + "\x00" + out.TargetTip
	if hit, ok := conflictsCache.get(key); ok {
		return hit, nil
	}
	rep, err := repo.Zit.MergeRegions(ctx, out.BranchTip, out.TargetTip)
	if err != nil {
		return landingConflictsOut{}, err
	}
	out = conflictReportOf(ctx, repo.Zit, out, rep)
	conflictsCache.put(key, out)
	return out, nil
}

// conflictReportOf is zit's report for the pair named by out, as the
// forge lists it: each of the first maxListedFiles files in the forge's
// terms, drawn up to maxConflictFiles of them, and the rest counted.
func conflictReportOf(ctx context.Context, z conflictBlobs, out landingConflictsOut, rep zitcli.MergeReport) landingConflictsOut {
	out.Verdict = rep.Verdict
	out.Total = len(rep.Files)
	for i, f := range rep.Files {
		if i >= maxListedFiles {
			out.Omitted = len(rep.Files) - maxListedFiles
			break
		}
		cf := conflictFileOf(f)
		switch {
		case i >= maxConflictFiles:
			cf.Omitted = "past the file limit"
		case f.Kind != "text":
			cf.Omitted = f.Kind
		default:
			if why := readConflictSides(ctx, z, &cf); why != "" {
				cf.Omitted = why
			}
		}
		if cf.Omitted != "" {
			cf.Regions = nil
		}
		out.Files = append(out.Files, cf)
	}
	return out
}

// conflictFileOf translates zit's file into the forge's sides: other is
// ours, current is theirs.
func conflictFileOf(f zitcli.MergeFile) conflictFile {
	cf := conflictFile{Path: f.Path, Kind: f.Kind,
		Ours:   conflictSide{Blob: f.Other, Mode: f.Mode["other"]},
		Theirs: conflictSide{Blob: f.Current, Mode: f.Mode["current"]},
		Base:   conflictSide{Blob: f.Base, Mode: f.Mode["base"]}}
	for _, r := range f.Regions {
		kind := r.Kind
		// both: the two sides made the same change, merged as either.
		switch kind {
		case "other":
			kind = "ours"
		case "current":
			kind = "theirs"
		}
		cf.Regions = append(cf.Regions, conflictRegion{Kind: kind, Base: spanOf(r.Base), Ours: spanOf(r.Other), Theirs: spanOf(r.Current)})
	}
	return cf
}

// conflictBlobs is what reading a conflict's sides needs from a store.
type conflictBlobs interface {
	ArtifactSize(ctx context.Context, hash string) (int, error)
	ArtifactRaw(ctx context.Context, hash string, max int) ([]byte, error)
}

// readConflictSides fills each side's lines by its blob hash, never by
// path at a tip, so the lines are the ones the regions index. A side
// with no blob (a file the other side added) has no lines. Returns why
// the file cannot be drawn, or "".
func readConflictSides(ctx context.Context, z conflictBlobs, cf *conflictFile) string {
	for _, s := range []*conflictSide{&cf.Ours, &cf.Theirs, &cf.Base} {
		if s.Blob == "" {
			continue
		}
		if n, err := z.ArtifactSize(ctx, s.Blob); err == nil && n > maxConflictBytes {
			return "too large"
		}
		b, err := z.ArtifactRaw(ctx, s.Blob, maxConflictBytes)
		if err != nil {
			return "unreadable"
		}
		s.Lines = splitLines(string(b))
	}
	return ""
}

// splitLines is a file's lines as zit counts them: a final newline ends
// the last line rather than starting another.
func splitLines(s string) []string {
	if s == "" {
		return []string{}
	}
	return strings.Split(strings.TrimSuffix(s, "\n"), "\n")
}

// conflictsCache keeps answers by repository and tip pair. Two
// check-ins' merge cannot change, and drawing it reads three blobs per
// file, so a page that re-renders — the live stream does on every
// event — must not read them again. Bounded by entries and by bytes,
// and dropped whole when either is reached: an open landing's pair is
// asked for again at once.
var conflictsCache = &pairCache{m: map[string]landingConflictsOut{}, maxBytes: pairCacheBytes}

const (
	pairCacheEntries = 32
	// pairCacheBytes is the most the cache holds, by an estimate of
	// each report's size: a report that alone exceeds it is answered
	// and not kept.
	pairCacheBytes = 96 << 20
)

type pairCache struct {
	mu       sync.Mutex
	m        map[string]landingConflictsOut
	bytes    int
	maxBytes int
}

// size estimates a report's memory: its text and a fixed cost per file
// and region.
func (o landingConflictsOut) size() int {
	n := 256
	for _, f := range o.Files {
		n += 256 + len(f.Path) + len(f.Kind) + len(f.Omitted) + 72*len(f.Regions)
		for _, s := range []conflictSide{f.Ours, f.Theirs, f.Base} {
			n += len(s.Blob) + len(s.Mode)
			for _, l := range s.Lines {
				n += len(l) + 16
			}
		}
	}
	return n
}

func (c *pairCache) get(k string) (landingConflictsOut, bool) {
	c.mu.Lock()
	defer c.mu.Unlock()
	v, ok := c.m[k]
	return v, ok
}

func (c *pairCache) put(k string, v landingConflictsOut) {
	sz := v.size()
	if sz > c.maxBytes {
		return
	}
	c.mu.Lock()
	defer c.mu.Unlock()
	if _, ok := c.m[k]; ok {
		return
	}
	if len(c.m) >= pairCacheEntries || c.bytes+sz > c.maxBytes {
		c.m, c.bytes = map[string]landingConflictsOut{}, 0
	}
	c.m[k] = v
	c.bytes += sz
}