@@ -265,6 +265,7 @@ func diffTreeIsEquals(a, b noder.Hasher) bool {
265
265
// the worktree to the index. If any of the files is already staged in the index
266
266
// no error is returned. When path is a file, the blob.Hash is returned.
267
267
func (w * Worktree ) Add (path string ) (plumbing.Hash , error ) {
268
+ // TODO(mcuadros): deprecate in favor of AddWithOption in v6.
268
269
return w .doAdd (path , make ([]gitignore.Pattern , 0 ))
269
270
}
270
271
@@ -308,16 +309,33 @@ func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string,
308
309
return
309
310
}
310
311
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
+
313
325
if opts .All {
314
- return w .doAdd ("." , w .Excludes )
326
+ _ , err := w .doAdd ("." , w .Excludes )
327
+ return err
315
328
}
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
317
336
}
318
337
319
338
func (w * Worktree ) doAdd (path string , ignorePattern []gitignore.Pattern ) (plumbing.Hash , error ) {
320
- // TODO(mcuadros): remove plumbing.Hash from signature at v5.
321
339
s , err := w .Status ()
322
340
if err != nil {
323
341
return plumbing .ZeroHash , err
@@ -353,6 +371,7 @@ func (w *Worktree) doAdd(path string, ignorePattern []gitignore.Pattern) (plumbi
353
371
// directory path, all directory contents are added to the index recursively. No
354
372
// error is returned if all matching paths are already staged in index.
355
373
func (w * Worktree ) AddGlob (pattern string ) error {
374
+ // TODO(mcuadros): deprecate in favor of AddWithOption in v6.
356
375
files , err := util .Glob (w .Filesystem , pattern )
357
376
if err != nil {
358
377
return err
0 commit comments