all repos

mugit @ f421981544afb124582785961edf224bd7117b3a

🐮 git server that your cow will love

mugit/internal/git/paths.go(view raw)

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
package git

import (
	"fmt"
	"strings"

	securejoin "github.com/cyphar/filepath-securejoin"
)

func ResolveName(name string) string {
	return strings.TrimSuffix(name, ".git") + ".git"
}

func ResolvePath(baseDir, repoName string) (string, error) {
	path, err := securejoin.SecureJoin(baseDir, repoName)
	if err != nil {
		return "", fmt.Errorf("failed to secure join paths: %w", err)
	}
	return path, err
}

// topLevelEntry returns the top-level entry name under base for a given path.
// e.g. base="lua", path="lua/plugins/foo.lua" -> "plugins"
// e.g. base="",    path="README.md"           -> "README.md"
// returns "" if path is not under base.
func topLevelEntry(fullPath, base string) string {
	if base != "" && base != "." {
		if !strings.HasPrefix(fullPath, base+"/") {
			return ""
		}
		fullPath = fullPath[len(base)+1:]
	}
	if before, _, ok := strings.Cut(fullPath, "/"); ok {
		return before
	}
	return fullPath
}