~kornellapacz/gmnigit

ref: f5c1f8b9b4bec8bf43a89b09f4c4b31ff41a8c83 gmnigit/main.go -rw-r--r-- 1.6 KiB
f5c1f8b9 — Omar Polo The following patch creates an additional file in /refs/index.gmi with 5 months ago
                                                                                
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
package main

import (
	"flag"
	"log"
	"os"
	"sync"

	"github.com/go-git/go-git/v5"
)

var wg sync.WaitGroup

const (
	treeSubPath    = "tree"
	commitsSubPath = "commits"
	refsSubPath    = "refs"
)

var (
	repositoryPath  string
	distPath        string
	cloneURL        string
	showPermissions bool
	generateRefs    bool
	projectName     string
)

func main() {
	flag.StringVar(&repositoryPath, "repo", "", "path to repository")
	flag.StringVar(&distPath, "dist", "", "destination path (if exists will be overwritten)")

	flag.StringVar(&cloneURL, "url", "", "clone URL for end user (optional)")
	flag.BoolVar(&showPermissions, "perms", false, "show files permissions in browsable tree")
	flag.BoolVar(&generateRefs, "refs", false, "generate the tags page")
	flag.StringVar(&projectName, "name", "", "project name, mandatory if generating the tags page")

	flag.Parse()

	if repositoryPath == "" || distPath == "" {
		flag.Usage()
		return
	}

	if generateRefs && projectName == "" {
		flag.Usage()
		return
	}

	if err := os.RemoveAll(distPath); err != nil {
		log.Fatalf("cannot remove dist directory: %s", err)
	}

	if err := os.Mkdir(distPath, os.ModePerm); err != nil {
		log.Fatalf("cannot create dist directory (%s)", err)
	}

	repository, err := git.PlainOpen(repositoryPath)

	if err != nil {
		log.Fatalf("cannot open repository with given path: %s, %s", repositoryPath, err)
	}

	wg.Add(1)

	if generateRefs {
		createRefsFile(repository)
	}

	go createCommitsFiles(repository)
	createBrowsableTree()
	createIndexFile()

	wg.Wait()
}

func check(err error) {
	if err != nil {
		log.Fatal(err)
	}
}