Skip to content

Commit

Permalink
worktree: Commit, tests improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed May 4, 2017
1 parent b8b61e7 commit 3713157
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
13 changes: 6 additions & 7 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type CloneOptions struct {
// Limit fetching to the specified number of commits.
Depth int
// RecurseSubmodules after the clone is created, initialize all submodules
// within, using their defaut settings. This option is ignored if the
// within, using their default settings. This option is ignored if the
// cloned repository does not have a worktree.
RecurseSubmodules SubmoduleRescursivity
// Progress is where the human readable information sent by the server is
Expand Down Expand Up @@ -73,7 +73,7 @@ func (o *CloneOptions) Validate() error {
type PullOptions struct {
// Name of the remote to be pulled. If empty, uses the default.
RemoteName string
// Remote branch to clone. If empty, uses HEAD.
// Remote branch to clone. If empty, uses HEAD.
ReferenceName plumbing.ReferenceName
// Fetch only ReferenceName if true.
SingleBranch bool
Expand Down Expand Up @@ -254,8 +254,7 @@ type LogOptions struct {
}

var (
ErrMissingAuthor = errors.New("author field is required")
ErrMissingCommitter = errors.New("committer field is required")
ErrMissingAuthor = errors.New("author field is required")
)

// CommitOptions describes how a commit operation should be performed.
Expand All @@ -266,10 +265,10 @@ type CommitOptions struct {
// Author is the author's signature of the commit.
Author *object.Signature
// Committer is the committer's signature of the commit. If Committer is
// equal to nil the Author signature is used.
// nil the Author signature is used.
Committer *object.Signature
// Parents parents commits for the new commit, by default is the hash of
// HEAD reference.
// Parents are the parents commits for the new commit, by default when
// len(Parents) is zero, the hash of HEAD reference is used.
Parents []plumbing.Hash
}

Expand Down
35 changes: 35 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package git

import (
. "gopkg.in/check.v1"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)

type OptionsSuite struct {
BaseSuite
}

var _ = Suite(&OptionsSuite{})

func (s *OptionsSuite) TestCommitOptionsParentsFromHEAD(c *C) {
o := CommitOptions{Author: &object.Signature{}}
err := o.Validate(s.Repository)
c.Assert(err, IsNil)
c.Assert(o.Parents, HasLen, 1)
}

func (s *OptionsSuite) TestCommitOptionsMissingAuthor(c *C) {
o := CommitOptions{}
err := o.Validate(s.Repository)
c.Assert(err, Equals, ErrMissingAuthor)
}

func (s *OptionsSuite) TestCommitOptionsCommitter(c *C) {
sig := &object.Signature{}

o := CommitOptions{Author: sig}
err := o.Validate(s.Repository)
c.Assert(err, IsNil)

c.Assert(o.Committer, Equals, o.Author)
}
4 changes: 2 additions & 2 deletions plumbing/object/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ type Commit struct {
Committer Signature
// Message is the commit message, contains arbitrary text.
Message string
// TreeHash hash of the tree pointed by the commit.
// TreeHash is the hash of the root tree of the commit.
TreeHash plumbing.Hash
// ParentHashes hashes of the parent commits of the commit.
// ParentHashes are the hashes of the parent commits of the commit.
ParentHashes []plumbing.Hash

s storer.EncodedObjectStorer
Expand Down
4 changes: 2 additions & 2 deletions worktree_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (w *Worktree) Commit(msg string, opts *CommitOptions) (plumbing.Hash, error
return plumbing.ZeroHash, err
}

if opts.All == true {
if opts.All {
if err := w.autoAddModifiedAndDeleted(); err != nil {
return plumbing.ZeroHash, err
}
Expand Down Expand Up @@ -103,7 +103,7 @@ func (w *Worktree) buildCommitObject(msg string, opts *CommitOptions, tree plumb
}

// commitIndexHelper converts a given index.Index file into multiple git objects
// reading the blogs from the given filesystem and creating the trees from the
// reading the blobs from the given filesystem and creating the trees from the
// index structure. The created objects are pushed to a given Storer.
type commitIndexHelper struct {
fs billy.Filesystem
Expand Down
13 changes: 13 additions & 0 deletions worktree_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ import (
"gopkg.in/src-d/go-billy.v2/memfs"
)

func (s *WorktreeSuite) TestCommitInvalidOptions(c *C) {
r, err := Init(memory.NewStorage(), memfs.New())
c.Assert(err, IsNil)

w, err := r.Worktree()
c.Assert(err, IsNil)

hash, err := w.Commit("", &CommitOptions{})
c.Assert(err, Equals, ErrMissingAuthor)
c.Assert(hash.IsZero(), Equals, true)
}

func (s *WorktreeSuite) TestCommitInitial(c *C) {
expected := plumbing.NewHash("98c4ac7c29c913f7461eae06e024dc18e80d23a4")

Expand Down Expand Up @@ -74,6 +86,7 @@ func (s *WorktreeSuite) TestCommitAll(c *C) {
c.Assert(err, IsNil)

billy.WriteFile(fs, "LICENSE", []byte("foo"), 0644)
billy.WriteFile(fs, "foo", []byte("foo"), 0644)

hash, err := w.Commit("foo\n", &CommitOptions{
All: true,
Expand Down

0 comments on commit 3713157

Please sign in to comment.