Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Fargher <jfargher@gitlab.com>2021-08-03 02:12:30 +0300
committerJames Fargher <jfargher@gitlab.com>2021-08-03 02:23:46 +0300
commit601e1bfbec8c93edf1fd16838c56846a0e4183b3 (patch)
tree43fe62217e5285d224dcfa3a30f8042365687187
parent1903b21fa10a4088383cace8c35152ff63251b4f (diff)
Refactor CalculateChecksum to use localrepoget_references_head
-rw-r--r--internal/gitaly/service/repository/calculate_checksum.go43
1 files changed, 16 insertions, 27 deletions
diff --git a/internal/gitaly/service/repository/calculate_checksum.go b/internal/gitaly/service/repository/calculate_checksum.go
index 7c2461313..5625d4feb 100644
--- a/internal/gitaly/service/repository/calculate_checksum.go
+++ b/internal/gitaly/service/repository/calculate_checksum.go
@@ -1,11 +1,11 @@
package repository
import (
- "bufio"
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
+ "fmt"
"math/big"
"regexp"
"strings"
@@ -19,35 +19,36 @@ import (
var refWhitelist = regexp.MustCompile(`HEAD|(refs/(heads|tags|keep-around|merge-requests|environments|notes)/)`)
func (s *server) CalculateChecksum(ctx context.Context, in *gitalypb.CalculateChecksumRequest) (*gitalypb.CalculateChecksumResponse, error) {
- repo := in.GetRepository()
+ repo := s.localrepo(in.GetRepository())
- repoPath, err := s.locator.GetRepoPath(repo)
- if err != nil {
- return nil, err
- }
+ refs, err := repo.GetReferences(ctx, true)
+ if len(refs) == 0 || err != nil {
+ if s.isValidRepo(ctx, in.GetRepository()) {
+ return &gitalypb.CalculateChecksumResponse{Checksum: git.ZeroOID.String()}, nil
+ }
- cmd, err := s.gitCmdFactory.New(ctx, repo, git.SubCmd{Name: "show-ref", Flags: []git.Option{git.Flag{Name: "--head"}}})
- if err != nil {
if _, ok := status.FromError(err); ok {
return nil, err
}
- return nil, status.Errorf(codes.Internal, "CalculateChecksum: gitCommand: %v", err)
+ repoPath, err := s.locator.GetRepoPath(repo)
+ if err != nil {
+ return nil, err
+ }
+
+ return nil, status.Errorf(codes.DataLoss, "CalculateChecksum: not a git repository '%s'", repoPath)
}
var checksum *big.Int
- scanner := bufio.NewScanner(cmd)
- for scanner.Scan() {
- ref := scanner.Bytes()
-
- if !refWhitelist.Match(ref) {
+ for _, ref := range refs {
+ if !refWhitelist.MatchString(ref.Name.String()) {
continue
}
h := sha1.New()
// hash.Hash will never return an error.
- _, _ = h.Write(ref)
+ _, _ = fmt.Fprintf(h, "%s %s", ref.Target, ref.Name)
hash := hex.EncodeToString(h.Sum(nil))
hashIntBase16, _ := (&big.Int{}).SetString(hash, 16)
@@ -59,18 +60,6 @@ func (s *server) CalculateChecksum(ctx context.Context, in *gitalypb.CalculateCh
}
}
- if err := scanner.Err(); err != nil {
- return nil, status.Errorf(codes.Internal, err.Error())
- }
-
- if err := cmd.Wait(); checksum == nil || err != nil {
- if s.isValidRepo(ctx, repo) {
- return &gitalypb.CalculateChecksumResponse{Checksum: git.ZeroOID.String()}, nil
- }
-
- return nil, status.Errorf(codes.DataLoss, "CalculateChecksum: not a git repository '%s'", repoPath)
- }
-
return &gitalypb.CalculateChecksumResponse{Checksum: hex.EncodeToString(checksum.Bytes())}, nil
}