History

Add require signed commit for protected branch (#9708)

* Add require signed commit for protected branch * Fix fmt * Make editor show if they will be signed * bugfix * Add basic merge check and better information for CRUD * linting comment * Add descriptors to merge signing * Slight refactor * Slight improvement to appearances * Handle Merge API * manage CRUD API * Move error to error.go * Remove fix to delete.go * prep for merge * need to tolerate \r\n in message * check protected branch before trying to load it * Apply suggestions from code review Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * fix commit-reader

zeripath committed 6 years ago

Commit 66ee9b81 parent 6b1fa1229 files · +618 −122Browse files

Showing the changes to modules/git/commit_reader.go (1 of 29 files). Show all

Unified · Split

modules/git/commit_reader.go+108−0
@@ -0,0 +1,108 @@
1// Copyright 2020 The Gitea Authors. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5package git
6
7import (
8 "bufio"
9 "bytes"
10 "io"
11 "strings"
12
13 "gopkg.in/src-d/go-git.v4/plumbing"
14)
15
16// CommitFromReader will generate a Commit from a provided reader
17// We will need this to interpret commits from cat-file
18func CommitFromReader(gitRepo *Repository, sha plumbing.Hash, reader io.Reader) (*Commit, error) {
19 commit := &Commit{
20 ID: sha,
21 }
22
23 payloadSB := new(strings.Builder)
24 signatureSB := new(strings.Builder)
25 messageSB := new(strings.Builder)
26 message := false
27 pgpsig := false
28
29 scanner := bufio.NewScanner(reader)
30 // Split by '\n' but include the '\n'
31 scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
32 if atEOF && len(data) == 0 {
33 return 0, nil, nil
34 }
35 if i := bytes.IndexByte(data, '\n'); i >= 0 {
36 // We have a full newline-terminated line.
37 return i + 1, data[0 : i+1], nil
38 }
39 // If we're at EOF, we have a final, non-terminated line. Return it.
40 if atEOF {
41 return len(data), data, nil
42 }
43 // Request more data.
44 return 0, nil, nil
45 })
46
47 for scanner.Scan() {
48 line := scanner.Bytes()
49 if pgpsig {
50 if len(line) > 0 && line[0] == ' ' {
51 _, _ = signatureSB.Write(line[1:])
52 continue
53 } else {
54 pgpsig = false
55 }
56 }
57
58 if !message {
59 // This is probably not correct but is copied from go-gits interpretation...
60 trimmed := bytes.TrimSpace(line)
61 if len(trimmed) == 0 {
62 message = true
63 _, _ = payloadSB.Write(line)
64 continue
65 }
66
67 split := bytes.SplitN(trimmed, []byte{' '}, 2)
68 var data []byte
69 if len(split) > 1 {
70 data = split[1]
71 }
72
73 switch string(split[0]) {
74 case "tree":
75 commit.Tree = *NewTree(gitRepo, plumbing.NewHash(string(data)))
76 _, _ = payloadSB.Write(line)
77 case "parent":
78 commit.Parents = append(commit.Parents, plumbing.NewHash(string(data)))
79 _, _ = payloadSB.Write(line)
80 case "author":
81 commit.Author = &Signature{}
82 commit.Author.Decode(data)
83 _, _ = payloadSB.Write(line)
84 case "committer":
85 commit.Committer = &Signature{}
86 commit.Committer.Decode(data)
87 _, _ = payloadSB.Write(line)
88 case "gpgsig":
89 _, _ = signatureSB.Write(data)
90 _ = signatureSB.WriteByte('\n')
91 pgpsig = true
92 }
93 } else {
94 _, _ = messageSB.Write(line)
95 }
96 }
97 commit.CommitMessage = messageSB.String()
98 _, _ = payloadSB.WriteString(commit.CommitMessage)
99 commit.Signature = &CommitGPGSignature{
100 Signature: signatureSB.String(),
101 Payload: payloadSB.String(),
102 }
103 if len(commit.Signature.Signature) == 0 {
104 commit.Signature = nil
105 }
106
107 return commit, scanner.Err()
108}