Skip to content
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

Enable cloning via Git Wire Protocol v2 over HTTP #12170

Merged
merged 3 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Enable Git Wire Protocol v2 over HTTP
  • Loading branch information
billiegoose committed Jul 7, 2020
commit 313a1810aee0bceafad8500a6c1771ec441531d3
6 changes: 6 additions & 0 deletions modules/git/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ func (c *Command) RunInDirWithEnv(dir string, env []string) (string, error) {
return string(stdout), nil
}

// RunInDirWithEnvBytes executes the command in given directory
// and returns stdout in []byte and error (combined with stderr).
func (c *Command) RunInDirWithEnvBytes(dir string, env []string) ([]byte, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wrap function seems unnecessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, this file scares me lol. They all look like mostly unnecessary wrapper functions to me. But I was trying to go along with the style. 🤷‍♂️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use RunInDirTimeoutEnv(env, -1, dir) directly instead. At some point we'll hopefully destroy all of these and replace them with a single RunOptions{} that can have things set.

Honestly, usually what happens is I end up escalating all the way up to FullPipeline to use Readers because a lot of functions misuse storing unbounded results and/or the error munging with these downstream functions ends up being unhelpful.

return c.RunInDirTimeoutEnv(env, -1, dir)
}

billiegoose marked this conversation as resolved.
Show resolved Hide resolved
// RunTimeout executes the command in default working directory with given timeout,
// and returns stdout in string and error (combined with stderr).
func (c *Command) RunTimeout(timeout time.Duration) (string, error) {
Expand Down
16 changes: 15 additions & 1 deletion routers/repo/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ var routes = []route{
{regexp.MustCompile(`(.*?)/objects/pack/pack-[0-9a-f]{40}\.idx$`), "GET", getIdxFile},
}

// one or more key=value pairs separated by colons
var safeGitProtocolHeader = regexp.MustCompile(`^[0-9a-zA-Z]+=[0-9a-zA-Z]+(:[0-9a-zA-Z]+=[0-9a-zA-Z]+)*$`)

func getGitConfig(option, dir string) string {
out, err := git.NewCommand("config", option).RunInDir(dir)
if err != nil {
Expand Down Expand Up @@ -553,13 +556,19 @@ func serviceRPC(h serviceHandler, service string) {
// set this for allow pre-receive and post-receive execute
h.environ = append(h.environ, "SSH_ORIGINAL_COMMAND="+service)

if protocol := h.r.Header.Get("Git-Protocol"); protocol != "" && safeGitProtocolHeader.MatchString(protocol) {
h.environ = append(h.environ, "GIT_PROTOCOL="+protocol)
}

ctx, cancel := gocontext.WithCancel(git.DefaultContext)
defer cancel()
var stderr bytes.Buffer
cmd := exec.CommandContext(ctx, git.GitExecutable, service, "--stateless-rpc", h.dir)
cmd.Dir = h.dir
if service == "receive-pack" {
cmd.Env = append(os.Environ(), h.environ...)
} else {
cmd.Env = h.environ
}
lafriks marked this conversation as resolved.
Show resolved Hide resolved
cmd.Stdout = h.w
cmd.Stdin = reqBody
Expand Down Expand Up @@ -610,7 +619,12 @@ func getInfoRefs(h serviceHandler) {
h.setHeaderNoCache()
if hasAccess(getServiceType(h.r), h, false) {
service := getServiceType(h.r)
refs, err := git.NewCommand(service, "--stateless-rpc", "--advertise-refs", ".").RunInDirBytes(h.dir)

if protocol := h.r.Header.Get("Git-Protocol"); protocol != "" && safeGitProtocolHeader.MatchString(protocol) {
h.environ = append(h.environ, "GIT_PROTOCOL="+protocol)
}

refs, err := git.NewCommand(service, "--stateless-rpc", "--advertise-refs", ".").RunInDirWithEnvBytes(h.dir, h.environ)
billiegoose marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Error(fmt.Sprintf("%v - %s", err, string(refs)))
}
Expand Down