From 6dc313a784c748aaa6847a9c5a1d469210fa1898 Mon Sep 17 00:00:00 2001 From: Ole Husgaard Date: Sun, 26 Sep 2021 13:10:40 +0200 Subject: [PATCH 1/8] Gitea Snap update issue #16209 --- modules/setting/setting.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index b054f27c59f0b..916024428221e 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -444,7 +444,19 @@ func getAppPath() (string, error) { } // Note: we don't use path.Dir here because it does not handle case // which path starts with two "/" in Windows: "//psf/Home/..." - return strings.ReplaceAll(appPath, "\\", "/"), err + appPath = strings.ReplaceAll(appPath, "\\", "/") + + // Fix issue 16209 + snap := os.Getenv("SNAP") + if snap != "" && strings.HasPrefix(appPath, snap) { + split := strings.Split(appPath, "/") + if len(split) >= 3 { + split[len(split)-2] = "current" + appPath = strings.Join(split, "/"); + } + } + + return appPath, err } func getWorkPath(appPath string) string { From 0fe5a4e8fcd39e4fcc3ef94cd56cab580930cc75 Mon Sep 17 00:00:00 2001 From: Ole Husgaard Date: Sun, 26 Sep 2021 14:17:03 +0200 Subject: [PATCH 2/8] Fix whitespace --- modules/setting/setting.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 916024428221e..93263fac8cb37 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -452,7 +452,7 @@ func getAppPath() (string, error) { split := strings.Split(appPath, "/") if len(split) >= 3 { split[len(split)-2] = "current" - appPath = strings.Join(split, "/"); + appPath = strings.Join(split, "/") } } From 4d6e8cf1b5a7747fe4c644c9111033134430c69d Mon Sep 17 00:00:00 2001 From: Ole Husgaard Date: Sun, 26 Sep 2021 16:55:44 +0200 Subject: [PATCH 3/8] More comments --- modules/setting/setting.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 93263fac8cb37..77f40165b9240 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -446,7 +446,12 @@ func getAppPath() (string, error) { // which path starts with two "/" in Windows: "//psf/Home/..." appPath = strings.ReplaceAll(appPath, "\\", "/") - // Fix issue 16209 + // Fix issue 16209: When runnung in a snap and repositories are + // created, paths in the hooks include the snap revision. But as new + // revisions come out the older revisions are eventually deleted, and + // the hooks begin to fail. + // The code below replaces the snap revision with "current", which + // always points to the current snap revision. snap := os.Getenv("SNAP") if snap != "" && strings.HasPrefix(appPath, snap) { split := strings.Split(appPath, "/") From 555d05c39b6fd36736f856c286809b6034095fca Mon Sep 17 00:00:00 2001 From: Ole Husgaard Date: Sun, 26 Sep 2021 16:58:47 +0200 Subject: [PATCH 4/8] Fix typo --- modules/setting/setting.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 77f40165b9240..8623e2e215b6a 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -446,7 +446,7 @@ func getAppPath() (string, error) { // which path starts with two "/" in Windows: "//psf/Home/..." appPath = strings.ReplaceAll(appPath, "\\", "/") - // Fix issue 16209: When runnung in a snap and repositories are + // Fix issue 16209: When running in a snap and repositories are // created, paths in the hooks include the snap revision. But as new // revisions come out the older revisions are eventually deleted, and // the hooks begin to fail. From e3825a7131dbdf5a51ebdeeecdf71b1e058c6f5b Mon Sep 17 00:00:00 2001 From: Ole Husgaard Date: Sun, 26 Sep 2021 18:31:39 +0200 Subject: [PATCH 5/8] Minor comment change to force CI tests run --- modules/setting/setting.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 8623e2e215b6a..6a34dde62ba4c 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -451,7 +451,7 @@ func getAppPath() (string, error) { // revisions come out the older revisions are eventually deleted, and // the hooks begin to fail. // The code below replaces the snap revision with "current", which - // always points to the current snap revision. + // is a link to the current snap revision. snap := os.Getenv("SNAP") if snap != "" && strings.HasPrefix(appPath, snap) { split := strings.Split(appPath, "/") From 35cad248c7bf4c06bef77d08c86ed0470a5df044 Mon Sep 17 00:00:00 2001 From: Ole Husgaard Date: Sun, 26 Sep 2021 21:03:37 +0200 Subject: [PATCH 6/8] Better checks and logging, test for failure case --- modules/setting/setting.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 6a34dde62ba4c..f15f3f7f352d4 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -456,12 +456,21 @@ func getAppPath() (string, error) { if snap != "" && strings.HasPrefix(appPath, snap) { split := strings.Split(appPath, "/") if len(split) >= 3 { + oldAppPath := appPath split[len(split)-2] = "current" - appPath = strings.Join(split, "/") + newAppPath := strings.Join(split, "-") + appPath, err = exec.LookPath(newAppPath) + if err != nil { + appPath = oldAppPath + log.error("Could not change AppPath from %s to %s while running in snap. Hooks for new repositories may fail after snap upgrades.", oldAppPath, newAppPath) + } else { + appPath = newAppPath + log.debug("Apppath changed from %s to %s because we are running in snap.", oldAppPath, newAppPath) + } } } - return appPath, err + return appPath, nil } func getWorkPath(appPath string) string { From bb4bb5156f760ae625ddc81ffa118956ed6f69da Mon Sep 17 00:00:00 2001 From: Ole Husgaard Date: Mon, 27 Sep 2021 14:08:27 +0200 Subject: [PATCH 7/8] Proper logging --- modules/setting/setting.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index f15f3f7f352d4..9673f7a3ed869 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -458,14 +458,14 @@ func getAppPath() (string, error) { if len(split) >= 3 { oldAppPath := appPath split[len(split)-2] = "current" - newAppPath := strings.Join(split, "-") + newAppPath := strings.Join(split, "/") appPath, err = exec.LookPath(newAppPath) if err != nil { appPath = oldAppPath - log.error("Could not change AppPath from %s to %s while running in snap. Hooks for new repositories may fail after snap upgrades.", oldAppPath, newAppPath) + log.Error("Could not change AppPath from %s to %s while running in snap. Hooks for new repositories may fail after snap upgrades.", oldAppPath, newAppPath) } else { appPath = newAppPath - log.debug("Apppath changed from %s to %s because we are running in snap.", oldAppPath, newAppPath) + log.Info("AppPath changed from %s to %s because we are running in snap.", oldAppPath, newAppPath) } } } From 2df0e4c73f5ea1a554bf059f3357dcc9cbe3171d Mon Sep 17 00:00:00 2001 From: Ole Husgaard Date: Mon, 27 Sep 2021 14:39:43 +0200 Subject: [PATCH 8/8] Use appPath returned from exec.LookPath() if ok --- modules/setting/setting.go | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 9673f7a3ed869..f1baa5a3344d5 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -464,7 +464,6 @@ func getAppPath() (string, error) { appPath = oldAppPath log.Error("Could not change AppPath from %s to %s while running in snap. Hooks for new repositories may fail after snap upgrades.", oldAppPath, newAppPath) } else { - appPath = newAppPath log.Info("AppPath changed from %s to %s because we are running in snap.", oldAppPath, newAppPath) } }