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
|
package gitrepo
import (
"errors"
"strings"
"gitweb/internal/store"
)
// Locate finds whether the upspin path "<user><clean>" points into a bare git
// repository. It scans path prefixes from the user root downward and returns
// the shallowest prefix that is a bare repo (repos do not nest), along with the
// remaining path after it.
//
// - user: validated upspin user, e.g. "u@d.com"
// - clean: the cleaned remainder, always beginning with "/", e.g. "/proj.git/log/main"
//
// On a match it returns repoPath="u@d.com/proj.git", rest="log/main", found=true.
// found=false means no repo lies on this path and the caller should fall back to
// plain namespace browsing. A store error other than not-found is returned.
func Locate(st store.Store, user, clean string) (repoPath, rest string, found bool, err error) {
trimmed := strings.Trim(clean, "/")
var segs []string
if trimmed != "" {
segs = strings.Split(trimmed, "/")
}
// Candidates shallow -> deep: user, user/seg1, user/seg1/seg2, ...
prefix := user
for i := 0; ; i++ {
ok, err := isRepo(st, prefix)
if err != nil {
return "", "", false, err
}
if ok {
return prefix, strings.Join(segs[i:], "/"), true, nil
}
if i >= len(segs) {
break
}
prefix = prefix + "/" + segs[i]
}
return "", "", false, nil
}
// isRepo reports whether prefix is a bare git repository: it must contain a
// HEAD file and an objects directory. Not-found on either means "not a repo".
func isRepo(st store.Store, prefix string) (bool, error) {
headIsDir, err := st.Stat(prefix + "/HEAD")
if err != nil {
if errors.Is(err, store.ErrNotFound) {
return false, nil
}
return false, err
}
if headIsDir {
return false, nil
}
objIsDir, err := st.Stat(prefix + "/objects")
if err != nil {
if errors.Is(err, store.ErrNotFound) {
return false, nil
}
return false, err
}
return objIsDir, nil
}
|