Skip to content
quarry Artifact [3ed8ab3eea]
Login

Artifact [3ed8ab3eea]

Artifact 3ed8ab3eeaa3834cb0e738182b3904c9c692d31a771d143c1d8522a35ddde8c1:


// quarry-premerge is the first step of a premerge pipeline (RFC 0009):
// it prepares the tree an accept would create and prints the directory,
// or exits 2 with the conflicts when the merge does not apply. It never
// commits, and it is meant to run with credentials that could not push
// anyway.
//
//	quarry-premerge --clone URL --branch-tip HASH --target-tip HASH [--dir D]
package main

import (
	"flag"
	"fmt"
	"os"

	"craftdesign.group/quarry/internal/premerge"
)

func main() {
	clone := flag.String("clone", "", "repository clone URL")
	branch := flag.String("branch-tip", "", "the proposed branch's tip check-in")
	target := flag.String("target-tip", "", "the target branch's tip check-in")
	dir := flag.String("dir", "", "working directory (default: a temp dir)")
	flag.Parse()
	if *clone == "" || *branch == "" || *target == "" {
		fmt.Fprintln(os.Stderr, "quarry-premerge: --clone, --branch-tip and --target-tip are required")
		os.Exit(64)
	}
	d := *dir
	if d == "" {
		var err error
		if d, err = os.MkdirTemp("", "premerge-*"); err != nil {
			fmt.Fprintln(os.Stderr, "quarry-premerge:", err)
			os.Exit(1)
		}
	}
	res, err := premerge.Prepare(*clone, *branch, *target, d)
	if err != nil {
		fmt.Fprintln(os.Stderr, "quarry-premerge:", err)
		os.Exit(1)
	}
	if res.Conflict {
		fmt.Fprintln(os.Stderr, "quarry-premerge: the merge does not apply:")
		for _, f := range res.Files {
			fmt.Fprintln(os.Stderr, " ", f)
		}
		os.Exit(2)
	}
	fmt.Println(res.Dir)
}