From ce081c57b4fedd63d27fffa2253dbd70636c2964 Mon Sep 17 00:00:00 2001 From: James Fargher Date: Tue, 18 Apr 2023 11:12:30 +1200 Subject: repository: Extract GetCustomHooks implementation to localrepo Soon we will be looking to implement server-side backups. To do this, all the RPCs used for creating a backup will need to be converted to localrepo. --- internal/git/localrepo/hooks.go | 32 ++++++++++++++++++++++ .../gitaly/service/repository/get_custom_hooks.go | 30 ++------------------ 2 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 internal/git/localrepo/hooks.go diff --git a/internal/git/localrepo/hooks.go b/internal/git/localrepo/hooks.go new file mode 100644 index 000000000..9c32070ee --- /dev/null +++ b/internal/git/localrepo/hooks.go @@ -0,0 +1,32 @@ +package localrepo + +import ( + "context" + "fmt" + "io" + "os" + "path/filepath" + + "gitlab.com/gitlab-org/gitaly/v15/internal/archive" + "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/hook" + "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" +) + +// GetCustomHooks writes the git hooks for a repository to out. The hooks are +// written in a tar archive containing a `custom_hooks` directory. If no hooks +// are present in the repository, the response will have no data. +func (repo *Repo) GetCustomHooks(ctx context.Context, out io.Writer) error { + repoPath, err := repo.locator.GetRepoPath(repo) + if err != nil { + return fmt.Errorf("getting repo path: %w", err) + } + + if _, err := os.Lstat(filepath.Join(repoPath, hook.CustomHooksDir)); os.IsNotExist(err) { + return nil + } + + if err := archive.WriteTarball(ctx, out, repoPath, hook.CustomHooksDir); err != nil { + return structerr.NewInternal("archiving hooks: %w", err) + } + return nil +} diff --git a/internal/gitaly/service/repository/get_custom_hooks.go b/internal/gitaly/service/repository/get_custom_hooks.go index 69c1e0749..280426e2f 100644 --- a/internal/gitaly/service/repository/get_custom_hooks.go +++ b/internal/gitaly/service/repository/get_custom_hooks.go @@ -1,15 +1,6 @@ package repository import ( - "context" - "fmt" - "io" - "os" - "path/filepath" - - "gitlab.com/gitlab-org/gitaly/v15/internal/archive" - "gitlab.com/gitlab-org/gitaly/v15/internal/git/repository" - "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/hook" "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/v15/internal/structerr" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" @@ -30,7 +21,7 @@ func (s *server) GetCustomHooks(in *gitalypb.GetCustomHooksRequest, stream gital return stream.Send(&gitalypb.GetCustomHooksResponse{Data: p}) }) - if err := s.getCustomHooks(ctx, writer, in.Repository); err != nil { + if err := s.localrepo(in.GetRepository()).GetCustomHooks(ctx, writer); err != nil { return structerr.NewInternal("reading custom hooks: %w", err) } @@ -51,26 +42,9 @@ func (s *server) BackupCustomHooks(in *gitalypb.BackupCustomHooksRequest, stream return stream.Send(&gitalypb.BackupCustomHooksResponse{Data: p}) }) - if err := s.getCustomHooks(ctx, writer, in.Repository); err != nil { + if err := s.localrepo(in.GetRepository()).GetCustomHooks(ctx, writer); err != nil { return structerr.NewInternal("reading custom hooks: %w", err) } return nil } - -func (s *server) getCustomHooks(ctx context.Context, writer io.Writer, repo repository.GitRepo) error { - repoPath, err := s.locator.GetRepoPath(repo) - if err != nil { - return fmt.Errorf("getting repo path: %w", err) - } - - if _, err := os.Lstat(filepath.Join(repoPath, hook.CustomHooksDir)); os.IsNotExist(err) { - return nil - } - - if err := archive.WriteTarball(ctx, writer, repoPath, hook.CustomHooksDir); err != nil { - return structerr.NewInternal("archiving hooks: %w", err) - } - - return nil -} -- cgit v1.2.3