Home

ugit @80d0d9b1deeb1803b0a2f8583119dd6d456d5927 - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / internal / git / repo.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package git

import (
	"encoding/json"
	"errors"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"time"

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

type Repo struct {
	path string
	Meta RepoMeta
}

func (r Repo) Name() string {
	return strings.TrimSuffix(filepath.Base(r.path), ".git")
}

func NewRepo(dir, name string) (*Repo, error) {
	if !strings.HasSuffix(name, ".git") {
		name += ".git"
	}
	r := &Repo{
		path: filepath.Join(dir, name),
	}

	_, err := os.Stat(r.path)
	if err != nil {
		return nil, err
	}

	if err := ensureJSONFile(r.metaPath()); err != nil {
		return nil, err
	}
	fi, err := os.Open(r.metaPath())
	if err != nil {
		return nil, err
	}
	defer fi.Close()

	if err := json.NewDecoder(fi).Decode(&r.Meta); err != nil {
		return nil, err
	}

	return r, nil
}

// DefaultBranch returns the branch referenced by HEAD, setting it if needed
func (r Repo) DefaultBranch() (string, error) {
	repo, err := r.Git()
	if err != nil {
		return "", err
	}

	ref, err := repo.Head()
	if err != nil {
		if !errors.Is(err, plumbing.ErrReferenceNotFound) {
			return "", err
		}
		brs, err := repo.Branches()
		if err != nil {
			return "", err
		}
		defer brs.Close()
		fb, err := brs.Next()
		if err != nil {
			return "", err
		}
		// Rename the default branch to the first branch available
		ref = fb
		sym := plumbing.NewSymbolicReference(plumbing.HEAD, fb.Name())
		if err := repo.Storer.SetReference(sym); err != nil {
			return "", err
		}
	}

	return strings.TrimPrefix(ref.Name().String(), "refs/heads/"), nil
}

// Git allows access to the git repository
func (r Repo) Git() (*git.Repository, error) {
	return git.PlainOpen(r.path)
}

// LastCommit returns the last commit of the repo
func (r Repo) LastCommit() (*object.Commit, error) {
	repo, err := r.Git()
	if err != nil {
		return nil, err
	}

	head, err := repo.Head()
	if err != nil {
		return nil, err
	}

	return repo.CommitObject(head.Hash())
}

// Branches is all repo branches, default first and sorted alphabetically after that
func (r Repo) Branches() ([]string, error) {
	repo, err := r.Git()
	if err != nil {
		return nil, err
	}

	def, err := r.DefaultBranch()
	if err != nil {
		return nil, err
	}

	brs, err := repo.Branches()
	if err != nil {
		return nil, err
	}

	var branches []string
	if err := brs.ForEach(func(branch *plumbing.Reference) error {
		branches = append(branches, branch.Name().Short())
		return nil
	}); err != nil {
		return nil, err
	}

	sort.Slice(branches, func(i, j int) bool {
		return branches[i] == def || branches[i] < branches[j]
	})

	return branches, nil
}

// Tag is a git tag, which may or may not have an annotation/signature
type Tag struct {
	Name       string
	Annotation string
	Signature  string
	When       time.Time
}

// Tags is all repo tags, sorted by time descending
func (r Repo) Tags() ([]Tag, error) {
	repo, err := r.Git()
	if err != nil {
		return nil, err
	}

	tgs, err := repo.Tags()
	if err != nil {
		return nil, err
	}

	var tags []Tag
	if err := tgs.ForEach(func(tag *plumbing.Reference) error {
		obj, err := repo.TagObject(tag.Hash())
		switch err {
		case nil:
			tags = append(tags, Tag{
				Name:       obj.Name,
				Annotation: obj.Message,
				Signature:  obj.PGPSignature,
				When:       obj.Tagger.When,
			})
		case plumbing.ErrObjectNotFound:
			commit, err := repo.CommitObject(tag.Hash())
			if err != nil {
				return err
			}
			tags = append(tags, Tag{
				Name:       tag.Name().Short(),
				Annotation: commit.Message,
				Signature:  commit.PGPSignature,
				When:       commit.Author.When,
			})
		default:
			return err
		}
		return nil
	}); err != nil {
		return nil, err
	}

	sort.Slice(tags, func(i, j int) bool {
		return tags[i].When.After(tags[j].When)
	})

	return tags, nil
}