Skip to content

Commit 6666c06

Browse files
committed
Split delegate hooks as two functions for repo and wiki
1 parent 6881151 commit 6666c06

File tree

8 files changed

+32
-26
lines changed

8 files changed

+32
-26
lines changed

modules/gitrepo/hooks.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,18 @@ done
106106
return hookNames, hookTpls, giteaHookTpls
107107
}
108108

109-
// CreateDelegateHooks creates all the hooks scripts for the repo
110-
func CreateDelegateHooks(ctx context.Context, repo Repository, isWiki bool) (err error) {
109+
// CreateDelegateHooksForRepo creates all the hooks scripts for the repo
110+
func CreateDelegateHooksForRepo(_ context.Context, repo Repository) (err error) {
111+
return createDelegateHooks(filepath.Join(repoPath(repo), "hooks"))
112+
}
113+
114+
// CreateDelegateHooksForWiki creates all the hooks scripts for the wiki repo
115+
func CreateDelegateHooksForWiki(_ context.Context, repo Repository) (err error) {
116+
return createDelegateHooks(filepath.Join(wikiPath(repo), "hooks"))
117+
}
118+
119+
func createDelegateHooks(hookDir string) (err error) {
111120
hookNames, hookTpls, giteaHookTpls := getHookTemplates()
112-
var hookDir string
113-
if isWiki {
114-
hookDir = filepath.Join(wikiPath(repo), "hooks")
115-
} else {
116-
hookDir = filepath.Join(repoPath(repo), "hooks")
117-
}
118121

119122
for i, hookName := range hookNames {
120123
oldHookPath := filepath.Join(hookDir, hookName)
@@ -175,16 +178,19 @@ func ensureExecutable(filename string) error {
175178
return os.Chmod(filename, mode)
176179
}
177180

178-
// CheckDelegateHooks checks the hooks scripts for the repo
179-
func CheckDelegateHooks(ctx context.Context, repo Repository, isWiki bool) ([]string, error) {
181+
// CheckDelegateHooksForRepo checks the hooks scripts for the repo
182+
func CheckDelegateHooksForRepo(_ context.Context, repo Repository) ([]string, error) {
183+
return checkDelegateHooks(filepath.Join(repoPath(repo), "hooks"))
184+
}
185+
186+
// CheckDelegateHooksForWiki checks the hooks scripts for the repo
187+
func CheckDelegateHooksForWiki(_ context.Context, repo Repository) ([]string, error) {
188+
return checkDelegateHooks(filepath.Join(wikiPath(repo), "hooks"))
189+
}
190+
191+
func checkDelegateHooks(hookDir string) ([]string, error) {
180192
hookNames, hookTpls, giteaHookTpls := getHookTemplates()
181193

182-
var hookDir string
183-
if isWiki {
184-
hookDir = filepath.Join(wikiPath(repo), "hooks")
185-
} else {
186-
hookDir = filepath.Join(repoPath(repo), "hooks")
187-
}
188194
results := make([]string, 0, 10)
189195

190196
for i, hookName := range hookNames {

modules/repository/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func CheckInitRepository(ctx context.Context, repo *repo_model.Repository) (err
138138
// Init git bare new repository.
139139
if err = git.InitRepository(ctx, repo.RepoPath(), true, repo.ObjectFormatName); err != nil {
140140
return fmt.Errorf("git.InitRepository: %w", err)
141-
} else if err = gitrepo.CreateDelegateHooks(ctx, repo, false); err != nil {
141+
} else if err = gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
142142
return fmt.Errorf("createDelegateHooks: %w", err)
143143
}
144144
return nil

services/doctor/misc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ func checkScriptType(ctx context.Context, logger log.Logger, autofix bool) error
4949

5050
func checkHooks(ctx context.Context, logger log.Logger, autofix bool) error {
5151
if err := iterateRepositories(ctx, func(repo *repo_model.Repository) error {
52-
results, err := gitrepo.CheckDelegateHooks(ctx, repo, false)
52+
results, err := gitrepo.CheckDelegateHooksForRepo(ctx, repo)
5353
if err != nil {
5454
logger.Critical("Unable to check delegate hooks for repo %-v. ERROR: %v", repo, err)
5555
return fmt.Errorf("Unable to check delegate hooks for repo %-v. ERROR: %w", repo, err)
5656
}
5757
if len(results) > 0 && autofix {
5858
logger.Warn("Regenerated hooks for %s", repo.FullName())
59-
if err := gitrepo.CreateDelegateHooks(ctx, repo, false); err != nil {
59+
if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
6060
logger.Critical("Unable to recreate delegate hooks for %-v. ERROR: %v", repo, err)
6161
return fmt.Errorf("Unable to recreate delegate hooks for %-v. ERROR: %w", repo, err)
6262
}

services/repository/adopt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func adoptRepository(ctx context.Context, repo *repo_model.Repository, defaultBr
115115
return fmt.Errorf("adoptRepository: path does not already exist: %s", repo.FullName())
116116
}
117117

118-
if err := gitrepo.CreateDelegateHooks(ctx, repo, false); err != nil {
118+
if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
119119
return fmt.Errorf("createDelegateHooks: %w", err)
120120
}
121121

services/repository/fork.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
170170
return fmt.Errorf("git update-server-info: %w", err)
171171
}
172172

173-
if err = gitrepo.CreateDelegateHooks(ctx, repo, false); err != nil {
173+
if err = gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
174174
return fmt.Errorf("createDelegateHooks: %w", err)
175175
}
176176

services/repository/hooks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func SyncRepositoryHooks(ctx context.Context) error {
3131
default:
3232
}
3333

34-
if err := gitrepo.CreateDelegateHooks(ctx, repo, false); err != nil {
34+
if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
3535
return fmt.Errorf("SyncRepositoryHook: %w", err)
3636
}
3737
if repo.HasWiki() {
38-
if err := gitrepo.CreateDelegateHooks(ctx, repo, true); err != nil {
38+
if err := gitrepo.CreateDelegateHooksForWiki(ctx, repo); err != nil {
3939
return fmt.Errorf("SyncRepositoryHook: %w", err)
4040
}
4141
}

services/repository/migrate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,11 @@ func cleanUpMigrateGitConfig(ctx context.Context, repoPath string) error {
265265

266266
// CleanUpMigrateInfo finishes migrating repository and/or wiki with things that don't need to be done for mirrors.
267267
func CleanUpMigrateInfo(ctx context.Context, repo *repo_model.Repository) (*repo_model.Repository, error) {
268-
if err := gitrepo.CreateDelegateHooks(ctx, repo, false); err != nil {
268+
if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
269269
return repo, fmt.Errorf("createDelegateHooks: %w", err)
270270
}
271271
if repo.HasWiki() {
272-
if err := gitrepo.CreateDelegateHooks(ctx, repo, true); err != nil {
272+
if err := gitrepo.CreateDelegateHooksForWiki(ctx, repo); err != nil {
273273
return repo, fmt.Errorf("createDelegateHooks.(wiki): %w", err)
274274
}
275275
}

services/wiki/wiki.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error {
4141

4242
if err := git.InitRepository(ctx, repo.WikiPath(), true, repo.ObjectFormatName); err != nil {
4343
return fmt.Errorf("InitRepository: %w", err)
44-
} else if err = gitrepo.CreateDelegateHooks(ctx, repo, true); err != nil {
44+
} else if err = gitrepo.CreateDelegateHooksForWiki(ctx, repo); err != nil {
4545
return fmt.Errorf("createDelegateHooks: %w", err)
4646
} else if _, _, err = git.NewCommand("symbolic-ref", "HEAD").AddDynamicArguments(git.BranchPrefix+repo.DefaultWikiBranch).RunStdString(ctx, &git.RunOpts{Dir: repo.WikiPath()}); err != nil {
4747
return fmt.Errorf("unable to set default wiki branch to %q: %w", repo.DefaultWikiBranch, err)

0 commit comments

Comments
 (0)