-
Notifications
You must be signed in to change notification settings - Fork 0
/
versions.go
117 lines (107 loc) · 2.74 KB
/
versions.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"errors"
"fmt"
"io/fs"
"log"
"os"
"sort"
"github.com/Masterminds/semver/v3"
)
type Version struct {
Name string
Version *semver.Version
IsDefault bool
FS fs.FS
}
type preferredVersion byte
const (
preferLatestTag preferredVersion = iota
preferWorkingDir
preferMainBranch
)
func GetDocVersions(config *Config) ([]Version, error) {
var prefVersion preferredVersion
if config.withWorkingDir {
prefVersion = preferWorkingDir
} else {
prefVersion = preferLatestTag
}
repo, err := NewGitRepository(config.repositoryPath)
if err != nil {
return nil, fmt.Errorf("could not open git repository: %w", err)
}
tags, err := repo.Tags()
if err != nil {
return nil, fmt.Errorf("could not get tags from repository: %w", err)
}
var versions []Version
latest := make(map[uint64]Version)
for _, tag := range tags {
v, err := semver.NewVersion(tag.Name())
if err != nil {
log.Printf("skipping tag %s: not a valid semver version", tag.Name())
continue
}
filesys, err := repo.FS(tag)
if err != nil {
return nil, fmt.Errorf("could not open repository filesystem for tag %s: %w", tag.Name(), err)
}
_, err = filesys.Open(config.docsDir)
if errors.Is(err, fs.ErrNotExist) {
log.Printf("skipping tag %s: directory %s does not exist", tag.Name(), config.docsDir)
continue
}
if other, ok := latest[v.Major()]; ok && v.LessThan(other.Version) {
// Not the newest version.
continue
}
versions = append(versions, Version{
Name: fmt.Sprintf("%d.x", v.Major()),
Version: v,
FS: filesys,
})
}
sort.Slice(versions, func(i, j int) bool {
return versions[j].Version.LessThan(versions[i].Version)
})
if prefVersion == preferLatestTag {
if len(versions) > 0 {
versions[0].IsDefault = true
} else {
prefVersion = preferMainBranch
}
}
branch, err := repo.Branch(config.mainBranch)
if err != nil {
return nil, fmt.Errorf("could not get branch %s from repository: %w", config.mainBranch, err)
}
filesys, err := repo.FS(branch)
if err != nil {
return nil, fmt.Errorf("could not open repository filesystem for branch %s: %w", config.mainBranch, err)
}
_, err = filesys.Open(config.docsDir)
if errors.Is(err, fs.ErrNotExist) {
log.Printf("skipping branch %s: directory %s does not exist", config.mainBranch, config.docsDir)
} else {
versions = append([]Version{
{
Name: config.mainBranch,
Version: nil,
IsDefault: prefVersion == preferMainBranch,
FS: filesys,
},
}, versions...)
}
if config.withWorkingDir {
versions = append([]Version{
{
Name: "dev",
Version: nil,
IsDefault: prefVersion == preferWorkingDir,
FS: os.DirFS(config.repositoryPath),
},
}, versions...)
}
return versions, nil
}