repos / git-pr

a self-hosted git collaboration server
git clone https://github.com/picosh/git-pr.git

commit
d16de93
parent
038aecf
author
Eric Bower
date
2024-06-27 18:53:41 -0400 EDT
feat: record base commit if it exists
2 files changed,  +28, -12
M db.go
M pr.go
M db.go
+2, -0
 1@@ -54,6 +54,7 @@ type Patch struct {
 2 	BodyAppendix   string    `db:"body_appendix"`
 3 	CommitSha      string    `db:"commit_sha"`
 4 	ContentSha     string    `db:"content_sha"`
 5+	BaseCommitSha  string    `db:"base_commit_sha"`
 6 	Review         bool      `db:"review"`
 7 	RawText        string    `db:"raw_text"`
 8 	CreatedAt      time.Time `db:"created_at"`
 9@@ -154,6 +155,7 @@ CREATE TABLE IF NOT EXISTS event_logs (
10 
11 var sqliteMigrations = []string{
12 	"", // migration #0 is reserved for schema initialization
13+	"ALTER TABLE patches ADD COLUMN base_commit_sha TEXT",
14 }
15 
16 // Open opens a database connection.
M pr.go
+26, -12
 1@@ -6,6 +6,7 @@ import (
 2 	"errors"
 3 	"fmt"
 4 	"io"
 5+	"regexp"
 6 	"strings"
 7 	"time"
 8 
 9@@ -367,10 +368,20 @@ func (cmd PrCmd) calcContentSha(diffFiles []*gitdiff.File, header *gitdiff.Patch
10 	return shaStr
11 }
12 
13-func (cmd PrCmd) splitPatchSet(patchset string) []string {
14+func splitPatchSet(patchset string) []string {
15 	return strings.Split(patchset, "\n\n\n")
16 }
17 
18+func findBaseCommit(patch string) string {
19+	re := regexp.MustCompile(`base-commit: (.+)\s*`)
20+	strs := re.FindStringSubmatch(patch)
21+	baseCommit := ""
22+	if len(strs) > 1 {
23+		baseCommit = strs[1]
24+	}
25+	return baseCommit
26+}
27+
28 func (cmd PrCmd) parsePatchSet(patchset io.Reader) ([]*Patch, error) {
29 	patches := []*Patch{}
30 	buf := new(strings.Builder)
31@@ -379,7 +390,7 @@ func (cmd PrCmd) parsePatchSet(patchset io.Reader) ([]*Patch, error) {
32 		return nil, err
33 	}
34 
35-	patchesRaw := cmd.splitPatchSet(buf.String())
36+	patchesRaw := splitPatchSet(buf.String())
37 	for _, patchRaw := range patchesRaw {
38 		reader := strings.NewReader(patchRaw)
39 		diffFiles, preamble, err := gitdiff.Parse(reader)
40@@ -396,6 +407,7 @@ func (cmd PrCmd) parsePatchSet(patchset io.Reader) ([]*Patch, error) {
41 			continue
42 		}
43 
44+		baseCommit := findBaseCommit(patchRaw)
45 		authorName := "Unknown"
46 		authorEmail := ""
47 		if header.Author != nil {
48@@ -406,15 +418,16 @@ func (cmd PrCmd) parsePatchSet(patchset io.Reader) ([]*Patch, error) {
49 		contentSha := cmd.calcContentSha(diffFiles, header)
50 
51 		patches = append(patches, &Patch{
52-			AuthorName:   authorName,
53-			AuthorEmail:  authorEmail,
54-			AuthorDate:   header.AuthorDate.UTC().String(),
55-			Title:        header.Title,
56-			Body:         header.Body,
57-			BodyAppendix: header.BodyAppendix,
58-			CommitSha:    header.SHA,
59-			ContentSha:   contentSha,
60-			RawText:      patchRaw,
61+			AuthorName:    authorName,
62+			AuthorEmail:   authorEmail,
63+			AuthorDate:    header.AuthorDate.UTC().String(),
64+			Title:         header.Title,
65+			Body:          header.Body,
66+			BodyAppendix:  header.BodyAppendix,
67+			CommitSha:     header.SHA,
68+			ContentSha:    contentSha,
69+			RawText:       patchRaw,
70+			BaseCommitSha: baseCommit,
71 		})
72 	}
73 
74@@ -430,7 +443,7 @@ func (cmd PrCmd) createPatch(tx *sqlx.Tx, review bool, patch *Patch) (int64, err
75 
76 	var patchID int64
77 	row := tx.QueryRow(
78-		"INSERT INTO patches (user_id, patch_request_id, author_name, author_email, author_date, title, body, body_appendix, commit_sha, content_sha, review, raw_text) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",
79+		"INSERT INTO patches (user_id, patch_request_id, author_name, author_email, author_date, title, body, body_appendix, commit_sha, content_sha, base_commit_sha, review, raw_text) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",
80 		patch.UserID,
81 		patch.PatchRequestID,
82 		patch.AuthorName,
83@@ -441,6 +454,7 @@ func (cmd PrCmd) createPatch(tx *sqlx.Tx, review bool, patch *Patch) (int64, err
84 		patch.BodyAppendix,
85 		patch.CommitSha,
86 		patch.ContentSha,
87+		patch.BaseCommitSha,
88 		review,
89 		patch.RawText,
90 	)