aboutsummaryrefslogtreecommitdiffstats
path: root/worktree_status.go
diff options
context:
space:
mode:
authorTaketoshi Fujiwara <tfujiwar@users.noreply.github.com>2023-01-05 18:35:52 +0900
committerGitHub <noreply@github.com>2023-01-05 10:35:52 +0100
commit5dabd83e3712e2554745c736b55df405a0ba4f33 (patch)
tree319aeb1098c9eaa43db4e4ee587cb6f39a01039e /worktree_status.go
parent6839cd553d609fe7f4ac9d4fb6975950fa832063 (diff)
downloadgo-git-5dabd83e3712e2554745c736b55df405a0ba4f33.tar.gz
Worktree: Add, fix add removed files. Fixes #223 (#652)v5.5.2
Diffstat (limited to 'worktree_status.go')
-rw-r--r--worktree_status.go41
1 files changed, 25 insertions, 16 deletions
diff --git a/worktree_status.go b/worktree_status.go
index c639f13..f3091cf 100644
--- a/worktree_status.go
+++ b/worktree_status.go
@@ -270,10 +270,6 @@ func (w *Worktree) Add(path string) (plumbing.Hash, error) {
}
func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string, ignorePattern []gitignore.Pattern) (added bool, err error) {
- files, err := w.Filesystem.ReadDir(directory)
- if err != nil {
- return false, err
- }
if len(ignorePattern) > 0 {
m := gitignore.NewMatcher(ignorePattern)
matchPath := strings.Split(directory, string(os.PathSeparator))
@@ -283,20 +279,13 @@ func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string,
}
}
- for _, file := range files {
- name := path.Join(directory, file.Name())
-
- var a bool
- if file.IsDir() {
- if file.Name() == GitDirName {
- // ignore special git directory
- continue
- }
- a, err = w.doAddDirectory(idx, s, name, ignorePattern)
- } else {
- a, _, err = w.doAddFile(idx, s, name, ignorePattern)
+ for name := range s {
+ if !isPathInDirectory(name, filepath.ToSlash(filepath.Clean(directory))) {
+ continue
}
+ var a bool
+ a, _, err = w.doAddFile(idx, s, name, ignorePattern)
if err != nil {
return
}
@@ -309,6 +298,26 @@ func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string,
return
}
+func isPathInDirectory(path, directory string) bool {
+ ps := strings.Split(path, "/")
+ ds := strings.Split(directory, "/")
+
+ if len(ds) == 1 && ds[0] == "." {
+ return true
+ }
+
+ if len(ps) < len(ds) {
+ return false
+ }
+
+ for i := 0; i < len(ds); i++ {
+ if ps[i] != ds[i] {
+ return false
+ }
+ }
+ return true
+}
+
// AddWithOptions file contents to the index, updates the index using the
// current content found in the working tree, to prepare the content staged for
// the next commit.