1
0
Fork 0
forked from forgejo/forgejo

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:
Sandro Santilli 2018-01-07 14:10:20 +01:00 committed by Lauris BH
parent 18bb0f8f13
commit 44053532bb
7 changed files with 79 additions and 6 deletions

View file

@ -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)

View file

@ -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 {

View file

@ -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
}