History

Refactor git command arguments and make all arguments to be safe to be used (#21535)

Follow #21464 Make all git command arguments strictly safe. Most changes are one-to-one replacing, keep all existing logic.

wxiaoguang committed 4 years ago

Commit dcd9fc71 parent 4eeea7b71 files · +424 −390Browse files

Showing the changes to services/gitdiff/gitdiff.go (1 of 71 files). Show all

Unified · Split

services/gitdiff/gitdiff.go+15−13
@@ -1056,7 +1056,7 @@ type DiffOptions struct { · 1055 unmodified lines
10561056 MaxLines int
10571057 MaxLineCharacters int
10581058 MaxFiles int
1059− WhitespaceBehavior string
1059+ WhitespaceBehavior git.CmdArg
10601060 DirectComparison bool
10611061 }
10621062
@@ -1082,7 +1082,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 19 unmodified lines
10821082 argsLength += len(files) + 1
10831083 }
10841084
1085− diffArgs := make([]string, 0, argsLength)
1085+ diffArgs := make([]git.CmdArg, 0, argsLength)
10861086 if (len(opts.BeforeCommitID) == 0 || opts.BeforeCommitID == git.EmptySHA) && commit.ParentCount() == 0 {
10871087 diffArgs = append(diffArgs, "diff", "--src-prefix=\\a/", "--dst-prefix=\\b/", "-M")
10881088 if len(opts.WhitespaceBehavior) != 0 {
@@ -1090,7 +1090,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 1 unmodified line
10901090 }
10911091 // append empty tree ref
10921092 diffArgs = append(diffArgs, "4b825dc642cb6eb9a060e54bf8d69288fbee4904")
1093− diffArgs = append(diffArgs, opts.AfterCommitID)
1093+ diffArgs = append(diffArgs, git.CmdArgCheck(opts.AfterCommitID))
10941094 } else {
10951095 actualBeforeCommitID := opts.BeforeCommitID
10961096 if len(actualBeforeCommitID) == 0 {
@@ -1101,8 +1101,8 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 4 unmodified lines
11011101 if len(opts.WhitespaceBehavior) != 0 {
11021102 diffArgs = append(diffArgs, opts.WhitespaceBehavior)
11031103 }
1104− diffArgs = append(diffArgs, actualBeforeCommitID)
1105− diffArgs = append(diffArgs, opts.AfterCommitID)
1104+ diffArgs = append(diffArgs, git.CmdArgCheck(actualBeforeCommitID))
1105+ diffArgs = append(diffArgs, git.CmdArgCheck(opts.AfterCommitID))
11061106 opts.BeforeCommitID = actualBeforeCommitID
11071107 }
11081108
@@ -1111,13 +1111,15 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 2 unmodified lines
11111111 // the skipping for us
11121112 parsePatchSkipToFile := opts.SkipTo
11131113 if opts.SkipTo != "" && git.CheckGitVersionAtLeast("2.31") == nil {
1114− diffArgs = append(diffArgs, "--skip-to="+opts.SkipTo)
1114+ diffArgs = append(diffArgs, git.CmdArg("--skip-to="+opts.SkipTo))
11151115 parsePatchSkipToFile = ""
11161116 }
11171117
11181118 if len(files) > 0 {
11191119 diffArgs = append(diffArgs, "--")
1120− diffArgs = append(diffArgs, files...)
1120+ for _, file := range files {
1121+ diffArgs = append(diffArgs, git.CmdArg(file)) // it's safe to cast it to CmdArg because there is a "--" before
1122+ }
11211123 }
11221124
11231125 reader, writer := io.Pipe()
@@ -1126,7 +1128,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 2 unmodified lines
11261128 _ = writer.Close()
11271129 }()
11281130
1129− go func(ctx context.Context, diffArgs []string, repoPath string, writer *io.PipeWriter) {
1131+ go func(ctx context.Context, diffArgs []git.CmdArg, repoPath string, writer *io.PipeWriter) {
11301132 cmd := git.NewCommand(ctx, diffArgs...)
11311133 cmd.SetDescription(fmt.Sprintf("GetDiffRange [repo_path: %s]", repoPath))
11321134 if err := cmd.Run(&git.RunOpts{
@@ -1199,15 +1201,15 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 66 unmodified lines
11991201 separator = ".."
12001202 }
12011203
1202− shortstatArgs := []string{opts.BeforeCommitID + separator + opts.AfterCommitID}
1204+ shortstatArgs := []git.CmdArg{git.CmdArgCheck(opts.BeforeCommitID + separator + opts.AfterCommitID)}
12031205 if len(opts.BeforeCommitID) == 0 || opts.BeforeCommitID == git.EmptySHA {
1204− shortstatArgs = []string{git.EmptyTreeSHA, opts.AfterCommitID}
1206+ shortstatArgs = []git.CmdArg{git.EmptyTreeSHA, git.CmdArgCheck(opts.AfterCommitID)}
12051207 }
12061208 diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(gitRepo.Ctx, repoPath, shortstatArgs...)
12071209 if err != nil && strings.Contains(err.Error(), "no merge base") {
12081210 // git >= 2.28 now returns an error if base and head have become unrelated.
12091211 // previously it would return the results of git diff --shortstat base head so let's try that...
1210− shortstatArgs = []string{opts.BeforeCommitID, opts.AfterCommitID}
1212+ shortstatArgs = []git.CmdArg{git.CmdArgCheck(opts.BeforeCommitID), git.CmdArgCheck(opts.AfterCommitID)}
12111213 diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(gitRepo.Ctx, repoPath, shortstatArgs...)
12121214 }
12131215 if err != nil {
@@ -1322,7 +1324,7 @@ func CommentMustAsDiff(c *issues_model.Comment) *Diff { · 108 unmodified lines
13221324 }
13231325
13241326 // GetWhitespaceFlag returns git diff flag for treating whitespaces
1325−func GetWhitespaceFlag(whitespaceBehavior string) string {
1327+func GetWhitespaceFlag(whitespaceBehavior string) git.CmdArg {
13261328 whitespaceFlags := map[string]string{
13271329 "ignore-all": "-w",
13281330 "ignore-change": "-b",
@@ -1331,7 +1333,7 @@ func GetWhitespaceFlag(whitespaceBehavior string) string { · 2 unmodified lines
13311333 }
13321334
13331335 if flag, ok := whitespaceFlags[whitespaceBehavior]; ok {
1334− return flag
1336+ return git.CmdArg(flag)
13351337 }
13361338 log.Warn("unknown whitespace behavior: %q, default to 'show-all'", whitespaceBehavior)
13371339 return ""