Skip to content

Commit

Permalink
Add support for Commit and Add specific paths and skip on git status … (
Browse files Browse the repository at this point in the history
#1)

* Add support for Commit and Add specific paths and skip on git status check to improve run time

* remove path option in commit
  • Loading branch information
moranCohen26 authored Jan 2, 2024
1 parent 724a9ba commit b3ae5ab
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
3 changes: 3 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,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.
Expand Down
32 changes: 31 additions & 1 deletion worktree_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")

Expand Down
27 changes: 18 additions & 9 deletions worktree_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -321,23 +321,22 @@ func (w *Worktree) AddWithOptions(opts *AddOptions) error {
}

if opts.All {
_, err := w.doAdd(".", w.Excludes)
_, err := w.doAdd(".", w.Excludes, false)
return err
}

if opts.Glob != "" {
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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b3ae5ab

Please sign in to comment.