8131a2270432c6e28954a3572a73edd4b33aedd6

Author
Ayman Bagabas <ayman.bagabas@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

fix(backend): use subdir collabs path

Diff

This diff is truncated to protect this page.

  1diff --git a/server/backend/file/file.go b/server/backend/file/file.go
  2index 84d2f2db5e605ed873eeff54cddd7b9229284494..8ec6523a2f037deb0b9eb17d062724930c2eab38 100644
  3--- a/server/backend/file/file.go
  4+++ b/server/backend/file/file.go
  5@@ -26,7 +26,6 @@ import (
  6 	"io/fs"
  7 	"os"
  8 	"path/filepath"
  9-	"strconv"
 10 	"strings"
 11 
 12 	"github.com/charmbracelet/log"
 13@@ -41,13 +40,11 @@ const (
 14 	anonAccess   = "anon-access"
 15 	allowKeyless = "allow-keyless"
 16 	admins       = "admins"
 17-	serverHost   = "host"
 18-	serverName   = "name"
 19-	serverPort   = "port"
 20 	repos        = "repos"
 21 	collabs      = "collaborators"
 22 	description  = "description"
 23 	exportOk     = "git-daemon-export-ok"
 24+	private      = "private"
 25 	settings     = "settings"
 26 )
 27 
 28@@ -55,9 +52,6 @@ var (
 29 	logger = log.WithPrefix("backend.file")
 30 
 31 	defaults = map[string]string{
 32-		serverName:   "Soft Serve",
 33-		serverHost:   "localhost",
 34-		serverPort:   "23231",
 35 		anonAccess:   backend.ReadOnlyAccess.String(),
 36 		allowKeyless: "true",
 37 	}
 38@@ -94,7 +88,7 @@ func (fb *FileBackend) adminsPath() string {
 39 }
 40 
 41 func (fb *FileBackend) collabsPath(repo string) string {
 42-	return filepath.Join(fb.path, collabs, repo)
 43+	return filepath.Join(fb.path, collabs, repo, collabs)
 44 }
 45 
 46 func sanatizeRepo(repo string) string {
 47@@ -136,7 +130,8 @@ func NewFileBackend(path string) (*FileBackend, error) {
 48 			return nil, err
 49 		}
 50 	}
 51-	for _, file := range []string{admins, anonAccess, allowKeyless, serverHost, serverName, serverPort} {
 52+
 53+	for _, file := range []string{admins, anonAccess, allowKeyless} {
 54 		fp := filepath.Join(fb.settingsPath(), file)
 55 		_, err := os.Stat(fp)
 56 		if errors.Is(err, fs.ErrNotExist) {
 57@@ -216,28 +211,29 @@ func (fb *FileBackend) AddAdmin(pk gossh.PublicKey, memo string) error {
 58 // AddCollaborator adds a public key to the list of collaborators for the given repo.
 59 //
 60 // It implements backend.Backend.
 61-func (fb *FileBackend) AddCollaborator(pk gossh.PublicKey, memo string, name string) error {
 62+func (fb *FileBackend) AddCollaborator(pk gossh.PublicKey, memo string, repo string) error {
 63+	name := sanatizeRepo(repo)
 64+	repo = name + ".git"
 65 	// Check if repo exists
 66-	if !exists(filepath.Join(fb.reposPath(), sanatizeRepo(name)+".git")) {
 67-		return fmt.Errorf("repository %s does not exist", name)
 68+	if !exists(filepath.Join(fb.reposPath(), repo)) {
 69+		return fmt.Errorf("repository %s does not exist", repo)
 70 	}
 71 
 72 	// Skip if the key already exists.
 73-	if fb.IsCollaborator(pk, name) {
 74+	if fb.IsCollaborator(pk, repo) {
 75 		return fmt.Errorf("key already exists")
 76 	}
 77 
 78 	ak := backend.MarshalAuthorizedKey(pk)
 79-	name = sanatizeRepo(name)
 80-	if err := os.MkdirAll(filepath.Dir(fb.collabsPath(name)), 0755); err != nil {
 81+	if err := os.MkdirAll(filepath.Dir(fb.collabsPath(repo)), 0755); err != nil {
 82 		logger.Debug("failed to create collaborators directory",
 83-			"err", err, "path", filepath.Dir(fb.collabsPath(name)))
 84+			"err", err, "path", filepath.Dir(fb.collabsPath(repo)))
 85 		return err
 86 	}
 87 
 88-	f, err := os.OpenFile(fb.collabsPath(name), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
 89+	f, err := os.OpenFile(fb.collabsPath(repo), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
 90 	if err != nil {
 91-		logger.Debug("failed to open collaborators file", "err", err, "path", fb.collabsPath(name))
 92+		logger.Debug("failed to open collaborators file", "err", err, "path", fb.collabsPath(repo))
 93 		return err
 94 	}
 95 
 96@@ -273,8 +269,10 @@ func (fb *FileBackend) Admins() ([]string, error) {
 97 //
 98 // It implements backend.Backend.
 99 func (fb *FileBackend) Collaborators(repo string) ([]string, error) {
100+	name := sanatizeRepo(repo)
101+	repo = name + ".git"
102 	// Check if repo exists
103-	if !exists(filepath.Join(fb.reposPath(), sanatizeRepo(repo)+".git")) {
104+	if !exists(filepath.Join(fb.reposPath(), repo)) {
105diff --git a/server/backend/file/repo.go b/server/backend/file/repo.go
106index 0697b51d0d420fd462ab781819b9f841033b404c..6e760d4295ea089d1951192ca369c1484e70038c 100644
107--- a/server/backend/file/repo.go
108+++ b/server/backend/file/repo.go
109@@ -45,8 +45,8 @@ func (r *Repo) Description() string {
110 //
111 // It implements backend.Repository.
112 func (r *Repo) IsPrivate() bool {
113-	_, err := os.Stat(filepath.Join(r.path, exportOk))
114-	return err != nil
115+	_, err := os.Stat(filepath.Join(r.path, private))
116+	return err == nil
117 }
118 
119 // Open returns the underlying git.Repository.
120diff --git a/server/backend/noop/noop.go b/server/backend/noop/noop.go
121index 45cf143964aeb2e7f1ba098c0f061b064f06fde5..b91ffbee395e9daca5a465a61175d33a90c119e4 100644
122--- a/server/backend/noop/noop.go
123+++ b/server/backend/noop/noop.go
124@@ -21,6 +21,11 @@ type Noop struct {
125 	Port string
126 }
127 
128+// RepositoryStorePath implements backend.Backend
129+func (*Noop) RepositoryStorePath() string {
130+	return ""
131+}
132+
133 // Admins implements backend.Backend
134 func (*Noop) Admins() ([]string, error) {
135 	return nil, nil
136@@ -122,21 +127,6 @@ func (*Noop) Repository(repo string) (backend.Repository, error) {
137 	return nil, ErrNotImpl
138 }
139 
140-// ServerHost implements backend.Backend
141-func (*Noop) ServerHost() string {
142-	return "localhost"
143-}
144-
145-// ServerName implements backend.Backend
146-func (*Noop) ServerName() string {
147-	return "Soft Serve"
148-}
149-
150-// ServerPort implements backend.Backend
151-func (n *Noop) ServerPort() string {
152-	return n.Port
153-}
154-
155 // SetAllowKeyless implements backend.Backend
156 func (*Noop) SetAllowKeyless(allow bool) error {
157 	return ErrNotImpl
158@@ -156,18 +146,3 @@ func (*Noop) SetDescription(repo string, desc string) error {
159 func (*Noop) SetPrivate(repo string, priv bool) error {
160 	return ErrNotImpl
161 }
162-
163-// SetServerHost implements backend.Backend
164-func (*Noop) SetServerHost(host string) error {
165-	return ErrNotImpl
166-}
167-
168-// SetServerName implements backend.Backend
169-func (*Noop) SetServerName(name string) error {
170-	return ErrNotImpl
171-}
172-
173-// SetServerPort implements backend.Backend
174-func (*Noop) SetServerPort(port string) error {
175-	return ErrNotImpl
176-}
177diff --git a/server/backend/server.go b/server/backend/server.go
178index 97c02b0966e631ac09139960bd3f09d94e06a560..72198c018d445afabc5b2a5e049e742894c1fb5c 100644
179--- a/server/backend/server.go
180+++ b/server/backend/server.go
181@@ -2,19 +2,6 @@ package backend
182 
183 // ServerBackend is an interface that handles server configuration.
184 type ServerBackend interface {
185-	// ServerName returns the server's name.
186-	ServerName() string
187-	// SetServerName sets the server's name.
188-	SetServerName(name string) error
189-	// ServerHost returns the server's host.
190-	ServerHost() string
191-	// SetServerHost sets the server's host.
192-	SetServerHost(host string) error
193-	// ServerPort returns the server's port.
194-	ServerPort() string
195-	// SetServerPort sets the server's port.
196-	SetServerPort(port string) error
197-
198 	// AnonAccess returns the access level for anonymous users.
199 	AnonAccess() AccessLevel
200 	// SetAnonAccess sets the access level for anonymous users.