Skip to content

Commit 7fd34c0

Browse files
authored
fix lfs version check warning log when using ssh protocol (#5501)
1 parent 2a660a1 commit 7fd34c0

File tree

5 files changed

+48
-30
lines changed

5 files changed

+48
-30
lines changed

cmd/hook.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"fmt"
1111
"net/url"
1212
"os"
13-
"path/filepath"
1413
"strconv"
1514
"strings"
1615

@@ -63,11 +62,6 @@ var (
6362
}
6463
)
6564

66-
func hookSetup(logPath string) {
67-
setting.NewContext()
68-
log.NewGitLogger(filepath.Join(setting.LogRootPath, logPath))
69-
}
70-
7165
func runHookPreReceive(c *cli.Context) error {
7266
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
7367
return nil
@@ -79,7 +73,7 @@ func runHookPreReceive(c *cli.Context) error {
7973
setting.CustomConf = c.GlobalString("config")
8074
}
8175

82-
hookSetup("hooks/pre-receive.log")
76+
setup("hooks/pre-receive.log")
8377

8478
// the environment setted on serv command
8579
repoID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchRepoID), 10, 64)
@@ -155,7 +149,7 @@ func runHookUpdate(c *cli.Context) error {
155149
setting.CustomConf = c.GlobalString("config")
156150
}
157151

158-
hookSetup("hooks/update.log")
152+
setup("hooks/update.log")
159153

160154
return nil
161155
}
@@ -171,7 +165,7 @@ func runHookPostReceive(c *cli.Context) error {
171165
setting.CustomConf = c.GlobalString("config")
172166
}
173167

174-
hookSetup("hooks/post-receive.log")
168+
setup("hooks/post-receive.log")
175169

176170
// the environment setted on serv command
177171
repoID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchRepoID), 10, 64)

cmd/serv.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515
"time"
1616

17+
"code.gitea.io/git"
1718
"code.gitea.io/gitea/models"
1819
"code.gitea.io/gitea/modules/log"
1920
"code.gitea.io/gitea/modules/pprof"
@@ -22,6 +23,7 @@ import (
2223

2324
"github.com/Unknwon/com"
2425
"github.com/dgrijalva/jwt-go"
26+
version "github.com/mcuadros/go-version"
2527
"github.com/urfave/cli"
2628
)
2729

@@ -48,8 +50,28 @@ var CmdServ = cli.Command{
4850
},
4951
}
5052

53+
func checkLFSVersion() {
54+
if setting.LFS.StartServer {
55+
//Disable LFS client hooks if installed for the current OS user
56+
//Needs at least git v2.1.2
57+
binVersion, err := git.BinVersion()
58+
if err != nil {
59+
fail(fmt.Sprintf("Error retrieving git version: %v", err), fmt.Sprintf("Error retrieving git version: %v", err))
60+
}
61+
62+
if !version.Compare(binVersion, "2.1.2", ">=") {
63+
setting.LFS.StartServer = false
64+
println("LFS server support needs at least Git v2.1.2, disabled")
65+
} else {
66+
git.GlobalCommandArgs = append(git.GlobalCommandArgs, "-c", "filter.lfs.required=",
67+
"-c", "filter.lfs.smudge=", "-c", "filter.lfs.clean=")
68+
}
69+
}
70+
}
71+
5172
func setup(logPath string) {
5273
setting.NewContext()
74+
checkLFSVersion()
5375
log.NewGitLogger(filepath.Join(setting.LogRootPath, logPath))
5476
}
5577

integrations/integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func initIntegrationTest() {
9999
}
100100

101101
setting.NewContext()
102+
setting.CheckLFSVersion()
102103
models.LoadConfigs()
103104

104105
switch {

modules/setting/setting.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,27 @@ func createPIDFile(pidPath string) {
693693
}
694694
}
695695

696+
// CheckLFSVersion will check lfs version, if not satisfied, then disable it.
697+
func CheckLFSVersion() {
698+
if LFS.StartServer {
699+
//Disable LFS client hooks if installed for the current OS user
700+
//Needs at least git v2.1.2
701+
702+
binVersion, err := git.BinVersion()
703+
if err != nil {
704+
log.Fatal(4, "Error retrieving git version: %v", err)
705+
}
706+
707+
if !version.Compare(binVersion, "2.1.2", ">=") {
708+
LFS.StartServer = false
709+
log.Error(4, "LFS server support needs at least Git v2.1.2")
710+
} else {
711+
git.GlobalCommandArgs = append(git.GlobalCommandArgs, "-c", "filter.lfs.required=",
712+
"-c", "filter.lfs.smudge=", "-c", "filter.lfs.clean=")
713+
}
714+
}
715+
}
716+
696717
// NewContext initializes configuration context.
697718
// NOTE: do not print any log except error.
698719
func NewContext() {
@@ -888,7 +909,6 @@ func NewContext() {
888909
LFS.HTTPAuthExpiry = sec.Key("LFS_HTTP_AUTH_EXPIRY").MustDuration(20 * time.Minute)
889910

890911
if LFS.StartServer {
891-
892912
if err := os.MkdirAll(LFS.ContentPath, 0700); err != nil {
893913
log.Fatal(4, "Failed to create '%s': %v", LFS.ContentPath, err)
894914
}
@@ -922,26 +942,6 @@ func NewContext() {
922942
return
923943
}
924944
}
925-
926-
//Disable LFS client hooks if installed for the current OS user
927-
//Needs at least git v2.1.2
928-
929-
binVersion, err := git.BinVersion()
930-
if err != nil {
931-
log.Fatal(4, "Error retrieving git version: %v", err)
932-
}
933-
934-
if !version.Compare(binVersion, "2.1.2", ">=") {
935-
936-
LFS.StartServer = false
937-
log.Error(4, "LFS server support needs at least Git v2.1.2")
938-
939-
} else {
940-
941-
git.GlobalCommandArgs = append(git.GlobalCommandArgs, "-c", "filter.lfs.required=",
942-
"-c", "filter.lfs.smudge=", "-c", "filter.lfs.clean=")
943-
944-
}
945945
}
946946

947947
sec = Cfg.Section("security")

routers/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func NewServices() {
4545
// GlobalInit is for global configuration reload-able.
4646
func GlobalInit() {
4747
setting.NewContext()
48+
setting.CheckLFSVersion()
4849
log.Trace("AppPath: %s", setting.AppPath)
4950
log.Trace("AppWorkPath: %s", setting.AppWorkPath)
5051
log.Trace("Custom path: %s", setting.CustomPath)

0 commit comments

Comments
 (0)