-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.go
More file actions
216 lines (183 loc) · 4.83 KB
/
filesystem.go
File metadata and controls
216 lines (183 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package swhid
import (
"os"
"path/filepath"
"sort"
"github.com/andrew/swhid-go/objects"
"github.com/go-git/go-git/v5"
)
// FromDirectoryPath computes the SWHID for a directory on the filesystem.
// It recursively hashes all files and subdirectories.
// If the directory is within a Git repository, it uses the Git index for file permissions.
func FromDirectoryPath(path string) (*Identifier, error) {
return FromDirectoryPathWithOptions(path, nil, nil)
}
// FromDirectoryPathWithOptions computes the SWHID with custom options.
// gitRepo can be provided to use Git index for permissions.
// permissions can be provided as a map of path -> mode for explicit permissions.
func FromDirectoryPathWithOptions(path string, gitRepo *git.Repository, permissions map[string]os.FileMode) (*Identifier, error) {
info, err := os.Stat(path)
if err != nil {
return nil, err
}
if !info.IsDir() {
return nil, &os.PathError{Op: "swhid", Path: path, Err: os.ErrInvalid}
}
// Try to discover Git repo if not provided
if gitRepo == nil {
gitRepo = discoverGitRepo(path)
}
entries, err := buildEntries(path, gitRepo, permissions)
if err != nil {
return nil, err
}
return FromDirectory(entries), nil
}
func discoverGitRepo(path string) *git.Repository {
// Walk up the directory tree looking for .git
absPath, err := filepath.Abs(path)
if err != nil {
return nil
}
for {
repo, err := git.PlainOpen(absPath)
if err == nil {
return repo
}
parent := filepath.Dir(absPath)
if parent == absPath {
break
}
absPath = parent
}
return nil
}
func buildEntries(dirPath string, gitRepo *git.Repository, permissions map[string]os.FileMode) ([]objects.DirectoryEntry, error) {
dirEntries, err := os.ReadDir(dirPath)
if err != nil {
return nil, err
}
var entries []objects.DirectoryEntry
for _, de := range dirEntries {
name := de.Name()
// Skip .git directory
if name == ".git" {
continue
}
fullPath := filepath.Join(dirPath, name)
info, err := de.Info()
if err != nil {
return nil, err
}
var entry objects.DirectoryEntry
// Check if it's a symlink
if info.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(fullPath)
if err != nil {
return nil, err
}
targetHash := objects.ComputeContentHash([]byte(target))
entry = objects.DirectoryEntry{
Name: name,
Type: objects.EntryTypeSymlink,
Target: targetHash,
}
} else if info.IsDir() {
// Recurse into subdirectory
subID, err := FromDirectoryPathWithOptions(fullPath, gitRepo, permissions)
if err != nil {
return nil, err
}
entry = objects.DirectoryEntry{
Name: name,
Type: objects.EntryTypeDirectory,
Target: subID.ObjectHash,
}
} else {
// Regular file
content, err := os.ReadFile(fullPath)
if err != nil {
return nil, err
}
targetHash := objects.ComputeContentHash(content)
entryType := objects.EntryTypeFile
if isExecutable(fullPath, info, gitRepo, permissions) {
entryType = objects.EntryTypeExecutable
}
entry = objects.DirectoryEntry{
Name: name,
Type: entryType,
Target: targetHash,
}
}
entries = append(entries, entry)
}
// Sort for deterministic output
sort.Slice(entries, func(i, j int) bool {
return entries[i].SortKey() < entries[j].SortKey()
})
return entries, nil
}
func isExecutable(fullPath string, info os.FileInfo, gitRepo *git.Repository, permissions map[string]os.FileMode) bool {
// Check explicit permissions map first
if permissions != nil {
if mode, ok := permissions[fullPath]; ok {
return mode&0111 != 0
}
// Try with resolved path
absPath, err := filepath.Abs(fullPath)
if err == nil {
if mode, ok := permissions[absPath]; ok {
return mode&0111 != 0
}
}
}
// Check Git index for tracked files
if gitRepo != nil {
relPath := relativePathInRepo(fullPath, gitRepo)
if relPath != "" {
// Try to get mode from index
idx, err := gitRepo.Storer.Index()
if err == nil {
for _, entry := range idx.Entries {
if entry.Name == relPath {
return entry.Mode&0111 != 0
}
}
}
}
}
// Fall back to filesystem
return info.Mode()&0111 != 0
}
func relativePathInRepo(fullPath string, gitRepo *git.Repository) string {
worktree, err := gitRepo.Worktree()
if err != nil {
return ""
}
repoRoot := worktree.Filesystem.Root()
absPath, err := filepath.Abs(fullPath)
if err != nil {
return ""
}
// Resolve symlinks
absPath, _ = filepath.EvalSymlinks(absPath)
repoRoot, _ = filepath.EvalSymlinks(repoRoot)
// Normalize separators
absPath = filepath.ToSlash(absPath)
repoRoot = filepath.ToSlash(repoRoot)
if !hasPrefix(absPath, repoRoot) {
return ""
}
rel := absPath[len(repoRoot):]
if len(rel) > 0 && rel[0] == '/' {
rel = rel[1:]
}
return rel
}
func hasPrefix(s, prefix string) bool {
if len(s) < len(prefix) {
return false
}
return s[:len(prefix)] == prefix
}