diff --git a/cmd/insert.go b/cmd/insert.go index a1634ca..5b8b38e 100644 --- a/cmd/insert.go +++ b/cmd/insert.go @@ -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 } diff --git a/cmd/rm.go b/cmd/rm.go index 99e68a1..5599851 100644 --- a/cmd/rm.go +++ b/cmd/rm.go @@ -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 } diff --git a/cmd/show.go b/cmd/show.go index c82dda5..1de11cd 100644 --- a/cmd/show.go +++ b/cmd/show.go @@ -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 } diff --git a/git/git.go b/git/git.go index 77ba163..9e5e5f4 100644 --- a/git/git.go +++ b/git/git.go @@ -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() @@ -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 @@ -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 diff --git a/git/git_test.go b/git/git_test.go index 4f22671..0fb52f3 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -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") @@ -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},