Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

knotserver: add helpers to read and parse .gitmodules files

Signed-off-by: oppiliappan <me@oppi.li>

authored by

oppiliappan and committed by
Tangled
0f46394e 7ed8a100

+60 -2
+60 -2
knotserver/git/git.go
··· 3 3 import ( 4 4 "archive/tar" 5 5 "bytes" 6 + "errors" 6 7 "fmt" 7 8 "io" 8 9 "io/fs" ··· 13 12 "time" 14 13 15 14 "github.com/go-git/go-git/v5" 15 + "github.com/go-git/go-git/v5/config" 16 16 "github.com/go-git/go-git/v5/plumbing" 17 17 "github.com/go-git/go-git/v5/plumbing/object" 18 18 ) 19 19 20 20 var ( 21 - ErrBinaryFile = fmt.Errorf("binary file") 22 - ErrNotBinaryFile = fmt.Errorf("not binary file") 21 + ErrBinaryFile = errors.New("binary file") 22 + ErrNotBinaryFile = errors.New("not binary file") 23 + ErrMissingGitModules = errors.New("no .gitmodules file found") 24 + ErrInvalidGitModules = errors.New("invalid .gitmodules file") 25 + ErrNotSubmodule = errors.New("path is not a submodule") 23 26 ) 24 27 25 28 type GitRepo struct { ··· 193 188 defer reader.Close() 194 189 195 190 return io.ReadAll(reader) 191 + } 192 + 193 + // read and parse .gitmodules 194 + func (g *GitRepo) Submodules() (*config.Modules, error) { 195 + c, err := g.r.CommitObject(g.h) 196 + if err != nil { 197 + return nil, fmt.Errorf("commit object: %w", err) 198 + } 199 + 200 + tree, err := c.Tree() 201 + if err != nil { 202 + return nil, fmt.Errorf("tree: %w", err) 203 + } 204 + 205 + // read .gitmodules file 206 + modulesEntry, err := tree.FindEntry(".gitmodules") 207 + if err != nil { 208 + return nil, fmt.Errorf("%w: %w", ErrMissingGitModules, err) 209 + } 210 + 211 + modulesFile, err := tree.TreeEntryFile(modulesEntry) 212 + if err != nil { 213 + return nil, fmt.Errorf("%w: failed to read file: %w", ErrInvalidGitModules, err) 214 + } 215 + 216 + content, err := modulesFile.Contents() 217 + if err != nil { 218 + return nil, fmt.Errorf("%w: failed to read contents: %w", ErrInvalidGitModules, err) 219 + } 220 + 221 + // parse .gitmodules 222 + modules := config.NewModules() 223 + if err = modules.Unmarshal([]byte(content)); err != nil { 224 + return nil, fmt.Errorf("%w: failed to parse: %w", ErrInvalidGitModules, err) 225 + } 226 + 227 + return modules, nil 228 + } 229 + 230 + func (g *GitRepo) Submodule(path string) (*config.Submodule, error) { 231 + modules, err := g.Submodules() 232 + if err != nil { 233 + return nil, err 234 + } 235 + 236 + for _, submodule := range modules.Submodules { 237 + if submodule.Path == path { 238 + return submodule, nil 239 + } 240 + } 241 + 242 + // path is not a submodule 243 + return nil, ErrNotSubmodule 196 244 } 197 245 198 246 func (g *GitRepo) Branch(name string) (*plumbing.Reference, error) {