AddGlob adds all paths, matching pattern, to the index. If pattern matches a directory path, all directory contents are added to the index recursively. No error is returned if all matching paths are already staged in index.
(pattern string)
| 394 | // directory path, all directory contents are added to the index recursively. No |
| 395 | // error is returned if all matching paths are already staged in index. |
| 396 | func (w *Worktree) AddGlob(pattern string) error { |
| 397 | // TODO(mcuadros): deprecate in favor of AddWithOption in v6. |
| 398 | files, err := util.Glob(w.Filesystem, pattern) |
| 399 | if err != nil { |
| 400 | return err |
| 401 | } |
| 402 | |
| 403 | if len(files) == 0 { |
| 404 | return ErrGlobNoMatches |
| 405 | } |
| 406 | |
| 407 | s, err := w.Status() |
| 408 | if err != nil { |
| 409 | return err |
| 410 | } |
| 411 | |
| 412 | idx, err := w.r.Storer.Index() |
| 413 | if err != nil { |
| 414 | return err |
| 415 | } |
| 416 | |
| 417 | var saveIndex bool |
| 418 | for _, file := range files { |
| 419 | fi, err := w.Filesystem.Lstat(file) |
| 420 | if err != nil { |
| 421 | return err |
| 422 | } |
| 423 | |
| 424 | var added bool |
| 425 | if fi.IsDir() { |
| 426 | added, err = w.doAddDirectory(idx, s, file, make([]gitignore.Pattern, 0)) |
| 427 | } else { |
| 428 | added, _, err = w.doAddFile(idx, s, file, make([]gitignore.Pattern, 0)) |
| 429 | } |
| 430 | |
| 431 | if err != nil { |
| 432 | return err |
| 433 | } |
| 434 | |
| 435 | if !saveIndex && added { |
| 436 | saveIndex = true |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | if saveIndex { |
| 441 | return w.r.Storer.SetIndex(idx) |
| 442 | } |
| 443 | |
| 444 | return nil |
| 445 | } |
| 446 | |
| 447 | // doAddFile create a new blob from path and update the index, added is true if |
| 448 | // the file added is different from the index. |