mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-29 03:57:49 +01:00
f6ba73de4c
Refs: https://forgejo.org/2023-08-release-v1-20-3-0/ (cherry picked from commita266dd0ce3
) (cherry picked from commitb9eb5eccd8
) (cherry picked from commit7fc2028ede
) (cherry picked from commit0c988e6120
) (cherry picked from commit7ba05e8c2b
) (cherry picked from commit2ed5068abe
) (cherry picked from commit353913a26d
) (cherry picked from commit4e63a01a8b
) (cherry picked from commit99f612aed3
) (cherry picked from commitb4fe189cae
) (cherry picked from commitbd35e3b7bc
) (cherry picked from commitf59d9f7088
) (cherry picked from commit0b2a93e044
) (cherry picked from commit8c5d8bfea0
) (cherry picked from commit2817ce027c
) (cherry picked from commit162056cbcf
) (cherry picked from commit07152a0ba6
) (cherry picked from commit72e3777915
) (cherry picked from commit3b1ebd95b9
) (cherry picked from commit14fc4f3fac
) (cherry picked from commit9052c86e8c
) (cherry picked from commit16f1cc21ea
) (cherry picked from commit0fef12f5d8
) (cherry picked from commitd187381f73
) (cherry picked from commit9dda715111
)
80 lines
1.6 KiB
Go
80 lines
1.6 KiB
Go
// SPDX-License-Identifier: MIT
|
|
|
|
package semver
|
|
|
|
import (
|
|
"context"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
"github.com/hashicorp/go-version"
|
|
)
|
|
|
|
func init() {
|
|
db.RegisterModel(new(ForgejoSemVer))
|
|
}
|
|
|
|
var DefaultVersionString = "1.0.0"
|
|
|
|
type ForgejoSemVer struct {
|
|
Version string
|
|
}
|
|
|
|
func GetVersion(ctx context.Context) (*version.Version, error) {
|
|
return GetVersionWithEngine(db.GetEngine(ctx))
|
|
}
|
|
|
|
func GetVersionWithEngine(e db.Engine) (*version.Version, error) {
|
|
versionString := DefaultVersionString
|
|
|
|
exists, err := e.IsTableExist("forgejo_sem_ver")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if exists {
|
|
var semver ForgejoSemVer
|
|
has, err := e.Get(&semver)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if has {
|
|
versionString = semver.Version
|
|
}
|
|
}
|
|
|
|
v, err := version.NewVersion(versionString)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
func SetVersionString(ctx context.Context, versionString string) error {
|
|
return SetVersionStringWithEngine(db.GetEngine(ctx), versionString)
|
|
}
|
|
|
|
func SetVersionStringWithEngine(e db.Engine, versionString string) error {
|
|
v, err := version.NewVersion(versionString)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return SetVersionWithEngine(e, v)
|
|
}
|
|
|
|
func SetVersion(ctx context.Context, v *version.Version) error {
|
|
return SetVersionWithEngine(db.GetEngine(ctx), v)
|
|
}
|
|
|
|
func SetVersionWithEngine(e db.Engine, v *version.Version) error {
|
|
var semver ForgejoSemVer
|
|
has, err := e.Get(&semver)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !has {
|
|
_, err = e.Exec("insert into forgejo_sem_ver values (?)", v.String())
|
|
} else {
|
|
_, err = e.Exec("update forgejo_sem_ver set version = ?", v.String())
|
|
}
|
|
return err
|
|
}
|