From 677c7f775c84bb91f512ef9eb39b8848dc704b15 Mon Sep 17 00:00:00 2001 From: David Gregory <2992938+DavidGregory084@users.noreply.github.com> Date: Wed, 7 Jun 2023 15:25:57 +0100 Subject: [PATCH 1/3] Add `--work-path` and `--custom-path` to git hooks Fixes #25107. --- modules/repository/hooks.go | 32 ++++++++++++++++++------- modules/repository/hooks_test.go | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 modules/repository/hooks_test.go diff --git a/modules/repository/hooks.go b/modules/repository/hooks.go index a95b9c2e99463..5811383730f3d 100644 --- a/modules/repository/hooks.go +++ b/modules/repository/hooks.go @@ -78,20 +78,32 @@ done // for pre-receive fmt.Sprintf(`#!/usr/bin/env %s # AUTO GENERATED BY GITEA, DO NOT MODIFY -%s hook --config=%s pre-receive -`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)), +%s hook --config=%s --work-path=%s --custom-path=%s pre-receive +`, setting.ScriptType, + util.ShellEscape(setting.AppPath), + util.ShellEscape(setting.CustomConf), + util.ShellEscape(setting.AppWorkPath), + util.ShellEscape(setting.CustomPath)), // for update fmt.Sprintf(`#!/usr/bin/env %s # AUTO GENERATED BY GITEA, DO NOT MODIFY -%s hook --config=%s update $1 $2 $3 -`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)), +%s hook --config=%s --work-path=%s --custom-path=%s update $1 $2 $3 +`, setting.ScriptType, + util.ShellEscape(setting.AppPath), + util.ShellEscape(setting.CustomConf), + util.ShellEscape(setting.AppWorkPath), + util.ShellEscape(setting.CustomPath)), // for post-receive fmt.Sprintf(`#!/usr/bin/env %s # AUTO GENERATED BY GITEA, DO NOT MODIFY -%s hook --config=%s post-receive -`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)), +%s hook --config=%s --work-path=%s --custom-path=%s post-receive +`, setting.ScriptType, + util.ShellEscape(setting.AppPath), + util.ShellEscape(setting.CustomConf), + util.ShellEscape(setting.AppWorkPath), + util.ShellEscape(setting.CustomPath)), } if git.SupportProcReceive { @@ -99,8 +111,12 @@ done hookTpls = append(hookTpls, fmt.Sprintf(`#!/usr/bin/env %s # AUTO GENERATED BY GITEA, DO NOT MODIFY -%s hook --config=%s proc-receive -`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf))) +%s hook --config=%s --work-path=%s --custom-path=%s proc-receive +`, setting.ScriptType, + util.ShellEscape(setting.AppPath), + util.ShellEscape(setting.CustomConf), + util.ShellEscape(setting.AppWorkPath), + util.ShellEscape(setting.CustomPath))) giteaHookTpls = append(giteaHookTpls, "") } diff --git a/modules/repository/hooks_test.go b/modules/repository/hooks_test.go new file mode 100644 index 0000000000000..df37c3894b230 --- /dev/null +++ b/modules/repository/hooks_test.go @@ -0,0 +1,40 @@ +package repository + +import ( + "testing" + + "code.gitea.io/gitea/modules/setting" + "github.com/stretchr/testify/assert" +) + +func TestGiteaHookTemplates(t *testing.T) { + oldAppPath := setting.AppPath + oldCustomConf := setting.CustomConf + oldWorkPath := setting.AppWorkPath + oldCustomPath := setting.CustomPath + + setting.AppPath = "/snap/gitea/1234/gitea" + setting.CustomConf = "/some/custom/app.ini" + setting.AppWorkPath = "/some/path/gitea" + setting.CustomPath = "/some/other/custom" + + defer func() { + setting.AppPath = oldAppPath + setting.CustomConf = oldCustomConf + setting.AppWorkPath = oldWorkPath + setting.CustomPath = oldCustomPath + }() + + hookNames, _, giteaHookTpls := getHookTemplates() + + for i, hookName := range hookNames { + giteaHookTpl := giteaHookTpls[i] + if giteaHookTpl != "" { + assert.Contains(t, giteaHookTpl, "/snap/gitea/1234/gitea hook") + assert.Contains(t, giteaHookTpl, "--config=/some/custom/app.ini") + assert.Contains(t, giteaHookTpl, "--work-path=/some/path/gitea") + assert.Contains(t, giteaHookTpl, "--custom-path=/some/other/custom") + assert.Contains(t, giteaHookTpl, hookName) + } + } +} From 84f675755e946c3854dd846488fb716b65e76b93 Mon Sep 17 00:00:00 2001 From: David Gregory <2992938+DavidGregory084@users.noreply.github.com> Date: Wed, 7 Jun 2023 16:02:05 +0100 Subject: [PATCH 2/3] Add license header to repository hook templates test --- modules/repository/hooks_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/repository/hooks_test.go b/modules/repository/hooks_test.go index df37c3894b230..2118037d56da4 100644 --- a/modules/repository/hooks_test.go +++ b/modules/repository/hooks_test.go @@ -1,3 +1,6 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + package repository import ( From 28d8fda5eeb86e0e49c62efc8747816990fe6728 Mon Sep 17 00:00:00 2001 From: David Gregory <2992938+DavidGregory084@users.noreply.github.com> Date: Wed, 7 Jun 2023 16:25:19 +0100 Subject: [PATCH 3/3] Appease the formatter --- modules/repository/hooks_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/repository/hooks_test.go b/modules/repository/hooks_test.go index 2118037d56da4..cdf9fd8ef8284 100644 --- a/modules/repository/hooks_test.go +++ b/modules/repository/hooks_test.go @@ -7,6 +7,7 @@ import ( "testing" "code.gitea.io/gitea/modules/setting" + "github.com/stretchr/testify/assert" )