mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 05:11:41 +01:00 
			
		
		
		
	Serve .patch for pull requests (#3305)
* Serve .patch for pull requests Closes #3259 Updates "git" module, for GetFormatPatch * Handle io.Copy error
This commit is contained in:
		
							parent
							
								
									18bb0f8f13
								
							
						
					
					
						commit
						44053532bb
					
				| @ -56,7 +56,15 @@ func TestPullCreate(t *testing.T) { | ||||
| 	// check .diff can be accessed and matches performed change | ||||
| 	req := NewRequest(t, "GET", url+".diff") | ||||
| 	resp = session.MakeRequest(t, req, http.StatusOK) | ||||
| 	assert.Regexp(t, "\\+Hello, World \\(Edited\\)", resp.Body) | ||||
| 	assert.Regexp(t, `\+Hello, World \(Edited\)`, resp.Body) | ||||
| 	assert.Regexp(t, "^diff", resp.Body) | ||||
| 	assert.NotRegexp(t, "diff.*diff", resp.Body) // not two diffs, just one | ||||
| 
 | ||||
| 	// check .patch can be accessed and matches performed change | ||||
| 	req = NewRequest(t, "GET", url+".patch") | ||||
| 	resp = session.MakeRequest(t, req, http.StatusOK) | ||||
| 	assert.Regexp(t, `\+Hello, World \(Edited\)`, resp.Body) | ||||
| 	assert.Regexp(t, "diff", resp.Body) | ||||
| 	assert.Regexp(t, `Subject: \[PATCH\] Update 'README.md'`, resp.Body) | ||||
| 	assert.NotRegexp(t, "diff.*diff", resp.Body) // not two diffs, just one | ||||
| } | ||||
|  | ||||
| @ -9,6 +9,7 @@ package repo | ||||
| import ( | ||||
| 	"container/list" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"path" | ||||
| 	"strings" | ||||
| 
 | ||||
| @ -1033,7 +1034,7 @@ func DownloadPullDiff(ctx *context.Context) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	// Redirect elsewhere if it's not a pull request | ||||
| 	// Return not found if it's not a pull request | ||||
| 	if !issue.IsPull { | ||||
| 		ctx.Handle(404, "DownloadPullDiff", | ||||
| 			fmt.Errorf("Issue is not a pull request")) | ||||
| @ -1054,3 +1055,48 @@ func DownloadPullDiff(ctx *context.Context) { | ||||
| 
 | ||||
| 	ctx.ServeFileContent(patch) | ||||
| } | ||||
| 
 | ||||
| // DownloadPullPatch render a pull's raw patch | ||||
| func DownloadPullPatch(ctx *context.Context) { | ||||
| 	issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) | ||||
| 	if err != nil { | ||||
| 		if models.IsErrIssueNotExist(err) { | ||||
| 			ctx.Handle(404, "GetIssueByIndex", err) | ||||
| 		} else { | ||||
| 			ctx.Handle(500, "GetIssueByIndex", err) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	// Return not found if it's not a pull request | ||||
| 	if !issue.IsPull { | ||||
| 		ctx.Handle(404, "DownloadPullDiff", | ||||
| 			fmt.Errorf("Issue is not a pull request")) | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	pr := issue.PullRequest | ||||
| 
 | ||||
| 	if err = pr.GetHeadRepo(); err != nil { | ||||
| 		ctx.Handle(500, "GetHeadRepo", err) | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath()) | ||||
| 	if err != nil { | ||||
| 		ctx.Handle(500, "OpenRepository", err) | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	patch, err := headGitRepo.GetFormatPatch(pr.MergeBase, pr.HeadBranch) | ||||
| 	if err != nil { | ||||
| 		ctx.Handle(500, "GetFormatPatch", err) | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	_, err = io.Copy(ctx, patch) | ||||
| 	if err != nil { | ||||
| 		ctx.Handle(500, "io.Copy", err) | ||||
| 		return | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -625,6 +625,7 @@ func RegisterRoutes(m *macaron.Macaron) { | ||||
| 
 | ||||
| 		m.Group("/pulls/:index", func() { | ||||
| 			m.Get(".diff", repo.DownloadPullDiff) | ||||
| 			m.Get(".patch", repo.DownloadPullPatch) | ||||
| 			m.Get("/commits", context.RepoRef(), repo.ViewPullCommits) | ||||
| 			m.Get("/files", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ViewPullFiles) | ||||
| 			m.Post("/merge", reqRepoWriter, bindIgnErr(auth.MergePullRequestForm{}), repo.MergePullRequest) | ||||
|  | ||||
							
								
								
									
										1
									
								
								vendor/code.gitea.io/git/MAINTAINERS
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								vendor/code.gitea.io/git/MAINTAINERS
									
									
									
										generated
									
									
										vendored
									
									
								
							| @ -7,6 +7,7 @@ Kim Carlbäcker <kim.carlbacker@gmail.com> (@bkcsoft) | ||||
| LefsFlare <nobody@nobody.tld> (@LefsFlarey) | ||||
| Lunny Xiao <xiaolunwen@gmail.com> (@lunny) | ||||
| Matthias Loibl <mail@matthiasloibl.com> (@metalmatze) | ||||
| Morgan Bazalgette <the@howl.moe> (@thehowl) | ||||
| Rachid Zarouali <nobody@nobody.tld> (@xinity) | ||||
| Rémy Boulanouar <admin@dblk.org> (@DblK) | ||||
| Sandro Santilli <strk@kbt.io> (@strk) | ||||
|  | ||||
							
								
								
									
										5
									
								
								vendor/code.gitea.io/git/command.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								vendor/code.gitea.io/git/command.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @ -17,6 +17,9 @@ import ( | ||||
| var ( | ||||
| 	// GlobalCommandArgs global command args for external package setting | ||||
| 	GlobalCommandArgs []string | ||||
| 
 | ||||
| 	// DefaultCommandExecutionTimeout default command execution timeout duration | ||||
| 	DefaultCommandExecutionTimeout = 60 * time.Second | ||||
| ) | ||||
| 
 | ||||
| // Command represents a command with its subcommands or arguments. | ||||
| @ -50,7 +53,7 @@ func (c *Command) AddArguments(args ...string) *Command { | ||||
| // it pipes stdout and stderr to given io.Writer. | ||||
| func (c *Command) RunInDirTimeoutPipeline(timeout time.Duration, dir string, stdout, stderr io.Writer) error { | ||||
| 	if timeout == -1 { | ||||
| 		timeout = 60 * time.Second | ||||
| 		timeout = DefaultCommandExecutionTimeout | ||||
| 	} | ||||
| 
 | ||||
| 	if len(dir) == 0 { | ||||
|  | ||||
							
								
								
									
										14
									
								
								vendor/code.gitea.io/git/repo_pull.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										14
									
								
								vendor/code.gitea.io/git/repo_pull.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @ -5,8 +5,10 @@ | ||||
| package git | ||||
| 
 | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"container/list" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
| 	"time" | ||||
| @ -73,3 +75,15 @@ func (repo *Repository) GetPullRequestInfo(basePath, baseBranch, headBranch stri | ||||
| func (repo *Repository) GetPatch(base, head string) ([]byte, error) { | ||||
| 	return NewCommand("diff", "-p", "--binary", base, head).RunInDirBytes(repo.Path) | ||||
| } | ||||
| 
 | ||||
| // GetFormatPatch generates and returns format-patch data between given revisions. | ||||
| func (repo *Repository) GetFormatPatch(base, head string) (io.Reader, error) { | ||||
| 	stdout := new(bytes.Buffer) | ||||
| 	stderr := new(bytes.Buffer) | ||||
| 
 | ||||
| 	if err := NewCommand("format-patch", "--binary", "--stdout", base+"..."+head). | ||||
| 		RunInDirPipeline(repo.Path, stdout, stderr); err != nil { | ||||
| 		return nil, concatenateError(err, stderr.String()) | ||||
| 	} | ||||
| 	return stdout, nil | ||||
| } | ||||
|  | ||||
							
								
								
									
										6
									
								
								vendor/vendor.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								vendor/vendor.json
									
									
									
									
										vendored
									
									
								
							| @ -3,10 +3,10 @@ | ||||
| 	"ignore": "test appengine", | ||||
| 	"package": [ | ||||
| 		{ | ||||
| 			"checksumSHA1": "Em29XiKkOh5rFFXdkCjqqsQ7fe4=", | ||||
| 			"checksumSHA1": "1WHdGmDRsFRTD5N69l+MEbZr+nM=", | ||||
| 			"path": "code.gitea.io/git", | ||||
| 			"revision": "4ec3654064ef7eef4f05f891073a38039ad8d0f7", | ||||
| 			"revisionTime": "2017-12-22T02:43:26Z" | ||||
| 			"revision": "f4a91053671bee69f1995e456c1541668717c19d", | ||||
| 			"revisionTime": "2018-01-07T06:11:05Z" | ||||
| 		}, | ||||
| 		{ | ||||
| 			"checksumSHA1": "Qtq0kW+BnpYMOriaoCjMa86WGG8=", | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user