Home

ugit @ba126a68908f21449d07eb2898587676322e577e - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / cmd / ugitd / main.go
- raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main

import (
	"errors"
	"flag"
	"fmt"
	"log/slog"
	"os"
	"os/signal"
	"path/filepath"
	"strconv"
	"strings"

	"github.com/charmbracelet/log"
	"github.com/go-chi/chi/v5/middleware"
	"github.com/go-chi/httplog/v2"
	"github.com/go-git/go-git/v5/plumbing/protocol/packp"
	"github.com/go-git/go-git/v5/utils/trace"
	"go.jolheiser.com/ugit/internal/git"
	"go.jolheiser.com/ugit/internal/http"
	"go.jolheiser.com/ugit/internal/ssh"
)

func main() {
	if len(os.Args) > 1 && os.Args[1] == "pre-receive-hook" {
		preReceive()
		return
	}

	args, err := parseArgs(os.Args[1:])
	if err != nil {
		if errors.Is(err, flag.ErrHelp) {
			return
		}
		panic(err)
	}
	args.RepoDir, err = filepath.Abs(args.RepoDir)
	if err != nil {
		panic(err)
	}

	log.SetLevel(args.Log.Level)
	middleware.DefaultLogger = httplog.RequestLogger(httplog.NewLogger("ugit", httplog.Options{
		JSON:     args.Log.JSON,
		LogLevel: slog.Level(args.Log.Level),
		Concise:  args.Log.Level != log.DebugLevel,
	}))

	if args.Log.Level == log.DebugLevel {
		trace.SetTarget(trace.Packet)
	} else {
		middleware.DefaultLogger = http.NoopLogger
		ssh.DefaultLogger = ssh.NoopLogger
	}

	if args.Log.JSON {
		log.SetFormatter(log.JSONFormatter)
	}

	if err := requiredFS(args.RepoDir); err != nil {
		panic(err)
	}

	sshSettings := ssh.Settings{
		AuthorizedKeys: args.SSH.AuthorizedKeys,
		CloneURL:       args.SSH.CloneURL,
		Port:           args.SSH.Port,
		HostKey:        args.SSH.HostKey,
		RepoDir:        args.RepoDir,
	}
	sshSrv, err := ssh.New(sshSettings)
	if err != nil {
		panic(err)
	}
	go func() {
		log.Debugf("SSH listening on ssh://localhost:%d\n", sshSettings.Port)
		if err := sshSrv.ListenAndServe(); err != nil {
			panic(err)
		}
	}()

	httpSettings := http.Settings{
		Title:       args.Meta.Title,
		Description: args.Meta.Description,
		CloneURL:    args.HTTP.CloneURL,
		Port:        args.HTTP.Port,
		RepoDir:     args.RepoDir,
		Profile: http.Profile{
			Username: args.Profile.Username,
			Email:    args.Profile.Email,
		},
	}
	for _, link := range args.Profile.Links {
		httpSettings.Profile.Links = append(httpSettings.Profile.Links, http.Link{
			Name: link.Name,
			URL:  link.URL,
		})
	}
	httpSrv := http.New(httpSettings)
	go func() {
		log.Debugf("HTTP listening on http://localhost:%d\n", httpSettings.Port)
		if err := httpSrv.ListenAndServe(); err != nil {
			panic(err)
		}
	}()

	ch := make(chan os.Signal, 1)
	signal.Notify(ch, os.Kill, os.Interrupt)
	<-ch
}

func requiredFS(repoDir string) error {
	if err := os.MkdirAll(repoDir, os.ModePerm); err != nil {
		return err
	}

	if !git.RequiresHook {
		return nil
	}
	bin, err := os.Executable()
	if err != nil {
		return err
	}

	fp := filepath.Join(repoDir, "hooks")
	if err := os.MkdirAll(fp, os.ModePerm); err != nil {
		return err
	}
	fp = filepath.Join(fp, "pre-receive")

	fi, err := os.Create(fp)
	if err != nil {
		return err
	}
	fi.WriteString("#!/usr/bin/env bash\n")
	fi.WriteString(fmt.Sprintf("%s pre-receive-hook\n", bin))
	fi.Close()

	return os.Chmod(fp, 0o755)
}

func preReceive() {
	repoDir, ok := os.LookupEnv("UGIT_REPODIR")
	if !ok {
		panic("UGIT_REPODIR is not set")
	}

	opts := make([]*packp.Option, 0)
	if pushCount, err := strconv.Atoi(os.Getenv("GIT_PUSH_OPTION_COUNT")); err == nil {
		for idx := 0; idx < pushCount; idx++ {
			opt := os.Getenv(fmt.Sprintf("GIT_PUSH_OPTION_%d", idx))
			kv := strings.SplitN(opt, "=", 2)
			if len(kv) == 2 {
				opts = append(opts, &packp.Option{
					Key:   kv[0],
					Value: kv[1],
				})
			}
		}
	}

	repo, err := git.NewRepo(filepath.Dir(repoDir), filepath.Base(repoDir))
	if err != nil {
		panic(err)
	}
	if err := git.HandlePushOptions(repo, opts); err != nil {
		panic(err)
	}
}