* repare and improve GetDiffRangeWithWhitespaceBehavior * Context with Timeout
Showing the changes to services/gitdiff/gitdiff.go (1 of 5 files). Show all
Unified · Split
| @@ -20,6 +20,7 @@ import ( · 19 unmodified lines | ||
| 20 | 20 | "regexp" |
| 21 | 21 | "sort" |
| 22 | 22 | "strings" |
| 23 | + "time" | |
| 23 | 24 | |
| 24 | 25 | "code.gitea.io/gitea/models" |
| 25 | 26 | "code.gitea.io/gitea/modules/charset" |
| @@ -1205,31 +1206,20 @@ func readFileName(rd *strings.Reader) (string, bool) { · 1179 unmodified lines | ||
| 1205 | 1206 | 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. | |
| 1211 | −func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) { | |
| 1212 | − return GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID, maxLines, maxLineCharacters, maxFiles, "") | |
| 1213 | −} | |
| 1214 | − | |
| 1215 | 1209 | // GetDiffRangeWithWhitespaceBehavior builds a Diff between two commits of a repository. |
| 1216 | 1210 | // Passing the empty string as beforeCommitID returns a diff from the parent commit. |
| 1217 | 1211 | // 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 | |
| 1224 | 1214 | |
| 1225 | 1215 | commit, err := gitRepo.GetCommit(afterCommitID) |
| 1226 | 1216 | if err != nil { |
| 1227 | 1217 | return nil, err |
| 1228 | 1218 | } |
| 1229 | 1219 | |
| 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) | |
| 1232 | 1221 | defer cancel() |
| 1222 | + | |
| 1233 | 1223 | var cmd *exec.Cmd |
| 1234 | 1224 | if (len(beforeCommitID) == 0 || beforeCommitID == git.EmptySHA) && commit.ParentCount() == 0 { |
| 1235 | 1225 | diffArgs := []string{"diff", "--src-prefix=\\a/", "--dst-prefix=\\b/", "-M"} |
| @@ -1303,15 +1293,10 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID · 67 unmodified lines | ||
| 1303 | 1293 | return diff, nil |
| 1304 | 1294 | } |
| 1305 | 1295 | |
| 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 | − | |
| 1311 | 1296 | // GetDiffCommitWithWhitespaceBehavior builds a Diff representing the given commitID. |
| 1312 | 1297 | // 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) | |
| 1315 | 1300 | } |
| 1316 | 1301 | |
| 1317 | 1302 | // CommentAsDiff returns c.Patch as *Diff |