Debian Codegraph @

storage/filesystem/dotgit/repository_filesystem.go

raw
1 package dotgit
2
3 import (
4 "os"
5 "path/filepath"
6 "strings"
7
8 "github.com/go-git/go-billy/v5"
9 )
10
11 // RepositoryFilesystem is a billy.Filesystem compatible object wrapper
12 // which handles dot-git filesystem operations and supports commondir according to git scm layout:
13 // https://github.com/git/git/blob/master/Documentation/gitrepository-layout.txt
14 type RepositoryFilesystem struct {
15 dotGitFs billy.Filesystem
16 commonDotGitFs billy.Filesystem
17 }
18
19 func NewRepositoryFilesystem(dotGitFs, commonDotGitFs billy.Filesystem) *RepositoryFilesystem {
20 return &RepositoryFilesystem{
21 dotGitFs: dotGitFs,
22 commonDotGitFs: commonDotGitFs,
23 }
24 }
25
26 func (fs *RepositoryFilesystem) mapToRepositoryFsByPath(path string) billy.Filesystem {
27 // Nothing to decide if commondir not defined
28 if fs.commonDotGitFs == nil {
29 return fs.dotGitFs
30 }
31
32 cleanPath := filepath.Clean(path)
33
34 // Check exceptions for commondir (https://git-scm.com/docs/gitrepository-layout#Documentation/gitrepository-layout.txt)
35 switch cleanPath {
36 case fs.dotGitFs.Join(logsPath, "HEAD"):
37 return fs.dotGitFs
38 case fs.dotGitFs.Join(refsPath, "bisect"), fs.dotGitFs.Join(refsPath, "rewritten"), fs.dotGitFs.Join(refsPath, "worktree"):
39 return fs.dotGitFs
40 }
41
42 // Determine dot-git root by first path element.
43 // There are some elements which should always use commondir when commondir defined.
44 // Usual dot-git root will be used for the rest of files.
45 switch strings.Split(cleanPath, string(filepath.Separator))[0] {
46 case objectsPath, refsPath, packedRefsPath, configPath, branchesPath, hooksPath, infoPath, remotesPath, logsPath, shallowPath, worktreesPath:
47 return fs.commonDotGitFs
48 default:
49 return fs.dotGitFs
50 }
51 }
52
53 func (fs *RepositoryFilesystem) Create(filename string) (billy.File, error) {
54 return fs.mapToRepositoryFsByPath(filename).Create(filename)
55 }
56
57 func (fs *RepositoryFilesystem) Open(filename string) (billy.File, error) {
58 return fs.mapToRepositoryFsByPath(filename).Open(filename)
59 }
60
61 func (fs *RepositoryFilesystem) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error) {
62 return fs.mapToRepositoryFsByPath(filename).OpenFile(filename, flag, perm)
63 }
64
65 func (fs *RepositoryFilesystem) Stat(filename string) (os.FileInfo, error) {
66 return fs.mapToRepositoryFsByPath(filename).Stat(filename)
67 }
68
69 func (fs *RepositoryFilesystem) Rename(oldpath, newpath string) error {
70 return fs.mapToRepositoryFsByPath(oldpath).Rename(oldpath, newpath)
71 }
72
73 func (fs *RepositoryFilesystem) Remove(filename string) error {
74 return fs.mapToRepositoryFsByPath(filename).Remove(filename)
75 }
76
77 func (fs *RepositoryFilesystem) Join(elem ...string) string {
78 return fs.dotGitFs.Join(elem...)
79 }
80
81 func (fs *RepositoryFilesystem) TempFile(dir, prefix string) (billy.File, error) {
82 return fs.mapToRepositoryFsByPath(dir).TempFile(dir, prefix)
83 }
84
85 func (fs *RepositoryFilesystem) ReadDir(path string) ([]os.FileInfo, error) {
86 return fs.mapToRepositoryFsByPath(path).ReadDir(path)
87 }
88
89 func (fs *RepositoryFilesystem) MkdirAll(filename string, perm os.FileMode) error {
90 return fs.mapToRepositoryFsByPath(filename).MkdirAll(filename, perm)
91 }
92
93 func (fs *RepositoryFilesystem) Lstat(filename string) (os.FileInfo, error) {
94 return fs.mapToRepositoryFsByPath(filename).Lstat(filename)
95 }
96
97 func (fs *RepositoryFilesystem) Symlink(target, link string) error {
98 return fs.mapToRepositoryFsByPath(target).Symlink(target, link)
99 }
100
101 func (fs *RepositoryFilesystem) Readlink(link string) (string, error) {
102 return fs.mapToRepositoryFsByPath(link).Readlink(link)
103 }
104
105 func (fs *RepositoryFilesystem) Chroot(path string) (billy.Filesystem, error) {
106 return fs.mapToRepositoryFsByPath(path).Chroot(path)
107 }
108
109 func (fs *RepositoryFilesystem) Root() string {
110 return fs.dotGitFs.Root()
111 }