Skip to content

Add --work-path and --custom-path to git hooks #25121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions modules/repository/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,29 +78,45 @@ 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 {
hookNames = append(hookNames, "proc-receive")
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, "")
}

Expand Down
44 changes: 44 additions & 0 deletions modules/repository/hooks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

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)
}
}
}