repos / git-pr

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

git-pr / contrib / dev
Eric Bower  ·  2025-08-22

main.go

  1package main
  2
  3import (
  4	"flag"
  5	"fmt"
  6	"log/slog"
  7	"net/http"
  8	"os"
  9	"os/signal"
 10	"syscall"
 11	"time"
 12
 13	"github.com/picosh/git-pr"
 14	"github.com/picosh/git-pr/fixtures"
 15	"github.com/picosh/git-pr/util"
 16)
 17
 18func main() {
 19	cleanupFlag := flag.Bool("cleanup", true, "Clean up tmp dir after quitting (default: true)")
 20	flag.Parse()
 21
 22	opts := &slog.HandlerOptions{
 23		AddSource: true,
 24	}
 25	logger := slog.New(
 26		slog.NewTextHandler(os.Stdout, opts),
 27	)
 28
 29	dataDir := util.CreateTmpDir()
 30	defer func() {
 31		if *cleanupFlag {
 32			_ = os.RemoveAll(dataDir)
 33		}
 34	}()
 35
 36	adminKey, userKey := util.GenerateKeys()
 37	cfgPath := util.CreateCfgFile(dataDir, cfgTmpl, adminKey)
 38	git.LoadConfigFile(cfgPath, logger)
 39	cfg := git.NewGitCfg(logger)
 40
 41	s := git.GitSshServer(cfg)
 42	go func() {
 43		_ = s.ListenAndServe()
 44	}()
 45	time.Sleep(time.Millisecond * 100)
 46	w := git.GitWebServer(cfg)
 47	addr := fmt.Sprintf("%s:%s", cfg.Host, cfg.WebPort)
 48	go func() {
 49		_ = http.ListenAndServe(addr, w)
 50	}()
 51
 52	// Hack to wait for startup
 53	time.Sleep(time.Millisecond * 100)
 54
 55	patch, err := fixtures.Fixtures.ReadFile("single.patch")
 56	if err != nil {
 57		panic(err)
 58	}
 59	otherPatch, err := fixtures.Fixtures.ReadFile("with-cover.patch")
 60	if err != nil {
 61		panic(err)
 62	}
 63	rd1, err := fixtures.Fixtures.ReadFile("a_b_reorder.patch")
 64	if err != nil {
 65		panic(err)
 66	}
 67	rd2, err := fixtures.Fixtures.ReadFile("a_c_changed_commit.patch")
 68	if err != nil {
 69		panic(err)
 70	}
 71
 72	// Accepted patch
 73	userKey.MustCmd(patch, "pr create test")
 74	userKey.MustCmd(nil, "pr edit 1 Accepted patch")
 75	adminKey.MustCmd(nil, `pr accept --comment "lgtm!" 1`)
 76
 77	// Closed patch (admin)
 78	userKey.MustCmd(patch, "pr create test")
 79	userKey.MustCmd(nil, "pr edit 2 Closed patch (admin)")
 80	adminKey.MustCmd(nil, `pr close --comment "Thanks for the effort! I think we might use PR #1 though." 2`)
 81
 82	// Closed patch (contributor)
 83	userKey.MustCmd(patch, "pr create test")
 84	userKey.MustCmd(nil, "pr edit 3 Closed patch (contributor)")
 85	userKey.MustCmd(nil, `pr close --comment "Woops, didn't mean to submit yet" 3`)
 86
 87	// Reviewed patch
 88	userKey.MustCmd(patch, "pr create test")
 89	userKey.MustCmd(nil, "pr edit 4 Reviewed patch")
 90	adminKey.MustCmd(otherPatch, "pr add --review 4")
 91
 92	// Accepted patch with review
 93	userKey.MustCmd(patch, "pr create test")
 94	userKey.MustCmd(nil, "pr edit 5 Accepted patch with review")
 95	adminKey.MustCmd(otherPatch, `pr add --accept --comment "L G T M" 5`)
 96
 97	// Closed patch with review
 98	userKey.MustCmd(patch, "pr create test")
 99	userKey.MustCmd(nil, "pr edit 6 Closed patch with review")
100	adminKey.MustCmd(otherPatch, `pr add --close --comment "So close! I think we might try something else instead." 6`)
101
102	// Range Diff
103	userKey.MustCmd(rd1, "pr create test")
104	userKey.MustCmd(nil, "pr edit 7 Range Diff")
105	userKey.MustCmd(rd2, "pr add 7")
106
107	fmt.Println("time to do some testing...")
108	ch := make(chan os.Signal, 1)
109	signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
110	<-ch
111}
112
113// args: tmpdir, adminKey
114var cfgTmpl = `
115url = "localhost"
116data_dir = %q
117admins = [%q]
118time_format = "01/02/2006 15:04:05 07:00"
119create_repo = "user"`