diff options
author | Stan Hu <stanhu@gmail.com> | 2020-02-23 08:17:29 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2020-02-23 08:20:29 +0300 |
commit | 3404e3b88c48bdbb2b65520251b06baf67086cdf (patch) | |
tree | 737a82a5bfcca6eb24aaf9b2010c8a2a8e8244c2 | |
parent | 52e06c2e6eb2d3ba6f4fdd5b48ce3a52043558a8 (diff) |
Gracefully handle diff paths with leading slashessh-fix-diffs-with-leading-slashes
If a user used the GitLab API to generate a discussion in a merge
request diff with a leading slash in a path, Gitaly would pass in the
path and cause Git to die. For example:
```
git diff HEAD^1 -- /filename
fatal: /filename: '/filename' is outside repository
```
We should just strip the leading slash to avoid this error.
Closes https://gitlab.com/gitlab-org/gitlab/issues/207586
4 files changed, 28 insertions, 1 deletions
diff --git a/changelogs/unreleased/sh-fix-diffs-with-leading-slashes.yml b/changelogs/unreleased/sh-fix-diffs-with-leading-slashes.yml new file mode 100644 index 000000000..4744da003 --- /dev/null +++ b/changelogs/unreleased/sh-fix-diffs-with-leading-slashes.yml @@ -0,0 +1,5 @@ +--- +title: Gracefully handle diff paths with leading slashes +merge_request: 1857 +author: +type: fixed diff --git a/internal/service/diff/commit.go b/internal/service/diff/commit.go index 7f46bdc9c..f62489254 100644 --- a/internal/service/diff/commit.go +++ b/internal/service/diff/commit.go @@ -3,6 +3,7 @@ package diff import ( "context" "fmt" + "strings" grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus" log "github.com/sirupsen/logrus" @@ -52,7 +53,8 @@ func (s *server) CommitDiff(in *gitalypb.CommitDiffRequest, stream gitalypb.Diff } if len(paths) > 0 { for _, path := range paths { - cmd.PostSepArgs = append(cmd.PostSepArgs, string(path)) + s := strings.TrimPrefix(string(path), "/") + cmd.PostSepArgs = append(cmd.PostSepArgs, s) } } diff --git a/internal/service/diff/commit_test.go b/internal/service/diff/commit_test.go index 89610cc1a..ded9a9e5a 100644 --- a/internal/service/diff/commit_test.go +++ b/internal/service/diff/commit_test.go @@ -197,6 +197,7 @@ func TestSuccessfulCommitDiffRequestWithPaths(t *testing.T) { IgnoreWhitespaceChange: false, Paths: [][]byte{ []byte("CONTRIBUTING.md"), + []byte("/MAINTENANCE.md"), []byte("README.md"), []byte("gitaly/named-file-with-mods"), []byte("gitaly/mode-file-with-mods"), @@ -222,6 +223,16 @@ func TestSuccessfulCommitDiffRequestWithPaths(t *testing.T) { Patch: testhelper.MustReadFile(t, "testdata/contributing-md-chunks.txt"), }, { + FromID: "95d9f0a5e7bb054e9dd3975589b8dfc689e20e88", + ToID: "5d9c7c0470bf368d61d9b6cd076300dc9d061f14", + OldMode: 0100644, + NewMode: 0100644, + FromPath: []byte("MAINTENANCE.md"), + ToPath: []byte("MAINTENANCE.md"), + Binary: false, + Patch: testhelper.MustReadFile(t, "testdata/maintenance-file-with-mods-chunks.txt"), + }, + { FromID: "faaf198af3a36dbf41961466703cc1d47c61d051", ToID: "877cee6ab11f9094e1bcdb7f1fd9c0001b572185", OldMode: 0100644, diff --git a/internal/service/diff/testdata/maintenance-file-with-mods-chunks.txt b/internal/service/diff/testdata/maintenance-file-with-mods-chunks.txt new file mode 100644 index 000000000..093586fb0 --- /dev/null +++ b/internal/service/diff/testdata/maintenance-file-with-mods-chunks.txt @@ -0,0 +1,9 @@ +@@ -16,7 +16,7 @@ GitLab follows the [Semantic Versioning](http://semver.org/) for its releases: + incorrect behavior. + + The current stable release will receive security patches and bug fixes +-(eg. `5.0` -> `5.0.1`). Feature releases will mark the next supported stable ++(eg. `5.0` -> `5.0.1`). Feature releases will mark the next supported stable + release where the minor version is increased numerically by increments of one + (eg. `5.0 -> 5.1`). + |