Skip to content

Commit

Permalink
hg: expose the Hg type again
Browse files Browse the repository at this point in the history
Signed-off-by: Christophe de Vienne <[email protected]>
  • Loading branch information
cdevienne committed Jun 7, 2024
1 parent ffd1bd9 commit cc864a7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
38 changes: 19 additions & 19 deletions pkg/vendir/fetch/hg/hg.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
ctlfetch "carvel.dev/vendir/pkg/vendir/fetch"
)

type hg struct {
type Hg struct {
opts ctlconf.DirectoryContentsHg
infoLog io.Writer
refFetcher ctlfetch.RefFetcher
Expand All @@ -28,11 +28,11 @@ type hg struct {
cacheID string
}

func newHg(opts ctlconf.DirectoryContentsHg,
func NewHg(opts ctlconf.DirectoryContentsHg,
infoLog io.Writer, refFetcher ctlfetch.RefFetcher,
tempArea ctlfetch.TempArea,
) (*hg, error) {
t := hg{opts, infoLog, refFetcher, "", nil, ""}
) (*Hg, error) {
t := Hg{opts, infoLog, refFetcher, "", nil, ""}
if err := t.setup(tempArea); err != nil {
return nil, err
}
Expand All @@ -43,19 +43,19 @@ func newHg(opts ctlconf.DirectoryContentsHg,
// It doesn't include the ref because we want to reuse a cache when only the ref
// is changed
// Basically we combine all data used to write the hgrc file
func (t *hg) getCacheID() string {
func (t *Hg) getCacheID() string {
return t.cacheID
}

//nolint:revive
type hgInfo struct {
type HgInfo struct {
SHA string
ChangeSetTitle string
}

// cloneHasTargetRef returns true if the given clone contains the target
// ref, and this ref is a revision id (not a tag or a branch)
func (t *hg) cloneHasTargetRef(dstPath string) bool {
func (t *Hg) cloneHasTargetRef(dstPath string) bool {
out, _, err := t.run([]string{"id", "--id", "-r", t.opts.Ref}, dstPath)
if err != nil {
return false
Expand All @@ -67,53 +67,53 @@ func (t *hg) cloneHasTargetRef(dstPath string) bool {
return false
}

func (t *hg) clone(dstPath string) error {
func (t *Hg) clone(dstPath string) error {
if err := t.initClone(dstPath); err != nil {
return err
}
return t.syncClone(dstPath)
}

func (t *hg) syncClone(dstPath string) error {
func (t *Hg) syncClone(dstPath string) error {
if _, _, err := t.run([]string{"pull"}, dstPath); err != nil {
return err
}
return nil
}

func (t *hg) checkout(dstPath string) (hgInfo, error) {
func (t *Hg) checkout(dstPath string) (HgInfo, error) {
if _, _, err := t.run([]string{"checkout", t.opts.Ref}, dstPath); err != nil {
return hgInfo{}, err
return HgInfo{}, err
}

info := hgInfo{}
info := HgInfo{}

// use hg log to retrieve full cset sha
out, _, err := t.run([]string{"log", "-r", ".", "-T", "{node}"}, dstPath)
if err != nil {
return hgInfo{}, err
return HgInfo{}, err
}

info.SHA = strings.TrimSpace(out)

out, _, err = t.run([]string{"log", "-l", "1", "-T", "{desc|firstline|strip}", "-r", info.SHA}, dstPath)
if err != nil {
return hgInfo{}, err
return HgInfo{}, err
}

info.ChangeSetTitle = strings.TrimSpace(out)

return info, nil
}

func (t *hg) Close() {
func (t *Hg) Close() {
if t.authDir != "" {
os.RemoveAll(t.authDir)
t.authDir = ""
}
}

func (t *hg) setup(tempArea ctlfetch.TempArea) error {
func (t *Hg) setup(tempArea ctlfetch.TempArea) error {
if len(t.opts.URL) == 0 {
return fmt.Errorf("Expected non-empty URL")
}
Expand Down Expand Up @@ -205,7 +205,7 @@ hgauth.password = %s
return nil
}

func (t *hg) initClone(dstPath string) error {
func (t *Hg) initClone(dstPath string) error {
hgURL := t.opts.URL

if _, _, err := t.run([]string{"init"}, dstPath); err != nil {
Expand All @@ -223,7 +223,7 @@ func (t *hg) initClone(dstPath string) error {
return nil
}

func (t *hg) run(args []string, dstPath string) (string, string, error) {
func (t *Hg) run(args []string, dstPath string) (string, string, error) {
var stdoutBs, stderrBs bytes.Buffer

cmd := exec.Command("hg", args...)
Expand Down Expand Up @@ -253,7 +253,7 @@ func (o hgAuthOpts) IsPresent() bool {
return o.PrivateKey != nil || o.KnownHosts != nil || o.Username != nil || o.Password != nil
}

func (t *hg) getAuthOpts() (hgAuthOpts, error) {
func (t *Hg) getAuthOpts() (hgAuthOpts, error) {
var opts hgAuthOpts

if t.opts.SecretRef != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/vendir/fetch/hg/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (d Sync) Sync(dstPath string, tempArea ctlfetch.TempArea) (ctlconf.LockDire

defer os.RemoveAll(incomingTmpPath)

hg, err := newHg(d.opts, d.log, d.refFetcher, tempArea)
hg, err := NewHg(d.opts, d.log, d.refFetcher, tempArea)
if err != nil {
return hgLockConf, fmt.Errorf("Setting up hg: %w", err)
}
Expand Down

0 comments on commit cc864a7

Please sign in to comment.