Skip to content
This repository was archived by the owner on Apr 23, 2024. It is now read-only.

Commit 38d0994

Browse files
committed
feat: Virtual major tags
If enabled in config, each major version will be shown as a tag. This tag will always link to the latest release from this version. This only will consider semantic versions. Example: v1.0.0, v1.1.0, v2.0.0 => Virtual tags "v1" and "v2" will be shown. "v1" links to "v1.1.0" and "v2" links to "v2.0.0". Pushing a release "v2.0.1" will make "v2" automatically refer to "v2.0.1". The "v" prefix may be omitted in tags. The virtual tag will also have no "v" prefix then.
1 parent 6a6c619 commit 38d0994

File tree

5 files changed

+86
-4
lines changed

5 files changed

+86
-4
lines changed

config.yml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ display:
1515
tags:
1616
order: desc
1717
show_date: true
18+
virtual_tags:
19+
enable_semver_major: true
1820
index:
1921
show_branches: true
2022
show_tags: true

config/config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ type Config struct {
3232
}
3333
Display struct {
3434
Tags struct {
35-
Order string `default:"desc" yaml:"order"`
36-
ShowDate bool `default:"true" yaml:"show_date"`
35+
Order string `default:"desc" yaml:"order"`
36+
ShowDate bool `default:"true" yaml:"show_date"`
37+
VirtualTags struct {
38+
EnableSemverMajor bool `default:"false" yaml:"enable_semver_major"`
39+
} `yaml:"virtual_tags"`
3740
}
3841
Index struct {
3942
ShowBranches bool `default:"true" yaml:"show_branches"`

git/virtual_tags.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package git
2+
3+
import (
4+
"fmt"
5+
"github.com/saitho/static-git-file-server/utils"
6+
"regexp"
7+
)
8+
9+
var re = regexp.MustCompile(`^(v?\d+)\.\d+\.\d+$`)
10+
11+
func ResolveVirtualTag(gitHandler *GitHandler, virtualTag string) (GitTag, error) {
12+
for _, tag := range gitHandler.GetTags() {
13+
majorTag := re.FindStringSubmatch(tag.Tag)
14+
if len(majorTag) < 2 {
15+
continue
16+
}
17+
if majorTag[1] == virtualTag {
18+
return tag, nil
19+
}
20+
}
21+
return GitTag{}, fmt.Errorf("cannot resolve virtual tag")
22+
}
23+
24+
func InsertVirtualTags(tags []GitTag) []GitTag {
25+
var newTags []GitTag
26+
var processedMajorTags []string
27+
for _, tag := range tags {
28+
majorTag := re.FindStringSubmatch(tag.Tag)[1]
29+
if len(majorTag) > 0 && !utils.Contains(processedMajorTags, majorTag) {
30+
newTags = append(newTags, GitTag{
31+
Tag: majorTag,
32+
Date: tag.Date,
33+
})
34+
processedMajorTags = append(processedMajorTags, majorTag)
35+
}
36+
newTags = append(newTags, tag)
37+
}
38+
return newTags
39+
}

main.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ package main
44

55
import (
66
"flag"
7-
"github.com/markbates/pkger"
8-
"github.com/saitho/static-git-file-server/rendering"
7+
"fmt"
98
"net/http"
109
"strings"
1110
"time"
1211

12+
"github.com/markbates/pkger"
13+
1314
"github.com/saitho/static-git-file-server/config"
1415
"github.com/saitho/static-git-file-server/git"
16+
"github.com/saitho/static-git-file-server/rendering"
1517
"github.com/saitho/static-git-file-server/webserver"
1618
)
1719

@@ -62,7 +64,29 @@ func main() {
6264
resp.Auto(http.StatusOK, content)
6365
}
6466

67+
resolveVirtualMajorTag := func(resp *webserver.Response, req *webserver.Request) {
68+
majorVersion := req.Params[0]
69+
path := ""
70+
if len(req.Params) > 1 {
71+
path = req.Params[1]
72+
}
73+
74+
latestTag, err := git.ResolveVirtualTag(gitHandler, majorVersion)
75+
if err != nil {
76+
resp.Text(http.StatusInternalServerError, fmt.Sprintf("Unable to resolve tag %s", majorVersion))
77+
return
78+
}
79+
80+
req.Params = []string{"tag", latestTag.Tag, path}
81+
handler(resp, req)
82+
}
83+
6584
server.AddHandler(`^/webhook/github`, webserver.GitHubWebHookEndpoint(cfg, gitHandler))
85+
if cfg.Display.Tags.VirtualTags.EnableSemverMajor {
86+
server.AddHandler(`^/tag/(v?\d+)/-/(.*)`, resolveVirtualMajorTag)
87+
server.AddHandler(`^/tag/(v?\d+)/?$`, resolveVirtualMajorTag)
88+
}
89+
6690
server.AddHandler(`^/(branch|tag)/(.*)/-/(.*)`, handler)
6791
server.AddHandler(`^/(branch|tag)/(.*)/?$`, handler)
6892
server.AddHandler(`^/$`, func(resp *webserver.Response, req *webserver.Request) {
@@ -87,6 +111,10 @@ func main() {
87111
}
88112
}
89113

114+
if cfg.Display.Tags.VirtualTags.EnableSemverMajor {
115+
tags = git.InsertVirtualTags(tags)
116+
}
117+
90118
content, err := rendering.RenderTemplate("/tmpl/index.html", IndexTmplParams{
91119
Cfg: cfg,
92120
ShowBranches: cfg.Display.Index.ShowBranches,

utils/slice.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package utils
2+
3+
func Contains(arr []string, str string) bool {
4+
for _, a := range arr {
5+
if a == str {
6+
return true
7+
}
8+
}
9+
return false
10+
}

0 commit comments

Comments
 (0)