aboutsummaryrefslogtreecommitdiffstats
path: root/worktree_status.go
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2024-08-03 10:23:28 +0100
committerPaulo Gomes <pjbgf@linux.com>2024-08-03 10:25:23 +0100
commite2d6e8f264604b1b71dc2c958f7bdd44a90d029b (patch)
tree5212ed1117f4e655356ddd6fdf7fce5647267d52 /worktree_status.go
parent093134604cde84f51625efdcf5266a62cd5ab6e9 (diff)
downloadgo-git-e2d6e8f264604b1b71dc2c958f7bdd44a90d029b.tar.gz
git: worktree, Add StatusWithOptions
The fix for #119 improves the Worktree.Status() behaviour by preloading all existing files and setting their status to unmodified. Which makes it more reliable when doing per file status verification, however breaks backwards compatibility in two ways: - Increased execution time and space: the preloading can be slow in very large repositories and will increase memory usage when representing the state. - Behaviour: the previous behaviour returned a map with a small subset of entries. The new behaviour will include a new entry for every file within the repository. This commit introduces reverts the change in the default behaviour, and introduces StatusWithOptions so that users can opt-in the new option. Signed-off-by: Paulo Gomes <pjbgf@linux.com>
Diffstat (limited to 'worktree_status.go')
-rw-r--r--worktree_status.go53
1 files changed, 16 insertions, 37 deletions
diff --git a/worktree_status.go b/worktree_status.go
index 2f865ce..6a0049c 100644
--- a/worktree_status.go
+++ b/worktree_status.go
@@ -29,10 +29,23 @@ var (
// ErrGlobNoMatches in an AddGlob if the glob pattern does not match any
// files in the worktree.
ErrGlobNoMatches = errors.New("glob pattern did not match any files")
+ // ErrUnsupportedStatusStrategy occurs when an invalid StatusStrategy is used
+ // when processing the Worktree status.
+ ErrUnsupportedStatusStrategy = errors.New("unsupported status strategy")
)
// Status returns the working tree status.
func (w *Worktree) Status() (Status, error) {
+ return w.StatusWithOptions(StatusOptions{Strategy: defaultStatusStrategy})
+}
+
+// StatusOptions defines the options for Worktree.StatusWithOptions().
+type StatusOptions struct {
+ Strategy StatusStrategy
+}
+
+// StatusWithOptions returns the working tree status.
+func (w *Worktree) StatusWithOptions(o StatusOptions) (Status, error) {
var hash plumbing.Hash
ref, err := w.r.Head()
@@ -44,45 +57,11 @@ func (w *Worktree) Status() (Status, error) {
hash = ref.Hash()
}
- return w.status(hash)
-}
-
-func (w *Worktree) newStatusFromIndex() (Status, error) {
- idx, err := w.r.Storer.Index()
- if err != nil {
- return nil, err
- }
-
- idxRoot := mindex.NewRootNode(idx)
- nodes := []noder.Noder{idxRoot}
-
- if err != nil {
- return nil, err
- }
-
- status := make(Status)
-
- for len(nodes) > 0 {
- var node noder.Noder
- node, nodes = nodes[0], nodes[1:]
- if node.IsDir() {
- children, err := node.Children()
- if err != nil {
- return nil, err
- }
- nodes = append(nodes, children...)
- continue
- }
- fs := status.File(node.Name())
- fs.Worktree = Unmodified
- fs.Staging = Unmodified
- }
-
- return status, nil
+ return w.status(o.Strategy, hash)
}
-func (w *Worktree) status(commit plumbing.Hash) (Status, error) {
- s, err := w.newStatusFromIndex()
+func (w *Worktree) status(ss StatusStrategy, commit plumbing.Hash) (Status, error) {
+ s, err := ss.new(w)
if err != nil {
return nil, err
}