~kornellapacz/gmnigit

ref: 9cdc82885cf4286f621f8cb7f26c45aa4e513ad6 gmnigit/main.go -rw-r--r-- 1.2 KiB
9cdc8288Korneliusz Łapacz specify url by command line argument 6 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
package main

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

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

var wg sync.WaitGroup

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

var (
	repositoryPath  string
	distPath        string
	cloneURL        string
	showPermissions bool
)

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.Parse()

	if repositoryPath == "" || distPath == "" {
		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)

	go createCommitsFiles(repository)
	createBrowsableTree()
	createIndexFile()

	wg.Wait()
}

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