diff options
author | Toon Claes <toon@gitlab.com> | 2021-04-28 19:42:26 +0300 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2021-04-28 19:42:26 +0300 |
commit | 16bab4e3edf16806b08a6f9ccd4c6aa0fe965cc5 (patch) | |
tree | 9dbb0ef03e0db147c157a521ed5fd17cdc281a39 | |
parent | c4862b6edf95f26d8e637663c92839b151d1ef9d (diff) |
Draft: Send commit message as part of commit difftc-commit-msg-as-diff
The make it possible to comment on the commit message this change sends
the commit message as a diff file together with all the other changes in
the commit.
-rw-r--r-- | internal/gitaly/service/blob/get_blobs.go | 29 | ||||
-rw-r--r-- | internal/gitaly/service/diff/commit.go | 23 | ||||
-rw-r--r-- | internal/gitaly/service/diff/numstat.go | 16 |
3 files changed, 68 insertions, 0 deletions
diff --git a/internal/gitaly/service/blob/get_blobs.go b/internal/gitaly/service/blob/get_blobs.go index 2dcf4122f..9229f384f 100644 --- a/internal/gitaly/service/blob/get_blobs.go +++ b/internal/gitaly/service/blob/get_blobs.go @@ -32,6 +32,35 @@ func sendGetBlobsResponse(req *gitalypb.GetBlobsRequest, stream gitalypb.BlobSer path = bytes.TrimRight(path, "/") } + if bytes.Equal(path, []byte("COMMIT_MSG")) { + object, _ := c.Commit(ctx, git.Revision(revisionPath.Revision)) + + response := &gitalypb.GetBlobsResponse{ + Revision: revision, + Oid: revision, + Path: path, + Size: object.ObjectInfo.Size, + Mode: 0100644, + } + + sw := streamio.NewWriter(func(p []byte) error { + msg := &gitalypb.GetBlobsResponse{} + if response != nil { + msg = response + response = nil + } + + msg.Data = bytes.SplitN(p, []byte("\n\n"), 2)[1] + msg.Size = int64(len(msg.Data)) + + return stream.Send(msg) + }) + + io.Copy(sw, object.Reader) + + continue + } + treeEntry, err := tef.FindByRevisionAndPath(ctx, revision, string(path)) if err != nil { return err diff --git a/internal/gitaly/service/diff/commit.go b/internal/gitaly/service/diff/commit.go index 7f940ef90..bf127fb89 100644 --- a/internal/gitaly/service/diff/commit.go +++ b/internal/gitaly/service/diff/commit.go @@ -1,12 +1,14 @@ package diff import ( + "bytes" "context" "fmt" "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus" log "github.com/sirupsen/logrus" "gitlab.com/gitlab-org/gitaly/internal/git" + "gitlab.com/gitlab-org/gitaly/internal/git/localrepo" "gitlab.com/gitlab-org/gitaly/internal/gitaly/diff" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" @@ -72,6 +74,27 @@ func (s *server) CommitDiff(in *gitalypb.CommitDiffRequest, stream gitalypb.Diff limits.SafeMaxLines = int(in.SafeMaxLines) limits.SafeMaxBytes = int(in.SafeMaxBytes) + { + repo := localrepo.New(s.gitCmdFactory, in.GetRepository(), s.cfg) + commit, err := repo.ReadCommit(stream.Context(), git.Revision(rightSha)) + + if err == nil { + patch := fmt.Sprintf("@@ -0,0 +1,%d @@\n%s", bytes.Count(commit.Body, []byte("\n")), commit.Body) + + response := &gitalypb.CommitDiffResponse{ + FromPath: []byte("COMMIT_MSG"), + ToPath: []byte("COMMIT_MSG"), + FromId: git.ZeroOID.String(), + ToId: rightSha, + OldMode: 0, + NewMode: 0100644, + RawPatchData: []byte(patch), + EndOfPatch: true, + } + stream.Send(response) + } + } + return s.eachDiff(stream.Context(), "CommitDiff", in.Repository, cmd, limits, func(diff *diff.Diff) error { response := &gitalypb.CommitDiffResponse{ FromPath: diff.FromPath, diff --git a/internal/gitaly/service/diff/numstat.go b/internal/gitaly/service/diff/numstat.go index 44c65bf9f..02ab319c0 100644 --- a/internal/gitaly/service/diff/numstat.go +++ b/internal/gitaly/service/diff/numstat.go @@ -1,9 +1,11 @@ package diff import ( + "bytes" "io" "gitlab.com/gitlab-org/gitaly/internal/git" + "gitlab.com/gitlab-org/gitaly/internal/git/localrepo" "gitlab.com/gitlab-org/gitaly/internal/gitaly/diff" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" @@ -35,6 +37,20 @@ func (s *server) DiffStats(in *gitalypb.DiffStatsRequest, stream gitalypb.DiffSe parser := diff.NewDiffNumStatParser(cmd) + { + repo := localrepo.New(s.gitCmdFactory, in.GetRepository(), s.cfg) + commit, err := repo.ReadCommit(stream.Context(), git.Revision(in.RightCommitId)) + if err == nil { + numStat := &gitalypb.DiffStats{ + Additions: int32(bytes.Count(commit.Body, []byte("\n"))), + Deletions: 0, + Path: []byte("COMMIT_MSG"), + OldPath: []byte("COMMIT_MSG"), + } + batch = append(batch, numStat) + } + } + for { stat, err := parser.NextNumStat() if err != nil { |