Skip to content

Commit ae693a9

Browse files
committed
Worktree.AddWithOptions: improve documentation and interface
1 parent b77bbc9 commit ae693a9

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

Diff for: options.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package git
22

33
import (
44
"errors"
5+
"fmt"
56
"regexp"
67
"strings"
78
"time"
@@ -375,8 +376,26 @@ var (
375376

376377
// AddOptions describes how a add operation should be performed
377378
type AddOptions struct {
378-
All bool
379+
// All equivalent to `git add -A`, update the index not only where the
380+
// working tree has a file matching `Path` but also where the index already
381+
// has an entry. This adds, modifies, and removes index entries to match the
382+
// working tree. If no `Path` nor `Glob` is given when `All` option is
383+
// used, all files in the entire working tree are updated.
384+
All bool
385+
// Path is the exact filepath to a the file or directory to be added.
379386
Path string
387+
// Glob adds all paths, matching pattern, to the index. If pattern matches a
388+
// directory path, all directory contents are added to the index recursively.
389+
Glob string
390+
}
391+
392+
// Validate validates the fields and sets the default values.
393+
func (o *AddOptions) Validate(r *Repository) error {
394+
if o.Path != "" && o.Glob != "" {
395+
return fmt.Errorf("fields Path and Glob are mutual exclusive")
396+
}
397+
398+
return nil
380399
}
381400

382401
// CommitOptions describes how a commit operation should be performed.

Diff for: worktree_status.go

+24-5
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ func diffTreeIsEquals(a, b noder.Hasher) bool {
265265
// the worktree to the index. If any of the files is already staged in the index
266266
// no error is returned. When path is a file, the blob.Hash is returned.
267267
func (w *Worktree) Add(path string) (plumbing.Hash, error) {
268+
// TODO(mcuadros): deprecate in favor of AddWithOption in v6.
268269
return w.doAdd(path, make([]gitignore.Pattern, 0))
269270
}
270271

@@ -308,16 +309,33 @@ func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string,
308309
return
309310
}
310311

311-
// add changes from all tracked and untracked files
312-
func (w *Worktree) AddWithOptions(opts *AddOptions) (plumbing.Hash, error) {
312+
// AddWithOptions file contents to the index, updates the index using the
313+
// current content found in the working tree, to prepare the content staged for
314+
// the next commit.
315+
//
316+
// It typically adds the current content of existing paths as a whole, but with
317+
// some options it can also be used to add content with only part of the changes
318+
// made to the working tree files applied, or remove paths that do not exist in
319+
// the working tree anymore.
320+
func (w *Worktree) AddWithOptions(opts *AddOptions) error {
321+
if err := opts.Validate(w.r); err != nil {
322+
return err
323+
}
324+
313325
if opts.All {
314-
return w.doAdd(".", w.Excludes)
326+
_, err := w.doAdd(".", w.Excludes)
327+
return err
315328
}
316-
return w.Add(opts.Path)
329+
330+
if opts.Glob != "" {
331+
return w.AddGlob(opts.Glob)
332+
}
333+
334+
_, err := w.Add(opts.Path)
335+
return err
317336
}
318337

319338
func (w *Worktree) doAdd(path string, ignorePattern []gitignore.Pattern) (plumbing.Hash, error) {
320-
// TODO(mcuadros): remove plumbing.Hash from signature at v5.
321339
s, err := w.Status()
322340
if err != nil {
323341
return plumbing.ZeroHash, err
@@ -353,6 +371,7 @@ func (w *Worktree) doAdd(path string, ignorePattern []gitignore.Pattern) (plumbi
353371
// directory path, all directory contents are added to the index recursively. No
354372
// error is returned if all matching paths are already staged in index.
355373
func (w *Worktree) AddGlob(pattern string) error {
374+
// TODO(mcuadros): deprecate in favor of AddWithOption in v6.
356375
files, err := util.Glob(w.Filesystem, pattern)
357376
if err != nil {
358377
return err

Diff for: worktree_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ func (s *WorktreeSuite) TestAddAll(c *C) {
13961396
w.Excludes = make([]gitignore.Pattern, 0)
13971397
w.Excludes = append(w.Excludes, gitignore.ParsePattern("file3", nil))
13981398

1399-
_, err = w.AddWithOptions(&AddOptions{All: true})
1399+
err = w.AddWithOptions(&AddOptions{All: true})
14001400
c.Assert(err, IsNil)
14011401

14021402
idx, err = w.r.Storer.Index()
@@ -1437,7 +1437,7 @@ func (s *WorktreeSuite) TestAddGlob(c *C) {
14371437
err = util.WriteFile(w.Filesystem, "qux/bar/baz", []byte("BAZ"), 0755)
14381438
c.Assert(err, IsNil)
14391439

1440-
err = w.AddGlob(w.Filesystem.Join("qux", "b*"))
1440+
err = w.AddWithOptions(&AddOptions{Glob: w.Filesystem.Join("qux", "b*")})
14411441
c.Assert(err, IsNil)
14421442

14431443
idx, err = w.r.Storer.Index()

0 commit comments

Comments
 (0)