From 7e065c55ad7fe87f04e80d435dc840bca8f623e3 Mon Sep 17 00:00:00 2001 From: Li Linchao Date: Wed, 21 Apr 2021 11:38:40 +0800 Subject: [PATCH] fix root path start with dash '-' If root path start with dash '-' in a repo, like "-dir/subdie/readme.md" when run command 'git ls-tree', git will treat '-dir' as a positional arg To fix this, add './' as prefix of the path, like './-dir/subdir/' --- internal/gitaly/service/commit/last_commit_for_path.go | 4 +++- internal/gitaly/service/commit/list_last_commits_for_tree.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/gitaly/service/commit/last_commit_for_path.go b/internal/gitaly/service/commit/last_commit_for_path.go index 5c6a1cbce94..92800b41601 100644 --- a/internal/gitaly/service/commit/last_commit_for_path.go +++ b/internal/gitaly/service/commit/last_commit_for_path.go @@ -29,6 +29,8 @@ func (s *server) lastCommitForPath(ctx context.Context, in *gitalypb.LastCommitF path = "." } + safePath := "./" + path + repo := in.GetRepository() c, err := catfile.New(ctx, s.gitCmdFactory, repo) if err != nil { @@ -48,7 +50,7 @@ func (s *server) lastCommitForPath(ctx context.Context, in *gitalypb.LastCommitF options.LiteralPathspecs = true } - commit, err := log.LastCommitForPath(ctx, s.gitCmdFactory, c, repo, git.Revision(in.GetRevision()), path, options) + commit, err := log.LastCommitForPath(ctx, s.gitCmdFactory, c, repo, git.Revision(in.GetRevision()), safePath, options) if log.IsNotFound(err) { return &gitalypb.LastCommitForPathResponse{}, nil } diff --git a/internal/gitaly/service/commit/list_last_commits_for_tree.go b/internal/gitaly/service/commit/list_last_commits_for_tree.go index 7c4adc05971..d5b8d747cde 100644 --- a/internal/gitaly/service/commit/list_last_commits_for_tree.go +++ b/internal/gitaly/service/commit/list_last_commits_for_tree.go @@ -122,11 +122,13 @@ func (s *server) newLSTreeParser(in *gitalypb.ListLastCommitsForTreeRequest, str path = "." } + safePath := "./" + path + opts := git.ConvertGlobalOptions(in.GetGlobalOptions()) cmd, err := s.gitCmdFactory.New(stream.Context(), in.GetRepository(), git.SubCmd{ Name: "ls-tree", Flags: []git.Option{git.Flag{Name: "-z"}, git.Flag{Name: "--full-name"}}, - Args: []string{in.GetRevision(), path}, + Args: []string{in.GetRevision(), safePath}, }, opts...) if err != nil { return nil, nil, err -- GitLab