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
2020 "regexp"
2121 "sort"
2222 "strings"
23+ "time"
2324
2425 "code.gitea.io/gitea/models"
2526 "code.gitea.io/gitea/modules/charset"
@@ -1205,31 +1206,20 @@ func readFileName(rd *strings.Reader) (string, bool) { · 1179 unmodified lines
12051206 return name[2:], ambiguity
12061207 }
12071208
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.
1211−func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
1212− return GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID, maxLines, maxLineCharacters, maxFiles, "")
1213−}
1214−
12151209 // GetDiffRangeWithWhitespaceBehavior builds a Diff between two commits of a repository.
12161210 // Passing the empty string as beforeCommitID returns a diff from the parent commit.
12171211 // The whitespaceBehavior is either an empty string or a git flag
1218−func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
1219− gitRepo, err := git.OpenRepository(repoPath)
1220− if err != nil {
1221− return nil, err
1222− }
1223− defer gitRepo.Close()
1212+func GetDiffRangeWithWhitespaceBehavior(gitRepo *git.Repository, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
1213+ repoPath := gitRepo.Path
12241214
12251215 commit, err := gitRepo.GetCommit(afterCommitID)
12261216 if err != nil {
12271217 return nil, err
12281218 }
12291219
1230− // FIXME: graceful: These commands should likely have a timeout
1231− ctx, cancel := context.WithCancel(git.DefaultContext)
1220+ ctx, cancel := context.WithTimeout(git.DefaultContext, time.Duration(setting.Git.Timeout.Default)*time.Second)
12321221 defer cancel()
1222+
12331223 var cmd *exec.Cmd
12341224 if (len(beforeCommitID) == 0 || beforeCommitID == git.EmptySHA) && commit.ParentCount() == 0 {
12351225 diffArgs := []string{"diff", "--src-prefix=\\a/", "--dst-prefix=\\b/", "-M"}
@@ -1303,15 +1293,10 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID · 67 unmodified lines
13031293 return diff, nil
13041294 }
13051295
1306−// GetDiffCommit builds a Diff representing the given commitID.
1307−func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
1308− return GetDiffRangeWithWhitespaceBehavior(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles, "")
1309−}
1310−
13111296 // GetDiffCommitWithWhitespaceBehavior builds a Diff representing the given commitID.
13121297 // The whitespaceBehavior is either an empty string or a git flag
1313−func GetDiffCommitWithWhitespaceBehavior(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
1314− return GetDiffRangeWithWhitespaceBehavior(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles, whitespaceBehavior)
1298+func GetDiffCommitWithWhitespaceBehavior(gitRepo *git.Repository, commitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
1299+ return GetDiffRangeWithWhitespaceBehavior(gitRepo, "", commitID, maxLines, maxLineCharacters, maxFiles, whitespaceBehavior)
13151300 }
13161301
13171302 // CommentAsDiff returns c.Patch as *Diff