summaryrefslogtreecommitdiffstats
path: root/modules/git/ref.go
blob: 2a2798b18f9ab1dc3e7e285b25c766c013a85424 (plain)
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
// Copyright 2018 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package git

import "strings"

// Reference represents a Git ref.
type Reference struct {
	Name   string
	repo   *Repository
	Object SHA1 // The id of this commit object
	Type   string
}

// Commit return the commit of the reference
func (ref *Reference) Commit() (*Commit, error) {
	return ref.repo.getCommit(ref.Object)
}

// ShortName returns the short name of the reference
func (ref *Reference) ShortName() string {
	if ref == nil {
		return ""
	}
	if strings.HasPrefix(ref.Name, "refs/heads/") {
		return ref.Name[11:]
	}
	if strings.HasPrefix(ref.Name, "refs/tags/") {
		return ref.Name[10:]
	}
	if strings.HasPrefix(ref.Name, "refs/remotes/") {
		return ref.Name[13:]
	}
	if strings.HasPrefix(ref.Name, "refs/pull/") && strings.IndexByte(ref.Name[10:], '/') > -1 {
		return ref.Name[10 : strings.IndexByte(ref.Name[10:], '/')+10]
	}

	return ref.Name
}

// RefGroup returns the group type of the reference
func (ref *Reference) RefGroup() string {
	if ref == nil {
		return ""
	}
	if strings.HasPrefix(ref.Name, "refs/heads/") {
		return "heads"
	}
	if strings.HasPrefix(ref.Name, "refs/tags/") {
		return "tags"
	}
	if strings.HasPrefix(ref.Name, "refs/remotes/") {
		return "remotes"
	}
	if strings.HasPrefix(ref.Name, "refs/pull/") && strings.IndexByte(ref.Name[10:], '/') > -1 {
		return "pull"
	}
	return ""
}