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

Add require signed commit for protected branch #9708

Merged
merged 28 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ba15ed9
Add require signed commit for protected branch
zeripath Jan 11, 2020
f482e28
Fix fmt
zeripath Jan 11, 2020
43c5df5
Merge branch 'master' into protect-branch-signed-commits-only
zeripath Jan 11, 2020
f9f8557
Make editor show if they will be signed
zeripath Jan 11, 2020
27c0354
Merge branch 'protect-branch-signed-commits-only' of github.com:zerip…
zeripath Jan 11, 2020
f2a28e8
bugfix
zeripath Jan 11, 2020
9a7ec7f
Merge branch 'master' into protect-branch-signed-commits-only
zeripath Jan 11, 2020
e4d987a
Merge branch 'master' into protect-branch-signed-commits-only
zeripath Jan 12, 2020
594126a
Add basic merge check and better information for CRUD
zeripath Jan 12, 2020
dd05f6a
linting comment
zeripath Jan 12, 2020
de67865
Add descriptors to merge signing
zeripath Jan 12, 2020
97b811d
Slight refactor
zeripath Jan 12, 2020
ea21e79
Merge branch 'master' into protect-branch-signed-commits-only
zeripath Jan 12, 2020
68a8e11
Slight improvement to appearances
zeripath Jan 12, 2020
acf011a
Handle Merge API
zeripath Jan 12, 2020
24eb4a2
manage CRUD API
zeripath Jan 12, 2020
bd8d89f
Move error to error.go
zeripath Jan 13, 2020
bfb7df0
Remove fix to delete.go
zeripath Jan 13, 2020
50cf492
prep for merge
zeripath Jan 13, 2020
6793e8d
Merge branch 'master' into protect-branch-signed-commits-only
zeripath Jan 13, 2020
7a04a47
need to tolerate \r\n in message
zeripath Jan 14, 2020
161d110
check protected branch before trying to load it
zeripath Jan 14, 2020
335ec50
Apply suggestions from code review
zeripath Jan 14, 2020
5ad5a56
Merge branch 'master' into protect-branch-signed-commits-only
zeripath Jan 14, 2020
ea40bd6
fix commit-reader
zeripath Jan 14, 2020
d554a62
Merge branch 'master' into protect-branch-signed-commits-only
zeripath Jan 14, 2020
2d454cd
Merge branch 'master' into protect-branch-signed-commits-only
zeripath Jan 14, 2020
788ed2f
Merge branch 'master' into protect-branch-signed-commits-only
zeripath Jan 15, 2020
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
Prev Previous commit
Next Next commit
Slight refactor
  • Loading branch information
zeripath committed Jan 12, 2020
commit 97b811db76a561ad62544120638aaa7b04e4233e
8 changes: 6 additions & 2 deletions modules/git/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (c *Command) RunInDirTimeoutEnvFullPipeline(env []string, timeout time.Dura

// RunInDirTimeoutEnvFullPipelineFunc executes the command in given directory with given timeout,
// it pipes stdout and stderr to given io.Writer and passes in an io.Reader as stdin. Between cmd.Start and cmd.Wait the passed in function is run.
func (c *Command) RunInDirTimeoutEnvFullPipelineFunc(env []string, timeout time.Duration, dir string, stdout, stderr io.Writer, stdin io.Reader, fn func(context.Context, context.CancelFunc)) error {
func (c *Command) RunInDirTimeoutEnvFullPipelineFunc(env []string, timeout time.Duration, dir string, stdout, stderr io.Writer, stdin io.Reader, fn func(context.Context, context.CancelFunc) error) error {

if timeout == -1 {
timeout = DefaultCommandExecutionTimeout
Expand Down Expand Up @@ -135,7 +135,11 @@ func (c *Command) RunInDirTimeoutEnvFullPipelineFunc(env []string, timeout time.
defer process.GetManager().Remove(pid)

if fn != nil {
fn(ctx, cancel)
err := fn(ctx, cancel)
if err != nil {
cancel()
return err
}
}

if err := cmd.Wait(); err != nil && ctx.Err() != context.DeadlineExceeded {
Expand Down
93 changes: 93 additions & 0 deletions modules/git/commit_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package git

import (
"bufio"
"bytes"
"io"
"strings"

"gopkg.in/src-d/go-git.v4/plumbing"
)

// CommitFromReader will generate a Commit from a provided reader
// We will need this to interpret commits from cat-file
func CommitFromReader(gitRepo *Repository, sha plumbing.Hash, reader io.Reader) (*Commit, error) {
commit := &Commit{
ID: sha,
}

payloadSB := new(strings.Builder)
signatureSB := new(strings.Builder)
messageSB := new(strings.Builder)
message := false
pgpsig := false

scanner := bufio.NewScanner(reader)
lafriks marked this conversation as resolved.
Show resolved Hide resolved
for scanner.Scan() {
line := scanner.Bytes()
if pgpsig {
if len(line) > 0 && line[0] == ' ' {
line = bytes.TrimLeft(line, " ")
_, _ = signatureSB.Write(line)
_ = signatureSB.WriteByte('\n')
continue
} else {
pgpsig = false
}
}

if !message {
trimmed := bytes.TrimSpace(line)
if len(trimmed) == 0 {
message = true
_, _ = payloadSB.WriteString("\n")
continue
}

split := bytes.SplitN(trimmed, []byte{' '}, 2)
zeripath marked this conversation as resolved.
Show resolved Hide resolved

switch string(split[0]) {
case "tree":
commit.Tree = *NewTree(gitRepo, plumbing.NewHash(string(split[1])))
_, _ = payloadSB.Write(line)
_ = payloadSB.WriteByte('\n')
case "parent":
commit.Parents = append(commit.Parents, plumbing.NewHash(string(split[1])))
_, _ = payloadSB.Write(line)
_ = payloadSB.WriteByte('\n')
case "author":
commit.Author = &Signature{}
commit.Author.Decode(split[1])
_, _ = payloadSB.Write(line)
_ = payloadSB.WriteByte('\n')
case "committer":
commit.Committer = &Signature{}
commit.Committer.Decode(split[1])
_, _ = payloadSB.Write(line)
_ = payloadSB.WriteByte('\n')
case "gpgsig":
_, _ = signatureSB.Write(split[1])
_ = signatureSB.WriteByte('\n')
pgpsig = true
}
} else {
_, _ = messageSB.Write(line)
_ = messageSB.WriteByte('\n')
}
}
commit.CommitMessage = messageSB.String()
_, _ = payloadSB.WriteString(commit.CommitMessage)
lafriks marked this conversation as resolved.
Show resolved Hide resolved
commit.Signature = &CommitGPGSignature{
Signature: signatureSB.String(),
Payload: payloadSB.String(),
}
if len(commit.Signature.Signature) == 0 {
commit.Signature = nil
}

return commit, scanner.Err()
}
3 changes: 2 additions & 1 deletion modules/repofiles/temp_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,15 @@ func (t *TemporaryUploadRepository) DiffIndex() (*gitdiff.Diff, error) {
var finalErr error

if err := git.NewCommand("diff-index", "--cached", "-p", "HEAD").
RunInDirTimeoutEnvFullPipelineFunc(nil, 30*time.Second, t.basePath, stdoutWriter, stderr, nil, func(ctx context.Context, cancel context.CancelFunc) {
RunInDirTimeoutEnvFullPipelineFunc(nil, 30*time.Second, t.basePath, stdoutWriter, stderr, nil, func(ctx context.Context, cancel context.CancelFunc) error {
_ = stdoutWriter.Close()
diff, finalErr = gitdiff.ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdoutReader)
if finalErr != nil {
log.Error("ParsePatch: %v", finalErr)
cancel()
}
_ = stdoutReader.Close()
return finalErr
}); err != nil {
if finalErr != nil {
log.Error("Unable to ParsePatch in temporary repo %s (%s). Error: %v", t.repo.FullName(), t.basePath, finalErr)
Expand Down
Loading