diff --git a/options.go b/options.go index 757bdc84e..67dc1f574 100644 --- a/options.go +++ b/options.go @@ -465,6 +465,9 @@ type AddOptions struct { // Glob adds all paths, matching pattern, to the index. If pattern matches a // directory path, all directory contents are added to the index recursively. Glob string + // Force add the path anyway with no status to check if modified/deleted. This option apply only if path passed + // Will not apply when All option is true + Force bool } // Validate validates the fields and sets the default values. diff --git a/worktree_commit_test.go b/worktree_commit_test.go index 1ac1990f4..525696bbf 100644 --- a/worktree_commit_test.go +++ b/worktree_commit_test.go @@ -131,7 +131,6 @@ func (s *WorktreeSuite) TestCommitAmend(c *C) { _, err = w.Commit("foo\n", &CommitOptions{Author: defaultSignature()}) c.Assert(err, IsNil) - amendedHash, err := w.Commit("bar\n", &CommitOptions{Amend: true}) c.Assert(err, IsNil) @@ -144,6 +143,37 @@ func (s *WorktreeSuite) TestCommitAmend(c *C) { assertStorageStatus(c, s.Repository, 13, 11, 11, amendedHash) } +func (s *WorktreeSuite) TestAddPathWithForce(c *C) { + expected := plumbing.NewHash("150c353bbc5d2eee51a62e80e8c53b31d3bf5f52") + + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{}) + c.Assert(err, IsNil) + + util.WriteFile(fs, "LICENSE", []byte("foo"), 0644) + util.WriteFile(fs, "foo", []byte("foo2"), 0644) + + err = w.AddWithOptions(&AddOptions{ + Path: "foo", + Force: true, + }) + c.Assert(err, IsNil) + + hash, err := w.Commit("commit foo only\n", &CommitOptions{ + Author: defaultSignature(), + }) + + c.Assert(hash, Equals, expected) + c.Assert(err, IsNil) + + assertStorageStatus(c, s.Repository, 13, 11, 10, expected) +} + func (s *WorktreeSuite) TestCommitAll(c *C) { expected := plumbing.NewHash("aede6f8c9c1c7ec9ca8d287c64b8ed151276fa28") diff --git a/worktree_status.go b/worktree_status.go index 730108754..f2c209f19 100644 --- a/worktree_status.go +++ b/worktree_status.go @@ -271,7 +271,7 @@ func diffTreeIsEquals(a, b noder.Hasher) bool { // no error is returned. When path is a file, the blob.Hash is returned. func (w *Worktree) Add(path string) (plumbing.Hash, error) { // TODO(mcuadros): deprecate in favor of AddWithOption in v6. - return w.doAdd(path, make([]gitignore.Pattern, 0)) + return w.doAdd(path, make([]gitignore.Pattern, 0), false) } func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string, ignorePattern []gitignore.Pattern) (added bool, err error) { @@ -321,7 +321,7 @@ func (w *Worktree) AddWithOptions(opts *AddOptions) error { } if opts.All { - _, err := w.doAdd(".", w.Excludes) + _, err := w.doAdd(".", w.Excludes, false) return err } @@ -329,15 +329,14 @@ func (w *Worktree) AddWithOptions(opts *AddOptions) error { return w.AddGlob(opts.Glob) } - _, err := w.Add(opts.Path) + _, err := w.doAdd(opts.Path, make([]gitignore.Pattern, 0), opts.Force) return err } -func (w *Worktree) doAdd(path string, ignorePattern []gitignore.Pattern) (plumbing.Hash, error) { - s, err := w.Status() - if err != nil { - return plumbing.ZeroHash, err - } +// if force is true, will skip the status check +func (w *Worktree) doAdd(path string, ignorePattern []gitignore.Pattern, force bool) (plumbing.Hash, error) { + var s Status + var err error idx, err := w.r.Storer.Index() if err != nil { @@ -348,6 +347,15 @@ func (w *Worktree) doAdd(path string, ignorePattern []gitignore.Pattern) (plumbi var added bool fi, err := w.Filesystem.Lstat(path) + + // status is required for doAddDirectory + if !force || fi.IsDir() { + s, err = w.Status() + if err != nil { + return plumbing.ZeroHash, err + } + } + if err != nil || !fi.IsDir() { added, h, err = w.doAddFile(idx, s, path, ignorePattern) } else { @@ -421,8 +429,9 @@ func (w *Worktree) AddGlob(pattern string) error { // doAddFile create a new blob from path and update the index, added is true if // the file added is different from the index. +// if s status is nil will skip the status check and update the index anyway func (w *Worktree) doAddFile(idx *index.Index, s Status, path string, ignorePattern []gitignore.Pattern) (added bool, h plumbing.Hash, err error) { - if s.File(path).Worktree == Unmodified { + if s != nil && s.File(path).Worktree == Unmodified { return false, h, nil } if len(ignorePattern) > 0 {