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
1056 MaxLines int1056 MaxLines int
1057 MaxLineCharacters int1057 MaxLineCharacters int
1058 MaxFiles int1058 MaxFiles int
1059 WhitespaceBehavior string1059 WhitespaceBehavior git.CmdArg
1060 DirectComparison bool1060 DirectComparison bool
1061}1061}
1062 1062
@@ -1082,7 +1082,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 19 unmodified lines
1082 argsLength += len(files) + 11082 argsLength += len(files) + 1
1083 }1083 }
1084 1084
1085 diffArgs := make([]string, 0, argsLength)1085 diffArgs := make([]git.CmdArg, 0, argsLength)
1086 if (len(opts.BeforeCommitID) == 0 || opts.BeforeCommitID == git.EmptySHA) && commit.ParentCount() == 0 {1086 if (len(opts.BeforeCommitID) == 0 || opts.BeforeCommitID == git.EmptySHA) && commit.ParentCount() == 0 {
1087 diffArgs = append(diffArgs, "diff", "--src-prefix=\\a/", "--dst-prefix=\\b/", "-M")1087 diffArgs = append(diffArgs, "diff", "--src-prefix=\\a/", "--dst-prefix=\\b/", "-M")
1088 if len(opts.WhitespaceBehavior) != 0 {1088 if len(opts.WhitespaceBehavior) != 0 {
@@ -1090,7 +1090,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 1 unmodified line
1090 }1090 }
1091 // append empty tree ref1091 // append empty tree ref
1092 diffArgs = append(diffArgs, "4b825dc642cb6eb9a060e54bf8d69288fbee4904")1092 diffArgs = append(diffArgs, "4b825dc642cb6eb9a060e54bf8d69288fbee4904")
1093 diffArgs = append(diffArgs, opts.AfterCommitID)1093 diffArgs = append(diffArgs, git.CmdArgCheck(opts.AfterCommitID))
1094 } else {1094 } else {
1095 actualBeforeCommitID := opts.BeforeCommitID1095 actualBeforeCommitID := opts.BeforeCommitID
1096 if len(actualBeforeCommitID) == 0 {1096 if len(actualBeforeCommitID) == 0 {
@@ -1101,8 +1101,8 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 4 unmodified lines
1101 if len(opts.WhitespaceBehavior) != 0 {1101 if len(opts.WhitespaceBehavior) != 0 {
1102 diffArgs = append(diffArgs, opts.WhitespaceBehavior)1102 diffArgs = append(diffArgs, opts.WhitespaceBehavior)
1103 }1103 }
1104 diffArgs = append(diffArgs, actualBeforeCommitID)1104 diffArgs = append(diffArgs, git.CmdArgCheck(actualBeforeCommitID))
1105 diffArgs = append(diffArgs, opts.AfterCommitID)1105 diffArgs = append(diffArgs, git.CmdArgCheck(opts.AfterCommitID))
1106 opts.BeforeCommitID = actualBeforeCommitID1106 opts.BeforeCommitID = actualBeforeCommitID
1107 }1107 }
1108 1108
@@ -1111,13 +1111,15 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 2 unmodified lines
1111 // the skipping for us1111 // the skipping for us
1112 parsePatchSkipToFile := opts.SkipTo1112 parsePatchSkipToFile := opts.SkipTo
1113 if opts.SkipTo != "" && git.CheckGitVersionAtLeast("2.31") == nil {1113 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))
1115 parsePatchSkipToFile = ""1115 parsePatchSkipToFile = ""
1116 }1116 }
1117 1117
1118 if len(files) > 0 {1118 if len(files) > 0 {
1119 diffArgs = append(diffArgs, "--")1119 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 }
1121 }1123 }
1122 1124
1123 reader, writer := io.Pipe()1125 reader, writer := io.Pipe()
@@ -1126,7 +1128,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 2 unmodified lines
1126 _ = writer.Close()1128 _ = writer.Close()
1127 }()1129 }()
1128 1130
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) {
1130 cmd := git.NewCommand(ctx, diffArgs...)1132 cmd := git.NewCommand(ctx, diffArgs...)
1131 cmd.SetDescription(fmt.Sprintf("GetDiffRange [repo_path: %s]", repoPath))1133 cmd.SetDescription(fmt.Sprintf("GetDiffRange [repo_path: %s]", repoPath))
1132 if err := cmd.Run(&git.RunOpts{1134 if err := cmd.Run(&git.RunOpts{
@@ -1199,15 +1201,15 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 66 unmodified lines
1199 separator = ".."1201 separator = ".."
1200 }1202 }
1201 1203
1202 shortstatArgs := []string{opts.BeforeCommitID + separator + opts.AfterCommitID}1204 shortstatArgs := []git.CmdArg{git.CmdArgCheck(opts.BeforeCommitID + separator + opts.AfterCommitID)}
1203 if len(opts.BeforeCommitID) == 0 || opts.BeforeCommitID == git.EmptySHA {1205 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)}
1205 }1207 }
1206 diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(gitRepo.Ctx, repoPath, shortstatArgs...)1208 diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(gitRepo.Ctx, repoPath, shortstatArgs...)
1207 if err != nil && strings.Contains(err.Error(), "no merge base") {1209 if err != nil && strings.Contains(err.Error(), "no merge base") {
1208 // git >= 2.28 now returns an error if base and head have become unrelated.1210 // git >= 2.28 now returns an error if base and head have become unrelated.
1209 // previously it would return the results of git diff --shortstat base head so let's try that...1211 // 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)}
1211 diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(gitRepo.Ctx, repoPath, shortstatArgs...)1213 diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(gitRepo.Ctx, repoPath, shortstatArgs...)
1212 }1214 }
1213 if err != nil {1215 if err != nil {
@@ -1322,7 +1324,7 @@ func CommentMustAsDiff(c *issues_model.Comment) *Diff { · 108 unmodified lines
1322}1324}
1323 1325
1324// GetWhitespaceFlag returns git diff flag for treating whitespaces1326// GetWhitespaceFlag returns git diff flag for treating whitespaces
1325func GetWhitespaceFlag(whitespaceBehavior string) string {1327func GetWhitespaceFlag(whitespaceBehavior string) git.CmdArg {
1326 whitespaceFlags := map[string]string{1328 whitespaceFlags := map[string]string{
1327 "ignore-all": "-w",1329 "ignore-all": "-w",
1328 "ignore-change": "-b",1330 "ignore-change": "-b",
@@ -1331,7 +1333,7 @@ func GetWhitespaceFlag(whitespaceBehavior string) string { · 2 unmodified lines
1331 }1333 }
1332 1334
1333 if flag, ok := whitespaceFlags[whitespaceBehavior]; ok {1335 if flag, ok := whitespaceFlags[whitespaceBehavior]; ok {
1334 return flag1336 return git.CmdArg(flag)
1335 }1337 }
1336 log.Warn("unknown whitespace behavior: %q, default to 'show-all'", whitespaceBehavior)1338 log.Warn("unknown whitespace behavior: %q, default to 'show-all'", whitespaceBehavior)
1337 return ""1339 return ""