|
4 | 4 | "bytes"
|
5 | 5 | "fmt"
|
6 | 6 | "path/filepath"
|
| 7 | + |
| 8 | + mindex "github.com/go-git/go-git/v5/utils/merkletrie/index" |
| 9 | + "github.com/go-git/go-git/v5/utils/merkletrie/noder" |
7 | 10 | )
|
8 | 11 |
|
9 | 12 | // Status represents the current status of a Worktree.
|
@@ -77,3 +80,69 @@ const (
|
77 | 80 | Copied StatusCode = 'C'
|
78 | 81 | UpdatedButUnmerged StatusCode = 'U'
|
79 | 82 | )
|
| 83 | + |
| 84 | +// StatusStrategy defines the different types of strategies when processing |
| 85 | +// the worktree status. |
| 86 | +type StatusStrategy int |
| 87 | + |
| 88 | +const ( |
| 89 | + // TODO: (V6) Review the default status strategy. |
| 90 | + // TODO: (V6) Review the type used to represent Status, to enable lazy |
| 91 | + // processing of statuses going direct to the backing filesystem. |
| 92 | + defaultStatusStrategy = Empty |
| 93 | + |
| 94 | + // Empty starts its status map from empty. Missing entries for a given |
| 95 | + // path means that the file is untracked. This causes a known issue (#119) |
| 96 | + // whereby unmodified files can be incorrectly reported as untracked. |
| 97 | + // |
| 98 | + // This can be used when returning the changed state within a modified Worktree. |
| 99 | + // For example, to check whether the current worktree is clean. |
| 100 | + Empty StatusStrategy = 0 |
| 101 | + // Preload goes through all existing nodes from the index and add them to the |
| 102 | + // status map as unmodified. This is currently the most reliable strategy |
| 103 | + // although it comes at a performance cost in large repositories. |
| 104 | + // |
| 105 | + // This method is recommended when fetching the status of unmodified files. |
| 106 | + // For example, to confirm the status of a specific file that is either |
| 107 | + // untracked or unmodified. |
| 108 | + Preload StatusStrategy = 1 |
| 109 | +) |
| 110 | + |
| 111 | +func (s StatusStrategy) new(w *Worktree) (Status, error) { |
| 112 | + switch s { |
| 113 | + case Preload: |
| 114 | + return preloadStatus(w) |
| 115 | + case Empty: |
| 116 | + return make(Status), nil |
| 117 | + } |
| 118 | + return nil, fmt.Errorf("%w: %+v", ErrUnsupportedStatusStrategy, s) |
| 119 | +} |
| 120 | + |
| 121 | +func preloadStatus(w *Worktree) (Status, error) { |
| 122 | + idx, err := w.r.Storer.Index() |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + |
| 127 | + idxRoot := mindex.NewRootNode(idx) |
| 128 | + nodes := []noder.Noder{idxRoot} |
| 129 | + |
| 130 | + status := make(Status) |
| 131 | + for len(nodes) > 0 { |
| 132 | + var node noder.Noder |
| 133 | + node, nodes = nodes[0], nodes[1:] |
| 134 | + if node.IsDir() { |
| 135 | + children, err := node.Children() |
| 136 | + if err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + nodes = append(nodes, children...) |
| 140 | + continue |
| 141 | + } |
| 142 | + fs := status.File(node.Name()) |
| 143 | + fs.Worktree = Unmodified |
| 144 | + fs.Staging = Unmodified |
| 145 | + } |
| 146 | + |
| 147 | + return status, nil |
| 148 | +} |
0 commit comments