Skip to content

Commit

Permalink
HgGetter: GetFile
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Oct 13, 2015
1 parent 82a375b commit c11ae6a
Show file tree
Hide file tree
Showing 18 changed files with 64 additions and 9 deletions.
33 changes: 33 additions & 0 deletions get_hg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package getter

import (
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"

urlhelper "github.com/hashicorp/terraform/helper/url"
Expand Down Expand Up @@ -55,6 +57,37 @@ func (g *HgGetter) Get(dst string, u *url.URL) error {
return g.update(dst, newURL, rev)
}

// GetFile for Hg doesn't support updating at this time. It will download
// the file every time.
func (g *HgGetter) GetFile(dst string, u *url.URL) error {
td, err := ioutil.TempDir("", "getter-hg")
if err != nil {
return err
}
if err := os.RemoveAll(td); err != nil {
return err
}

// Get the filename, and strip the filename from the URL so we can
// just get the repository directly.
filename := filepath.Base(u.Path)
u.Path = filepath.Dir(u.Path)

// Get the full repository
if err := g.Get(td, u); err != nil {
return err
}

// Copy the single file
u, err = urlhelper.Parse(fmtFileURL(filepath.Join(td, filename)))
if err != nil {
return err
}

fg := &FileGetter{Copy: true}
return fg.GetFile(dst, u)
}

func (g *HgGetter) clone(dst string, u *url.URL) error {
cmd := exec.Command("hg", "clone", "-U", u.String(), dst)
return getRunCommand(cmd)
Expand Down
25 changes: 21 additions & 4 deletions get_hg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ func TestHgGetter_impl(t *testing.T) {
}

func TestHgGetter(t *testing.T) {
t.Parallel()

if !testHasHg {
t.Log("hg not found, skipping")
t.Skip()
Expand All @@ -43,8 +41,6 @@ func TestHgGetter(t *testing.T) {
}

func TestHgGetter_branch(t *testing.T) {
t.Parallel()

if !testHasHg {
t.Log("hg not found, skipping")
t.Skip()
Expand Down Expand Up @@ -79,3 +75,24 @@ func TestHgGetter_branch(t *testing.T) {
t.Fatalf("err: %s", err)
}
}

func TestHgGetter_GetFile(t *testing.T) {
if !testHasHg {
t.Log("hg not found, skipping")
t.Skip()
}

g := new(HgGetter)
dst := tempFile(t)

// Download
if err := g.GetFile(dst, testModuleURL("basic-hg/foo.txt")); err != nil {
t.Fatalf("err: %s", err)
}

// Verify the main file exists
if _, err := os.Stat(dst); err != nil {
t.Fatalf("err: %s", err)
}
assertContents(t, dst, "Hello\n")
}
4 changes: 2 additions & 2 deletions test-fixtures/basic-hg/.hg/cache/branch2-served
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
c65e998d747ffbb1fe3b1c067a50664bb3fb5da4 1
dcaed7754d58264cb9a5916215a5442377307bd1 o default
992604507bcd66370bf91a0c9d526ccd833412bf 2
992604507bcd66370bf91a0c9d526ccd833412bf o default
c65e998d747ffbb1fe3b1c067a50664bb3fb5da4 o test-branch
1 change: 1 addition & 0 deletions test-fixtures/basic-hg/.hg/cache/rbc-names-v1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default
Binary file added test-fixtures/basic-hg/.hg/cache/rbc-revs-v1
Binary file not shown.
Binary file modified test-fixtures/basic-hg/.hg/dirstate
Binary file not shown.
2 changes: 1 addition & 1 deletion test-fixtures/basic-hg/.hg/last-message.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Branch
add file

Binary file modified test-fixtures/basic-hg/.hg/store/00changelog.i
Binary file not shown.
Binary file modified test-fixtures/basic-hg/.hg/store/00manifest.i
Binary file not shown.
Binary file added test-fixtures/basic-hg/.hg/store/data/foo.txt.i
Binary file not shown.
1 change: 1 addition & 0 deletions test-fixtures/basic-hg/.hg/store/fncache
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
data/main.tf.i
data/foo.txt.i
data/main_branch.tf.i
Binary file modified test-fixtures/basic-hg/.hg/store/undo
Binary file not shown.
2 changes: 2 additions & 0 deletions test-fixtures/basic-hg/.hg/store/undo.backup.fncache
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data/main.tf.i
data/main_branch.tf.i
Binary file added test-fixtures/basic-hg/.hg/store/undo.backupfiles
Binary file not shown.
2 changes: 1 addition & 1 deletion test-fixtures/basic-hg/.hg/undo.branch
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test-branch
default
2 changes: 1 addition & 1 deletion test-fixtures/basic-hg/.hg/undo.desc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
1
2
commit
Binary file modified test-fixtures/basic-hg/.hg/undo.dirstate
Binary file not shown.
1 change: 1 addition & 0 deletions test-fixtures/basic-hg/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello

0 comments on commit c11ae6a

Please sign in to comment.