-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Save patches to temporary files #9296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
26d4e75
Save patches to temporary files
zeripath f6cfef0
Copy file instead
zeripath f86ac61
Import io
zeripath 5d6d313
Always close tmpPatchFile
zeripath e349449
fmting
zeripath 810898f
Apply suggestions from code review
zeripath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ package models | |||||||||
| import ( | ||||||||||
| "bufio" | ||||||||||
| "fmt" | ||||||||||
| "io/ioutil" | ||||||||||
| "os" | ||||||||||
| "path" | ||||||||||
| "path/filepath" | ||||||||||
|
|
@@ -595,11 +596,11 @@ func (pr *PullRequest) testPatch(e Engine) (err error) { | |||||||||
| } | ||||||||||
|
|
||||||||||
| // NewPullRequest creates new pull request with labels for repository. | ||||||||||
| func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patch []byte) (err error) { | ||||||||||
| func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patchFileSize int64, patchFileName string) (err error) { | ||||||||||
| // Retry several times in case INSERT fails due to duplicate key for (repo_id, index); see #7887 | ||||||||||
| i := 0 | ||||||||||
| for { | ||||||||||
| if err = newPullRequestAttempt(repo, pull, labelIDs, uuids, pr, patch); err == nil { | ||||||||||
| if err = newPullRequestAttempt(repo, pull, labelIDs, uuids, pr, patchFileSize, patchFileName); err == nil { | ||||||||||
| return nil | ||||||||||
| } | ||||||||||
| if !IsErrNewIssueInsert(err) { | ||||||||||
|
|
@@ -613,7 +614,7 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str | |||||||||
| return fmt.Errorf("NewPullRequest: too many errors attempting to insert the new issue. Last error was: %v", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func newPullRequestAttempt(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patch []byte) (err error) { | ||||||||||
| func newPullRequestAttempt(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patchFileSize int64, patchFileName string) (err error) { | ||||||||||
| sess := x.NewSession() | ||||||||||
| defer sess.Close() | ||||||||||
| if err = sess.Begin(); err != nil { | ||||||||||
|
|
@@ -636,8 +637,8 @@ func newPullRequestAttempt(repo *Repository, pull *Issue, labelIDs []int64, uuid | |||||||||
| pr.Index = pull.Index | ||||||||||
| pr.BaseRepo = repo | ||||||||||
| pr.Status = PullRequestStatusChecking | ||||||||||
| if len(patch) > 0 { | ||||||||||
| if err = repo.savePatch(sess, pr.Index, patch); err != nil { | ||||||||||
| if patchFileSize > 0 { | ||||||||||
| if err = repo.savePatch(sess, pr.Index, patchFileName); err != nil { | ||||||||||
| return fmt.Errorf("SavePatch: %v", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -800,12 +801,23 @@ func (pr *PullRequest) UpdatePatch() (err error) { | |||||||||
| return fmt.Errorf("Update: %v", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| patch, err := headGitRepo.GetPatch(pr.MergeBase, pr.HeadBranch) | ||||||||||
| tmpPatchFile, err := ioutil.TempFile("", "patch") | ||||||||||
| if err != nil { | ||||||||||
| return fmt.Errorf("GetPatch: %v", err) | ||||||||||
| log.Error("Unable to create temporary patch file! Error: %v", err) | ||||||||||
| return fmt.Errorf("Unable to create temporary patch file! Error: %v", err) | ||||||||||
| } | ||||||||||
| defer func() { | ||||||||||
| _ = os.Remove(tmpPatchFile.Name()) | ||||||||||
| }() | ||||||||||
|
|
||||||||||
| if err := headGitRepo.GetPatch(pr.MergeBase, pr.HeadBranch, tmpPatchFile); err != nil { | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| tmpPatchFile.Close() | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| log.Error("Unable to get patch file from %s to %s in %s/%s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.MustOwner().Name, pr.BaseRepo.Name, err) | ||||||||||
zeripath marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| return fmt.Errorf("Unable to get patch file from %s to %s in %s/%s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.MustOwner().Name, pr.BaseRepo.Name, err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if err = pr.BaseRepo.SavePatch(pr.Index, patch); err != nil { | ||||||||||
| tmpPatchFile.Close() | ||||||||||
| if err = pr.BaseRepo.SavePatch(pr.Index, tmpPatchFile.Name()); err != nil { | ||||||||||
zeripath marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| return fmt.Errorf("BaseRepo.SavePatch: %v", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the patch should be saved even if it's got 0 bytes. The reason is that a previous PR creation attempt (from a different PR, with patch data) could have failed between this step and the commit, so a left-over patch file (named after this same index number) might still be lingering in the directory. I hope that makes sense.
In fact, there are scenarios where that patch could be broken anyway with this retry mechanism. Mmm.... we should be locking that path, at the very least? Sorry, I wasn't aware that there were destinations other than the database when I wrote the retry code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would make it simpler if we dropped that file size test.
Tbh I'm not certain that the patch files are ever actually cleaned up. I don't remember seeing any code looking at deleting them. I actually forgot to look explicitly - that's not like me!!
You're also right about the lack of file locking. This might be because we have generally migrated from the repoworkingcopy lock mechanism to rely on git locking but obviously this doesn't use that mechanism. #9302 obviously isn't affected by this.
I guess the question is how important is it provide a backport?
If we're not going to backport - then I'm happy to close this and move to the more radical refactor.
(It's not like this is the only place we still read in potentially huge amounts of data - diff and blame are both still doing that, and a change to streaming there will require huge refactoring.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, backporting is not my call, but I'm OK with not backporting this as long as #9302 makes it in time for 1.11.