Skip to content

Commit

Permalink
removing git testing and only using go-git plumbing
Browse files Browse the repository at this point in the history
  • Loading branch information
eiso committed Jan 18, 2018
1 parent b04fa07 commit 5b28404
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 70 deletions.
2 changes: 1 addition & 1 deletion cmd/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *InsertCmd) Execute(cmd *cobra.Command, args []string) error {
return err
}

if err := r.SwitchBranch("gpass"); err != nil {
if err := r.CheckoutBranch("gpass"); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (c *RmCmd) Execute(cmd *cobra.Command, args []string) error {
return nil
}

if err := r.SwitchBranch(filename); err != nil {
if err := r.CheckoutBranch(filename); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c *ShowCmd) Execute(cmd *cobra.Command, args []string) error {
return fmt.Errorf("the account does not exist")
}

if err := r.SwitchBranch(filename); err != nil {
if err := r.CheckoutBranch(filename); err != nil {
return err
}

Expand Down
8 changes: 4 additions & 4 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ func (r *Repository) CreateOrphanBranch(u *User, s string) error {
return nil
}

// SwitchBranch switches to an existing branch or returns an error
func (r *Repository) SwitchBranch(s string) error {
// CheckoutBranch switches to an existing branch or returns an error
func (r *Repository) CheckoutBranch(s string) error {
name := fmt.Sprintf("refs/heads/%s", s)

w, err := r.root.Worktree()
Expand All @@ -176,7 +176,7 @@ func (r *Repository) SwitchBranch(s string) error {
o.Create = false

if err = w.Checkout(o); err != nil {
return fmt.Errorf("Unable to create a new branch: %s", err)
return fmt.Errorf("Unable to checkout branch: %s", err)
}

return nil
Expand Down Expand Up @@ -286,7 +286,7 @@ func (r *Repository) Commit(u *User, filename string, msg string) error {

}

// ListBranches returns a list of all branches on the repository
// ListBranches returns a list of all branches in the repository
func (r *Repository) ListBranches() []string {
var b []string

Expand Down
90 changes: 27 additions & 63 deletions git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,32 @@ func (s *GitSuite) newUser() *User {
HomeFolder: "/home/john-doe/"}
}

func (s *GitSuite) newRepository(name string) *Repository {
// newTestRepository creates a git repository using the systems `git` used for testing
func (s *GitSuite) newTestRepository(name string) *Repository {
dir, err := ioutil.TempDir("", name)
require.NoError(s.T(), err)

dotgit := filepath.Join(dir, ".git")

gitExec(s, dotgit, dir, "init")

return &Repository{Path: dir}
}

func (s *GitSuite) TestCreateBranch() {
gpass := s.newRepository("gpass-test")
defer os.RemoveAll(gpass.Path)
fs := s.newRepository("git-test")
defer os.RemoveAll(fs.Path)

dotgit := filepath.Join(fs.Path, ".git")
origin := "master"
n := "testbranch"
ref1 := fmt.Sprintf("refs/heads/%s", origin)
ref2 := fmt.Sprintf("refs/heads/%s", n)

// gpass case:
// create an origin branch so we can test a checkout
// based on an exiting branch
err := gpass.Load()

repo := &Repository{Path: dir}

// create an master branch with a single file and a single commit
err = repo.Load()
require.NoError(s.T(), err)

w, err := gpass.root.Worktree()
w, err := repo.root.Worktree()
require.NoError(s.T(), err)

o := &git.CheckoutOptions{}
o.Branch = plumbing.ReferenceName(ref1)
o.Branch = plumbing.ReferenceName("refs/heads/master")
o.Create = true

err = w.Checkout(o)
require.NoError(s.T(), err)

err = ioutil.WriteFile(filepath.Join(gpass.Path, ".empty"), []byte(""), 0600)
err = ioutil.WriteFile(filepath.Join(repo.Path, ".empty"), []byte(""), 0600)
require.NoError(s.T(), err)

_, err = w.Add(".empty")
Expand All @@ -90,69 +76,47 @@ func (s *GitSuite) TestCreateBranch() {
})
require.NoError(s.T(), err)

// create a new branch based on origin
gpass.CreateBranch(origin, n)
require.NoError(s.T(), err)
return repo
}

// git case:
err = fs.Load()
require.NoError(s.T(), err)
func (s *GitSuite) TestCreateBranch() {
gpass := s.newTestRepository("gpass-test")
defer os.RemoveAll(gpass.Path)

gitExec(s, dotgit, fs.Path, "checkout", "-b", origin)
master := "master"
n := "testbranch"
ref := fmt.Sprintf("refs/heads/%s", n)

err = ioutil.WriteFile(filepath.Join(fs.Path, ".empty"), []byte(""), 0600)
err := gpass.Load()
require.NoError(s.T(), err)

err = gpass.CreateBranch(master, n)
require.NoError(s.T(), err)

gitExec(s, dotgit, fs.Path, "add", ".")
gitExec(s, dotgit, fs.Path, "commit", "-m", "add .empty")

gitExec(s, dotgit, fs.Path, "checkout", "-b", n, origin)

// verify both git & gpass case
gpassRef, err := gpass.root.Head()
gitRef, err := gpass.root.Head()

s.Equal(string(gpassRef.Name()), ref2)
s.Equal(string(gitRef.Name()), ref2)
s.Equal(string(gpassRef.Name()), ref)
}

func (s *GitSuite) TestCreateOrphanBranch() {
gpass := s.newRepository("gpass-test")
gpass := s.newTestRepository("gpass-test")
defer os.RemoveAll(gpass.Path)
fs := s.newRepository("git-test")
defer os.RemoveAll(fs.Path)

dotgit := filepath.Join(fs.Path, ".git")
n := "testbranch"
n := "orphanbranch"
ref := fmt.Sprintf("refs/heads/%s", n)

// gpass case:
err := gpass.Load()
require.NoError(s.T(), err)

err = gpass.CreateOrphanBranch(s.user, n)
require.NoError(s.T(), err)

// git case:
err = fs.Load()
require.NoError(s.T(), err)

gitExec(s, dotgit, fs.Path, "checkout", "--orphan", n)

err = ioutil.WriteFile(filepath.Join(fs.Path, ".empty"), []byte(""), 0600)
require.NoError(s.T(), err)

gitExec(s, dotgit, fs.Path, "add", ".")
gitExec(s, dotgit, fs.Path, "commit", "-m", "creating branch for "+n)

// verify both git & gpass case:
_, err = gpass.root.Reference(plumbing.ReferenceName(ref), false)
require.NoError(s.T(), err)

_, err = fs.root.Reference(plumbing.ReferenceName(ref), false)
require.NoError(s.T(), err)
}

// TODO: should be removed once creating git repositories with go-git is added to this package
// creates an unnecessary dependency for `git` to exist on the system
func gitExec(s *GitSuite, dir string, worktree string, command ...string) {
cmd := exec.Command("git",
append([]string{"--git-dir", dir, "--work-tree", worktree},
Expand Down

0 comments on commit 5b28404

Please sign in to comment.