@@ -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.
267267func (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
319338func (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.
355373func (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
0 commit comments