History

Repare and Improve GetDiffRangeWithWhitespaceBehavior (#16894)

* repare and improve GetDiffRangeWithWhitespaceBehavior * Context with Timeout

6543 committed 5 years ago

Commit bb4cc871 parent f2b4b0f5 files · +26 −37Browse files

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

Unified · Split

services/gitdiff/gitdiff.go+7−22
@@ -20,6 +20,7 @@ import ( · 19 unmodified lines
20 "regexp"20 "regexp"
21 "sort"21 "sort"
22 "strings"22 "strings"
23 "time"
23 24
24 "code.gitea.io/gitea/models"25 "code.gitea.io/gitea/models"
25 "code.gitea.io/gitea/modules/charset"26 "code.gitea.io/gitea/modules/charset"
@@ -1205,31 +1206,20 @@ func readFileName(rd *strings.Reader) (string, bool) { · 1179 unmodified lines
1205 return name[2:], ambiguity1206 return name[2:], ambiguity
1206}1207}
1207 1208
1208// GetDiffRange builds a Diff between two commits of a repository.
1209// passing the empty string as beforeCommitID returns a diff from the
1210// parent commit.
1211func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
1212 return GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID, maxLines, maxLineCharacters, maxFiles, "")
1213}
1214
1215// GetDiffRangeWithWhitespaceBehavior builds a Diff between two commits of a repository.1209// GetDiffRangeWithWhitespaceBehavior builds a Diff between two commits of a repository.
1216// Passing the empty string as beforeCommitID returns a diff from the parent commit.1210// Passing the empty string as beforeCommitID returns a diff from the parent commit.
1217// The whitespaceBehavior is either an empty string or a git flag1211// The whitespaceBehavior is either an empty string or a git flag
1218func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {1212func GetDiffRangeWithWhitespaceBehavior(gitRepo *git.Repository, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
1219 gitRepo, err := git.OpenRepository(repoPath)1213 repoPath := gitRepo.Path
1220 if err != nil {
1221 return nil, err
1222 }
1223 defer gitRepo.Close()
1224 1214
1225 commit, err := gitRepo.GetCommit(afterCommitID)1215 commit, err := gitRepo.GetCommit(afterCommitID)
1226 if err != nil {1216 if err != nil {
1227 return nil, err1217 return nil, err
1228 }1218 }
1229 1219
1230 // FIXME: graceful: These commands should likely have a timeout1220 ctx, cancel := context.WithTimeout(git.DefaultContext, time.Duration(setting.Git.Timeout.Default)*time.Second)
1231 ctx, cancel := context.WithCancel(git.DefaultContext)
1232 defer cancel()1221 defer cancel()
1222
1233 var cmd *exec.Cmd1223 var cmd *exec.Cmd
1234 if (len(beforeCommitID) == 0 || beforeCommitID == git.EmptySHA) && commit.ParentCount() == 0 {1224 if (len(beforeCommitID) == 0 || beforeCommitID == git.EmptySHA) && commit.ParentCount() == 0 {
1235 diffArgs := []string{"diff", "--src-prefix=\\a/", "--dst-prefix=\\b/", "-M"}1225 diffArgs := []string{"diff", "--src-prefix=\\a/", "--dst-prefix=\\b/", "-M"}
@@ -1303,15 +1293,10 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID · 67 unmodified lines
1303 return diff, nil1293 return diff, nil
1304}1294}
1305 1295
1306// GetDiffCommit builds a Diff representing the given commitID.
1307func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
1308 return GetDiffRangeWithWhitespaceBehavior(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles, "")
1309}
1310
1311// GetDiffCommitWithWhitespaceBehavior builds a Diff representing the given commitID.1296// GetDiffCommitWithWhitespaceBehavior builds a Diff representing the given commitID.
1312// The whitespaceBehavior is either an empty string or a git flag1297// The whitespaceBehavior is either an empty string or a git flag
1313func GetDiffCommitWithWhitespaceBehavior(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {1298func GetDiffCommitWithWhitespaceBehavior(gitRepo *git.Repository, commitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
1314 return GetDiffRangeWithWhitespaceBehavior(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles, whitespaceBehavior)1299 return GetDiffRangeWithWhitespaceBehavior(gitRepo, "", commitID, maxLines, maxLineCharacters, maxFiles, whitespaceBehavior)
1315}1300}
1316 1301
1317// CommentAsDiff returns c.Patch as *Diff1302// CommentAsDiff returns c.Patch as *Diff