~bigbes/sr-ht-compare

ref: eb87ed542adbba81c07706b367fd6aecc94317f6 sr-ht-compare/gitx/refs.go -rw-r--r-- 2.9 KiB
eb87ed54 — bigbes authz: bootstrap the tests from ecoretest 9 days 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
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
package gitx

import (
	"context"
	"sort"
	"time"

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

// Ref is a named git reference paired with the object id it points at (the tag
// object for annotated tags, matching git for-each-ref's %(objectname)).
type Ref struct {
	Name string
	SHA  string
}

// Refs lists the repository's branches and tags. Branches are returned with the
// default (HEAD) branch first and the remainder alphabetical; tags are ordered
// newest-first by creator date (tagger date for annotated tags, committer date
// for lightweight tags).
func (r *Repo) Refs(ctx context.Context) (branches, tags []Ref, err error) {
	_, cancel := r.withTimeout(ctx)
	defer cancel()

	bIter, err := r.repo.Branches()
	if err != nil {
		return nil, nil, err
	}
	err = bIter.ForEach(func(ref *plumbing.Reference) error {
		branches = append(branches, Ref{Name: ref.Name().Short(), SHA: ref.Hash().String()})
		return nil
	})
	if err != nil {
		return nil, nil, err
	}
	def, _ := r.DefaultBranch(ctx)
	sortBranches(branches, def)

	type tagRef struct {
		ref  Ref
		when time.Time
	}
	var trefs []tagRef
	tIter, err := r.repo.Tags()
	if err != nil {
		return nil, nil, err
	}
	err = tIter.ForEach(func(ref *plumbing.Reference) error {
		trefs = append(trefs, tagRef{
			ref:  Ref{Name: ref.Name().Short(), SHA: ref.Hash().String()},
			when: r.tagWhen(ref.Hash()),
		})
		return nil
	})
	if err != nil {
		return nil, nil, err
	}
	sort.SliceStable(trefs, func(i, j int) bool {
		if !trefs[i].when.Equal(trefs[j].when) {
			return trefs[i].when.After(trefs[j].when)
		}
		return trefs[i].ref.Name > trefs[j].ref.Name
	})
	for _, t := range trefs {
		tags = append(tags, t.ref)
	}

	return branches, tags, nil
}

// DefaultBranch returns the short name of the branch HEAD points at (e.g.
// "main"). It fails if HEAD is detached (not a symbolic reference).
func (r *Repo) DefaultBranch(ctx context.Context) (string, error) {
	_, cancel := r.withTimeout(ctx)
	defer cancel()

	ref, err := r.repo.Reference(plumbing.HEAD, false)
	if err != nil {
		return "", err
	}
	if ref.Type() != plumbing.SymbolicReference {
		return "", plumbing.ErrReferenceNotFound
	}
	return ref.Target().Short(), nil
}

// tagWhen returns the creation time of a tag reference: the tagger time for an
// annotated tag, else the committer time of the pointed-at commit. A zero time
// is returned when neither can be resolved.
func (r *Repo) tagWhen(h plumbing.Hash) time.Time {
	if t, err := r.repo.TagObject(h); err == nil {
		return t.Tagger.When
	}
	if c, err := r.repo.CommitObject(h); err == nil {
		return c.Committer.When
	}
	return time.Time{}
}

// sortBranches orders refs alphabetically but floats the default branch to the
// front.
func sortBranches(refs []Ref, defaultBranch string) {
	sort.SliceStable(refs, func(i, j int) bool {
		if refs[i].Name == defaultBranch {
			return refs[j].Name != defaultBranch
		}
		if refs[j].Name == defaultBranch {
			return false
		}
		return refs[i].Name < refs[j].Name
	})
}