package hgvcs
import (
)
const Name = "hg"
const (
defaultRev = "."
defaultTagCommitMessage = "chore: add release tags"
syntheticTipTag = "tip"
hgtagsFile = ".hgtags"
)
type Config struct {
Program string `json:"program,omitzero"`
TagCommitMessage string `json:"tagCommitMessage,omitzero"`
}
func ( Config) () string {
return cmp.Or(.TagCommitMessage, defaultTagCommitMessage)
}
func ( Config) vcs.Opener {
return vcs.Opener{
Name: Name,
Open: func( context.Context, string) (vcs.VCS, error) {
return Open(, , )
},
}
}
type Repo struct {
root string
program string
cfg Config
}
func ( context.Context, string, Config) (vcs.VCS, error) {
, := exec.LookPath(cmp.Or(.Program, Name))
if != nil {
return nil, fmt.Errorf("hg binary not found: %w", )
}
, := runHg(, , , "root")
if != nil {
return nil, fmt.Errorf("not a mercurial repository: %w", )
}
return &Repo{
root: filepath.Clean(strings.TrimSpace()),
program: ,
cfg: ,
}, nil
}
func ( *Repo) () string { return Name }
func ( *Repo) () string { return .root }
func ( *Repo) ( context.Context, string) (vcs.Revision, error) {
= cmp.Or(, defaultRev)
, := runHg(, .program, .root, "log", "-r", , "-T", "{node}\n{branch}\n{activebookmark}")
if != nil {
return vcs.Revision{}, fmt.Errorf("resolve hg revision %q: %w", , )
}
:= strings.Split(strings.TrimRight(, "\r\n"), "\n")
if len() == 0 || strings.TrimSpace([0]) == "" {
return vcs.Revision{}, fmt.Errorf("resolve hg revision %q: empty output", )
}
:= vcs.Revision{ID: strings.TrimSpace([0])}
if len() > 1 {
.Label = strings.TrimSpace([1])
}
if len() > 2 && strings.TrimSpace([2]) != "" {
:= strings.TrimSpace([2])
.Ref =
.Label =
}
return , nil
}
func ( *Repo) ( context.Context) ([]string, error) {
, := runHg(, .program, .root, "tags", "-q")
if != nil {
return nil, fmt.Errorf("list hg tags: %w", )
}
var []string
for := range strings.Lines() {
if = strings.TrimSpace(); != "" && != "tip" {
= append(, )
}
}
return , nil
}
func ( *Repo) ( context.Context, , , string) ([]string, error) {
:= []string{"status", "--rev", , "--rev", , "--no-status", "-0"}
if := vcs.CleanRelDir(); != "" {
= append(, )
}
, := runHgBytes(, .program, .root, ...)
if != nil {
return nil, fmt.Errorf("hg status %s..%s: %w", , , )
}
var []string
for := range bytes.SplitSeq(, []byte{0}) {
if len() > 0 {
= append(, filepath.ToSlash(string()))
}
}
return , nil
}
func ( *Repo) ( context.Context, , string) ([]vcs.FileEntry, error) {
return listFiles(, .program, .root, , )
}
func ( *Repo) ( context.Context, , string) ([]byte, error) {
:= vcs.CleanRelPath()
, := runHgBytes(, .program, .root, "cat", "-r", , "--", )
if != nil {
return nil, fmt.Errorf("hg cat %s@%s: %w", , , )
}
return , nil
}
func ( *Repo) ( context.Context, , string) error {
if , := runHg(, .program, .root, "archive", "-r", , "-t", "files", ); != nil {
return fmt.Errorf("hg archive %s: %w", , )
}
return nil
}
func ( *Repo) ( context.Context, vcs.Revision) (vcs.Session, error) {
, := os.MkdirTemp("", "goship-hg-clone-*")
if != nil {
return nil, fmt.Errorf("create temp hg clone dir: %w", )
}
if , := runHg(, .program, .root, "clone", "--noupdate", .root, ); != nil {
_ = os.RemoveAll()
return nil, fmt.Errorf("hg clone: %w", )
}
if , := runHg(, .program, , "update", "-r", .ID); != nil {
_ = os.RemoveAll()
return nil, fmt.Errorf("hg update %s: %w", .ID, )
}
, := runHg(, .program, .root, "config", "ui.username")
return &session{
repo: ,
dir: ,
rev: ,
head: .ID,
username: strings.TrimSpace(),
}, nil
}
func ( *Repo) ( context.Context, , string, []string) error {
:= []string{"push"}
if != "" {
= append(, )
}
if , := runHg(, .program, .root, ...); != nil {
return fmt.Errorf("hg push: %w", )
}
return nil
}
func ( *Repo) ( string, vcs.PublishResult) string {
if != "" {
return "hg push " +
}
return "hg push"
}
func ( context.Context, , , , string) ([]vcs.FileEntry, error) {
:= []string{"files", "-r", , "-0"}
if := vcs.CleanRelDir(); != "" {
= append(, )
}
, := runHgBytes(, , , ...)
if != nil {
return nil, fmt.Errorf("hg files -r %s: %w", , )
}
var []vcs.FileEntry
for := range bytes.SplitSeq(, []byte{0}) {
if len() == 0 {
continue
}
= append(, vcs.FileEntry{
Path: filepath.ToSlash(string()),
Mode: fs.FileMode(0o644),
})
}
return , nil
}
func ( context.Context, , string, ...string) (string, error) {
, := runHgBytes(, , , ...)
return string(),
}
func ( context.Context, , string, ...string) ([]byte, error) {
return vcs.RunCmd(, , nil, []string{"HGPLAIN=1"}, , ...)
}