Goit

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

Goit/src/repo/refs.go (142 lines, 3.6 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
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
3
package repo
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
4
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
5
import (
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
6
	"errors"
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
7
	"log"
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
8
	"net/http"
08dff9a Jakob Wakeling 2023-11-07 21:03:04
9
	"path/filepath"
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
10
	"slices"
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
11
	"strings"
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
12
	"time"
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
13
945fd71 Jakob Wakeling 2023-11-05 15:39:31
14
	"github.com/Jamozed/Goit/src/goit"
3f4c3f4 Jakob Wakeling 2023-12-01 23:26:03
15
	"github.com/go-chi/chi/v5"
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
16
	"github.com/go-git/go-git/v5"
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
17
	"github.com/go-git/go-git/v5/plumbing"
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
18
	"github.com/go-git/go-git/v5/plumbing/object"
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
19
)
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
20
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
21
func HandleRefs(w http.ResponseWriter, r *http.Request) {
473d02c Jakob Wakeling 2023-11-28 00:01:06
22
	auth, user, err := goit.Auth(w, r, true)
473d02c Jakob Wakeling 2023-11-28 00:01:06
23
	if err != nil {
473d02c Jakob Wakeling 2023-11-28 00:01:06
24
		log.Println("[admin]", err.Error())
473d02c Jakob Wakeling 2023-11-28 00:01:06
25
		goit.HttpError(w, http.StatusInternalServerError)
473d02c Jakob Wakeling 2023-11-28 00:01:06
26
	}
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
27
3f4c3f4 Jakob Wakeling 2023-12-01 23:26:03
28
	repo, err := goit.GetRepoByName(chi.URLParam(r, "repo"))
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
29
	if err != nil {
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
30
		goit.HttpError(w, http.StatusInternalServerError)
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
31
		return
778d467 Jakob Wakeling 2024-01-17 18:29:55
32
	} else if repo == nil || !goit.IsVisible(repo, auth, user) {
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
33
		goit.HttpError(w, http.StatusNotFound)
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
34
		return
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
35
	}
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
36
d6784d5 Jakob Wakeling 2023-12-25 23:47:47
37
	type row struct {
d6784d5 Jakob Wakeling 2023-12-25 23:47:47
38
		Name, Hash, Message, Author, LastCommit string
d6784d5 Jakob Wakeling 2023-12-25 23:47:47
39
		Commits                                 uint64
d6784d5 Jakob Wakeling 2023-12-25 23:47:47
40
	}
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
41
	data := struct {
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
42
		HeaderFields
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
43
		Title          string
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
44
		Branches, Tags []row
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
45
	}{
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
46
		Title:        repo.Name + " - References",
2e13a8c Jakob Wakeling 2023-12-23 19:40:09
47
		HeaderFields: GetHeaderFields(auth, user, repo, r.Host),
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
48
	}
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
49
08dff9a Jakob Wakeling 2023-11-07 21:03:04
50
	gr, err := git.PlainOpen(goit.RepoPath(repo.Name, true))
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
51
	if err != nil {
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
52
		log.Println("[/repo/refs]", err.Error())
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
53
		goit.HttpError(w, http.StatusInternalServerError)
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
54
		return
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
55
	}
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
56
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
57
	ref, err := gr.Head()
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
58
	if err != nil {
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
59
		if !errors.Is(err, plumbing.ErrReferenceNotFound) {
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
60
			log.Println("[/repo/log]", err.Error())
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
61
			goit.HttpError(w, http.StatusInternalServerError)
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
62
			return
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
63
		}
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
64
	} else {
b4b291e Jakob Wakeling 2023-12-23 17:05:01
65
		if readme, _ := findPattern(gr, ref, readmePattern); readme != "" {
08dff9a Jakob Wakeling 2023-11-07 21:03:04
66
			data.Readme = filepath.Join("/", repo.Name, "file", readme)
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
67
		}
b4b291e Jakob Wakeling 2023-12-23 17:05:01
68
		if licence, _ := findPattern(gr, ref, licencePattern); licence != "" {
08dff9a Jakob Wakeling 2023-11-07 21:03:04
69
			data.Licence = filepath.Join("/", repo.Name, "file", licence)
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
70
		}
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
71
	}
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
72
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
73
	if iter, err := gr.Branches(); err != nil {
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
74
		log.Println("[/repo/refs]", err.Error())
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
75
		goit.HttpError(w, http.StatusInternalServerError)
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
76
		return
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
77
	} else if err := iter.ForEach(func(r *plumbing.Reference) error {
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
78
		commit, err := gr.CommitObject(r.Hash())
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
79
		if err != nil {
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
80
			return err
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
81
		}
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
82
d6784d5 Jakob Wakeling 2023-12-25 23:47:47
83
		commits, err := goit.CommitCount(repo.Name, r.Name().Short(), r.Hash())
d6784d5 Jakob Wakeling 2023-12-25 23:47:47
84
		if err != nil {
d6784d5 Jakob Wakeling 2023-12-25 23:47:47
85
			return err
d6784d5 Jakob Wakeling 2023-12-25 23:47:47
86
		}
d6784d5 Jakob Wakeling 2023-12-25 23:47:47
87
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
88
		data.Branches = append(data.Branches, row{
d6784d5 Jakob Wakeling 2023-12-25 23:47:47
89
			Name: r.Name().Short(), Hash: r.Hash().String(), Message: strings.SplitN(commit.Message, "\n", 2)[0],
d6784d5 Jakob Wakeling 2023-12-25 23:47:47
90
			Author: commit.Author.Name, LastCommit: commit.Author.When.UTC().Format(time.DateTime), Commits: commits,
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
91
		})
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
92
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
93
		return nil
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
94
	}); err != nil {
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
95
		log.Println("[/repo/refs]", err.Error())
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
96
		goit.HttpError(w, http.StatusInternalServerError)
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
97
		return
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
98
	}
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
99
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
100
	if iter, err := gr.Tags(); err != nil {
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
101
		log.Println("[/repo/refs]", err.Error())
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
102
		goit.HttpError(w, http.StatusInternalServerError)
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
103
		return
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
104
	} else if err := iter.ForEach(func(r *plumbing.Reference) error {
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
105
		var c *object.Commit
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
106
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
107
		if tag, err := gr.TagObject(r.Hash()); err != nil {
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
108
			if !errors.Is(err, plumbing.ErrObjectNotFound) {
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
109
				return err
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
110
			}
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
111
		} else {
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
112
			if c, err = gr.CommitObject(tag.Target); err != nil {
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
113
				return err
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
114
			}
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
115
		}
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
116
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
117
		if c == nil {
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
118
			if c, err = gr.CommitObject(r.Hash()); err != nil {
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
119
				return err
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
120
			}
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
121
		}
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
122
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
123
		data.Tags = append(data.Tags, row{
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
124
			Name: r.Name().Short(), Message: strings.SplitN(c.Message, "\n", 2)[0], Author: c.Author.Name,
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
125
			LastCommit: c.Author.When.UTC().Format(time.DateTime), Hash: r.Hash().String(),
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
126
		})
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
127
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
128
		return nil
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
129
	}); err != nil {
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
130
		log.Println("[/repo/refs]", err.Error())
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
131
		goit.HttpError(w, http.StatusInternalServerError)
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
132
		return
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
133
	}
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
134
2af4be7 Jakob Wakeling 2023-12-23 17:43:05
135
	slices.Reverse(data.Tags)
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
136
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
137
	if err := goit.Tmpl.ExecuteTemplate(w, "repo/refs", data); err != nil {
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
138
		log.Println("[/repo/refs]", err.Error())
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
139
	}
0dadb00 Jakob Wakeling 2023-08-07 19:22:06
140
}
141