Skip to content

Commit

Permalink
hg: refactor for later evolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
cdevienne committed Apr 2, 2024
1 parent 58ff583 commit 97da79a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
68 changes: 47 additions & 21 deletions pkg/vendir/fetch/hg/hg.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ type Hg struct {
opts ctlconf.DirectoryContentsHg
infoLog io.Writer
refFetcher ctlfetch.RefFetcher
authDir string
env []string
}

func NewHg(opts ctlconf.DirectoryContentsHg,
infoLog io.Writer, refFetcher ctlfetch.RefFetcher) *Hg {

return &Hg{opts, infoLog, refFetcher}
infoLog io.Writer, refFetcher ctlfetch.RefFetcher,
tempArea ctlfetch.TempArea,
) (*Hg, error) {
t := Hg{opts, infoLog, refFetcher, "", nil}
if err := t.setup(tempArea); err != nil {
return nil, err
}
return &t, nil
}

//nolint:revive
Expand All @@ -48,14 +55,14 @@ func (t *Hg) Retrieve(dstPath string, tempArea ctlfetch.TempArea) (HgInfo, error
info := HgInfo{}

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

info.SHA = strings.TrimSpace(out)

out, _, err = t.run([]string{"log", "-l", "1", "-T", "{desc|firstline|strip}", "-r", info.SHA}, nil, dstPath)
out, _, err = t.run([]string{"log", "-l", "1", "-T", "{desc|firstline|strip}", "-r", info.SHA}, dstPath)
if err != nil {
return HgInfo{}, err
}
Expand All @@ -65,7 +72,14 @@ func (t *Hg) Retrieve(dstPath string, tempArea ctlfetch.TempArea) (HgInfo, error
return info, nil
}

func (t *Hg) fetch(dstPath string, tempArea ctlfetch.TempArea) error {
func (t *Hg) Close() {
if t.authDir != "" {
os.RemoveAll(t.authDir)
t.authDir = ""
}
}

func (t *Hg) setup(tempArea ctlfetch.TempArea) error {
authOpts, err := t.getAuthOpts()
if err != nil {
return err
Expand All @@ -76,17 +90,12 @@ func (t *Hg) fetch(dstPath string, tempArea ctlfetch.TempArea) error {
return err
}

defer os.RemoveAll(authDir)
t.authDir = authDir

env := os.Environ()
t.env = os.Environ()

hgURL := t.opts.URL

_, _, err = t.run([]string{"init"}, env, dstPath)
if err != nil {
return err
}

var hgRc string

if t.opts.Evolve {
Expand Down Expand Up @@ -147,39 +156,56 @@ hgauth.password = %s
if err != nil {
return fmt.Errorf("Writing %s: %s", hgRcPath, err)
}
env = append(env, "HGRCPATH="+hgRcPath)
t.env = append(t.env, "HGRCPATH="+hgRcPath)
}

return nil
}

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

if _, _, err := t.run([]string{"init"}, dstPath); err != nil {
return err
}

repoHgRcPath := filepath.Join(dstPath, ".hg", "hgrc")

repoHgRc := fmt.Sprintf("[paths]\ndefault = %s\n", hgURL)

err = os.WriteFile(repoHgRcPath, []byte(repoHgRc), 0600)
if err != nil {
if err := os.WriteFile(repoHgRcPath, []byte(repoHgRc), 0600); err != nil {
return fmt.Errorf("Writing %s: %s", repoHgRcPath, err)
}

return nil
}

func (t *Hg) fetch(dstPath string, tempArea ctlfetch.TempArea) error {
if err := t.initClone(dstPath); err != nil {
return err
}

return t.runMultiple([][]string{
{"pull"},
{"checkout", t.opts.Ref},
}, env, dstPath)
}, dstPath)
}

func (t *Hg) runMultiple(argss [][]string, env []string, dstPath string) error {
func (t *Hg) runMultiple(argss [][]string, dstPath string) error {
for _, args := range argss {
_, _, err := t.run(args, env, dstPath)
_, _, err := t.run(args, dstPath)
if err != nil {
return err
}
}
return nil
}

func (t *Hg) run(args []string, env []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...)
cmd.Env = env
cmd.Env = t.env
cmd.Dir = dstPath
cmd.Stdout = io.MultiWriter(t.infoLog, &stdoutBs)
cmd.Stderr = io.MultiWriter(t.infoLog, &stderrBs)
Expand Down
6 changes: 5 additions & 1 deletion pkg/vendir/fetch/hg/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ func (d Sync) Sync(dstPath string, tempArea ctlfetch.TempArea) (ctlconf.LockDire

defer os.RemoveAll(incomingTmpPath)

hg := NewHg(d.opts, d.log, d.refFetcher)
hg, err := NewHg(d.opts, d.log, d.refFetcher, tempArea)
if err != nil {
return hgLockConf, err
}
defer hg.Close()

info, err := hg.Retrieve(incomingTmpPath, tempArea)
if err != nil {
Expand Down

0 comments on commit 97da79a

Please sign in to comment.