Skip to content

Commit

Permalink
Support bazaar checkout and bazaar branch
Browse files Browse the repository at this point in the history
Config:

[ghq]
    vcs = bazaar-checkout

or

[ghq]
    vcs = bazaar-branch

Examples:

# checkout
$ ghq get https://launchpad.net/terminator
     clone https://launchpad.net/terminator -> /path/to/launchpad.net/terminator
       bzr checkout https://launchpad.net/terminator /path/to/launchpad.net/terminator
$ ghq get -u https://launchpad.net/terminator
    update /path/to/launchpad.net/terminator
       bzr update
Tree is up to date at revision 1779 of branch http://bazaar.launchpad.net/~gnome-terminator/terminator/gtk3

# checkout lightweight
$ ghq get --shallow https://launchpad.net/terminator
     clone https://launchpad.net/terminator -> /path/to/launchpad.net/terminator
       bzr checkout --lightweight https://launchpad.net/terminator /path/to/launchpad.net/terminator
$ ghq get -u https://launchpad.net/terminator
    update /path/to/launchpad.net/terminator
       bzr update
Tree is up to date at revision 1779 of branch http://bazaar.launchpad.net/~gnome-terminator/terminator/gtk3

# branch
$ ghq get https://launchpad.net/terminator
     clone https://launchpad.net/terminator -> /path/to/launchpad.net/terminator
       bzr branch https://launchpad.net/terminator /path/to/launchpad.net/terminator
Branched 1779 revisions.
$ ghq get -u https://launchpad.net/terminator
    update /path/to/launchpad.net/terminator
       bzr pull
Using saved parent location: http://bazaar.launchpad.net/~gnome-terminator/terminator/gtk3/
No revisions or tags to pull.
  • Loading branch information
shigemk2 committed Oct 8, 2017
1 parent 1dd91c7 commit 390be0d
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 1 deletion.
19 changes: 18 additions & 1 deletion local_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"net/url"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -132,10 +133,26 @@ func (repo *LocalRepository) VCS() *VCSBackend {
return DarcsBackend
}

fi, err = os.Stat(filepath.Join(repo.FullPath, ".bzr"))
if err == nil && fi.IsDir() {
conf, err := ioutil.ReadFile(filepath.Join(repo.FullPath, ".bzr/branch/branch.conf"))

if err != nil {
return BazaarCheckoutBackend
}

file := string(conf)
if (strings.Contains(file, "parent_location")) {
return BazaarBranchBackend
} else {
return BazaarCheckoutBackend
}
}

return nil
}

var vcsDirs = []string{".git", ".svn", ".hg", "_darcs"}
var vcsDirs = []string{".git", ".svn", ".hg", "_darcs", ".bzr"}

func walkLocalRepositories(callback func(*LocalRepository)) {
for _, root := range localRepositoryRoots() {
Expand Down
9 changes: 9 additions & 0 deletions remote_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ func (repo *OtherRepository) VCS() *VCSBackend {
if vcs == "darcs" {
return DarcsBackend
}

if vcs == "bazaar-checkout" {
return BazaarCheckoutBackend
}

if vcs == "bazaar-branch" {
return BazaarBranchBackend
}

} else {
utils.Log("warning", "This version of Git does not support `config --get-urlmatch`; per-URL settings are not available")
}
Expand Down
39 changes: 39 additions & 0 deletions vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,42 @@ var DarcsBackend = &VCSBackend{
return utils.RunInDir(local, "darcs", "pull")
},
}

var BazaarCheckoutBackend = &VCSBackend{
Clone: func(remote *url.URL, local string, shallow bool) error {
dir, _ := filepath.Split(local)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}

args := []string{"checkout"}
if shallow {
args = append(args, "--lightweight")
}
args = append(args, remote.String(), local)

return utils.Run("bzr", args...)
},
Update: func(local string) error {
return utils.RunInDir(local, "bzr", "update")
},
}

var BazaarBranchBackend = &VCSBackend{
Clone: func(remote *url.URL, local string, shallow bool) error {
dir, _ := filepath.Split(local)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}

args := []string{"branch"}
args = append(args, remote.String(), local)

return utils.Run("bzr", args...)
},
Update: func(local string) error {
return utils.RunInDir(local, "bzr", "pull")
},
}
90 changes: 90 additions & 0 deletions vcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,93 @@ func TestDarcsBackend(t *testing.T) {
"darcs", "pull",
}))
}

func TestBazaarCheckoutBackend(t *testing.T) {
RegisterTestingT(t)

tempDir, err := ioutil.TempDir("", "ghq-test")
if err != nil {
t.Fatal(err)
}

localDir := filepath.Join(tempDir, "repo")

remoteURL, err := url.Parse("https://example.com/git/repo")
if err != nil {
t.Fatal(err)
}

commands := []*exec.Cmd{}
lastCommand := func() *exec.Cmd { return commands[len(commands)-1] }
utils.CommandRunner = func(cmd *exec.Cmd) error {
commands = append(commands, cmd)
return nil
}

err = BazaarCheckoutBackend.Clone(remoteURL, localDir, false)

Expect(err).NotTo(HaveOccurred())
Expect(commands).To(HaveLen(1))
Expect(lastCommand().Args).To(Equal([]string{
"bzr", "checkout", remoteURL.String(), localDir,
}))

err = BazaarCheckoutBackend.Clone(remoteURL, localDir, true)

Expect(err).NotTo(HaveOccurred())
Expect(commands).To(HaveLen(2))
Expect(lastCommand().Args).To(Equal([]string{
"bzr", "checkout", "--lightweight", remoteURL.String(), localDir,
}))

err = BazaarCheckoutBackend.Update(localDir)

Expect(err).NotTo(HaveOccurred())
Expect(commands).To(HaveLen(3))
Expect(lastCommand().Args).To(Equal([]string{
"bzr", "update",
}))
Expect(lastCommand().Dir).To(Equal(localDir))
}

func TestBazaarBranchBackend(t *testing.T) {
RegisterTestingT(t)

tempDir, err := ioutil.TempDir("", "ghq-test")
if err != nil {
t.Fatal(err)
}

localDir := filepath.Join(tempDir, "repo")

remoteURL, err := url.Parse("https://example.com/git/repo")
if err != nil {
t.Fatal(err)
}

commands := []*exec.Cmd{}
lastCommand := func() *exec.Cmd { return commands[len(commands)-1] }
utils.CommandRunner = func(cmd *exec.Cmd) error {
commands = append(commands, cmd)
return nil
}

err = BazaarBranchBackend.Clone(remoteURL, localDir, false)

Expect(err).NotTo(HaveOccurred())
Expect(commands).To(HaveLen(1))
Expect(lastCommand().Args).To(Equal([]string{
"bzr", "branch", remoteURL.String(), localDir,
}))

// There is no shallow clone option in `bzr branch`

err = BazaarBranchBackend.Update(localDir)

Expect(err).NotTo(HaveOccurred())
Expect(commands).To(HaveLen(2))
Expect(lastCommand().Args).To(Equal([]string{
"bzr", "pull",
}))
Expect(lastCommand().Dir).To(Equal(localDir))
}

0 comments on commit 390be0d

Please sign in to comment.