Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/repo/repo.go (53 lines, 1.3 KiB) -rw-r--r-- file download

c2c5250 Jakob Wakeling 2023-12-12 09:13:37
0
// Copyright (C) 2023, Jakob Wakeling
c2c5250 Jakob Wakeling 2023-12-12 09:13:37
1
// All rights reserved.
c2c5250 Jakob Wakeling 2023-12-12 09:13:37
2
64718e1 Jakob Wakeling 2023-07-24 21:56:07
3
package repo
64718e1 Jakob Wakeling 2023-07-24 21:56:07
4
64718e1 Jakob Wakeling 2023-07-24 21:56:07
5
import (
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
6
	"regexp"
64718e1 Jakob Wakeling 2023-07-24 21:56:07
7
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
8
	"github.com/Jamozed/Goit/src/goit"
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
9
	"github.com/Jamozed/Goit/src/util"
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
10
	"github.com/go-git/go-git/v5"
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
11
	"github.com/go-git/go-git/v5/plumbing"
64718e1 Jakob Wakeling 2023-07-24 21:56:07
12
)
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
13
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
14
type HeaderFields struct {
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
15
	Name, Description, Url  string
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
16
	Readme, Licence, Mirror string
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
17
	Editable                bool
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
18
}
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
19
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
20
func GetHeaderFields(auth bool, user *goit.User, repo *goit.Repo, host string) HeaderFields {
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
21
	return HeaderFields{
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
22
		Name: repo.Name, Description: repo.Description,
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
23
		Url:      util.If(goit.Conf.UsesHttps, "https://", "http://") + host + "/" + repo.Name,
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
24
		Editable: (auth && repo.OwnerId == user.Id),
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
25
		Mirror:   util.If(repo.IsMirror, repo.Upstream, ""),
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
26
	}
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
27
}
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
28
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
29
var readmePattern = regexp.MustCompile(`(?i)^readme(?:\.?(?:md|txt))?$`)
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
30
var licencePattern = regexp.MustCompile(`(?i)^licence(?:\.?(?:md|txt))?$`)
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
31
b4b291e Jakob Wakeling 2023-12-23 17:05:01
32
/* Find a file that matches a regular expression in the root level of a reference. */
b4b291e Jakob Wakeling 2023-12-23 17:05:01
33
func findPattern(gr *git.Repository, ref *plumbing.Reference, re *regexp.Regexp) (string, error) {
b4b291e Jakob Wakeling 2023-12-23 17:05:01
34
	c, err := gr.CommitObject(ref.Hash())
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
35
	if err != nil {
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
36
		return "", err
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
37
	}
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
38
b4b291e Jakob Wakeling 2023-12-23 17:05:01
39
	t, err := c.Tree()
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
40
	if err != nil {
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
41
		return "", err
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
42
	}
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
43
b4b291e Jakob Wakeling 2023-12-23 17:05:01
44
	for _, e := range t.Entries {
b4b291e Jakob Wakeling 2023-12-23 17:05:01
45
		if re.MatchString(e.Name) {
b4b291e Jakob Wakeling 2023-12-23 17:05:01
46
			return e.Name, nil
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
47
		}
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
48
	}
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
49
b4b291e Jakob Wakeling 2023-12-23 17:05:01
50
	return "", nil
7ba1b21 Jakob Wakeling 2023-08-02 21:44:07
51
}
52