diff options
author | James Fargher <jfargher@gitlab.com> | 2023-04-18 02:12:30 +0300 |
---|---|---|
committer | James Fargher <jfargher@gitlab.com> | 2023-04-18 02:12:30 +0300 |
commit | ce081c57b4fedd63d27fffa2253dbd70636c2964 (patch) | |
tree | 283e2a87f4ad6f2d5620c702af51d59a113cccae | |
parent | 69399ae8a177876028e2f9f8c41d937ff85cf71e (diff) |
repository: Extract GetCustomHooks implementation to localrepolocalrepo_hooks
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.
-rw-r--r-- | internal/git/localrepo/hooks.go | 32 | ||||
-rw-r--r-- | internal/gitaly/service/repository/get_custom_hooks.go | 30 |
2 files changed, 34 insertions, 28 deletions
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 -} |