repos / git-pr

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

commit
04029e4
parent
fbfd037
author
Eric Bower
date
2024-05-11 10:15:33 -0400 EDT
changes
3 files changed,  +65, -0
M mdw.go
M ssh.go
M mdw.go
+12, -0
 1@@ -100,6 +100,18 @@ func GitPatchRequestMiddleware(be *Backend, pr GitPatchRequest) wish.Middleware
 2 				}
 3 				writer.Flush()
 4 			} else if cmd == "pr" {
 5+				/*
 6+					ssh git.sh ls
 7+					ssh git.sh pr ls
 8+					git format-patch -1 HEAD~1 --stdout | ssh git.sh pr create
 9+					ssh git.sh pr print 1
10+					ssh git.sh pr print 1 --summary
11+					ssh git.sh pr print 1 --ls
12+					ssh git.sh pr accept 1
13+					ssh git.sh pr close 1
14+					git format-patch -1 HEAD~1 --stdout | ssh git.sh pr review 1
15+					echo "my feedback" | ssh git.sh pr comment 1
16+				*/
17 				prCmd := flagSet(sesh, "pr")
18 				out := prCmd.Bool("stdout", false, "print patchset to stdout")
19 				accept := prCmd.Bool("accept", false, "mark patch request as accepted")
M ssh.go
+8, -0
 1@@ -35,6 +35,14 @@ func GitSshServer() {
 2 		panic(err)
 3 	}
 4 	dbh.Migrate()
 5+
 6+	keys, err := getAuthorizedKeys(filepath.Join(cfg.DataPath, "authorized_keys"))
 7+	if err == nil {
 8+		cfg.Admins = keys
 9+	} else {
10+		logger.Error("could not parse authorized keys file", "err", err)
11+	}
12+
13 	be := &Backend{
14 		DB:     dbh,
15 		Logger: logger,
A util.go
+45, -0
 1@@ -0,0 +1,45 @@
 2+package git
 3+
 4+import (
 5+	"bufio"
 6+	"bytes"
 7+	"errors"
 8+	"io"
 9+	"os"
10+	"strings"
11+
12+	"github.com/charmbracelet/ssh"
13+)
14+
15+func getAuthorizedKeys(path string) ([]ssh.PublicKey, error) {
16+	keys := []ssh.PublicKey{}
17+	f, err := os.Open(path)
18+	if err != nil {
19+		return keys, err
20+	}
21+	defer f.Close() // nolint: errcheck
22+
23+	rd := bufio.NewReader(f)
24+	for {
25+		line, _, err := rd.ReadLine()
26+		if err != nil {
27+			if errors.Is(err, io.EOF) {
28+				break
29+			}
30+			return keys, err
31+		}
32+		if strings.TrimSpace(string(line)) == "" {
33+			continue
34+		}
35+		if bytes.HasPrefix(line, []byte{'#'}) {
36+			continue
37+		}
38+		upk, _, _, _, err := ssh.ParseAuthorizedKey(line)
39+		if err != nil {
40+			return keys, err
41+		}
42+		keys = append(keys, upk)
43+	}
44+
45+	return keys, nil
46+}